Home | History | Annotate | Line # | Download | only in pci
yds.c revision 1.33
      1 /*	$NetBSD: yds.c,v 1.33 2006/08/17 17:11:28 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000, 2001 Kazuki Sakamoto and Minoura Makoto.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Yamaha YMF724[B-F]/740[B-C]/744/754
     30  *
     31  * Documentation links:
     32  * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/
     33  * - ftp://ftp.alsa-project.org/pub/manuals/yamaha/pci/
     34  *
     35  * TODO:
     36  * - FM synth volume (difficult: mixed before ac97)
     37  * - Digital in/out (SPDIF) support
     38  * - Effect??
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: yds.c,v 1.33 2006/08/17 17:11:28 christos Exp $");
     43 
     44 #include "mpu.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/fcntl.h>
     50 #include <sys/malloc.h>
     51 #include <sys/device.h>
     52 #include <sys/proc.h>
     53 
     54 #include <dev/pci/pcidevs.h>
     55 #include <dev/pci/pcireg.h>
     56 #include <dev/pci/pcivar.h>
     57 
     58 #include <sys/audioio.h>
     59 #include <dev/audio_if.h>
     60 #include <dev/mulaw.h>
     61 #include <dev/auconv.h>
     62 #include <dev/ic/ac97reg.h>
     63 #include <dev/ic/ac97var.h>
     64 #include <dev/ic/mpuvar.h>
     65 
     66 #include <machine/bus.h>
     67 #include <machine/intr.h>
     68 
     69 #include <dev/microcode/yds/yds_hwmcode.h>
     70 #include <dev/pci/ydsreg.h>
     71 #include <dev/pci/ydsvar.h>
     72 
     73 /* Debug */
     74 #undef YDS_USE_REC_SLOT
     75 #define YDS_USE_P44
     76 
     77 #ifdef AUDIO_DEBUG
     78 # define DPRINTF(x)	if (ydsdebug) printf x
     79 # define DPRINTFN(n,x)	if (ydsdebug>(n)) printf x
     80 int	ydsdebug = 0;
     81 #else
     82 # define DPRINTF(x)
     83 # define DPRINTFN(n,x)
     84 #endif
     85 #ifdef YDS_USE_REC_SLOT
     86 # define YDS_INPUT_SLOT 0	/* REC slot = ADC + loopbacks */
     87 #else
     88 # define YDS_INPUT_SLOT 1	/* ADC slot */
     89 #endif
     90 
     91 static int	yds_match(struct device *, struct cfdata *, void *);
     92 static void	yds_attach(struct device *, struct device *, void *);
     93 static int	yds_intr(void *);
     94 
     95 #define DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
     96 #define KERNADDR(p)	((void *)((p)->addr))
     97 
     98 static int	yds_allocmem(struct yds_softc *, size_t, size_t,
     99 			     struct yds_dma *);
    100 static int	yds_freemem(struct yds_softc *, struct yds_dma *);
    101 
    102 #ifndef AUDIO_DEBUG
    103 #define YWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
    104 #define YWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
    105 #define YWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
    106 #define YREAD1(sc, r)	bus_space_read_1((sc)->memt, (sc)->memh, (r))
    107 #define YREAD2(sc, r)	bus_space_read_2((sc)->memt, (sc)->memh, (r))
    108 #define YREAD4(sc, r)	bus_space_read_4((sc)->memt, (sc)->memh, (r))
    109 #else
    110 static uint16_t YREAD2(struct yds_softc *sc, bus_size_t r)
    111 {
    112 	DPRINTFN(5, (" YREAD2(0x%lX)\n", (unsigned long)r));
    113 	return bus_space_read_2(sc->memt, sc->memh, r);
    114 }
    115 
    116 static uint32_t YREAD4(struct yds_softc *sc, bus_size_t r)
    117 {
    118 	DPRINTFN(5, (" YREAD4(0x%lX)\n", (unsigned long)r));
    119 	return bus_space_read_4(sc->memt, sc->memh, r);
    120 }
    121 
    122 #ifdef notdef
    123 static void YWRITE1(struct yds_softc *sc, bus_size_t r, uint8_t x)
    124 {
    125 	DPRINTFN(5, (" YWRITE1(0x%lX,0x%lX)\n", (unsigned long)r,
    126 		     (unsigned long)x));
    127 	bus_space_write_1(sc->memt, sc->memh, r, x);
    128 }
    129 #endif
    130 
    131 static void YWRITE2(struct yds_softc *sc, bus_size_t r, uint16_t x)
    132 {
    133 	DPRINTFN(5, (" YWRITE2(0x%lX,0x%lX)\n", (unsigned long)r,
    134 		     (unsigned long)x));
    135 	bus_space_write_2(sc->memt, sc->memh, r, x);
    136 }
    137 
    138 static void YWRITE4(struct yds_softc *sc, bus_size_t r, uint32_t x)
    139 {
    140 	DPRINTFN(5, (" YWRITE4(0x%lX,0x%lX)\n", (unsigned long)r,
    141 		     (unsigned long)x));
    142 	bus_space_write_4(sc->memt, sc->memh, r, x);
    143 }
    144 #endif
    145 
    146 #define	YWRITEREGION4(sc, r, x, c)	\
    147 	bus_space_write_region_4((sc)->memt, (sc)->memh, (r), (x), (c) / 4)
    148 
    149 CFATTACH_DECL(yds, sizeof(struct yds_softc),
    150     yds_match, yds_attach, NULL, NULL);
    151 
    152 static int	yds_open(void *, int);
    153 static void	yds_close(void *);
    154 static int	yds_query_encoding(void *, struct audio_encoding *);
    155 static int	yds_set_params(void *, int, int, audio_params_t *,
    156 			       audio_params_t *, stream_filter_list_t *,
    157 			       stream_filter_list_t *);
    158 static int	yds_round_blocksize(void *, int, int, const audio_params_t *);
    159 static int	yds_trigger_output(void *, void *, void *, int,
    160 				   void (*)(void *), void *,
    161 				   const audio_params_t *);
    162 static int	yds_trigger_input(void *, void *, void *, int,
    163 				  void (*)(void *), void *,
    164 				  const audio_params_t *);
    165 static int	yds_halt_output(void *);
    166 static int	yds_halt_input(void *);
    167 static int	yds_getdev(void *, struct audio_device *);
    168 static int	yds_mixer_set_port(void *, mixer_ctrl_t *);
    169 static int	yds_mixer_get_port(void *, mixer_ctrl_t *);
    170 static void   *yds_malloc(void *, int, size_t, struct malloc_type *, int);
    171 static void	yds_free(void *, void *, struct malloc_type *);
    172 static size_t	yds_round_buffersize(void *, int, size_t);
    173 static paddr_t yds_mappage(void *, void *, off_t, int);
    174 static int	yds_get_props(void *);
    175 static int	yds_query_devinfo(void *, mixer_devinfo_t *);
    176 
    177 static int     yds_attach_codec(void *, struct ac97_codec_if *);
    178 static int	yds_read_codec(void *, uint8_t, uint16_t *);
    179 static int	yds_write_codec(void *, uint8_t, uint16_t);
    180 static int     yds_reset_codec(void *);
    181 
    182 static u_int	yds_get_dstype(int);
    183 static int	yds_download_mcode(struct yds_softc *);
    184 static int	yds_allocate_slots(struct yds_softc *);
    185 static void	yds_configure_legacy(struct device *);
    186 static void	yds_enable_dsp(struct yds_softc *);
    187 static int	yds_disable_dsp(struct yds_softc *);
    188 static int	yds_ready_codec(struct yds_codec_softc *);
    189 static int	yds_halt(struct yds_softc *);
    190 static uint32_t yds_get_lpfq(u_int);
    191 static uint32_t yds_get_lpfk(u_int);
    192 static struct yds_dma *yds_find_dma(struct yds_softc *, void *);
    193 
    194 static int	yds_init(struct yds_softc *);
    195 static void	yds_powerhook(int, void *);
    196 
    197 #ifdef AUDIO_DEBUG
    198 static void	yds_dump_play_slot(struct yds_softc *, int);
    199 #define	YDS_DUMP_PLAY_SLOT(n, sc, bank) \
    200 	if (ydsdebug > (n)) yds_dump_play_slot(sc, bank)
    201 #else
    202 #define	YDS_DUMP_PLAY_SLOT(n, sc, bank)
    203 #endif /* AUDIO_DEBUG */
    204 
    205 static const struct audio_hw_if yds_hw_if = {
    206 	yds_open,
    207 	yds_close,
    208 	NULL,
    209 	yds_query_encoding,
    210 	yds_set_params,
    211 	yds_round_blocksize,
    212 	NULL,
    213 	NULL,
    214 	NULL,
    215 	NULL,
    216 	NULL,
    217 	yds_halt_output,
    218 	yds_halt_input,
    219 	NULL,
    220 	yds_getdev,
    221 	NULL,
    222 	yds_mixer_set_port,
    223 	yds_mixer_get_port,
    224 	yds_query_devinfo,
    225 	yds_malloc,
    226 	yds_free,
    227 	yds_round_buffersize,
    228 	yds_mappage,
    229 	yds_get_props,
    230 	yds_trigger_output,
    231 	yds_trigger_input,
    232 	NULL,
    233 };
    234 
    235 static const struct audio_device yds_device = {
    236 	"Yamaha DS-1",
    237 	"",
    238 	"yds"
    239 };
    240 
    241 static const struct {
    242 	uint	id;
    243 	u_int	flags;
    244 #define YDS_CAP_MCODE_1			0x0001
    245 #define YDS_CAP_MCODE_1E		0x0002
    246 #define YDS_CAP_LEGACY_SELECTABLE	0x0004
    247 #define YDS_CAP_LEGACY_FLEXIBLE		0x0008
    248 #define YDS_CAP_HAS_P44			0x0010
    249 } yds_chip_capabliity_list[] = {
    250 	{ PCI_PRODUCT_YAMAHA_YMF724,
    251 	  YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },
    252 	/* 740[C] has only 32 slots.  But anyway we use only 2 */
    253 	{ PCI_PRODUCT_YAMAHA_YMF740,
    254 	  YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },	/* XXX NOT TESTED */
    255 	{ PCI_PRODUCT_YAMAHA_YMF740C,
    256 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
    257 	{ PCI_PRODUCT_YAMAHA_YMF724F,
    258 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
    259 	{ PCI_PRODUCT_YAMAHA_YMF744B,
    260 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE },
    261 	{ PCI_PRODUCT_YAMAHA_YMF754,
    262 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE|YDS_CAP_HAS_P44 },
    263 	{ 0, 0 }
    264 };
    265 #ifdef AUDIO_DEBUG
    266 #define YDS_CAP_BITS	"\020\005P44\004LEGFLEX\003LEGSEL\002MCODE1E\001MCODE1"
    267 #endif
    268 
    269 static const struct audio_format yds_formats[] = {
    270 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    271 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    272 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    273 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    274 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    275 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    276 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    277 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    278 };
    279 #define	YDS_NFORMATS	(sizeof(yds_formats) / sizeof(struct audio_format))
    280 
    281 #ifdef AUDIO_DEBUG
    282 static void
    283 yds_dump_play_slot(struct yds_softc *sc, int bank)
    284 {
    285 	int i, j;
    286 	uint32_t *p;
    287 	uint32_t num;
    288 	char *pa;
    289 
    290 	for (i = 0; i < N_PLAY_SLOTS; i++) {
    291 		printf("pbankp[%d] = %p,", i*2, sc->pbankp[i*2]);
    292 		printf("pbankp[%d] = %p\n", i*2+1, sc->pbankp[i*2+1]);
    293 	}
    294 
    295 	pa = (char *)DMAADDR(&sc->sc_ctrldata) + sc->pbankoff;
    296 	p = (uint32_t *)sc->ptbl;
    297 	printf("ptbl + 0: %d\n", *p++);
    298 	for (i = 0; i < N_PLAY_SLOTS; i++) {
    299 		printf("ptbl + %d: 0x%x, should be %p\n",
    300 		       i+1, *p,
    301 		       pa + i * sizeof(struct play_slot_ctrl_bank) *
    302 				N_PLAY_SLOT_CTRL_BANK);
    303 		p++;
    304 	}
    305 
    306 	num = le32toh(*(uint32_t*)sc->ptbl);
    307 	printf("numofplay = %d\n", num);
    308 
    309 	for (i = 0; i < num; i++) {
    310 		p = (uint32_t *)sc->pbankp[i*2];
    311 
    312 		printf("  pbankp[%d], bank 0 : %p\n", i*2, p);
    313 		for (j = 0;
    314 		     j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
    315 		     j++) {
    316 			printf("    0x%02x: 0x%08x\n",
    317 			       (unsigned)(j * sizeof(uint32_t)),
    318 			       (unsigned)*p++);
    319 		}
    320 
    321 		p = (uint32_t *)sc->pbankp[i*2 + 1];
    322 		printf("  pbankp[%d], bank 1 : %p\n", i*2 + 1, p);
    323 		for (j = 0;
    324 		     j < sizeof(struct play_slot_ctrl_bank) / sizeof(uint32_t);
    325 		     j++) {
    326 			printf("    0x%02x: 0x%08x\n",
    327 			       (unsigned)(j * sizeof(uint32_t)),
    328 			       (unsigned)*p++);
    329 		}
    330 	}
    331 }
    332 #endif /* AUDIO_DEBUG */
    333 
    334 static u_int
    335 yds_get_dstype(int id)
    336 {
    337 	int i;
    338 
    339 	for (i = 0; yds_chip_capabliity_list[i].id; i++) {
    340 		if (PCI_PRODUCT(id) == yds_chip_capabliity_list[i].id)
    341 			return yds_chip_capabliity_list[i].flags;
    342 	}
    343 
    344 	return -1;
    345 }
    346 
    347 static int
    348 yds_download_mcode(struct yds_softc *sc)
    349 {
    350 	static struct {
    351 		const uint32_t *mcode;
    352 		size_t size;
    353 	} ctrls[] = {
    354 		{yds_ds1_ctrl_mcode, sizeof(yds_ds1_ctrl_mcode)},
    355 		{yds_ds1e_ctrl_mcode, sizeof(yds_ds1e_ctrl_mcode)},
    356 	};
    357 	u_int ctrl;
    358 	const uint32_t *p;
    359 	size_t size;
    360 	int dstype;
    361 
    362 	if (sc->sc_flags & YDS_CAP_MCODE_1)
    363 		dstype = YDS_DS_1;
    364 	else if (sc->sc_flags & YDS_CAP_MCODE_1E)
    365 		dstype = YDS_DS_1E;
    366 	else
    367 		return 1;	/* unknown */
    368 
    369 	if (yds_disable_dsp(sc))
    370 		return 1;
    371 
    372 	/* Software reset */
    373 	YWRITE4(sc, YDS_MODE, YDS_MODE_RESET);
    374 	YWRITE4(sc, YDS_MODE, 0);
    375 
    376 	YWRITE4(sc, YDS_MAPOF_REC, 0);
    377 	YWRITE4(sc, YDS_MAPOF_EFFECT, 0);
    378 	YWRITE4(sc, YDS_PLAY_CTRLBASE, 0);
    379 	YWRITE4(sc, YDS_REC_CTRLBASE, 0);
    380 	YWRITE4(sc, YDS_EFFECT_CTRLBASE, 0);
    381 	YWRITE4(sc, YDS_WORK_BASE, 0);
    382 
    383 	ctrl = YREAD2(sc, YDS_GLOBAL_CONTROL);
    384 	YWRITE2(sc, YDS_GLOBAL_CONTROL, ctrl & ~0x0007);
    385 
    386 	/* Download DSP microcode. */
    387 	p = yds_dsp_mcode;
    388 	size = sizeof(yds_dsp_mcode);
    389 	YWRITEREGION4(sc, YDS_DSP_INSTRAM, p, size);
    390 
    391 	/* Download CONTROL microcode. */
    392 	p = ctrls[dstype].mcode;
    393 	size = ctrls[dstype].size;
    394 	YWRITEREGION4(sc, YDS_CTRL_INSTRAM, p, size);
    395 
    396 	yds_enable_dsp(sc);
    397 	delay(10 * 1000);		/* nessesary on my 724F (??) */
    398 
    399 	return 0;
    400 }
    401 
    402 static int
    403 yds_allocate_slots(struct yds_softc *sc)
    404 {
    405 	size_t pcs, rcs, ecs, ws, memsize;
    406 	void *mp;
    407 	uint32_t da;		/* DMA address */
    408 	char *va;		/* KVA */
    409 	off_t cb;
    410 	int i;
    411 	struct yds_dma *p;
    412 
    413 	/* Alloc DSP Control Data */
    414 	pcs = YREAD4(sc, YDS_PLAY_CTRLSIZE) * sizeof(uint32_t);
    415 	rcs = YREAD4(sc, YDS_REC_CTRLSIZE) * sizeof(uint32_t);
    416 	ecs = YREAD4(sc, YDS_EFFECT_CTRLSIZE) * sizeof(uint32_t);
    417 	ws = WORK_SIZE;
    418 	YWRITE4(sc, YDS_WORK_SIZE, ws / sizeof(uint32_t));
    419 
    420 	DPRINTF(("play control size : %d\n", (unsigned int)pcs));
    421 	DPRINTF(("rec control size : %d\n", (unsigned int)rcs));
    422 	DPRINTF(("eff control size : %d\n", (unsigned int)ecs));
    423 	DPRINTF(("work size : %d\n", (unsigned int)ws));
    424 #ifdef DIAGNOSTIC
    425 	if (pcs != sizeof(struct play_slot_ctrl_bank)) {
    426 		printf("%s: invalid play slot ctrldata %d != %d\n",
    427 		       sc->sc_dev.dv_xname, (unsigned int)pcs,
    428 		       (unsigned int)sizeof(struct play_slot_ctrl_bank));
    429 	if (rcs != sizeof(struct rec_slot_ctrl_bank))
    430 		printf("%s: invalid rec slot ctrldata %d != %d\n",
    431 		       sc->sc_dev.dv_xname, (unsigned int)rcs,
    432 		       (unsigned int)sizeof(struct rec_slot_ctrl_bank));
    433 	}
    434 #endif
    435 
    436 	memsize = N_PLAY_SLOTS*N_PLAY_SLOT_CTRL_BANK*pcs +
    437 		  N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK*rcs + ws;
    438 	memsize += (N_PLAY_SLOTS+1)*sizeof(uint32_t);
    439 
    440 	p = &sc->sc_ctrldata;
    441 	if (KERNADDR(p) == NULL) {
    442 		i = yds_allocmem(sc, memsize, 16, p);
    443 		if (i) {
    444 			printf("%s: couldn't alloc/map DSP DMA buffer, reason %d\n",
    445 				sc->sc_dev.dv_xname, i);
    446 			free(p, M_DEVBUF);
    447 			return 1;
    448 		}
    449 	}
    450 	mp = KERNADDR(p);
    451 	da = DMAADDR(p);
    452 
    453 	DPRINTF(("mp:%p, DMA addr:%p\n",
    454 		 mp, (void *)sc->sc_ctrldata.map->dm_segs[0].ds_addr));
    455 
    456 	memset(mp, 0, memsize);
    457 
    458 	/* Work space */
    459 	cb = 0;
    460 	va = (uint8_t *)mp;
    461 	YWRITE4(sc, YDS_WORK_BASE, da + cb);
    462 	cb += ws;
    463 
    464 	/* Play control data table */
    465 	sc->ptbl = (uint32_t *)(va + cb);
    466 	sc->ptbloff = cb;
    467 	YWRITE4(sc, YDS_PLAY_CTRLBASE, da + cb);
    468 	cb += (N_PLAY_SLOT_CTRL + 1) * sizeof(uint32_t);
    469 
    470 	/* Record slot control data */
    471 	sc->rbank = (struct rec_slot_ctrl_bank *)(va + cb);
    472 	YWRITE4(sc, YDS_REC_CTRLBASE, da + cb);
    473 	sc->rbankoff = cb;
    474 	cb += N_REC_SLOT_CTRL * N_REC_SLOT_CTRL_BANK * rcs;
    475 
    476 #if 0
    477 	/* Effect slot control data -- unused */
    478 	YWRITE4(sc, YDS_EFFECT_CTRLBASE, da + cb);
    479 	cb += N_EFFECT_SLOT_CTRL * N_EFFECT_SLOT_CTRL_BANK * ecs;
    480 #endif
    481 
    482 	/* Play slot control data */
    483 	sc->pbankoff = cb;
    484 	for (i=0; i < N_PLAY_SLOT_CTRL; i++) {
    485 		sc->pbankp[i*2] = (struct play_slot_ctrl_bank *)(va + cb);
    486 		*(sc->ptbl + i+1) = htole32(da + cb);
    487 		cb += pcs;
    488 
    489 		sc->pbankp[i*2+1] = (struct play_slot_ctrl_bank *)(va + cb);
    490 		cb += pcs;
    491 	}
    492 	/* Sync play control data table */
    493 	bus_dmamap_sync(sc->sc_dmatag, p->map,
    494 			sc->ptbloff, (N_PLAY_SLOT_CTRL+1) * sizeof(uint32_t),
    495 			BUS_DMASYNC_PREWRITE);
    496 
    497 	return 0;
    498 }
    499 
    500 static void
    501 yds_enable_dsp(struct yds_softc *sc)
    502 {
    503 
    504 	YWRITE4(sc, YDS_CONFIG, YDS_DSP_SETUP);
    505 }
    506 
    507 static int
    508 yds_disable_dsp(struct yds_softc *sc)
    509 {
    510 	int to;
    511 	uint32_t data;
    512 
    513 	data = YREAD4(sc, YDS_CONFIG);
    514 	if (data)
    515 		YWRITE4(sc, YDS_CONFIG, YDS_DSP_DISABLE);
    516 
    517 	for (to = 0; to < YDS_WORK_TIMEOUT; to++) {
    518 		if ((YREAD4(sc, YDS_STATUS) & YDS_STAT_WORK) == 0)
    519 			return 0;
    520 		delay(1);
    521 	}
    522 
    523 	return 1;
    524 }
    525 
    526 static int
    527 yds_match(struct device *parent, struct cfdata *match, void *aux)
    528 {
    529 	struct pci_attach_args *pa;
    530 
    531 	pa = (struct pci_attach_args *)aux;
    532 	switch (PCI_VENDOR(pa->pa_id)) {
    533 	case PCI_VENDOR_YAMAHA:
    534 		switch (PCI_PRODUCT(pa->pa_id)) {
    535 		case PCI_PRODUCT_YAMAHA_YMF724:
    536 		case PCI_PRODUCT_YAMAHA_YMF740:
    537 		case PCI_PRODUCT_YAMAHA_YMF740C:
    538 		case PCI_PRODUCT_YAMAHA_YMF724F:
    539 		case PCI_PRODUCT_YAMAHA_YMF744B:
    540 		case PCI_PRODUCT_YAMAHA_YMF754:
    541 			return 1;
    542 		}
    543 		break;
    544 	}
    545 
    546 	return 0;
    547 }
    548 
    549 /*
    550  * This routine is called after all the ISA devices are configured,
    551  * to avoid conflict.
    552  */
    553 static void
    554 yds_configure_legacy(struct device *arg)
    555 #define FLEXIBLE	(sc->sc_flags & YDS_CAP_LEGACY_FLEXIBLE)
    556 #define SELECTABLE	(sc->sc_flags & YDS_CAP_LEGACY_SELECTABLE)
    557 {
    558 	static const bus_addr_t opl_addrs[] = {0x388, 0x398, 0x3A0, 0x3A8};
    559 	static const bus_addr_t mpu_addrs[] = {0x330, 0x300, 0x332, 0x334};
    560 	struct yds_softc *sc;
    561 	pcireg_t reg;
    562 	struct device *dev;
    563 	int i;
    564 
    565 	sc = (struct yds_softc*) arg;
    566 	if (!FLEXIBLE && !SELECTABLE)
    567 		return;
    568 
    569 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY);
    570 	reg &= ~0x8133c03f;	/* these bits are out of interest */
    571 	reg |= ((YDS_PCI_EX_LEGACY_IMOD) |
    572 		(YDS_PCI_LEGACY_FMEN |
    573 		 YDS_PCI_LEGACY_MEN /*| YDS_PCI_LEGACY_MIEN*/));
    574 	reg |= YDS_PCI_EX_LEGACY_SMOD_DISABLE;
    575 	if (FLEXIBLE) {
    576 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
    577 		delay(100*1000);
    578 	}
    579 
    580 	/* Look for OPL */
    581 	dev = 0;
    582 	for (i = 0; i < sizeof(opl_addrs) / sizeof(bus_addr_t); i++) {
    583 		if (SELECTABLE) {
    584 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    585 				       YDS_PCI_LEGACY, reg | (i << (0+16)));
    586 			delay(100*1000);	/* wait 100ms */
    587 		} else
    588 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    589 				       YDS_PCI_FM_BA, opl_addrs[i]);
    590 		if (bus_space_map(sc->sc_opl_iot,
    591 				  opl_addrs[i], 4, 0, &sc->sc_opl_ioh) == 0) {
    592 			struct audio_attach_args aa;
    593 
    594 			aa.type = AUDIODEV_TYPE_OPL;
    595 			aa.hwif = aa.hdl = NULL;
    596 			dev = config_found(&sc->sc_dev, &aa, audioprint);
    597 			if (dev == 0)
    598 				bus_space_unmap(sc->sc_opl_iot,
    599 						sc->sc_opl_ioh, 4);
    600 			else {
    601 				if (SELECTABLE)
    602 					reg |= (i << (0+16));
    603 				break;
    604 			}
    605 		}
    606 	}
    607 	if (dev == 0) {
    608 		reg &= ~YDS_PCI_LEGACY_FMEN;
    609 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    610 			       YDS_PCI_LEGACY, reg);
    611 	} else {
    612 		/* Max. volume */
    613 		YWRITE4(sc, YDS_LEGACY_OUT_VOLUME, 0x3fff3fff);
    614 		YWRITE4(sc, YDS_LEGACY_REC_VOLUME, 0x3fff3fff);
    615 	}
    616 
    617 	/* Look for MPU */
    618 	dev = 0;
    619 	for (i = 0; i < sizeof(mpu_addrs) / sizeof(bus_addr_t); i++) {
    620 		if (SELECTABLE)
    621 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    622 				       YDS_PCI_LEGACY, reg | (i << (4+16)));
    623 		else
    624 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
    625 				       YDS_PCI_MPU_BA, mpu_addrs[i]);
    626 		if (bus_space_map(sc->sc_mpu_iot,
    627 				  mpu_addrs[i], 2, 0, &sc->sc_mpu_ioh) == 0) {
    628 			struct audio_attach_args aa;
    629 
    630 			aa.type = AUDIODEV_TYPE_MPU;
    631 			aa.hwif = aa.hdl = NULL;
    632 			dev = config_found(&sc->sc_dev, &aa, audioprint);
    633 			if (dev == 0)
    634 				bus_space_unmap(sc->sc_mpu_iot,
    635 						sc->sc_mpu_ioh, 2);
    636 			else {
    637 				if (SELECTABLE)
    638 					reg |= (i << (4+16));
    639 				break;
    640 			}
    641 		}
    642 	}
    643 	if (dev == 0) {
    644 		reg &= ~(YDS_PCI_LEGACY_MEN | YDS_PCI_LEGACY_MIEN);
    645 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
    646 	}
    647 	sc->sc_mpu = dev;
    648 }
    649 #undef FLEXIBLE
    650 #undef SELECTABLE
    651 
    652 static int
    653 yds_init(struct yds_softc *sc)
    654 {
    655 	uint32_t reg;
    656 
    657 	DPRINTF(("yds_init()\n"));
    658 
    659 	/* Download microcode */
    660 	if (yds_download_mcode(sc)) {
    661 		printf("%s: download microcode failed\n", sc->sc_dev.dv_xname);
    662 		return 1;
    663 	}
    664 
    665 	/* Allocate DMA buffers */
    666 	if (yds_allocate_slots(sc)) {
    667 		printf("%s: could not allocate slots\n", sc->sc_dev.dv_xname);
    668 		return 1;
    669 	}
    670 
    671 	/* Warm reset */
    672 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
    673 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL,
    674 		reg | YDS_DSCTRL_WRST);
    675 	delay(50000);
    676 
    677 	return 0;
    678 }
    679 
    680 static void
    681 yds_powerhook(int why, void *addr)
    682 {
    683 	struct yds_softc *sc;
    684 	pci_chipset_tag_t pc;
    685 	pcitag_t tag;
    686 	pcireg_t reg;
    687 	int s;
    688 
    689 	sc = (struct yds_softc *)addr;
    690 	pc = sc->sc_pc;
    691 	tag = sc->sc_pcitag;
    692 
    693 	s = splaudio();
    694 	switch (why) {
    695 	case PWR_SUSPEND:
    696 		pci_conf_capture(pc, tag, &sc->sc_pciconf);
    697 
    698 		sc->sc_dsctrl = pci_conf_read(pc, tag, YDS_PCI_DSCTRL);
    699 		sc->sc_legacy = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
    700 		sc->sc_ba[0] = pci_conf_read(pc, tag, YDS_PCI_FM_BA);
    701 		sc->sc_ba[1] = pci_conf_read(pc, tag, YDS_PCI_MPU_BA);
    702 		break;
    703 	case PWR_RESUME:
    704 		pci_conf_restore(pc, tag, &sc->sc_pciconf);
    705 
    706 		/* Disable legacy mode */
    707 		reg = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
    708 		pci_conf_write(pc, tag, YDS_PCI_LEGACY,
    709 		    reg & YDS_PCI_LEGACY_LAD);
    710 
    711 		/* Enable the device. */
    712 		reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    713 		reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    714 			PCI_COMMAND_MASTER_ENABLE);
    715 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, reg);
    716 		reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    717 		if (yds_init(sc)) {
    718 			printf("%s: reinitialize failed\n",
    719 				sc->sc_dev.dv_xname);
    720 			splx(s);
    721 			return;
    722 		}
    723 		pci_conf_write(pc, tag, YDS_PCI_DSCTRL, sc->sc_dsctrl);
    724 		sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
    725 		break;
    726 	case PWR_SOFTRESUME:
    727 		pci_conf_write(pc, tag, YDS_PCI_LEGACY, sc->sc_legacy);
    728 		pci_conf_write(pc, tag, YDS_PCI_FM_BA, sc->sc_ba[0]);
    729 		pci_conf_write(pc, tag, YDS_PCI_MPU_BA, sc->sc_ba[1]);
    730 #if notyet
    731 		yds_configure_legacy(addr);
    732 #endif
    733 		break;
    734 	}
    735 	splx(s);
    736 
    737 	return;
    738 }
    739 
    740 static void
    741 yds_attach(struct device *parent, struct device *self, void *aux)
    742 {
    743 	struct yds_softc *sc;
    744 	struct pci_attach_args *pa;
    745 	pci_chipset_tag_t pc;
    746 	char const *intrstr;
    747 	pci_intr_handle_t ih;
    748 	pcireg_t reg;
    749 	struct yds_codec_softc *codec;
    750 	char devinfo[256];
    751 	int i, r, to;
    752 	int revision;
    753 	int ac97_id2;
    754 
    755 	sc = (struct yds_softc *)self;
    756 	pa = (struct pci_attach_args *)aux;
    757 	pc = pa->pa_pc;
    758 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    759 	revision = PCI_REVISION(pa->pa_class);
    760 	printf(": %s (rev. 0x%02x)\n", devinfo, revision);
    761 
    762 	/* Map register to memory */
    763 	if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
    764 			   &sc->memt, &sc->memh, NULL, NULL)) {
    765 		printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
    766 		return;
    767 	}
    768 
    769 	/* Map and establish the interrupt. */
    770 	if (pci_intr_map(pa, &ih)) {
    771 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    772 		return;
    773 	}
    774 	intrstr = pci_intr_string(pc, ih);
    775 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
    776 	if (sc->sc_ih == NULL) {
    777 		printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
    778 		if (intrstr != NULL)
    779 			printf(" at %s", intrstr);
    780 		printf("\n");
    781 		return;
    782 	}
    783 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    784 
    785 	sc->sc_dmatag = pa->pa_dmat;
    786 	sc->sc_pc = pc;
    787 	sc->sc_pcitag = pa->pa_tag;
    788 	sc->sc_id = pa->pa_id;
    789 	sc->sc_revision = revision;
    790 	sc->sc_flags = yds_get_dstype(sc->sc_id);
    791 #ifdef AUDIO_DEBUG
    792 	if (ydsdebug) {
    793 		char bits[80];
    794 
    795 		printf("%s: chip has %s\n", sc->sc_dev.dv_xname,
    796 		       bitmask_snprintf(sc->sc_flags, YDS_CAP_BITS, bits,
    797 					sizeof(bits)));
    798 	}
    799 #endif
    800 
    801 	/* Disable legacy mode */
    802 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
    803 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
    804 		       reg & YDS_PCI_LEGACY_LAD);
    805 
    806 	/* Enable the device. */
    807 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    808 	reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    809 		PCI_COMMAND_MASTER_ENABLE);
    810 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    811 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    812 
    813 	/* Mute all volumes */
    814 	for (i = 0x80; i < 0xc0; i += 2)
    815 		YWRITE2(sc, i, 0);
    816 
    817 	/* Initialize the device */
    818 	if (yds_init(sc)) {
    819 		printf("%s: initialize failed\n", sc->sc_dev.dv_xname);
    820 		return;
    821 	}
    822 
    823 	/*
    824 	 * Detect primary/secondary AC97
    825 	 *	YMF754 Hardware Specification Rev 1.01 page 24
    826 	 */
    827 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
    828 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
    829 	delay(400000);		/* Needed for 740C. */
    830 
    831 	/* Primary */
    832 	for (to = 0; to < AC97_TIMEOUT; to++) {
    833 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
    834 			break;
    835 		delay(1);
    836 	}
    837 	if (to == AC97_TIMEOUT) {
    838 		printf("%s: no AC97 available\n", sc->sc_dev.dv_xname);
    839 		return;
    840 	}
    841 
    842 	/* Secondary */
    843 	/* Secondary AC97 is used for 4ch audio. Currently unused. */
    844 	ac97_id2 = -1;
    845 	if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
    846 		goto detected;
    847 #if 0				/* reset secondary... */
    848 	YWRITE2(sc, YDS_GPIO_OCTRL,
    849 		YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
    850 	YWRITE2(sc, YDS_GPIO_FUNCE,
    851 		(YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
    852 #endif
    853 	for (to = 0; to < AC97_TIMEOUT; to++) {
    854 		if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
    855 			break;
    856 		delay(1);
    857 	}
    858 	if (to < AC97_TIMEOUT) {
    859 		/* detect id */
    860 		for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
    861 			YWRITE2(sc, AC97_CMD_ADDR,
    862 				AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
    863 
    864 			for (to = 0; to < AC97_TIMEOUT; to++) {
    865 				if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
    866 				    == 0)
    867 					goto detected;
    868 				delay(1);
    869 			}
    870 		}
    871 		if (ac97_id2 == 4)
    872 			ac97_id2 = -1;
    873 detected:
    874 		;
    875 	}
    876 
    877 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
    878 	delay (20);
    879 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
    880 	delay (400000);
    881 	for (to = 0; to < AC97_TIMEOUT; to++) {
    882 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
    883 			break;
    884 		delay(1);
    885 	}
    886 
    887 	/*
    888 	 * Attach ac97 codec
    889 	 */
    890 	for (i = 0; i < 2; i++) {
    891 		static struct {
    892 			int data;
    893 			int addr;
    894 		} statregs[] = {
    895 			{AC97_STAT_DATA1, AC97_STAT_ADDR1},
    896 			{AC97_STAT_DATA2, AC97_STAT_ADDR2},
    897 		};
    898 
    899 		if (i == 1 && ac97_id2 == -1)
    900 			break;		/* secondary ac97 not available */
    901 
    902 		codec = &sc->sc_codec[i];
    903 		memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
    904 		codec->sc = sc;
    905 		codec->id = i == 1 ? ac97_id2 : 0;
    906 		codec->status_data = statregs[i].data;
    907 		codec->status_addr = statregs[i].addr;
    908 		codec->host_if.arg = codec;
    909 		codec->host_if.attach = yds_attach_codec;
    910 		codec->host_if.read = yds_read_codec;
    911 		codec->host_if.write = yds_write_codec;
    912 		codec->host_if.reset = yds_reset_codec;
    913 
    914 		if ((r = ac97_attach(&codec->host_if, self)) != 0) {
    915 			printf("%s: can't attach codec (error 0x%X)\n",
    916 			       sc->sc_dev.dv_xname, r);
    917 			return;
    918 		}
    919 	}
    920 
    921 	if (0 != auconv_create_encodings(yds_formats, YDS_NFORMATS,
    922 					 &sc->sc_encodings))
    923 		return;
    924 
    925 	audio_attach_mi(&yds_hw_if, sc, &sc->sc_dev);
    926 
    927 	sc->sc_legacy_iot = pa->pa_iot;
    928 	config_defer((struct device*) sc, yds_configure_legacy);
    929 
    930 	powerhook_establish(yds_powerhook, sc);
    931 }
    932 
    933 static int
    934 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    935 {
    936 	struct yds_codec_softc *sc;
    937 
    938 	sc = sc_;
    939 	sc->codec_if = codec_if;
    940 	return 0;
    941 }
    942 
    943 static int
    944 yds_ready_codec(struct yds_codec_softc *sc)
    945 {
    946 	int to;
    947 
    948 	for (to = 0; to < AC97_TIMEOUT; to++) {
    949 		if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
    950 			return 0;
    951 		delay(1);
    952 	}
    953 
    954 	return 1;
    955 }
    956 
    957 static int
    958 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
    959 {
    960 	struct yds_codec_softc *sc;
    961 
    962 	sc = sc_;
    963 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
    964 
    965 	if (yds_ready_codec(sc)) {
    966 		printf("%s: yds_read_codec timeout\n",
    967 		       sc->sc->sc_dev.dv_xname);
    968 		return EIO;
    969 	}
    970 
    971 	if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
    972 	    sc->sc->sc_revision < 2) {
    973 		int i;
    974 		for (i=0; i<600; i++)
    975 			(void)YREAD2(sc->sc, sc->status_data);
    976 	}
    977 
    978 	*data = YREAD2(sc->sc, sc->status_data);
    979 
    980 	return 0;
    981 }
    982 
    983 static int
    984 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
    985 {
    986 	struct yds_codec_softc *sc;
    987 
    988 	sc = sc_;
    989 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
    990 	YWRITE2(sc->sc, AC97_CMD_DATA, data);
    991 
    992 	if (yds_ready_codec(sc)) {
    993 		printf("%s: yds_write_codec timeout\n",
    994 			sc->sc->sc_dev.dv_xname);
    995 		return EIO;
    996 	}
    997 
    998 	return 0;
    999 }
   1000 
   1001 /*
   1002  * XXX: Must handle the secondary differntly!!
   1003  */
   1004 static int
   1005 yds_reset_codec(void *sc_)
   1006 {
   1007 	struct yds_codec_softc *codec;
   1008 	struct yds_softc *sc;
   1009 	pcireg_t reg;
   1010 
   1011 	codec = sc_;
   1012 	sc = codec->sc;
   1013 	/* reset AC97 codec */
   1014 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
   1015 	if (reg & 0x03) {
   1016 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   1017 			       YDS_PCI_DSCTRL, reg & ~0x03);
   1018 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   1019 			       YDS_PCI_DSCTRL, reg | 0x03);
   1020 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   1021 			       YDS_PCI_DSCTRL, reg & ~0x03);
   1022 		delay(50000);
   1023 	}
   1024 
   1025 	yds_ready_codec(sc_);
   1026 	return 0;
   1027 }
   1028 
   1029 static int
   1030 yds_intr(void *p)
   1031 {
   1032 	struct yds_softc *sc;
   1033 	u_int status;
   1034 
   1035 	sc = p;
   1036 	status = YREAD4(sc, YDS_STATUS);
   1037 	DPRINTFN(1, ("yds_intr: status=%08x\n", status));
   1038 	if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
   1039 #if NMPU > 0
   1040 		if (sc->sc_mpu)
   1041 			return mpu_intr(sc->sc_mpu);
   1042 #endif
   1043 		return 0;
   1044 	}
   1045 
   1046 	if (status & YDS_STAT_TINT) {
   1047 		YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
   1048 		printf ("yds_intr: timeout!\n");
   1049 	}
   1050 
   1051 	if (status & YDS_STAT_INT) {
   1052 		int nbank;
   1053 
   1054 		nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
   1055 		/* Clear interrupt flag */
   1056 		YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
   1057 
   1058 		/* Buffer for the next frame is always ready. */
   1059 		YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
   1060 
   1061 		if (sc->sc_play.intr) {
   1062 			u_int dma, ccpu, blk, len;
   1063 
   1064 			/* Sync play slot control data */
   1065 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1066 					sc->pbankoff,
   1067 					sizeof(struct play_slot_ctrl_bank)*
   1068 					    le32toh(*sc->ptbl)*
   1069 					    N_PLAY_SLOT_CTRL_BANK,
   1070 					BUS_DMASYNC_POSTWRITE|
   1071 					BUS_DMASYNC_POSTREAD);
   1072 			dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
   1073 			ccpu = sc->sc_play.offset;
   1074 			blk = sc->sc_play.blksize;
   1075 			len = sc->sc_play.length;
   1076 
   1077 			if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
   1078 			    ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
   1079 				/* We can fill the next block */
   1080 				/* Sync ring buffer for previous write */
   1081 				bus_dmamap_sync(sc->sc_dmatag,
   1082 						sc->sc_play.dma->map,
   1083 						ccpu, blk,
   1084 						BUS_DMASYNC_POSTWRITE);
   1085 				sc->sc_play.intr(sc->sc_play.intr_arg);
   1086 				sc->sc_play.offset += blk;
   1087 				if (sc->sc_play.offset >= len) {
   1088 					sc->sc_play.offset -= len;
   1089 #ifdef DIAGNOSTIC
   1090 					if (sc->sc_play.offset != 0)
   1091 						printf ("Audio ringbuffer botch\n");
   1092 #endif
   1093 				}
   1094 				/* Sync ring buffer for next write */
   1095 				bus_dmamap_sync(sc->sc_dmatag,
   1096 						sc->sc_play.dma->map,
   1097 						ccpu, blk,
   1098 						BUS_DMASYNC_PREWRITE);
   1099 			}
   1100 		}
   1101 		if (sc->sc_rec.intr) {
   1102 			u_int dma, ccpu, blk, len;
   1103 
   1104 			/* Sync rec slot control data */
   1105 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1106 					sc->rbankoff,
   1107 					sizeof(struct rec_slot_ctrl_bank)*
   1108 					    N_REC_SLOT_CTRL*
   1109 					    N_REC_SLOT_CTRL_BANK,
   1110 					BUS_DMASYNC_POSTWRITE|
   1111 					BUS_DMASYNC_POSTREAD);
   1112 			dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
   1113 			ccpu = sc->sc_rec.offset;
   1114 			blk = sc->sc_rec.blksize;
   1115 			len = sc->sc_rec.length;
   1116 
   1117 			if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
   1118 			    ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
   1119 				/* We can drain the current block */
   1120 				/* Sync ring buffer first */
   1121 				bus_dmamap_sync(sc->sc_dmatag,
   1122 						sc->sc_rec.dma->map,
   1123 						ccpu, blk,
   1124 						BUS_DMASYNC_POSTREAD);
   1125 				sc->sc_rec.intr(sc->sc_rec.intr_arg);
   1126 				sc->sc_rec.offset += blk;
   1127 				if (sc->sc_rec.offset >= len) {
   1128 					sc->sc_rec.offset -= len;
   1129 #ifdef DIAGNOSTIC
   1130 					if (sc->sc_rec.offset != 0)
   1131 						printf ("Audio ringbuffer botch\n");
   1132 #endif
   1133 				}
   1134 				/* Sync ring buffer for next read */
   1135 				bus_dmamap_sync(sc->sc_dmatag,
   1136 						sc->sc_rec.dma->map,
   1137 						ccpu, blk,
   1138 						BUS_DMASYNC_PREREAD);
   1139 			}
   1140 		}
   1141 	}
   1142 
   1143 	return 1;
   1144 }
   1145 
   1146 static int
   1147 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
   1148 {
   1149 	int error;
   1150 
   1151 	p->size = size;
   1152 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
   1153 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
   1154 				 &p->nsegs, BUS_DMA_NOWAIT);
   1155 	if (error)
   1156 		return error;
   1157 
   1158 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
   1159 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   1160 	if (error)
   1161 		goto free;
   1162 
   1163 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
   1164 				  0, BUS_DMA_NOWAIT, &p->map);
   1165 	if (error)
   1166 		goto unmap;
   1167 
   1168 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
   1169 				BUS_DMA_NOWAIT);
   1170 	if (error)
   1171 		goto destroy;
   1172 	return 0;
   1173 
   1174 destroy:
   1175 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1176 unmap:
   1177 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1178 free:
   1179 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1180 	return error;
   1181 }
   1182 
   1183 static int
   1184 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
   1185 {
   1186 
   1187 	bus_dmamap_unload(sc->sc_dmatag, p->map);
   1188 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
   1189 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
   1190 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
   1191 	return 0;
   1192 }
   1193 
   1194 static int
   1195 yds_open(void *addr, int flags)
   1196 {
   1197 	struct yds_softc *sc;
   1198 	uint32_t mode;
   1199 
   1200 	sc = addr;
   1201 	/* Select bank 0. */
   1202 	YWRITE4(sc, YDS_CONTROL_SELECT, 0);
   1203 
   1204 	/* Start the DSP operation. */
   1205 	mode = YREAD4(sc, YDS_MODE);
   1206 	mode |= YDS_MODE_ACTV;
   1207 	mode &= ~YDS_MODE_ACTV2;
   1208 	YWRITE4(sc, YDS_MODE, mode);
   1209 
   1210 	return 0;
   1211 }
   1212 
   1213 /*
   1214  * Close function is called at splaudio().
   1215  */
   1216 static void
   1217 yds_close(void *addr)
   1218 {
   1219 
   1220 	yds_halt(addr);
   1221 }
   1222 
   1223 static int
   1224 yds_query_encoding(void *addr, struct audio_encoding *fp)
   1225 {
   1226 	struct yds_softc *sc;
   1227 
   1228 	sc = addr;
   1229 	return auconv_query_encoding(sc->sc_encodings, fp);
   1230 }
   1231 
   1232 static int
   1233 yds_set_params(void *addr, int setmode, int usemode,
   1234 	       audio_params_t *play, audio_params_t* rec,
   1235 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   1236 {
   1237 	if (setmode & AUMODE_RECORD) {
   1238 		if (auconv_set_converter(yds_formats, YDS_NFORMATS,
   1239 					 AUMODE_RECORD, rec, FALSE, rfil) < 0)
   1240 			return EINVAL;
   1241 	}
   1242 	if (setmode & AUMODE_PLAY) {
   1243 		if (auconv_set_converter(yds_formats, YDS_NFORMATS,
   1244 					 AUMODE_PLAY, play, FALSE, pfil) < 0)
   1245 			return EINVAL;
   1246 	}
   1247 	return 0;
   1248 }
   1249 
   1250 static int
   1251 yds_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
   1252 {
   1253 
   1254 	/*
   1255 	 * Block size must be bigger than a frame.
   1256 	 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
   1257 	 */
   1258 	if (blk < 1024)
   1259 		blk = 1024;
   1260 
   1261 	return blk & ~4;
   1262 }
   1263 
   1264 static uint32_t
   1265 yds_get_lpfq(u_int sample_rate)
   1266 {
   1267 	int i;
   1268 	static struct lpfqt {
   1269 		u_int rate;
   1270 		uint32_t lpfq;
   1271 	} lpfqt[] = {
   1272 		{8000,  0x32020000},
   1273 		{11025, 0x31770000},
   1274 		{16000, 0x31390000},
   1275 		{22050, 0x31c90000},
   1276 		{32000, 0x33d00000},
   1277 		{48000, 0x40000000},
   1278 		{0, 0}
   1279 	};
   1280 
   1281 	if (sample_rate == 44100)		/* for P44 slot? */
   1282 		return 0x370A0000;
   1283 
   1284 	for (i = 0; lpfqt[i].rate != 0; i++)
   1285 		if (sample_rate <= lpfqt[i].rate)
   1286 			break;
   1287 
   1288 	return lpfqt[i].lpfq;
   1289 }
   1290 
   1291 static uint32_t
   1292 yds_get_lpfk(u_int sample_rate)
   1293 {
   1294 	int i;
   1295 	static struct lpfkt {
   1296 		u_int rate;
   1297 		uint32_t lpfk;
   1298 	} lpfkt[] = {
   1299 		{8000,  0x18b20000},
   1300 		{11025, 0x20930000},
   1301 		{16000, 0x2b9a0000},
   1302 		{22050, 0x35a10000},
   1303 		{32000, 0x3eaa0000},
   1304 		{48000, 0x40000000},
   1305 		{0, 0}
   1306 	};
   1307 
   1308 	if (sample_rate == 44100)		/* for P44 slot? */
   1309 		return 0x46460000;
   1310 
   1311 	for (i = 0; lpfkt[i].rate != 0; i++)
   1312 		if (sample_rate <= lpfkt[i].rate)
   1313 			break;
   1314 
   1315 	return lpfkt[i].lpfk;
   1316 }
   1317 
   1318 static int
   1319 yds_trigger_output(void *addr, void *start, void *end, int blksize,
   1320 		   void (*intr)(void *), void *arg, const audio_params_t *param)
   1321 #define P44		(sc->sc_flags & YDS_CAP_HAS_P44)
   1322 {
   1323 	struct yds_softc *sc;
   1324 	struct yds_dma *p;
   1325 	struct play_slot_ctrl_bank *psb;
   1326 	const u_int gain = 0x40000000;
   1327 	bus_addr_t s;
   1328 	size_t l;
   1329 	int i;
   1330 	int p44, channels;
   1331 	uint32_t format;
   1332 
   1333 	sc = addr;
   1334 #ifdef DIAGNOSTIC
   1335 	if (sc->sc_play.intr)
   1336 		panic("yds_trigger_output: already running");
   1337 #endif
   1338 
   1339 	sc->sc_play.intr = intr;
   1340 	sc->sc_play.intr_arg = arg;
   1341 	sc->sc_play.offset = 0;
   1342 	sc->sc_play.blksize = blksize;
   1343 
   1344 	DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
   1345 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1346 
   1347 	p = yds_find_dma(sc, start);
   1348 	if (!p) {
   1349 		printf("yds_trigger_output: bad addr %p\n", start);
   1350 		return EINVAL;
   1351 	}
   1352 	sc->sc_play.dma = p;
   1353 
   1354 #ifdef YDS_USE_P44
   1355 	/* The document says the P44 SRC supports only stereo, 16bit PCM. */
   1356 	if (P44)
   1357 		p44 = ((param->sample_rate == 44100) &&
   1358 		       (param->channels == 2) &&
   1359 		       (param->precision == 16));
   1360 	else
   1361 #endif
   1362 		p44 = 0;
   1363 	channels = p44 ? 1 : param->channels;
   1364 
   1365 	s = DMAADDR(p);
   1366 	l = ((char *)end - (char *)start);
   1367 	sc->sc_play.length = l;
   1368 
   1369 	*sc->ptbl = htole32(channels);	/* Num of play */
   1370 
   1371 	sc->sc_play.factor = 1;
   1372 	if (param->channels == 2)
   1373 		sc->sc_play.factor *= 2;
   1374 	if (param->precision != 8)
   1375 		sc->sc_play.factor *= 2;
   1376 	l /= sc->sc_play.factor;
   1377 
   1378 	format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
   1379 		  (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
   1380 		  (p44 ? PSLT_FORMAT_SRC441 : 0));
   1381 
   1382 	psb = sc->pbankp[0];
   1383 	memset(psb, 0, sizeof(*psb));
   1384 	psb->format = htole32(format);
   1385 	psb->pgbase = htole32(s);
   1386 	psb->pgloopend = htole32(l);
   1387 	if (!p44) {
   1388 		psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
   1389 		psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
   1390 		psb->eggainend = htole32(gain);
   1391 		psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
   1392 		psb->pgdelta = htole32(psb->pgdeltaend);
   1393 		psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
   1394 		psb->eggain = htole32(gain);
   1395 	}
   1396 
   1397 	for (i = 0; i < channels; i++) {
   1398 		/* i == 0: left or mono, i == 1: right */
   1399 		psb = sc->pbankp[i*2];
   1400 		if (i)
   1401 			/* copy from left */
   1402 			*psb = *(sc->pbankp[0]);
   1403 		if (channels == 2) {
   1404 			/* stereo */
   1405 			if (i == 0) {
   1406 				psb->lchgain = psb->lchgainend = htole32(gain);
   1407 			} else {
   1408 				psb->lchgain = psb->lchgainend = 0;
   1409 				psb->rchgain = psb->rchgainend = htole32(gain);
   1410 				psb->format |= htole32(PSLT_FORMAT_RCH);
   1411 			}
   1412 		} else if (!p44) {
   1413 			/* mono */
   1414 			psb->lchgain = psb->rchgain = htole32(gain);
   1415 			psb->lchgainend = psb->rchgainend = htole32(gain);
   1416 		}
   1417 		/* copy to the other bank */
   1418 		*(sc->pbankp[i*2+1]) = *psb;
   1419 	}
   1420 
   1421 	YDS_DUMP_PLAY_SLOT(5, sc, 0);
   1422 	YDS_DUMP_PLAY_SLOT(5, sc, 1);
   1423 
   1424 	if (p44)
   1425 		YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
   1426 	else
   1427 		YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
   1428 
   1429 	/* Now the play slot for the next frame is set up!! */
   1430 	/* Sync play slot control data for both directions */
   1431 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1432 			sc->ptbloff,
   1433 			sizeof(struct play_slot_ctrl_bank) *
   1434 			    channels * N_PLAY_SLOT_CTRL_BANK,
   1435 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1436 	/* Sync ring buffer */
   1437 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
   1438 			BUS_DMASYNC_PREWRITE);
   1439 	/* HERE WE GO!! */
   1440 	YWRITE4(sc, YDS_MODE,
   1441 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
   1442 
   1443 	return 0;
   1444 }
   1445 #undef P44
   1446 
   1447 static int
   1448 yds_trigger_input(void *addr, void *start, void *end, int blksize,
   1449 		  void (*intr)(void *), void *arg, const audio_params_t *param)
   1450 {
   1451 	struct yds_softc *sc;
   1452 	struct yds_dma *p;
   1453 	u_int srate, format;
   1454 	struct rec_slot_ctrl_bank *rsb;
   1455 	bus_addr_t s;
   1456 	size_t l;
   1457 
   1458 	sc = addr;
   1459 #ifdef DIAGNOSTIC
   1460 	if (sc->sc_rec.intr)
   1461 		panic("yds_trigger_input: already running");
   1462 #endif
   1463 	sc->sc_rec.intr = intr;
   1464 	sc->sc_rec.intr_arg = arg;
   1465 	sc->sc_rec.offset = 0;
   1466 	sc->sc_rec.blksize = blksize;
   1467 
   1468 	DPRINTFN(1, ("yds_trigger_input: "
   1469 	    "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
   1470 	    addr, start, end, blksize, intr, arg));
   1471 	DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
   1472 	    param->sample_rate, param->precision, param->channels));
   1473 
   1474 	p = yds_find_dma(sc, start);
   1475 	if (!p) {
   1476 		printf("yds_trigger_input: bad addr %p\n", start);
   1477 		return EINVAL;
   1478 	}
   1479 	sc->sc_rec.dma = p;
   1480 
   1481 	s = DMAADDR(p);
   1482 	l = ((char *)end - (char *)start);
   1483 	sc->sc_rec.length = l;
   1484 
   1485 	sc->sc_rec.factor = 1;
   1486 	if (param->channels == 2)
   1487 		sc->sc_rec.factor *= 2;
   1488 	if (param->precision != 8)
   1489 		sc->sc_rec.factor *= 2;
   1490 
   1491 	rsb = &sc->rbank[0];
   1492 	memset(rsb, 0, sizeof(*rsb));
   1493 	rsb->pgbase = htole32(s);
   1494 	rsb->pgloopendadr = htole32(l);
   1495 	/* Seems all 4 banks must be set up... */
   1496 	sc->rbank[1] = *rsb;
   1497 	sc->rbank[2] = *rsb;
   1498 	sc->rbank[3] = *rsb;
   1499 
   1500 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
   1501 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
   1502 	srate = 48000 * 4096 / param->sample_rate - 1;
   1503 	format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
   1504 		  (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
   1505 	DPRINTF(("srate=%d, format=%08x\n", srate, format));
   1506 #ifdef YDS_USE_REC_SLOT
   1507 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
   1508 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
   1509 	YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
   1510 	YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
   1511 	YWRITE4(sc, YDS_REC_FORMAT, format);
   1512 #else
   1513 	YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
   1514 	YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
   1515 	YWRITE4(sc, YDS_ADC_FORMAT, format);
   1516 #endif
   1517 	/* Now the rec slot for the next frame is set up!! */
   1518 	/* Sync record slot control data */
   1519 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1520 			sc->rbankoff,
   1521 			sizeof(struct rec_slot_ctrl_bank)*
   1522 			    N_REC_SLOT_CTRL*
   1523 			    N_REC_SLOT_CTRL_BANK,
   1524 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1525 	/* Sync ring buffer */
   1526 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
   1527 			BUS_DMASYNC_PREREAD);
   1528 	/* HERE WE GO!! */
   1529 	YWRITE4(sc, YDS_MODE,
   1530 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
   1531 
   1532 	return 0;
   1533 }
   1534 
   1535 static int
   1536 yds_halt(struct yds_softc *sc)
   1537 {
   1538 	uint32_t mode;
   1539 
   1540 	/* Stop the DSP operation. */
   1541 	mode = YREAD4(sc, YDS_MODE);
   1542 	YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
   1543 
   1544 	/* Paranoia...  mute all */
   1545 	YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
   1546 	YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
   1547 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
   1548 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
   1549 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
   1550 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
   1551 
   1552 	return 0;
   1553 }
   1554 
   1555 static int
   1556 yds_halt_output(void *addr)
   1557 {
   1558 	struct yds_softc *sc;
   1559 
   1560 	DPRINTF(("yds: yds_halt_output\n"));
   1561 	sc = addr;
   1562 	if (sc->sc_play.intr) {
   1563 		sc->sc_play.intr = 0;
   1564 		/* Sync play slot control data */
   1565 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1566 				sc->pbankoff,
   1567 				sizeof(struct play_slot_ctrl_bank)*
   1568 				    (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
   1569 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
   1570 		/* Stop the play slot operation */
   1571 		sc->pbankp[0]->status =
   1572 		sc->pbankp[1]->status =
   1573 		sc->pbankp[2]->status =
   1574 		sc->pbankp[3]->status = 1;
   1575 		/* Sync ring buffer */
   1576 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
   1577 				0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
   1578 	}
   1579 
   1580 	return 0;
   1581 }
   1582 
   1583 static int
   1584 yds_halt_input(void *addr)
   1585 {
   1586 	struct yds_softc *sc;
   1587 
   1588 	DPRINTF(("yds: yds_halt_input\n"));
   1589 	sc = addr;
   1590 	sc->sc_rec.intr = NULL;
   1591 	if (sc->sc_rec.intr) {
   1592 		/* Stop the rec slot operation */
   1593 		YWRITE4(sc, YDS_MAPOF_REC, 0);
   1594 		sc->sc_rec.intr = 0;
   1595 		/* Sync rec slot control data */
   1596 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
   1597 				sc->rbankoff,
   1598 				sizeof(struct rec_slot_ctrl_bank)*
   1599 				    N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
   1600 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
   1601 		/* Sync ring buffer */
   1602 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
   1603 				0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
   1604 	}
   1605 
   1606 	return 0;
   1607 }
   1608 
   1609 static int
   1610 yds_getdev(void *addr, struct audio_device *retp)
   1611 {
   1612 
   1613 	*retp = yds_device;
   1614 	return 0;
   1615 }
   1616 
   1617 static int
   1618 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1619 {
   1620 	struct yds_softc *sc;
   1621 
   1622 	sc = addr;
   1623 	return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
   1624 	    sc->sc_codec[0].codec_if, cp);
   1625 }
   1626 
   1627 static int
   1628 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1629 {
   1630 	struct yds_softc *sc;
   1631 
   1632 	sc = addr;
   1633 	return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
   1634 	    sc->sc_codec[0].codec_if, cp);
   1635 }
   1636 
   1637 static int
   1638 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1639 {
   1640 	struct yds_softc *sc;
   1641 
   1642 	sc = addr;
   1643 	return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
   1644 	    sc->sc_codec[0].codec_if, dip);
   1645 }
   1646 
   1647 static void *
   1648 yds_malloc(void *addr, int direction, size_t size,
   1649 	   struct malloc_type *pool, int flags)
   1650 {
   1651 	struct yds_softc *sc;
   1652 	struct yds_dma *p;
   1653 	int error;
   1654 
   1655 	p = malloc(sizeof(*p), pool, flags);
   1656 	if (p == NULL)
   1657 		return NULL;
   1658 	sc = addr;
   1659 	error = yds_allocmem(sc, size, 16, p);
   1660 	if (error) {
   1661 		free(p, pool);
   1662 		return NULL;
   1663 	}
   1664 	p->next = sc->sc_dmas;
   1665 	sc->sc_dmas = p;
   1666 	return KERNADDR(p);
   1667 }
   1668 
   1669 static void
   1670 yds_free(void *addr, void *ptr, struct malloc_type *pool)
   1671 {
   1672 	struct yds_softc *sc;
   1673 	struct yds_dma **pp, *p;
   1674 
   1675 	sc = addr;
   1676 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1677 		if (KERNADDR(p) == ptr) {
   1678 			yds_freemem(sc, p);
   1679 			*pp = p->next;
   1680 			free(p, pool);
   1681 			return;
   1682 		}
   1683 	}
   1684 }
   1685 
   1686 static struct yds_dma *
   1687 yds_find_dma(struct yds_softc *sc, void *addr)
   1688 {
   1689 	struct yds_dma *p;
   1690 
   1691 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
   1692 		continue;
   1693 
   1694 	return p;
   1695 }
   1696 
   1697 static size_t
   1698 yds_round_buffersize(void *addr, int direction, size_t size)
   1699 {
   1700 
   1701 	/*
   1702 	 * Buffer size should be at least twice as bigger as a frame.
   1703 	 */
   1704 	if (size < 1024 * 3)
   1705 		size = 1024 * 3;
   1706 	return size;
   1707 }
   1708 
   1709 static paddr_t
   1710 yds_mappage(void *addr, void *mem, off_t off, int prot)
   1711 {
   1712 	struct yds_softc *sc;
   1713 	struct yds_dma *p;
   1714 
   1715 	if (off < 0)
   1716 		return -1;
   1717 	sc = addr;
   1718 	p = yds_find_dma(sc, mem);
   1719 	if (p == NULL)
   1720 		return -1;
   1721 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1722 	    off, prot, BUS_DMA_WAITOK);
   1723 }
   1724 
   1725 static int
   1726 yds_get_props(void *addr)
   1727 {
   1728 
   1729 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1730 	    AUDIO_PROP_FULLDUPLEX;
   1731 }
   1732