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