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