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