xref: /netbsd-src/sys/dev/pci/yds.c (revision eb961d0e02b7a46a9acfa877b02df48df6637278)
1 /*	$NetBSD: yds.c,v 1.30 2005/12/11 12:22:51 christos 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.30 2005/12/11 12:22:51 christos 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 
683 	if (why == PWR_RESUME) {
684 		sc = addr;
685 		if (yds_init(sc)) {
686 			printf("%s: reinitialize failed\n",
687 				sc->sc_dev.dv_xname);
688 			return;
689 		}
690 		sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
691 	}
692 }
693 
694 static void
695 yds_attach(struct device *parent, struct device *self, void *aux)
696 {
697 	struct yds_softc *sc;
698 	struct pci_attach_args *pa;
699 	pci_chipset_tag_t pc;
700 	char const *intrstr;
701 	pci_intr_handle_t ih;
702 	pcireg_t reg;
703 	struct yds_codec_softc *codec;
704 	char devinfo[256];
705 	int i, r, to;
706 	int revision;
707 	int ac97_id2;
708 
709 	sc = (struct yds_softc *)self;
710 	pa = (struct pci_attach_args *)aux;
711 	pc = pa->pa_pc;
712 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
713 	revision = PCI_REVISION(pa->pa_class);
714 	printf(": %s (rev. 0x%02x)\n", devinfo, revision);
715 
716 	/* Map register to memory */
717 	if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
718 			   &sc->memt, &sc->memh, NULL, NULL)) {
719 		printf("%s: can't map memory space\n", sc->sc_dev.dv_xname);
720 		return;
721 	}
722 
723 	/* Map and establish the interrupt. */
724 	if (pci_intr_map(pa, &ih)) {
725 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
726 		return;
727 	}
728 	intrstr = pci_intr_string(pc, ih);
729 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, yds_intr, sc);
730 	if (sc->sc_ih == NULL) {
731 		printf("%s: couldn't establish interrupt", sc->sc_dev.dv_xname);
732 		if (intrstr != NULL)
733 			printf(" at %s", intrstr);
734 		printf("\n");
735 		return;
736 	}
737 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
738 
739 	sc->sc_dmatag = pa->pa_dmat;
740 	sc->sc_pc = pc;
741 	sc->sc_pcitag = pa->pa_tag;
742 	sc->sc_id = pa->pa_id;
743 	sc->sc_revision = revision;
744 	sc->sc_flags = yds_get_dstype(sc->sc_id);
745 #ifdef AUDIO_DEBUG
746 	if (ydsdebug) {
747 		char bits[80];
748 
749 		printf("%s: chip has %s\n", sc->sc_dev.dv_xname,
750 		       bitmask_snprintf(sc->sc_flags, YDS_CAP_BITS, bits,
751 					sizeof(bits)));
752 	}
753 #endif
754 
755 	/* Disable legacy mode */
756 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
757 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
758 		       reg & YDS_PCI_LEGACY_LAD);
759 
760 	/* Enable the device. */
761 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
762 	reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
763 		PCI_COMMAND_MASTER_ENABLE);
764 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
765 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
766 
767 	/* Mute all volumes */
768 	for (i = 0x80; i < 0xc0; i += 2)
769 		YWRITE2(sc, i, 0);
770 
771 	/* Initialize the device */
772 	if (yds_init(sc)) {
773 		printf("%s: initialize failed\n", sc->sc_dev.dv_xname);
774 		return;
775 	}
776 
777 	/*
778 	 * Detect primary/secondary AC97
779 	 *	YMF754 Hardware Specification Rev 1.01 page 24
780 	 */
781 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
782 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
783 	delay(400000);		/* Needed for 740C. */
784 
785 	/* Primary */
786 	for (to = 0; to < AC97_TIMEOUT; to++) {
787 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
788 			break;
789 		delay(1);
790 	}
791 	if (to == AC97_TIMEOUT) {
792 		printf("%s: no AC97 available\n", sc->sc_dev.dv_xname);
793 		return;
794 	}
795 
796 	/* Secondary */
797 	/* Secondary AC97 is used for 4ch audio. Currently unused. */
798 	ac97_id2 = -1;
799 	if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
800 		goto detected;
801 #if 0				/* reset secondary... */
802 	YWRITE2(sc, YDS_GPIO_OCTRL,
803 		YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
804 	YWRITE2(sc, YDS_GPIO_FUNCE,
805 		(YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
806 #endif
807 	for (to = 0; to < AC97_TIMEOUT; to++) {
808 		if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
809 			break;
810 		delay(1);
811 	}
812 	if (to < AC97_TIMEOUT) {
813 		/* detect id */
814 		for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
815 			YWRITE2(sc, AC97_CMD_ADDR,
816 				AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
817 
818 			for (to = 0; to < AC97_TIMEOUT; to++) {
819 				if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
820 				    == 0)
821 					goto detected;
822 				delay(1);
823 			}
824 		}
825 		if (ac97_id2 == 4)
826 			ac97_id2 = -1;
827 detected:
828 		;
829 	}
830 
831 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
832 	delay (20);
833 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
834 	delay (400000);
835 	for (to = 0; to < AC97_TIMEOUT; to++) {
836 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
837 			break;
838 		delay(1);
839 	}
840 
841 	/*
842 	 * Attach ac97 codec
843 	 */
844 	for (i = 0; i < 2; i++) {
845 		static struct {
846 			int data;
847 			int addr;
848 		} statregs[] = {
849 			{AC97_STAT_DATA1, AC97_STAT_ADDR1},
850 			{AC97_STAT_DATA2, AC97_STAT_ADDR2},
851 		};
852 
853 		if (i == 1 && ac97_id2 == -1)
854 			break;		/* secondary ac97 not available */
855 
856 		codec = &sc->sc_codec[i];
857 		memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
858 		codec->sc = sc;
859 		codec->id = i == 1 ? ac97_id2 : 0;
860 		codec->status_data = statregs[i].data;
861 		codec->status_addr = statregs[i].addr;
862 		codec->host_if.arg = codec;
863 		codec->host_if.attach = yds_attach_codec;
864 		codec->host_if.read = yds_read_codec;
865 		codec->host_if.write = yds_write_codec;
866 		codec->host_if.reset = yds_reset_codec;
867 
868 		if ((r = ac97_attach(&codec->host_if, self)) != 0) {
869 			printf("%s: can't attach codec (error 0x%X)\n",
870 			       sc->sc_dev.dv_xname, r);
871 			return;
872 		}
873 	}
874 
875 	if (0 != auconv_create_encodings(yds_formats, YDS_NFORMATS,
876 					 &sc->sc_encodings))
877 		return;
878 
879 	audio_attach_mi(&yds_hw_if, sc, &sc->sc_dev);
880 
881 	sc->sc_legacy_iot = pa->pa_iot;
882 	config_defer((struct device*) sc, yds_configure_legacy);
883 
884 	powerhook_establish(yds_powerhook, sc);
885 }
886 
887 static int
888 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
889 {
890 	struct yds_codec_softc *sc;
891 
892 	sc = sc_;
893 	sc->codec_if = codec_if;
894 	return 0;
895 }
896 
897 static int
898 yds_ready_codec(struct yds_codec_softc *sc)
899 {
900 	int to;
901 
902 	for (to = 0; to < AC97_TIMEOUT; to++) {
903 		if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
904 			return 0;
905 		delay(1);
906 	}
907 
908 	return 1;
909 }
910 
911 static int
912 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
913 {
914 	struct yds_codec_softc *sc;
915 
916 	sc = sc_;
917 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
918 
919 	if (yds_ready_codec(sc)) {
920 		printf("%s: yds_read_codec timeout\n",
921 		       sc->sc->sc_dev.dv_xname);
922 		return EIO;
923 	}
924 
925 	if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
926 	    sc->sc->sc_revision < 2) {
927 		int i;
928 		for (i=0; i<600; i++)
929 			YREAD2(sc->sc, sc->status_data);
930 	}
931 
932 	*data = YREAD2(sc->sc, sc->status_data);
933 
934 	return 0;
935 }
936 
937 static int
938 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
939 {
940 	struct yds_codec_softc *sc;
941 
942 	sc = sc_;
943 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
944 	YWRITE2(sc->sc, AC97_CMD_DATA, data);
945 
946 	if (yds_ready_codec(sc)) {
947 		printf("%s: yds_write_codec timeout\n",
948 			sc->sc->sc_dev.dv_xname);
949 		return EIO;
950 	}
951 
952 	return 0;
953 }
954 
955 /*
956  * XXX: Must handle the secondary differntly!!
957  */
958 static int
959 yds_reset_codec(void *sc_)
960 {
961 	struct yds_codec_softc *codec;
962 	struct yds_softc *sc;
963 	pcireg_t reg;
964 
965 	codec = sc_;
966 	sc = codec->sc;
967 	/* reset AC97 codec */
968 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
969 	if (reg & 0x03) {
970 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
971 			       YDS_PCI_DSCTRL, reg & ~0x03);
972 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
973 			       YDS_PCI_DSCTRL, reg | 0x03);
974 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
975 			       YDS_PCI_DSCTRL, reg & ~0x03);
976 		delay(50000);
977 	}
978 
979 	yds_ready_codec(sc_);
980 	return 0;
981 }
982 
983 static int
984 yds_intr(void *p)
985 {
986 	struct yds_softc *sc;
987 	u_int status;
988 
989 	sc = p;
990 	status = YREAD4(sc, YDS_STATUS);
991 	DPRINTFN(1, ("yds_intr: status=%08x\n", status));
992 	if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
993 #if NMPU > 0
994 		if (sc->sc_mpu)
995 			return mpu_intr(sc->sc_mpu);
996 #endif
997 		return 0;
998 	}
999 
1000 	if (status & YDS_STAT_TINT) {
1001 		YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
1002 		printf ("yds_intr: timeout!\n");
1003 	}
1004 
1005 	if (status & YDS_STAT_INT) {
1006 		int nbank;
1007 
1008 		nbank = (YREAD4(sc, YDS_CONTROL_SELECT) == 0);
1009 		/* Clear interrupt flag */
1010 		YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
1011 
1012 		/* Buffer for the next frame is always ready. */
1013 		YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
1014 
1015 		if (sc->sc_play.intr) {
1016 			u_int dma, ccpu, blk, len;
1017 
1018 			/* Sync play slot control data */
1019 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1020 					sc->pbankoff,
1021 					sizeof(struct play_slot_ctrl_bank)*
1022 					    le32toh(*sc->ptbl)*
1023 					    N_PLAY_SLOT_CTRL_BANK,
1024 					BUS_DMASYNC_POSTWRITE|
1025 					BUS_DMASYNC_POSTREAD);
1026 			dma = le32toh(sc->pbankp[nbank]->pgstart) * sc->sc_play.factor;
1027 			ccpu = sc->sc_play.offset;
1028 			blk = sc->sc_play.blksize;
1029 			len = sc->sc_play.length;
1030 
1031 			if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1032 			    ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1033 				/* We can fill the next block */
1034 				/* Sync ring buffer for previous write */
1035 				bus_dmamap_sync(sc->sc_dmatag,
1036 						sc->sc_play.dma->map,
1037 						ccpu, blk,
1038 						BUS_DMASYNC_POSTWRITE);
1039 				sc->sc_play.intr(sc->sc_play.intr_arg);
1040 				sc->sc_play.offset += blk;
1041 				if (sc->sc_play.offset >= len) {
1042 					sc->sc_play.offset -= len;
1043 #ifdef DIAGNOSTIC
1044 					if (sc->sc_play.offset != 0)
1045 						printf ("Audio ringbuffer botch\n");
1046 #endif
1047 				}
1048 				/* Sync ring buffer for next write */
1049 				bus_dmamap_sync(sc->sc_dmatag,
1050 						sc->sc_play.dma->map,
1051 						ccpu, blk,
1052 						BUS_DMASYNC_PREWRITE);
1053 			}
1054 		}
1055 		if (sc->sc_rec.intr) {
1056 			u_int dma, ccpu, blk, len;
1057 
1058 			/* Sync rec slot control data */
1059 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1060 					sc->rbankoff,
1061 					sizeof(struct rec_slot_ctrl_bank)*
1062 					    N_REC_SLOT_CTRL*
1063 					    N_REC_SLOT_CTRL_BANK,
1064 					BUS_DMASYNC_POSTWRITE|
1065 					BUS_DMASYNC_POSTREAD);
1066 			dma = le32toh(sc->rbank[YDS_INPUT_SLOT*2 + nbank].pgstartadr);
1067 			ccpu = sc->sc_rec.offset;
1068 			blk = sc->sc_rec.blksize;
1069 			len = sc->sc_rec.length;
1070 
1071 			if (((dma > ccpu) && (dma - ccpu > blk * 2)) ||
1072 			    ((ccpu > dma) && (dma + len - ccpu > blk * 2))) {
1073 				/* We can drain the current block */
1074 				/* Sync ring buffer first */
1075 				bus_dmamap_sync(sc->sc_dmatag,
1076 						sc->sc_rec.dma->map,
1077 						ccpu, blk,
1078 						BUS_DMASYNC_POSTREAD);
1079 				sc->sc_rec.intr(sc->sc_rec.intr_arg);
1080 				sc->sc_rec.offset += blk;
1081 				if (sc->sc_rec.offset >= len) {
1082 					sc->sc_rec.offset -= len;
1083 #ifdef DIAGNOSTIC
1084 					if (sc->sc_rec.offset != 0)
1085 						printf ("Audio ringbuffer botch\n");
1086 #endif
1087 				}
1088 				/* Sync ring buffer for next read */
1089 				bus_dmamap_sync(sc->sc_dmatag,
1090 						sc->sc_rec.dma->map,
1091 						ccpu, blk,
1092 						BUS_DMASYNC_PREREAD);
1093 			}
1094 		}
1095 	}
1096 
1097 	return 1;
1098 }
1099 
1100 static int
1101 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
1102 {
1103 	int error;
1104 
1105 	p->size = size;
1106 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
1107 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1108 				 &p->nsegs, BUS_DMA_NOWAIT);
1109 	if (error)
1110 		return error;
1111 
1112 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
1113 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
1114 	if (error)
1115 		goto free;
1116 
1117 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1118 				  0, BUS_DMA_NOWAIT, &p->map);
1119 	if (error)
1120 		goto unmap;
1121 
1122 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1123 				BUS_DMA_NOWAIT);
1124 	if (error)
1125 		goto destroy;
1126 	return 0;
1127 
1128 destroy:
1129 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
1130 unmap:
1131 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1132 free:
1133 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1134 	return error;
1135 }
1136 
1137 static int
1138 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
1139 {
1140 
1141 	bus_dmamap_unload(sc->sc_dmatag, p->map);
1142 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
1143 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1144 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1145 	return 0;
1146 }
1147 
1148 static int
1149 yds_open(void *addr, int flags)
1150 {
1151 	struct yds_softc *sc;
1152 	uint32_t mode;
1153 
1154 	sc = addr;
1155 	/* Select bank 0. */
1156 	YWRITE4(sc, YDS_CONTROL_SELECT, 0);
1157 
1158 	/* Start the DSP operation. */
1159 	mode = YREAD4(sc, YDS_MODE);
1160 	mode |= YDS_MODE_ACTV;
1161 	mode &= ~YDS_MODE_ACTV2;
1162 	YWRITE4(sc, YDS_MODE, mode);
1163 
1164 	return 0;
1165 }
1166 
1167 /*
1168  * Close function is called at splaudio().
1169  */
1170 static void
1171 yds_close(void *addr)
1172 {
1173 
1174 	yds_halt(addr);
1175 }
1176 
1177 static int
1178 yds_query_encoding(void *addr, struct audio_encoding *fp)
1179 {
1180 	struct yds_softc *sc;
1181 
1182 	sc = addr;
1183 	return auconv_query_encoding(sc->sc_encodings, fp);
1184 }
1185 
1186 static int
1187 yds_set_params(void *addr, int setmode, int usemode,
1188 	       audio_params_t *play, audio_params_t* rec,
1189 	       stream_filter_list_t *pfil, stream_filter_list_t *rfil)
1190 {
1191 	if (setmode & AUMODE_RECORD) {
1192 		if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1193 					 AUMODE_RECORD, rec, FALSE, rfil) < 0)
1194 			return EINVAL;
1195 	}
1196 	if (setmode & AUMODE_PLAY) {
1197 		if (auconv_set_converter(yds_formats, YDS_NFORMATS,
1198 					 AUMODE_PLAY, play, FALSE, pfil) < 0)
1199 			return EINVAL;
1200 	}
1201 	return 0;
1202 }
1203 
1204 static int
1205 yds_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
1206 {
1207 
1208 	/*
1209 	 * Block size must be bigger than a frame.
1210 	 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
1211 	 */
1212 	if (blk < 1024)
1213 		blk = 1024;
1214 
1215 	return blk & ~4;
1216 }
1217 
1218 static uint32_t
1219 yds_get_lpfq(u_int sample_rate)
1220 {
1221 	int i;
1222 	static struct lpfqt {
1223 		u_int rate;
1224 		uint32_t lpfq;
1225 	} lpfqt[] = {
1226 		{8000,  0x32020000},
1227 		{11025, 0x31770000},
1228 		{16000, 0x31390000},
1229 		{22050, 0x31c90000},
1230 		{32000, 0x33d00000},
1231 		{48000, 0x40000000},
1232 		{0, 0}
1233 	};
1234 
1235 	if (sample_rate == 44100)		/* for P44 slot? */
1236 		return 0x370A0000;
1237 
1238 	for (i = 0; lpfqt[i].rate != 0; i++)
1239 		if (sample_rate <= lpfqt[i].rate)
1240 			break;
1241 
1242 	return lpfqt[i].lpfq;
1243 }
1244 
1245 static uint32_t
1246 yds_get_lpfk(u_int sample_rate)
1247 {
1248 	int i;
1249 	static struct lpfkt {
1250 		u_int rate;
1251 		uint32_t lpfk;
1252 	} lpfkt[] = {
1253 		{8000,  0x18b20000},
1254 		{11025, 0x20930000},
1255 		{16000, 0x2b9a0000},
1256 		{22050, 0x35a10000},
1257 		{32000, 0x3eaa0000},
1258 		{48000, 0x40000000},
1259 		{0, 0}
1260 	};
1261 
1262 	if (sample_rate == 44100)		/* for P44 slot? */
1263 		return 0x46460000;
1264 
1265 	for (i = 0; lpfkt[i].rate != 0; i++)
1266 		if (sample_rate <= lpfkt[i].rate)
1267 			break;
1268 
1269 	return lpfkt[i].lpfk;
1270 }
1271 
1272 static int
1273 yds_trigger_output(void *addr, void *start, void *end, int blksize,
1274 		   void (*intr)(void *), void *arg, const audio_params_t *param)
1275 #define P44		(sc->sc_flags & YDS_CAP_HAS_P44)
1276 {
1277 	struct yds_softc *sc;
1278 	struct yds_dma *p;
1279 	struct play_slot_ctrl_bank *psb;
1280 	const u_int gain = 0x40000000;
1281 	bus_addr_t s;
1282 	size_t l;
1283 	int i;
1284 	int p44, channels;
1285 	uint32_t format;
1286 
1287 	sc = addr;
1288 #ifdef DIAGNOSTIC
1289 	if (sc->sc_play.intr)
1290 		panic("yds_trigger_output: already running");
1291 #endif
1292 
1293 	sc->sc_play.intr = intr;
1294 	sc->sc_play.intr_arg = arg;
1295 	sc->sc_play.offset = 0;
1296 	sc->sc_play.blksize = blksize;
1297 
1298 	DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
1299 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1300 
1301 	p = yds_find_dma(sc, start);
1302 	if (!p) {
1303 		printf("yds_trigger_output: bad addr %p\n", start);
1304 		return EINVAL;
1305 	}
1306 	sc->sc_play.dma = p;
1307 
1308 #ifdef YDS_USE_P44
1309 	/* The document says the P44 SRC supports only stereo, 16bit PCM. */
1310 	if (P44)
1311 		p44 = ((param->sample_rate == 44100) &&
1312 		       (param->channels == 2) &&
1313 		       (param->precision == 16));
1314 	else
1315 #endif
1316 		p44 = 0;
1317 	channels = p44 ? 1 : param->channels;
1318 
1319 	s = DMAADDR(p);
1320 	l = ((char *)end - (char *)start);
1321 	sc->sc_play.length = l;
1322 
1323 	*sc->ptbl = htole32(channels);	/* Num of play */
1324 
1325 	sc->sc_play.factor = 1;
1326 	if (param->channels == 2)
1327 		sc->sc_play.factor *= 2;
1328 	if (param->precision != 8)
1329 		sc->sc_play.factor *= 2;
1330 	l /= sc->sc_play.factor;
1331 
1332 	format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
1333 		  (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
1334 		  (p44 ? PSLT_FORMAT_SRC441 : 0));
1335 
1336 	psb = sc->pbankp[0];
1337 	memset(psb, 0, sizeof(*psb));
1338 	psb->format = htole32(format);
1339 	psb->pgbase = htole32(s);
1340 	psb->pgloopend = htole32(l);
1341 	if (!p44) {
1342 		psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
1343 		psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
1344 		psb->eggainend = htole32(gain);
1345 		psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
1346 		psb->pgdelta = htole32(psb->pgdeltaend);
1347 		psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
1348 		psb->eggain = htole32(gain);
1349 	}
1350 
1351 	for (i = 0; i < channels; i++) {
1352 		/* i == 0: left or mono, i == 1: right */
1353 		psb = sc->pbankp[i*2];
1354 		if (i)
1355 			/* copy from left */
1356 			*psb = *(sc->pbankp[0]);
1357 		if (channels == 2) {
1358 			/* stereo */
1359 			if (i == 0) {
1360 				psb->lchgain = psb->lchgainend = htole32(gain);
1361 			} else {
1362 				psb->lchgain = psb->lchgainend = 0;
1363 				psb->rchgain = psb->rchgainend = htole32(gain);
1364 				psb->format |= htole32(PSLT_FORMAT_RCH);
1365 			}
1366 		} else if (!p44) {
1367 			/* mono */
1368 			psb->lchgain = psb->rchgain = htole32(gain);
1369 			psb->lchgainend = psb->rchgainend = htole32(gain);
1370 		}
1371 		/* copy to the other bank */
1372 		*(sc->pbankp[i*2+1]) = *psb;
1373 	}
1374 
1375 	YDS_DUMP_PLAY_SLOT(5, sc, 0);
1376 	YDS_DUMP_PLAY_SLOT(5, sc, 1);
1377 
1378 	if (p44)
1379 		YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
1380 	else
1381 		YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
1382 
1383 	/* Now the play slot for the next frame is set up!! */
1384 	/* Sync play slot control data for both directions */
1385 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1386 			sc->ptbloff,
1387 			sizeof(struct play_slot_ctrl_bank) *
1388 			    channels * N_PLAY_SLOT_CTRL_BANK,
1389 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1390 	/* Sync ring buffer */
1391 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1392 			BUS_DMASYNC_PREWRITE);
1393 	/* HERE WE GO!! */
1394 	YWRITE4(sc, YDS_MODE,
1395 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1396 
1397 	return 0;
1398 }
1399 #undef P44
1400 
1401 static int
1402 yds_trigger_input(void *addr, void *start, void *end, int blksize,
1403 		  void (*intr)(void *), void *arg, const audio_params_t *param)
1404 {
1405 	struct yds_softc *sc;
1406 	struct yds_dma *p;
1407 	u_int srate, format;
1408 	struct rec_slot_ctrl_bank *rsb;
1409 	bus_addr_t s;
1410 	size_t l;
1411 
1412 	sc = addr;
1413 #ifdef DIAGNOSTIC
1414 	if (sc->sc_rec.intr)
1415 		panic("yds_trigger_input: already running");
1416 #endif
1417 	sc->sc_rec.intr = intr;
1418 	sc->sc_rec.intr_arg = arg;
1419 	sc->sc_rec.offset = 0;
1420 	sc->sc_rec.blksize = blksize;
1421 
1422 	DPRINTFN(1, ("yds_trigger_input: "
1423 	    "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1424 	    addr, start, end, blksize, intr, arg));
1425 	DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
1426 	    param->sample_rate, param->precision, param->channels));
1427 
1428 	p = yds_find_dma(sc, start);
1429 	if (!p) {
1430 		printf("yds_trigger_input: bad addr %p\n", start);
1431 		return EINVAL;
1432 	}
1433 	sc->sc_rec.dma = p;
1434 
1435 	s = DMAADDR(p);
1436 	l = ((char *)end - (char *)start);
1437 	sc->sc_rec.length = l;
1438 
1439 	sc->sc_rec.factor = 1;
1440 	if (param->channels == 2)
1441 		sc->sc_rec.factor *= 2;
1442 	if (param->precision != 8)
1443 		sc->sc_rec.factor *= 2;
1444 
1445 	rsb = &sc->rbank[0];
1446 	memset(rsb, 0, sizeof(*rsb));
1447 	rsb->pgbase = htole32(s);
1448 	rsb->pgloopendadr = htole32(l);
1449 	/* Seems all 4 banks must be set up... */
1450 	sc->rbank[1] = *rsb;
1451 	sc->rbank[2] = *rsb;
1452 	sc->rbank[3] = *rsb;
1453 
1454 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
1455 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
1456 	srate = 48000 * 4096 / param->sample_rate - 1;
1457 	format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
1458 		  (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
1459 	DPRINTF(("srate=%d, format=%08x\n", srate, format));
1460 #ifdef YDS_USE_REC_SLOT
1461 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
1462 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
1463 	YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
1464 	YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
1465 	YWRITE4(sc, YDS_REC_FORMAT, format);
1466 #else
1467 	YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
1468 	YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
1469 	YWRITE4(sc, YDS_ADC_FORMAT, format);
1470 #endif
1471 	/* Now the rec slot for the next frame is set up!! */
1472 	/* Sync record slot control data */
1473 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1474 			sc->rbankoff,
1475 			sizeof(struct rec_slot_ctrl_bank)*
1476 			    N_REC_SLOT_CTRL*
1477 			    N_REC_SLOT_CTRL_BANK,
1478 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1479 	/* Sync ring buffer */
1480 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1481 			BUS_DMASYNC_PREREAD);
1482 	/* HERE WE GO!! */
1483 	YWRITE4(sc, YDS_MODE,
1484 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1485 
1486 	return 0;
1487 }
1488 
1489 static int
1490 yds_halt(struct yds_softc *sc)
1491 {
1492 	uint32_t mode;
1493 
1494 	/* Stop the DSP operation. */
1495 	mode = YREAD4(sc, YDS_MODE);
1496 	YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
1497 
1498 	/* Paranoia...  mute all */
1499 	YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
1500 	YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
1501 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
1502 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
1503 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
1504 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
1505 
1506 	return 0;
1507 }
1508 
1509 static int
1510 yds_halt_output(void *addr)
1511 {
1512 	struct yds_softc *sc;
1513 
1514 	DPRINTF(("yds: yds_halt_output\n"));
1515 	sc = addr;
1516 	if (sc->sc_play.intr) {
1517 		sc->sc_play.intr = 0;
1518 		/* Sync play slot control data */
1519 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1520 				sc->pbankoff,
1521 				sizeof(struct play_slot_ctrl_bank)*
1522 				    (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
1523 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1524 		/* Stop the play slot operation */
1525 		sc->pbankp[0]->status =
1526 		sc->pbankp[1]->status =
1527 		sc->pbankp[2]->status =
1528 		sc->pbankp[3]->status = 1;
1529 		/* Sync ring buffer */
1530 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
1531 				0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
1532 	}
1533 
1534 	return 0;
1535 }
1536 
1537 static int
1538 yds_halt_input(void *addr)
1539 {
1540 	struct yds_softc *sc;
1541 
1542 	DPRINTF(("yds: yds_halt_input\n"));
1543 	sc = addr;
1544 	sc->sc_rec.intr = NULL;
1545 	if (sc->sc_rec.intr) {
1546 		/* Stop the rec slot operation */
1547 		YWRITE4(sc, YDS_MAPOF_REC, 0);
1548 		sc->sc_rec.intr = 0;
1549 		/* Sync rec slot control data */
1550 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1551 				sc->rbankoff,
1552 				sizeof(struct rec_slot_ctrl_bank)*
1553 				    N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
1554 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1555 		/* Sync ring buffer */
1556 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
1557 				0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
1558 	}
1559 
1560 	return 0;
1561 }
1562 
1563 static int
1564 yds_getdev(void *addr, struct audio_device *retp)
1565 {
1566 
1567 	*retp = yds_device;
1568 	return 0;
1569 }
1570 
1571 static int
1572 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1573 {
1574 	struct yds_softc *sc;
1575 
1576 	sc = addr;
1577 	return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
1578 	    sc->sc_codec[0].codec_if, cp);
1579 }
1580 
1581 static int
1582 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1583 {
1584 	struct yds_softc *sc;
1585 
1586 	sc = addr;
1587 	return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
1588 	    sc->sc_codec[0].codec_if, cp);
1589 }
1590 
1591 static int
1592 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
1593 {
1594 	struct yds_softc *sc;
1595 
1596 	sc = addr;
1597 	return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
1598 	    sc->sc_codec[0].codec_if, dip);
1599 }
1600 
1601 static void *
1602 yds_malloc(void *addr, int direction, size_t size,
1603 	   struct malloc_type *pool, int flags)
1604 {
1605 	struct yds_softc *sc;
1606 	struct yds_dma *p;
1607 	int error;
1608 
1609 	p = malloc(sizeof(*p), pool, flags);
1610 	if (p == NULL)
1611 		return NULL;
1612 	sc = addr;
1613 	error = yds_allocmem(sc, size, 16, p);
1614 	if (error) {
1615 		free(p, pool);
1616 		return NULL;
1617 	}
1618 	p->next = sc->sc_dmas;
1619 	sc->sc_dmas = p;
1620 	return KERNADDR(p);
1621 }
1622 
1623 static void
1624 yds_free(void *addr, void *ptr, struct malloc_type *pool)
1625 {
1626 	struct yds_softc *sc;
1627 	struct yds_dma **pp, *p;
1628 
1629 	sc = addr;
1630 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1631 		if (KERNADDR(p) == ptr) {
1632 			yds_freemem(sc, p);
1633 			*pp = p->next;
1634 			free(p, pool);
1635 			return;
1636 		}
1637 	}
1638 }
1639 
1640 static struct yds_dma *
1641 yds_find_dma(struct yds_softc *sc, void *addr)
1642 {
1643 	struct yds_dma *p;
1644 
1645 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1646 		continue;
1647 
1648 	return p;
1649 }
1650 
1651 static size_t
1652 yds_round_buffersize(void *addr, int direction, size_t size)
1653 {
1654 
1655 	/*
1656 	 * Buffer size should be at least twice as bigger as a frame.
1657 	 */
1658 	if (size < 1024 * 3)
1659 		size = 1024 * 3;
1660 	return size;
1661 }
1662 
1663 static paddr_t
1664 yds_mappage(void *addr, void *mem, off_t off, int prot)
1665 {
1666 	struct yds_softc *sc;
1667 	struct yds_dma *p;
1668 
1669 	if (off < 0)
1670 		return -1;
1671 	sc = addr;
1672 	p = yds_find_dma(sc, mem);
1673 	if (p == NULL)
1674 		return -1;
1675 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
1676 	    off, prot, BUS_DMA_WAITOK);
1677 }
1678 
1679 static int
1680 yds_get_props(void *addr)
1681 {
1682 
1683 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
1684 	    AUDIO_PROP_FULLDUPLEX;
1685 }
1686