Home | History | Annotate | Line # | Download | only in pci
neo.c revision 1.37
      1 /*	$NetBSD: neo.c,v 1.37 2007/12/09 20:28:11 jmcneill 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.37 2007/12/09 20:28:11 jmcneill Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/malloc.h>
     41 #include <sys/device.h>
     42 
     43 #include <sys/bus.h>
     44 
     45 #include <dev/pci/pcidevs.h>
     46 #include <dev/pci/pcivar.h>
     47 
     48 #include <dev/pci/neoreg.h>
     49 #include <dev/pci/neo-coeff.h>
     50 
     51 #include <sys/audioio.h>
     52 #include <dev/audio_if.h>
     53 #include <dev/mulaw.h>
     54 #include <dev/auconv.h>
     55 
     56 #include <dev/ic/ac97var.h>
     57 
     58 
     59 /* -------------------------------------------------------------------- */
     60 /*
     61  * As of 04/13/00, public documentation on the Neomagic 256 is not available.
     62  * These comments were gleaned by looking at the driver carefully.
     63  *
     64  * The Neomagic 256 AV/ZX chips provide both video and audio capabilities
     65  * on one chip. About 2-6 megabytes of memory are associated with
     66  * the chip. Most of this goes to video frame buffers, but some is used for
     67  * audio buffering
     68  *
     69  * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA.
     70  * Instead, the chip allows you to carve out two ring buffers out of its
     71  * memory. However you carve this and how much you can carve seems to be
     72  * voodoo. The algorithm is in nm_init.
     73  *
     74  * Most Neomagic audio chips use the AC-97 codec interface. However, there
     75  * seem to be a select few chips 256AV chips that do not support AC-97.
     76  * This driver does not support them but there are rumors that it
     77  * might work with wss isa drivers. This might require some playing around
     78  * with your BIOS.
     79  *
     80  * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of
     81  * them describe a memory region. The frame buffer is the first region
     82  * and the register set is the secodn region.
     83  *
     84  * The register manipulation logic is taken from the Linux driver,
     85  * which is in the public domain.
     86  *
     87  * The Neomagic is even nice enough to map the AC-97 codec registers into
     88  * the register space to allow direct manipulation. Watch out, accessing
     89  * AC-97 registers on the Neomagic requires great delicateness, otherwise
     90  * the thing will hang the PCI bus, rendering your system frozen.
     91  *
     92  * For one, it seems the Neomagic status register that reports AC-97
     93  * readiness should NOT be polled more often than once each 1ms.
     94  *
     95  * Also, writes to the AC-97 register space may take order 40us to
     96  * complete.
     97  *
     98  * Unlike many sound engines, the Neomagic does not support (as far as
     99  * we know :) the notion of interrupting every n bytes transferred,
    100  * unlike many DMA engines.  Instead, it allows you to specify one
    101  * location in each ring buffer (called the watermark). When the chip
    102  * passes that location while playing, it signals an interrupt.
    103  *
    104  * The ring buffer size is currently 16k. That is about 100ms of audio
    105  * at 44.1kHz/stero/16 bit. However, to keep the buffer full, interrupts
    106  * are generated more often than that, so 20-40 interrupts per second
    107  * should not be unexpected. Increasing BUFFSIZE should help minimize
    108  * of glitches due to drivers that spend to much time looping at high
    109  * privelege levels as well as the impact of badly written audio
    110  * interface clients.
    111  *
    112  * TO-DO list:
    113  *    Figure out interaction with video stuff (look at Xfree86 driver?)
    114  *
    115  *    Figure out how to shrink that huge table neo-coeff.h
    116  */
    117 
    118 #define	NM_BUFFSIZE	16384
    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 	uint32_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 	vaddr_t	buf_vaddr;
    140 	vaddr_t	rbuf_vaddr;
    141 	vaddr_t	pbuf_vaddr;
    142 	int	pbuf_allocated;
    143 	int	rbuf_allocated;
    144 
    145 	bus_addr_t buf_pciaddr;
    146 	bus_addr_t rbuf_pciaddr;
    147 	bus_addr_t pbuf_pciaddr;
    148 
    149 	uint32_t	ac97_base, ac97_status, ac97_busy;
    150 	uint32_t	buftop, pbuf, rbuf, cbuf, acbuf;
    151 	uint32_t	playint, recint, misc1int, misc2int;
    152 	uint32_t	irsz, badintr;
    153 
    154 	uint32_t	pbufsize;
    155 	uint32_t	rbufsize;
    156 
    157 	uint32_t	pblksize;
    158 	uint32_t	rblksize;
    159 
    160 	uint32_t	pwmark;
    161 	uint32_t	rwmark;
    162 
    163 	struct ac97_codec_if *codec_if;
    164 	struct ac97_host_if host_if;
    165 };
    166 
    167 /* -------------------------------------------------------------------- */
    168 
    169 /*
    170  * prototypes
    171  */
    172 
    173 static int	nm_waitcd(struct neo_softc *);
    174 static int	nm_loadcoeff(struct neo_softc *, int, int);
    175 static int	nm_init(struct neo_softc *);
    176 
    177 static int	neo_match(struct device *, struct cfdata *, void *);
    178 static void	neo_attach(struct device *, struct device *, void *);
    179 static int	neo_intr(void *);
    180 
    181 static int	neo_query_encoding(void *, struct audio_encoding *);
    182 static int	neo_set_params(void *, int, int, audio_params_t *,
    183 			       audio_params_t *, stream_filter_list_t *,
    184 			       stream_filter_list_t *);
    185 static int	neo_round_blocksize(void *, int, int, const audio_params_t *);
    186 static int	neo_trigger_output(void *, void *, void *, int,
    187 				   void (*)(void *), void *,
    188 				   const audio_params_t *);
    189 static int	neo_trigger_input(void *, void *, void *, int,
    190 				  void (*)(void *), void *,
    191 				  const audio_params_t *);
    192 static int	neo_halt_output(void *);
    193 static int	neo_halt_input(void *);
    194 static int	neo_getdev(void *, struct audio_device *);
    195 static int	neo_mixer_set_port(void *, mixer_ctrl_t *);
    196 static int	neo_mixer_get_port(void *, mixer_ctrl_t *);
    197 static int	neo_attach_codec(void *, struct ac97_codec_if *);
    198 static int	neo_read_codec(void *, uint8_t, uint16_t *);
    199 static int	neo_write_codec(void *, uint8_t, uint16_t);
    200 static int     neo_reset_codec(void *);
    201 static enum ac97_host_flags neo_flags_codec(void *);
    202 static int	neo_query_devinfo(void *, mixer_devinfo_t *);
    203 static void *	neo_malloc(void *, int, size_t, struct malloc_type *, int);
    204 static void	neo_free(void *, void *, struct malloc_type *);
    205 static size_t	neo_round_buffersize(void *, int, size_t);
    206 static paddr_t	neo_mappage(void *, void *, off_t, int);
    207 static int	neo_get_props(void *);
    208 
    209 CFATTACH_DECL(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 	NULL,
    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 	rv = 0;
    369 	status = (sc->irsz == 2) ?
    370 	    nm_rd_2(sc, NM_INT_REG) :
    371 	    nm_rd_4(sc, NM_INT_REG);
    372 
    373 	if (status & sc->playint) {
    374 		status &= ~sc->playint;
    375 
    376 		sc->pwmark += sc->pblksize;
    377 		sc->pwmark %= sc->pbufsize;
    378 
    379 		nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    380 
    381 		nm_ackint(sc, sc->playint);
    382 
    383 		if (sc->pintr)
    384 			(*sc->pintr)(sc->parg);
    385 
    386 		rv = 1;
    387 	}
    388 	if (status & sc->recint) {
    389 		status &= ~sc->recint;
    390 
    391 		sc->rwmark += sc->rblksize;
    392 		sc->rwmark %= sc->rbufsize;
    393 		nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
    394 		nm_ackint(sc, sc->recint);
    395 		if (sc->rintr)
    396 			(*sc->rintr)(sc->rarg);
    397 
    398 		rv = 1;
    399 	}
    400 	if (status & sc->misc1int) {
    401 		status &= ~sc->misc1int;
    402 		nm_ackint(sc, sc->misc1int);
    403 		x = nm_rd_1(sc, 0x400);
    404 		nm_wr_1(sc, 0x400, x | 2);
    405 		printf("%s: misc int 1\n", sc->dev.dv_xname);
    406 		rv = 1;
    407 	}
    408 	if (status & sc->misc2int) {
    409 		status &= ~sc->misc2int;
    410 		nm_ackint(sc, sc->misc2int);
    411 		x = nm_rd_1(sc, 0x400);
    412 		nm_wr_1(sc, 0x400, x & ~2);
    413 		printf("%s: misc int 2\n", sc->dev.dv_xname);
    414 		rv = 1;
    415 	}
    416 	if (status) {
    417 		status &= ~sc->misc2int;
    418 		nm_ackint(sc, sc->misc2int);
    419 		printf("%s: unknown int\n", sc->dev.dv_xname);
    420 		rv = 1;
    421 	}
    422 
    423 	return rv;
    424 }
    425 
    426 /* -------------------------------------------------------------------- */
    427 
    428 /*
    429  * Probe and attach the card
    430  */
    431 
    432 static int
    433 nm_init(struct neo_softc *sc)
    434 {
    435 	uint32_t ofs, i;
    436 
    437 	switch (sc->type) {
    438 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    439 		sc->ac97_base = NM_MIXER_OFFSET;
    440 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
    441 		sc->ac97_busy = NM_MIXER_READY_MASK;
    442 
    443 		sc->buftop = 2560 * 1024;
    444 
    445 		sc->irsz = 2;
    446 		sc->playint = NM_PLAYBACK_INT;
    447 		sc->recint = NM_RECORD_INT;
    448 		sc->misc1int = NM_MISC_INT_1;
    449 		sc->misc2int = NM_MISC_INT_2;
    450 		break;
    451 
    452 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    453 		sc->ac97_base = NM_MIXER_OFFSET;
    454 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
    455 		sc->ac97_busy = NM2_MIXER_READY_MASK;
    456 
    457 		sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
    458 
    459 		sc->irsz = 4;
    460 		sc->playint = NM2_PLAYBACK_INT;
    461 		sc->recint = NM2_RECORD_INT;
    462 		sc->misc1int = NM2_MISC_INT_1;
    463 		sc->misc2int = NM2_MISC_INT_2;
    464 		break;
    465 #ifdef DIAGNOSTIC
    466 	default:
    467 		panic("nm_init: impossible");
    468 #endif
    469 	}
    470 
    471 	sc->badintr = 0;
    472 	ofs = sc->buftop - 0x0400;
    473 	sc->buftop -= 0x1400;
    474 
    475 	if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
    476 		i = nm_rdbuf_4(sc, ofs + 4);
    477 		if (i != 0 && i != 0xffffffff)
    478 			sc->buftop = i;
    479 	}
    480 
    481 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
    482 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
    483 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
    484 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
    485 
    486 	sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
    487 	sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
    488 	sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
    489 
    490 	sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
    491 	sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
    492 
    493 	nm_wr_1(sc, 0, 0x11);
    494 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    495 	nm_wr_2(sc, 0x214, 0);
    496 
    497 	return 0;
    498 }
    499 
    500 static int
    501 neo_match(struct device *parent, struct cfdata *match,
    502     void *aux)
    503 {
    504 	struct pci_attach_args *pa;
    505 	pcireg_t subdev;
    506 
    507 	pa = aux;
    508 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
    509 		return 0;
    510 
    511 	subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    512 
    513 	switch (PCI_PRODUCT(pa->pa_id)) {
    514 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    515 		/*
    516 		 * We have to weed-out the non-AC'97 versions of
    517 		 * the chip (i.e. the ones that are known to work
    518 		 * in WSS emulation mode), as they won't work with
    519 		 * this driver.
    520 		 */
    521 		switch (PCI_VENDOR(subdev)) {
    522 		case PCI_VENDOR_DELL:
    523 			switch (PCI_PRODUCT(subdev)) {
    524 			case 0x008f:
    525 				return 0;
    526 			}
    527 			break;
    528 
    529 		case PCI_VENDOR_HP:
    530 			switch (PCI_PRODUCT(subdev)) {
    531 			case 0x0007:
    532 				return 0;
    533 			}
    534 			break;
    535 
    536 		case PCI_VENDOR_IBM:
    537 			switch (PCI_PRODUCT(subdev)) {
    538 			case 0x00dd:
    539 				return 0;
    540 			}
    541 			break;
    542 		}
    543 		return 1;
    544 
    545 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    546 		return 1;
    547 	}
    548 
    549 	return 0;
    550 }
    551 
    552 static bool
    553 neo_resume(device_t dv)
    554 {
    555 	struct neo_softc *sc = device_private(dv);
    556 
    557 	nm_init(sc);
    558 	sc->codec_if->vtbl->restore_ports(sc->codec_if);
    559 
    560 	return true;
    561 }
    562 
    563 static void
    564 neo_attach(struct device *parent, struct device *self, void *aux)
    565 {
    566 	struct neo_softc *sc;
    567 	struct pci_attach_args *pa;
    568 	pci_chipset_tag_t pc;
    569 	char const *intrstr;
    570 	pci_intr_handle_t ih;
    571 	pcireg_t csr;
    572 
    573 	sc = (struct neo_softc *)self;
    574 	pa = (struct pci_attach_args *)aux;
    575 	pc = pa->pa_pc;
    576 
    577 	sc->type = PCI_PRODUCT(pa->pa_id);
    578 
    579 	printf(": NeoMagic 256%s audio\n",
    580 	    sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
    581 
    582 	/* Map I/O register */
    583 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
    584 			   &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
    585 		printf("%s: can't map buffer\n", sc->dev.dv_xname);
    586 		return;
    587 	}
    588 
    589 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
    590 	    BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) {
    591 		printf("%s: can't map registers\n", sc->dev.dv_xname);
    592 		return;
    593 	}
    594 
    595 	/* Map and establish the interrupt. */
    596 	if (pci_intr_map(pa, &ih)) {
    597 		printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
    598 		return;
    599 	}
    600 
    601 	intrstr = pci_intr_string(pc, ih);
    602 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
    603 
    604 	if (sc->ih == NULL) {
    605 		printf("%s: couldn't establish interrupt",
    606 		       sc->dev.dv_xname);
    607 		if (intrstr != NULL)
    608 			printf(" at %s", intrstr);
    609 		printf("\n");
    610 		return;
    611 	}
    612 	printf("%s: interrupting at %s\n", sc->dev.dv_xname, intrstr);
    613 
    614 	if (nm_init(sc) != 0)
    615 		return;
    616 
    617 	/* Enable the device. */
    618 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    619 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    620 		       csr | PCI_COMMAND_MASTER_ENABLE);
    621 
    622 	sc->host_if.arg = sc;
    623 
    624 	sc->host_if.attach = neo_attach_codec;
    625 	sc->host_if.read   = neo_read_codec;
    626 	sc->host_if.write  = neo_write_codec;
    627 	sc->host_if.reset  = neo_reset_codec;
    628 	sc->host_if.flags  = neo_flags_codec;
    629 
    630 	if (ac97_attach(&sc->host_if, self) != 0)
    631 		return;
    632 
    633 	if (!pmf_device_register(self, NULL, neo_resume))
    634 		aprint_error_dev(self, "couldn't establish power handler\n");
    635 
    636 	audio_attach_mi(&neo_hw_if, sc, &sc->dev);
    637 }
    638 
    639 static int
    640 neo_read_codec(void *v, uint8_t a, uint16_t *d)
    641 {
    642 	struct neo_softc *sc;
    643 
    644 	sc = v;
    645 	if (!nm_waitcd(sc)) {
    646 		*d = nm_rd_2(sc, sc->ac97_base + a);
    647 		DELAY(1000);
    648 		return 0;
    649 	}
    650 
    651 	return ENXIO;
    652 }
    653 
    654 
    655 static int
    656 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
    657 {
    658 	struct neo_softc *sc;
    659 	int cnt;
    660 
    661 	sc = v;
    662 	cnt = 3;
    663 	if (!nm_waitcd(sc)) {
    664 		while (cnt-- > 0) {
    665 			nm_wr_2(sc, sc->ac97_base + a, d);
    666 			if (!nm_waitcd(sc)) {
    667 				DELAY(1000);
    668 				return 0;
    669 			}
    670 		}
    671 	}
    672 
    673 	return ENXIO;
    674 }
    675 
    676 static int
    677 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
    678 {
    679 	struct neo_softc *sc;
    680 
    681 	sc = v;
    682 	sc->codec_if = codec_if;
    683 	return 0;
    684 }
    685 
    686 static int
    687 neo_reset_codec(void *v)
    688 {
    689 	struct neo_softc *sc;
    690 
    691 	sc = v;
    692 	nm_wr_1(sc, 0x6c0, 0x01);
    693 	nm_wr_1(sc, 0x6cc, 0x87);
    694 	nm_wr_1(sc, 0x6cc, 0x80);
    695 	nm_wr_1(sc, 0x6cc, 0x00);
    696 	return 0;
    697 }
    698 
    699 static enum ac97_host_flags
    700 neo_flags_codec(void *v)
    701 {
    702 
    703 	return AC97_HOST_DONT_READ;
    704 }
    705 
    706 static int
    707 neo_query_encoding(void *addr, struct audio_encoding *fp)
    708 {
    709 
    710 	switch (fp->index) {
    711 	case 0:
    712 		strcpy(fp->name, AudioEulinear);
    713 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    714 		fp->precision = 8;
    715 		fp->flags = 0;
    716 		return 0;
    717 	case 1:
    718 		strcpy(fp->name, AudioEmulaw);
    719 		fp->encoding = AUDIO_ENCODING_ULAW;
    720 		fp->precision = 8;
    721 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    722 		return 0;
    723 	case 2:
    724 		strcpy(fp->name, AudioEalaw);
    725 		fp->encoding = AUDIO_ENCODING_ALAW;
    726 		fp->precision = 8;
    727 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    728 		return 0;
    729 	case 3:
    730 		strcpy(fp->name, AudioEslinear);
    731 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    732 		fp->precision = 8;
    733 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    734 		return (0);
    735 	case 4:
    736 		strcpy(fp->name, AudioEslinear_le);
    737 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    738 		fp->precision = 16;
    739 		fp->flags = 0;
    740 		return 0;
    741 	case 5:
    742 		strcpy(fp->name, AudioEulinear_le);
    743 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    744 		fp->precision = 16;
    745 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    746 		return 0;
    747 	case 6:
    748 		strcpy(fp->name, AudioEslinear_be);
    749 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    750 		fp->precision = 16;
    751 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    752 		return 0;
    753 	case 7:
    754 		strcpy(fp->name, AudioEulinear_be);
    755 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    756 		fp->precision = 16;
    757 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    758 		return 0;
    759 	default:
    760 		return EINVAL;
    761 	}
    762 }
    763 
    764 /* Todo: don't commit settings to card until we've verified all parameters */
    765 static int
    766 neo_set_params(void *addr, int setmode, int usemode,
    767     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
    768     stream_filter_list_t *rfil)
    769 {
    770 	struct neo_softc *sc;
    771 	audio_params_t *p;
    772 	stream_filter_list_t *fil;
    773 	uint32_t base;
    774 	uint8_t x;
    775 	int mode, i;
    776 
    777 	sc = addr;
    778 	for (mode = AUMODE_RECORD; mode != -1;
    779 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    780 		if ((setmode & mode) == 0)
    781 			continue;
    782 
    783 		p = mode == AUMODE_PLAY ? play : rec;
    784 
    785 		if (p == NULL) continue;
    786 
    787 		for (x = 0; x < 8; x++) {
    788 			if (p->sample_rate <
    789 			    (samplerates[x] + samplerates[x + 1]) / 2)
    790 				break;
    791 		}
    792 		if (x == 8)
    793 			return EINVAL;
    794 
    795 		p->sample_rate = samplerates[x];
    796 		nm_loadcoeff(sc, mode, x);
    797 
    798 		x <<= 4;
    799 		x &= NM_RATE_MASK;
    800 		if (p->precision == 16)
    801 			x |= NM_RATE_BITS_16;
    802 		if (p->channels == 2)
    803 			x |= NM_RATE_STEREO;
    804 
    805 		base = (mode == AUMODE_PLAY)?
    806 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
    807 		nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
    808 
    809 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    810 		i = auconv_set_converter(neo_formats, NEO_NFORMATS,
    811 					 mode, p, FALSE, fil);
    812 		if (i < 0)
    813 			return EINVAL;
    814 	}
    815 
    816 	return 0;
    817 }
    818 
    819 static int
    820 neo_round_blocksize(void *addr, int blk, int mode,
    821     const audio_params_t *param)
    822 {
    823 
    824 	return NM_BUFFSIZE / 2;
    825 }
    826 
    827 static int
    828 neo_trigger_output(void *addr, void *start, void *end, int blksize,
    829     void (*intr)(void *), void *arg, const audio_params_t *param)
    830 {
    831 	struct neo_softc *sc;
    832 	int ssz;
    833 
    834 	sc = addr;
    835 	sc->pintr = intr;
    836 	sc->parg = arg;
    837 
    838 	ssz = (param->precision == 16) ? 2 : 1;
    839 	if (param->channels == 2)
    840 		ssz <<= 1;
    841 
    842 	sc->pbufsize = ((char*)end - (char *)start);
    843 	sc->pblksize = blksize;
    844 	sc->pwmark = blksize;
    845 
    846 	nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
    847 	nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
    848 	nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
    849 	nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    850 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
    851 	    NM_PLAYBACK_ENABLE_FLAG);
    852 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
    853 
    854 	return 0;
    855 }
    856 
    857 static int
    858 neo_trigger_input(void *addr, void *start, void *end, int blksize,
    859     void (*intr)(void *), void *arg, const audio_params_t *param)
    860 {
    861 	struct neo_softc *sc;
    862 	int ssz;
    863 
    864 	sc = addr;
    865 	sc->rintr = intr;
    866 	sc->rarg = arg;
    867 
    868 	ssz = (param->precision == 16) ? 2 : 1;
    869 	if (param->channels == 2)
    870 		ssz <<= 1;
    871 
    872 	sc->rbufsize = ((char*)end - (char *)start);
    873 	sc->rblksize = blksize;
    874 	sc->rwmark = blksize;
    875 
    876 	nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
    877 	nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
    878 	nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
    879 	nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
    880 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
    881 	    NM_RECORD_ENABLE_FLAG);
    882 
    883 	return 0;
    884 }
    885 
    886 static int
    887 neo_halt_output(void *addr)
    888 {
    889 	struct neo_softc *sc;
    890 
    891 	sc = (struct neo_softc *)addr;
    892 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
    893 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
    894 	sc->pintr = 0;
    895 
    896 	return 0;
    897 }
    898 
    899 static int
    900 neo_halt_input(void *addr)
    901 {
    902 	struct neo_softc *sc;
    903 
    904 	sc = (struct neo_softc *)addr;
    905 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    906 	sc->rintr = 0;
    907 
    908 	return 0;
    909 }
    910 
    911 static int
    912 neo_getdev(void *addr, struct audio_device *retp)
    913 {
    914 
    915 	*retp = neo_device;
    916 	return 0;
    917 }
    918 
    919 static int
    920 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    921 {
    922 	struct neo_softc *sc;
    923 
    924 	sc = addr;
    925 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    926 }
    927 
    928 static int
    929 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    930 {
    931 	struct neo_softc *sc;
    932 
    933 	sc = addr;
    934 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    935 }
    936 
    937 static int
    938 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
    939 {
    940 	struct neo_softc *sc;
    941 
    942 	sc = addr;
    943 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
    944 }
    945 
    946 static void *
    947 neo_malloc(void *addr, int direction, size_t size,
    948     struct malloc_type *pool, int flags)
    949 {
    950 	struct neo_softc *sc;
    951 	void *rv;
    952 
    953 	sc = addr;
    954 	rv = NULL;
    955 	switch (direction) {
    956 	case AUMODE_PLAY:
    957 		if (sc->pbuf_allocated == 0) {
    958 			rv = (void *) sc->pbuf_vaddr;
    959 			sc->pbuf_allocated = 1;
    960 		}
    961 		break;
    962 
    963 	case AUMODE_RECORD:
    964 		if (sc->rbuf_allocated == 0) {
    965 			rv = (void *) sc->rbuf_vaddr;
    966 			sc->rbuf_allocated = 1;
    967 		}
    968 		break;
    969 	}
    970 
    971 	return rv;
    972 }
    973 
    974 static void
    975 neo_free(void *addr, void *ptr, struct malloc_type *pool)
    976 {
    977 	struct neo_softc *sc;
    978 	vaddr_t v;
    979 
    980 	sc = addr;
    981 	v = (vaddr_t)ptr;
    982 	if (v == sc->pbuf_vaddr)
    983 		sc->pbuf_allocated = 0;
    984 	else if (v == sc->rbuf_vaddr)
    985 		sc->rbuf_allocated = 0;
    986 	else
    987 		printf("neo_free: bad address %p\n", ptr);
    988 }
    989 
    990 static size_t
    991 neo_round_buffersize(void *addr, int direction,
    992     size_t size)
    993 {
    994 
    995 	return NM_BUFFSIZE;
    996 }
    997 
    998 static paddr_t
    999 neo_mappage(void *addr, void *mem, off_t off, int prot)
   1000 {
   1001 	struct neo_softc *sc;
   1002 	vaddr_t v;
   1003 	bus_addr_t pciaddr;
   1004 
   1005 	sc = addr;
   1006 	v = (vaddr_t)mem;
   1007 	if (v == sc->pbuf_vaddr)
   1008 		pciaddr = sc->pbuf_pciaddr;
   1009 	else if (v == sc->rbuf_vaddr)
   1010 		pciaddr = sc->rbuf_pciaddr;
   1011 	else
   1012 		return -1;
   1013 
   1014 	return bus_space_mmap(sc->bufiot, pciaddr, off, prot,
   1015 	    BUS_SPACE_MAP_LINEAR);
   1016 }
   1017 
   1018 static int
   1019 neo_get_props(void *addr)
   1020 {
   1021 
   1022 	return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
   1023 	    AUDIO_PROP_FULLDUPLEX;
   1024 }
   1025