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