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