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