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