xref: /netbsd/sys/dev/pci/yds.c (revision beecddb6)
1 /*	$NetBSD: yds.c,v 1.68 2021/08/07 16:19:14 thorpej 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.68 2021/08/07 16:19:14 thorpej 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/kmem.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/audio_if.h>
60 #include <dev/ic/ac97reg.h>
61 #include <dev/ic/ac97var.h>
62 #include <dev/ic/mpuvar.h>
63 
64 #include <sys/bus.h>
65 #include <sys/intr.h>
66 
67 #include <dev/microcode/yds/yds_hwmcode.h>
68 #include <dev/pci/ydsreg.h>
69 #include <dev/pci/ydsvar.h>
70 
71 /* Debug */
72 #undef YDS_USE_REC_SLOT
73 #define YDS_USE_P44
74 
75 #ifdef AUDIO_DEBUG
76 # define DPRINTF(x)	if (ydsdebug) printf x
77 # define DPRINTFN(n,x)	if (ydsdebug>(n)) printf x
78 int	ydsdebug = 0;
79 #else
80 # define DPRINTF(x)
81 # define DPRINTFN(n,x)
82 #endif
83 #ifdef YDS_USE_REC_SLOT
84 # define YDS_INPUT_SLOT 0	/* REC slot = ADC + loopbacks */
85 #else
86 # define YDS_INPUT_SLOT 1	/* ADC slot */
87 #endif
88 
89 static int	yds_match(device_t, cfdata_t, void *);
90 static void	yds_attach(device_t, device_t, void *);
91 static int	yds_intr(void *);
92 
93 #define DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
94 #define KERNADDR(p)	((void *)((p)->addr))
95 
96 static int	yds_allocmem(struct yds_softc *, size_t, size_t,
97 			     struct yds_dma *);
98 static int	yds_freemem(struct yds_softc *, struct yds_dma *);
99 
100 #ifndef AUDIO_DEBUG
101 #define YWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
102 #define YWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
103 #define YWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
104 #define YREAD1(sc, r)	bus_space_read_1((sc)->memt, (sc)->memh, (r))
105 #define YREAD2(sc, r)	bus_space_read_2((sc)->memt, (sc)->memh, (r))
106 #define YREAD4(sc, r)	bus_space_read_4((sc)->memt, (sc)->memh, (r))
107 #else
YREAD2(struct yds_softc * sc,bus_size_t r)108 static uint16_t YREAD2(struct yds_softc *sc, bus_size_t r)
109 {
110 	DPRINTFN(5, (" YREAD2(0x%lX)\n", (unsigned long)r));
111 	return bus_space_read_2(sc->memt, sc->memh, r);
112 }
113 
YREAD4(struct yds_softc * sc,bus_size_t r)114 static uint32_t YREAD4(struct yds_softc *sc, bus_size_t r)
115 {
116 	DPRINTFN(5, (" YREAD4(0x%lX)\n", (unsigned long)r));
117 	return bus_space_read_4(sc->memt, sc->memh, r);
118 }
119 
120 #ifdef notdef
YWRITE1(struct yds_softc * sc,bus_size_t r,uint8_t x)121 static void YWRITE1(struct yds_softc *sc, bus_size_t r, uint8_t x)
122 {
123 	DPRINTFN(5, (" YWRITE1(0x%lX,0x%lX)\n", (unsigned long)r,
124 		     (unsigned long)x));
125 	bus_space_write_1(sc->memt, sc->memh, r, x);
126 }
127 #endif
128 
YWRITE2(struct yds_softc * sc,bus_size_t r,uint16_t x)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 
YWRITE4(struct yds_softc * sc,bus_size_t r,uint32_t x)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_NEW(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_format(void *, audio_format_query_t *);
153 static int	yds_set_format(void *, int,
154 			       const audio_params_t *, const audio_params_t *,
155 			       audio_filter_reg_t *, audio_filter_reg_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);
169 static void	yds_free(void *, void *, size_t);
170 static size_t	yds_round_buffersize(void *, int, size_t);
171 static int	yds_get_props(void *);
172 static int	yds_query_devinfo(void *, mixer_devinfo_t *);
173 static void	yds_get_locks(void *, kmutex_t **, kmutex_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(device_t);
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 
194 #ifdef AUDIO_DEBUG
195 static void	yds_dump_play_slot(struct yds_softc *, int);
196 #define	YDS_DUMP_PLAY_SLOT(n, sc, bank) \
197 	if (ydsdebug > (n)) yds_dump_play_slot(sc, bank)
198 #else
199 #define	YDS_DUMP_PLAY_SLOT(n, sc, bank)
200 #endif /* AUDIO_DEBUG */
201 
202 static const struct audio_hw_if yds_hw_if = {
203 	.open		  = yds_open,
204 	.close		  = yds_close,
205 	.query_format	  = yds_query_format,
206 	.set_format	  = yds_set_format,
207 	.round_blocksize  = yds_round_blocksize,
208 	.commit_settings  = NULL,
209 	.init_output	  = NULL,
210 	.init_input	  = NULL,
211 	.start_output	  = NULL,
212 	.start_input	  = NULL,
213 	.halt_output	  = yds_halt_output,
214 	.halt_input	  = yds_halt_input,
215 	.speaker_ctl	  = NULL,
216 	.getdev		  = yds_getdev,
217 	.set_port	  = yds_mixer_set_port,
218 	.get_port	  = yds_mixer_get_port,
219 	.query_devinfo	  = yds_query_devinfo,
220 	.allocm		  = yds_malloc,
221 	.freem		  = yds_free,
222 	.round_buffersize = yds_round_buffersize,
223 	.get_props	  = yds_get_props,
224 	.trigger_output	  = yds_trigger_output,
225 	.trigger_input	  = yds_trigger_input,
226 	.dev_ioctl	  = NULL,
227 	.get_locks	  = yds_get_locks,
228 };
229 
230 static const struct audio_device yds_device = {
231 	.name    = "Yamaha DS-1",
232 	.version = "",
233 	.config  = "yds"
234 };
235 
236 static const struct {
237 	uint	id;
238 	u_int	flags;
239 #define YDS_CAP_MCODE_1			0x0001
240 #define YDS_CAP_MCODE_1E		0x0002
241 #define YDS_CAP_LEGACY_SELECTABLE	0x0004
242 #define YDS_CAP_LEGACY_FLEXIBLE		0x0008
243 #define YDS_CAP_HAS_P44			0x0010
244 } yds_chip_capabliity_list[] = {
245 	{ PCI_PRODUCT_YAMAHA_YMF724,
246 	  YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },
247 	/* 740[C] has only 32 slots.  But anyway we use only 2 */
248 	{ PCI_PRODUCT_YAMAHA_YMF740,
249 	  YDS_CAP_MCODE_1|YDS_CAP_LEGACY_SELECTABLE },	/* XXX NOT TESTED */
250 	{ PCI_PRODUCT_YAMAHA_YMF740C,
251 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
252 	{ PCI_PRODUCT_YAMAHA_YMF724F,
253 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_SELECTABLE },
254 	{ PCI_PRODUCT_YAMAHA_YMF744B,
255 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE },
256 	{ PCI_PRODUCT_YAMAHA_YMF754,
257 	  YDS_CAP_MCODE_1E|YDS_CAP_LEGACY_FLEXIBLE|YDS_CAP_HAS_P44 },
258 	{ 0, 0 }
259 };
260 #ifdef AUDIO_DEBUG
261 #define YDS_CAP_BITS	"\020\005P44\004LEGFLEX\003LEGSEL\002MCODE1E\001MCODE1"
262 #endif
263 
264 static const struct audio_format yds_formats[] = {
265 	{
266 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
267 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
268 		.validbits	= 16,
269 		.precision	= 16,
270 		.channels	= 2,
271 		.channel_mask	= AUFMT_STEREO,
272 		.frequency_type	= 8,
273 		.frequency	=
274 		    { 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000 },
275 	},
276 };
277 #define	YDS_NFORMATS	(sizeof(yds_formats) / sizeof(struct audio_format))
278 
279 #ifdef AUDIO_DEBUG
280 static void
yds_dump_play_slot(struct yds_softc * sc,int bank)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 	bus_addr_t 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 = DMAADDR(&sc->sc_ctrldata) + sc->pbankoff;
294 	p = sc->ptbl;
295 	printf("ptbl + 0: %d\n", *p++);
296 	for (i = 0; i < N_PLAY_SLOTS; i++) {
297 		printf("ptbl + %d: %#x, should be %#" PRIxPADDR "\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
yds_get_dstype(int id)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
yds_download_mcode(struct yds_softc * sc)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
yds_allocate_slots(struct yds_softc * sc)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 #ifndef AUDIO_DEBUG
422 	__USE(ecs);
423 #endif
424 	DPRINTF(("work size : %d\n", (unsigned int)ws));
425 #ifdef DIAGNOSTIC
426 	if (pcs != sizeof(struct play_slot_ctrl_bank)) {
427 		aprint_error_dev(sc->sc_dev, "invalid play slot ctrldata %d != %d\n",
428 		       (unsigned int)pcs,
429 		       (unsigned int)sizeof(struct play_slot_ctrl_bank));
430 	if (rcs != sizeof(struct rec_slot_ctrl_bank))
431 		aprint_error_dev(sc->sc_dev, "invalid rec slot ctrldata %d != %d\n",
432 		       (unsigned int)rcs,
433 		       (unsigned int)sizeof(struct rec_slot_ctrl_bank));
434 	}
435 #endif
436 
437 	memsize = N_PLAY_SLOTS*N_PLAY_SLOT_CTRL_BANK*pcs +
438 		  N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK*rcs + ws;
439 	memsize += (N_PLAY_SLOTS+1)*sizeof(uint32_t);
440 
441 	p = &sc->sc_ctrldata;
442 	if (KERNADDR(p) == NULL) {
443 		i = yds_allocmem(sc, memsize, 16, p);
444 		if (i) {
445 			aprint_error_dev(sc->sc_dev, "couldn't alloc/map DSP DMA buffer, reason %d\n", i);
446 			return 1;
447 		}
448 	}
449 	mp = KERNADDR(p);
450 	da = DMAADDR(p);
451 
452 	DPRINTF(("mp:%p, DMA addr:%#" PRIxPADDR "\n",
453 		 mp, sc->sc_ctrldata.map->dm_segs[0].ds_addr));
454 
455 	memset(mp, 0, memsize);
456 
457 	/* Work space */
458 	cb = 0;
459 	va = (uint8_t *)mp;
460 	YWRITE4(sc, YDS_WORK_BASE, da + cb);
461 	cb += ws;
462 
463 	/* Play control data table */
464 	sc->ptbl = (uint32_t *)(va + cb);
465 	sc->ptbloff = cb;
466 	YWRITE4(sc, YDS_PLAY_CTRLBASE, da + cb);
467 	cb += (N_PLAY_SLOT_CTRL + 1) * sizeof(uint32_t);
468 
469 	/* Record slot control data */
470 	sc->rbank = (struct rec_slot_ctrl_bank *)(va + cb);
471 	YWRITE4(sc, YDS_REC_CTRLBASE, da + cb);
472 	sc->rbankoff = cb;
473 	cb += N_REC_SLOT_CTRL * N_REC_SLOT_CTRL_BANK * rcs;
474 
475 #if 0
476 	/* Effect slot control data -- unused */
477 	YWRITE4(sc, YDS_EFFECT_CTRLBASE, da + cb);
478 	cb += N_EFFECT_SLOT_CTRL * N_EFFECT_SLOT_CTRL_BANK * ecs;
479 #endif
480 
481 	/* Play slot control data */
482 	sc->pbankoff = cb;
483 	for (i=0; i < N_PLAY_SLOT_CTRL; i++) {
484 		sc->pbankp[i*2] = (struct play_slot_ctrl_bank *)(va + cb);
485 		*(sc->ptbl + i+1) = htole32(da + cb);
486 		cb += pcs;
487 
488 		sc->pbankp[i*2+1] = (struct play_slot_ctrl_bank *)(va + cb);
489 		cb += pcs;
490 	}
491 	/* Sync play control data table */
492 	bus_dmamap_sync(sc->sc_dmatag, p->map,
493 			sc->ptbloff, (N_PLAY_SLOT_CTRL+1) * sizeof(uint32_t),
494 			BUS_DMASYNC_PREWRITE);
495 
496 	return 0;
497 }
498 
499 static void
yds_enable_dsp(struct yds_softc * sc)500 yds_enable_dsp(struct yds_softc *sc)
501 {
502 
503 	YWRITE4(sc, YDS_CONFIG, YDS_DSP_SETUP);
504 }
505 
506 static int
yds_disable_dsp(struct yds_softc * sc)507 yds_disable_dsp(struct yds_softc *sc)
508 {
509 	int to;
510 	uint32_t data;
511 
512 	data = YREAD4(sc, YDS_CONFIG);
513 	if (data)
514 		YWRITE4(sc, YDS_CONFIG, YDS_DSP_DISABLE);
515 
516 	for (to = 0; to < YDS_WORK_TIMEOUT; to++) {
517 		if ((YREAD4(sc, YDS_STATUS) & YDS_STAT_WORK) == 0)
518 			return 0;
519 		delay(1);
520 	}
521 
522 	return 1;
523 }
524 
525 static int
yds_match(device_t parent,cfdata_t match,void * aux)526 yds_match(device_t parent, cfdata_t match, void *aux)
527 {
528 	struct pci_attach_args *pa;
529 
530 	pa = (struct pci_attach_args *)aux;
531 	switch (PCI_VENDOR(pa->pa_id)) {
532 	case PCI_VENDOR_YAMAHA:
533 		switch (PCI_PRODUCT(pa->pa_id)) {
534 		case PCI_PRODUCT_YAMAHA_YMF724:
535 		case PCI_PRODUCT_YAMAHA_YMF740:
536 		case PCI_PRODUCT_YAMAHA_YMF740C:
537 		case PCI_PRODUCT_YAMAHA_YMF724F:
538 		case PCI_PRODUCT_YAMAHA_YMF744B:
539 		case PCI_PRODUCT_YAMAHA_YMF754:
540 			return 1;
541 		}
542 		break;
543 	}
544 
545 	return 0;
546 }
547 
548 /*
549  * This routine is called after all the ISA devices are configured,
550  * to avoid conflict.
551  */
552 static void
yds_configure_legacy(device_t self)553 yds_configure_legacy(device_t self)
554 #define FLEXIBLE	(sc->sc_flags & YDS_CAP_LEGACY_FLEXIBLE)
555 #define SELECTABLE	(sc->sc_flags & YDS_CAP_LEGACY_SELECTABLE)
556 {
557 	static const bus_addr_t opl_addrs[] = {0x388, 0x398, 0x3A0, 0x3A8};
558 	static const bus_addr_t mpu_addrs[] = {0x330, 0x300, 0x332, 0x334};
559 	struct yds_softc *sc;
560 	pcireg_t reg;
561 	device_t dev;
562 	int i;
563 
564 	sc = device_private(self);
565 	if (!FLEXIBLE && !SELECTABLE)
566 		return;
567 
568 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY);
569 	reg &= ~0x8133c03f;	/* these bits are out of interest */
570 	reg |= ((YDS_PCI_EX_LEGACY_IMOD) |
571 		(YDS_PCI_LEGACY_FMEN |
572 		 YDS_PCI_LEGACY_MEN /*| YDS_PCI_LEGACY_MIEN*/));
573 	reg |= YDS_PCI_EX_LEGACY_SMOD_DISABLE;
574 	if (FLEXIBLE) {
575 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
576 		delay(100*1000);
577 	}
578 
579 	/* Look for OPL */
580 	dev = 0;
581 	for (i = 0; i < sizeof(opl_addrs) / sizeof(bus_addr_t); i++) {
582 		if (SELECTABLE) {
583 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
584 				       YDS_PCI_LEGACY, reg | (i << (0+16)));
585 			delay(100*1000);	/* wait 100ms */
586 		} else
587 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
588 				       YDS_PCI_FM_BA, opl_addrs[i]);
589 		if (bus_space_map(sc->sc_opl_iot,
590 				  opl_addrs[i], 4, 0, &sc->sc_opl_ioh) == 0) {
591 			struct audio_attach_args aa;
592 
593 			aa.type = AUDIODEV_TYPE_OPL;
594 			aa.hwif = aa.hdl = NULL;
595 			dev = config_found(self, &aa, audioprint, CFARGS_NONE);
596 			if (dev == 0)
597 				bus_space_unmap(sc->sc_opl_iot,
598 						sc->sc_opl_ioh, 4);
599 			else {
600 				if (SELECTABLE)
601 					reg |= (i << (0+16));
602 				break;
603 			}
604 		}
605 	}
606 	if (dev == 0) {
607 		reg &= ~YDS_PCI_LEGACY_FMEN;
608 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
609 			       YDS_PCI_LEGACY, reg);
610 	} else {
611 		/* Max. volume */
612 		YWRITE4(sc, YDS_LEGACY_OUT_VOLUME, 0x3fff3fff);
613 		YWRITE4(sc, YDS_LEGACY_REC_VOLUME, 0x3fff3fff);
614 	}
615 
616 	/* Look for MPU */
617 	dev = NULL;
618 	for (i = 0; i < sizeof(mpu_addrs) / sizeof(bus_addr_t); i++) {
619 		if (SELECTABLE)
620 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
621 				       YDS_PCI_LEGACY, reg | (i << (4+16)));
622 		else
623 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
624 				       YDS_PCI_MPU_BA, mpu_addrs[i]);
625 		if (bus_space_map(sc->sc_mpu_iot,
626 				  mpu_addrs[i], 2, 0, &sc->sc_mpu_ioh) == 0) {
627 			struct audio_attach_args aa;
628 
629 			aa.type = AUDIODEV_TYPE_MPU;
630 			aa.hwif = aa.hdl = NULL;
631 			dev = config_found(self, &aa, audioprint, CFARGS_NONE);
632 			if (dev == 0)
633 				bus_space_unmap(sc->sc_mpu_iot,
634 						sc->sc_mpu_ioh, 2);
635 			else {
636 				if (SELECTABLE)
637 					reg |= (i << (4+16));
638 				break;
639 			}
640 		}
641 	}
642 	if (dev == 0) {
643 		reg &= ~(YDS_PCI_LEGACY_MEN | YDS_PCI_LEGACY_MIEN);
644 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_LEGACY, reg);
645 	}
646 	sc->sc_mpu = dev;
647 }
648 #undef FLEXIBLE
649 #undef SELECTABLE
650 
651 static int
yds_init(struct yds_softc * sc)652 yds_init(struct yds_softc *sc)
653 {
654 	uint32_t reg;
655 
656 	DPRINTF(("yds_init()\n"));
657 
658 	/* Download microcode */
659 	if (yds_download_mcode(sc)) {
660 		aprint_error_dev(sc->sc_dev, "download microcode failed\n");
661 		return 1;
662 	}
663 
664 	/* Allocate DMA buffers */
665 	if (yds_allocate_slots(sc)) {
666 		aprint_error_dev(sc->sc_dev, "could not allocate slots\n");
667 		return 1;
668 	}
669 
670 	/* Warm reset */
671 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
672 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL,
673 		reg | YDS_DSCTRL_WRST);
674 	delay(50000);
675 
676 	return 0;
677 }
678 
679 static bool
yds_suspend(device_t dv,const pmf_qual_t * qual)680 yds_suspend(device_t dv, const pmf_qual_t *qual)
681 {
682 	struct yds_softc *sc = device_private(dv);
683 	pci_chipset_tag_t pc = sc->sc_pc;
684 	pcitag_t tag = sc->sc_pcitag;
685 
686 	mutex_enter(&sc->sc_lock);
687 	mutex_spin_enter(&sc->sc_intr_lock);
688 	sc->sc_enabled = 0;
689 	sc->sc_dsctrl = pci_conf_read(pc, tag, YDS_PCI_DSCTRL);
690 	sc->sc_legacy = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
691 	sc->sc_ba[0] = pci_conf_read(pc, tag, YDS_PCI_FM_BA);
692 	sc->sc_ba[1] = pci_conf_read(pc, tag, YDS_PCI_MPU_BA);
693 	mutex_spin_exit(&sc->sc_intr_lock);
694 	mutex_exit(&sc->sc_lock);
695 
696 	return true;
697 }
698 
699 static bool
yds_resume(device_t dv,const pmf_qual_t * qual)700 yds_resume(device_t dv, const pmf_qual_t *qual)
701 {
702 	struct yds_softc *sc = device_private(dv);
703 	pci_chipset_tag_t pc = sc->sc_pc;
704 	pcitag_t tag = sc->sc_pcitag;
705 	pcireg_t reg;
706 
707 	/* Disable legacy mode */
708 	mutex_enter(&sc->sc_lock);
709 	mutex_spin_enter(&sc->sc_intr_lock);
710 	reg = pci_conf_read(pc, tag, YDS_PCI_LEGACY);
711 	pci_conf_write(pc, tag, YDS_PCI_LEGACY, reg & YDS_PCI_LEGACY_LAD);
712 
713 	/* Enable the device. */
714 	reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
715 	reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
716 		PCI_COMMAND_MASTER_ENABLE);
717 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, reg);
718 	reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
719 	mutex_spin_exit(&sc->sc_intr_lock);
720 	if (yds_init(sc)) {
721 		aprint_error_dev(dv, "reinitialize failed\n");
722 		mutex_exit(&sc->sc_lock);
723 		return false;
724 	}
725 
726 	pci_conf_write(pc, tag, YDS_PCI_DSCTRL, sc->sc_dsctrl);
727 	sc->sc_enabled = 1;
728 	sc->sc_codec[0].codec_if->vtbl->restore_ports(sc->sc_codec[0].codec_if);
729 	mutex_exit(&sc->sc_lock);
730 
731 	return true;
732 }
733 
734 static void
yds_attach(device_t parent,device_t self,void * aux)735 yds_attach(device_t parent, device_t self, void *aux)
736 {
737 	struct yds_softc *sc;
738 	struct pci_attach_args *pa;
739 	pci_chipset_tag_t pc;
740 	char const *intrstr;
741 	pci_intr_handle_t ih;
742 	pcireg_t reg;
743 	struct yds_codec_softc *codec;
744 	int i, r, to;
745 	int revision;
746 	int ac97_id2;
747 	char intrbuf[PCI_INTRSTR_LEN];
748 
749 	sc = device_private(self);
750 	sc->sc_dev = self;
751 	pa = (struct pci_attach_args *)aux;
752 	pc = pa->pa_pc;
753 	revision = PCI_REVISION(pa->pa_class);
754 
755 	pci_aprint_devinfo(pa, NULL);
756 
757 	/* Map register to memory */
758 	if (pci_mapreg_map(pa, YDS_PCI_MBA, PCI_MAPREG_TYPE_MEM, 0,
759 			   &sc->memt, &sc->memh, NULL, NULL)) {
760 		aprint_error_dev(self, "can't map memory space\n");
761 		return;
762 	}
763 
764 	/* Map and establish the interrupt. */
765 	if (pci_intr_map(pa, &ih)) {
766 		aprint_error_dev(self, "couldn't map interrupt\n");
767 		return;
768 	}
769 
770 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
771 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
772 
773 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
774 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, yds_intr, sc,
775 	    device_xname(self));
776 	if (sc->sc_ih == NULL) {
777 		aprint_error_dev(self, "couldn't establish interrupt");
778 		if (intrstr != NULL)
779 			aprint_error(" at %s", intrstr);
780 		aprint_error("\n");
781 		mutex_destroy(&sc->sc_lock);
782 		mutex_destroy(&sc->sc_intr_lock);
783 		return;
784 	}
785 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
786 
787 	sc->sc_enabled = 0;
788 	sc->sc_dmatag = pa->pa_dmat;
789 	sc->sc_pc = pc;
790 	sc->sc_pcitag = pa->pa_tag;
791 	sc->sc_id = pa->pa_id;
792 	sc->sc_revision = revision;
793 	sc->sc_flags = yds_get_dstype(sc->sc_id);
794 #ifdef AUDIO_DEBUG
795 	if (ydsdebug) {
796 		char bits[80];
797 
798 		snprintb(bits, sizeof(bits), YDS_CAP_BITS, sc->sc_flags);
799 		printf("%s: chip has %s\n", device_xname(self), bits);
800 	}
801 #endif
802 
803 	/* Disable legacy mode */
804 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_LEGACY);
805 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_LEGACY,
806 		       reg & YDS_PCI_LEGACY_LAD);
807 
808 	/* Enable the device. */
809 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
810 	reg |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
811 		PCI_COMMAND_MASTER_ENABLE);
812 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
813 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
814 
815 	/* Mute all volumes */
816 	for (i = 0x80; i < 0xc0; i += 2)
817 		YWRITE2(sc, i, 0);
818 
819 	/* Initialize the device */
820 	if (yds_init(sc)) {
821 		aprint_error_dev(self, "initialize failed\n");
822 		mutex_destroy(&sc->sc_lock);
823 		mutex_destroy(&sc->sc_intr_lock);
824 		return;
825 	}
826 
827 	/*
828 	 * Detect primary/secondary AC97
829 	 *	YMF754 Hardware Specification Rev 1.01 page 24
830 	 */
831 	reg = pci_conf_read(pc, pa->pa_tag, YDS_PCI_DSCTRL);
832 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
833 	delay(400000);		/* Needed for 740C. */
834 
835 	/* Primary */
836 	for (to = 0; to < AC97_TIMEOUT; to++) {
837 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
838 			break;
839 		delay(1);
840 	}
841 	if (to == AC97_TIMEOUT) {
842 		aprint_error_dev(self, "no AC97 available\n");
843 		mutex_destroy(&sc->sc_lock);
844 		mutex_destroy(&sc->sc_intr_lock);
845 		return;
846 	}
847 
848 	/* Secondary */
849 	/* Secondary AC97 is used for 4ch audio. Currently unused. */
850 	ac97_id2 = -1;
851 	if ((YREAD2(sc, YDS_ACTIVITY) & YDS_ACTIVITY_DOCKA) == 0)
852 		goto detected;
853 #if 0				/* reset secondary... */
854 	YWRITE2(sc, YDS_GPIO_OCTRL,
855 		YREAD2(sc, YDS_GPIO_OCTRL) & ~YDS_GPIO_GPO2);
856 	YWRITE2(sc, YDS_GPIO_FUNCE,
857 		(YREAD2(sc, YDS_GPIO_FUNCE)&(~YDS_GPIO_GPC2))|YDS_GPIO_GPE2);
858 #endif
859 	for (to = 0; to < AC97_TIMEOUT; to++) {
860 		if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY) == 0)
861 			break;
862 		delay(1);
863 	}
864 	if (to < AC97_TIMEOUT) {
865 		/* detect id */
866 		for (ac97_id2 = 1; ac97_id2 < 4; ac97_id2++) {
867 			YWRITE2(sc, AC97_CMD_ADDR,
868 				AC97_CMD_READ | AC97_ID(ac97_id2) | 0x28);
869 
870 			for (to = 0; to < AC97_TIMEOUT; to++) {
871 				if ((YREAD2(sc, AC97_STAT_ADDR2) & AC97_BUSY)
872 				    == 0)
873 					goto detected;
874 				delay(1);
875 			}
876 		}
877 		if (ac97_id2 == 4)
878 			ac97_id2 = -1;
879 detected:
880 		;
881 	}
882 
883 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg | YDS_DSCTRL_CRST);
884 	delay (20);
885 	pci_conf_write(pc, pa->pa_tag, YDS_PCI_DSCTRL, reg & ~YDS_DSCTRL_CRST);
886 	delay (400000);
887 	for (to = 0; to < AC97_TIMEOUT; to++) {
888 		if ((YREAD2(sc, AC97_STAT_ADDR1) & AC97_BUSY) == 0)
889 			break;
890 		delay(1);
891 	}
892 
893 	/*
894 	 * Attach ac97 codec
895 	 */
896 	for (i = 0; i < 2; i++) {
897 		static struct {
898 			int data;
899 			int addr;
900 		} statregs[] = {
901 			{AC97_STAT_DATA1, AC97_STAT_ADDR1},
902 			{AC97_STAT_DATA2, AC97_STAT_ADDR2},
903 		};
904 
905 		if (i == 1 && ac97_id2 == -1)
906 			break;		/* secondary ac97 not available */
907 
908 		codec = &sc->sc_codec[i];
909 		codec->sc = sc;
910 		codec->id = i == 1 ? ac97_id2 : 0;
911 		codec->status_data = statregs[i].data;
912 		codec->status_addr = statregs[i].addr;
913 		codec->host_if.arg = codec;
914 		codec->host_if.attach = yds_attach_codec;
915 		codec->host_if.read = yds_read_codec;
916 		codec->host_if.write = yds_write_codec;
917 		codec->host_if.reset = yds_reset_codec;
918 
919 		r = ac97_attach(&codec->host_if, self, &sc->sc_lock);
920 		if (r != 0) {
921 			aprint_error_dev(self,
922 			    "can't attach codec (error 0x%X)\n", r);
923 			mutex_destroy(&sc->sc_lock);
924 			mutex_destroy(&sc->sc_intr_lock);
925 			return;
926 		}
927 	}
928 
929 	audio_attach_mi(&yds_hw_if, sc, self);
930 
931 	sc->sc_legacy_iot = pa->pa_iot;
932 	config_defer(self, yds_configure_legacy);
933 
934 	if (!pmf_device_register(self, yds_suspend, yds_resume))
935 		aprint_error_dev(self, "couldn't establish power handler\n");
936 
937 	mutex_spin_enter(&sc->sc_intr_lock);
938 	sc->sc_enabled = 1;
939 	mutex_spin_exit(&sc->sc_intr_lock);
940 }
941 
942 static int
yds_attach_codec(void * sc_,struct ac97_codec_if * codec_if)943 yds_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
944 {
945 	struct yds_codec_softc *sc;
946 
947 	sc = sc_;
948 	sc->codec_if = codec_if;
949 	return 0;
950 }
951 
952 static int
yds_ready_codec(struct yds_codec_softc * sc)953 yds_ready_codec(struct yds_codec_softc *sc)
954 {
955 	int to;
956 
957 	for (to = 0; to < AC97_TIMEOUT; to++) {
958 		if ((YREAD2(sc->sc, sc->status_addr) & AC97_BUSY) == 0)
959 			return 0;
960 		delay(1);
961 	}
962 
963 	return 1;
964 }
965 
966 static int
yds_read_codec(void * sc_,uint8_t reg,uint16_t * data)967 yds_read_codec(void *sc_, uint8_t reg, uint16_t *data)
968 {
969 	struct yds_codec_softc *sc;
970 
971 	sc = sc_;
972 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_READ | AC97_ID(sc->id) | reg);
973 
974 	if (yds_ready_codec(sc)) {
975 		aprint_error_dev(sc->sc->sc_dev, "yds_read_codec timeout\n");
976 		return EIO;
977 	}
978 
979 	if (PCI_PRODUCT(sc->sc->sc_id) == PCI_PRODUCT_YAMAHA_YMF744B &&
980 	    sc->sc->sc_revision < 2) {
981 		int i;
982 		for (i=0; i<600; i++)
983 			(void)YREAD2(sc->sc, sc->status_data);
984 	}
985 
986 	*data = YREAD2(sc->sc, sc->status_data);
987 
988 	return 0;
989 }
990 
991 static int
yds_write_codec(void * sc_,uint8_t reg,uint16_t data)992 yds_write_codec(void *sc_, uint8_t reg, uint16_t data)
993 {
994 	struct yds_codec_softc *sc;
995 
996 	sc = sc_;
997 	YWRITE2(sc->sc, AC97_CMD_ADDR, AC97_CMD_WRITE | AC97_ID(sc->id) | reg);
998 	YWRITE2(sc->sc, AC97_CMD_DATA, data);
999 
1000 	if (yds_ready_codec(sc)) {
1001 		aprint_error_dev(sc->sc->sc_dev, "yds_write_codec timeout\n");
1002 		return EIO;
1003 	}
1004 
1005 	return 0;
1006 }
1007 
1008 /*
1009  * XXX: Must handle the secondary differntly!!
1010  */
1011 static int
yds_reset_codec(void * sc_)1012 yds_reset_codec(void *sc_)
1013 {
1014 	struct yds_codec_softc *codec;
1015 	struct yds_softc *sc;
1016 	pcireg_t reg;
1017 
1018 	codec = sc_;
1019 	sc = codec->sc;
1020 	/* reset AC97 codec */
1021 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, YDS_PCI_DSCTRL);
1022 	if (reg & 0x03) {
1023 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1024 			       YDS_PCI_DSCTRL, reg & ~0x03);
1025 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1026 			       YDS_PCI_DSCTRL, reg | 0x03);
1027 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
1028 			       YDS_PCI_DSCTRL, reg & ~0x03);
1029 		delay(50000);
1030 	}
1031 
1032 	yds_ready_codec(sc_);
1033 	return 0;
1034 }
1035 
1036 static int
yds_intr(void * p)1037 yds_intr(void *p)
1038 {
1039 	struct yds_softc *sc = p;
1040 #if NMPU > 0
1041 	struct mpu_softc *sc_mpu = device_private(sc->sc_mpu);
1042 #endif
1043 	u_int status;
1044 
1045 	mutex_spin_enter(&sc->sc_intr_lock);
1046 	if (!sc->sc_enabled) {
1047 		mutex_spin_exit(&sc->sc_intr_lock);
1048 		return 0;
1049 	}
1050 
1051 	status = YREAD4(sc, YDS_STATUS);
1052 	DPRINTFN(1, ("yds_intr: status=%08x\n", status));
1053 	if ((status & (YDS_STAT_INT|YDS_STAT_TINT)) == 0) {
1054 #if NMPU > 0
1055 		if (sc_mpu)
1056 			return mpu_intr(sc_mpu);
1057 #endif
1058 		mutex_spin_exit(&sc->sc_intr_lock);
1059 		return 0;
1060 	}
1061 
1062 	if (status & YDS_STAT_TINT) {
1063 		YWRITE4(sc, YDS_STATUS, YDS_STAT_TINT);
1064 		printf ("yds_intr: timeout!\n");
1065 	}
1066 
1067 	/*
1068 	 * XXX
1069 	 * An interrupt in YMF754 occurs when next hardware frame is
1070 	 * requested, not when current hardware frame processing is
1071 	 * completed.  According to the datasheet, only access to the
1072 	 * inactive bank is permitted, but in fact, fields in inactive
1073 	 * bank that the chip should write to may or may not be filled
1074 	 * at that time.  On the other hand, both the CPU and the device
1075 	 * must guarantee that the fields in active bank are determined
1076 	 * at the beginning of the interrupt.
1077 	 * Therefore, we read active bank.
1078 	 */
1079 
1080 	if (status & YDS_STAT_INT) {
1081 		int nbank;
1082 		u_int pdma = 0;
1083 		u_int rdma = 0;
1084 
1085 		/* nbank is bank number that YDS is processing now. */
1086 		nbank = YREAD4(sc, YDS_CONTROL_SELECT) & 1;
1087 
1088 		/* Clear interrupt flag */
1089 		YWRITE4(sc, YDS_STATUS, YDS_STAT_INT);
1090 
1091 		/* Read current data offset before ACTV2 */
1092 		if (sc->sc_play.intr) {
1093 			/* Sync play slot control data */
1094 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1095 					sc->pbankoff,
1096 					sizeof(struct play_slot_ctrl_bank)*
1097 					    le32toh(*sc->ptbl)*
1098 					    N_PLAY_SLOT_CTRL_BANK,
1099 					BUS_DMASYNC_POSTWRITE|
1100 					BUS_DMASYNC_POSTREAD);
1101 			/* start offset of current processing bank */
1102 			pdma = le32toh(sc->pbankp[nbank]->pgstart) *
1103 			    sc->sc_play.factor;
1104 		}
1105 
1106 		if (sc->sc_rec.intr) {
1107 			/* Sync rec slot control data */
1108 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1109 					sc->rbankoff,
1110 					sizeof(struct rec_slot_ctrl_bank)*
1111 					    N_REC_SLOT_CTRL*
1112 					    N_REC_SLOT_CTRL_BANK,
1113 					BUS_DMASYNC_POSTWRITE|
1114 					BUS_DMASYNC_POSTREAD);
1115 			/* start offset of current processing bank */
1116 			rdma = le32toh(
1117 			    sc->rbank[YDS_INPUT_SLOT * 2 + nbank].pgstartadr);
1118 		}
1119 
1120 		/* Buffer for the next frame is always ready. */
1121 		YWRITE4(sc, YDS_MODE, YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV2);
1122 
1123 		if (sc->sc_play.intr) {
1124 			if (pdma < sc->sc_play.offset)
1125 				pdma += sc->sc_play.length;
1126 			if (pdma >= sc->sc_play.offset + sc->sc_play.blksize) {
1127 				/* We can fill the next block */
1128 				/* Sync ring buffer for previous write */
1129 				bus_dmamap_sync(sc->sc_dmatag,
1130 						sc->sc_play.dma->map,
1131 						0, sc->sc_play.length,
1132 						BUS_DMASYNC_POSTWRITE);
1133 				sc->sc_play.intr(sc->sc_play.intr_arg);
1134 				sc->sc_play.offset += sc->sc_play.blksize;
1135 				if (sc->sc_play.offset >= sc->sc_play.length) {
1136 					sc->sc_play.offset -= sc->sc_play.length;
1137 #ifdef DIAGNOSTIC
1138 					if (sc->sc_play.offset != 0)
1139 						printf ("Audio ringbuffer botch\n");
1140 #endif
1141 				}
1142 				/* Sync ring buffer for next write */
1143 				bus_dmamap_sync(sc->sc_dmatag,
1144 						sc->sc_play.dma->map,
1145 						0, sc->sc_play.length,
1146 						BUS_DMASYNC_PREWRITE);
1147 			}
1148 		}
1149 		if (sc->sc_rec.intr) {
1150 			if (rdma < sc->sc_rec.offset)
1151 				rdma += sc->sc_rec.length;
1152 			if (rdma >= sc->sc_rec.offset + sc->sc_rec.blksize) {
1153 				/* We can drain the current block */
1154 				/* Sync ring buffer first */
1155 				bus_dmamap_sync(sc->sc_dmatag,
1156 						sc->sc_rec.dma->map,
1157 						0, sc->sc_rec.length,
1158 						BUS_DMASYNC_POSTREAD);
1159 				sc->sc_rec.intr(sc->sc_rec.intr_arg);
1160 				sc->sc_rec.offset += sc->sc_rec.blksize;
1161 				if (sc->sc_rec.offset >= sc->sc_rec.length) {
1162 					sc->sc_rec.offset -= sc->sc_rec.length;
1163 #ifdef DIAGNOSTIC
1164 					if (sc->sc_rec.offset != 0)
1165 						printf ("Audio ringbuffer botch\n");
1166 #endif
1167 				}
1168 				/* Sync ring buffer for next read */
1169 				bus_dmamap_sync(sc->sc_dmatag,
1170 						sc->sc_rec.dma->map,
1171 						0, sc->sc_rec.length,
1172 						BUS_DMASYNC_PREREAD);
1173 			}
1174 		}
1175 	}
1176 
1177 	mutex_spin_exit(&sc->sc_intr_lock);
1178 	return 1;
1179 }
1180 
1181 static int
yds_allocmem(struct yds_softc * sc,size_t size,size_t align,struct yds_dma * p)1182 yds_allocmem(struct yds_softc *sc, size_t size, size_t align, struct yds_dma *p)
1183 {
1184 	int error;
1185 
1186 	p->size = size;
1187 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
1188 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1189 				 &p->nsegs, BUS_DMA_WAITOK);
1190 	if (error)
1191 		return error;
1192 
1193 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
1194 			       &p->addr, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
1195 	if (error)
1196 		goto free;
1197 
1198 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
1199 				  0, BUS_DMA_WAITOK, &p->map);
1200 	if (error)
1201 		goto unmap;
1202 
1203 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
1204 				BUS_DMA_WAITOK);
1205 	if (error)
1206 		goto destroy;
1207 	return 0;
1208 
1209 destroy:
1210 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
1211 unmap:
1212 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1213 free:
1214 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1215 	return error;
1216 }
1217 
1218 static int
yds_freemem(struct yds_softc * sc,struct yds_dma * p)1219 yds_freemem(struct yds_softc *sc, struct yds_dma *p)
1220 {
1221 
1222 	bus_dmamap_unload(sc->sc_dmatag, p->map);
1223 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
1224 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
1225 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
1226 	return 0;
1227 }
1228 
1229 static int
yds_open(void * addr,int flags)1230 yds_open(void *addr, int flags)
1231 {
1232 	struct yds_softc *sc;
1233 	uint32_t mode;
1234 
1235 	sc = addr;
1236 	/* Select bank 0. */
1237 	YWRITE4(sc, YDS_CONTROL_SELECT, 0);
1238 
1239 	/* Start the DSP operation. */
1240 	mode = YREAD4(sc, YDS_MODE);
1241 	mode |= YDS_MODE_ACTV;
1242 	mode &= ~YDS_MODE_ACTV2;
1243 	YWRITE4(sc, YDS_MODE, mode);
1244 
1245 	return 0;
1246 }
1247 
1248 static void
yds_close(void * addr)1249 yds_close(void *addr)
1250 {
1251 
1252 	yds_halt(addr);
1253 }
1254 
1255 static int
yds_query_format(void * addr,audio_format_query_t * afp)1256 yds_query_format(void *addr, audio_format_query_t *afp)
1257 {
1258 
1259 	return audio_query_format(yds_formats, YDS_NFORMATS, afp);
1260 }
1261 
1262 static int
yds_set_format(void * addr,int setmode,const audio_params_t * play,const audio_params_t * rec,audio_filter_reg_t * pfil,audio_filter_reg_t * rfil)1263 yds_set_format(void *addr, int setmode,
1264 	const audio_params_t *play, const audio_params_t *rec,
1265 	audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
1266 {
1267 	return 0;
1268 }
1269 
1270 static int
yds_round_blocksize(void * addr,int blk,int mode,const audio_params_t * param)1271 yds_round_blocksize(void *addr, int blk, int mode,
1272     const audio_params_t *param)
1273 {
1274 
1275 	/*
1276 	 * Block size must be bigger than a frame.
1277 	 * That is 1024bytes at most, i.e. for 48000Hz, 16bit, 2ch.
1278 	 */
1279 	if (blk < 1024)
1280 		blk = 1024;
1281 
1282 	return blk;
1283 }
1284 
1285 static uint32_t
yds_get_lpfq(u_int sample_rate)1286 yds_get_lpfq(u_int sample_rate)
1287 {
1288 	int i;
1289 	static struct lpfqt {
1290 		u_int rate;
1291 		uint32_t lpfq;
1292 	} lpfqt[] = {
1293 		{8000,  0x32020000},
1294 		{11025, 0x31770000},
1295 		{16000, 0x31390000},
1296 		{22050, 0x31c90000},
1297 		{32000, 0x33d00000},
1298 		{48000, 0x40000000},
1299 		{0, 0}
1300 	};
1301 
1302 	if (sample_rate == 44100)		/* for P44 slot? */
1303 		return 0x370A0000;
1304 
1305 	for (i = 0; lpfqt[i].rate != 0; i++)
1306 		if (sample_rate <= lpfqt[i].rate)
1307 			break;
1308 
1309 	return lpfqt[i].lpfq;
1310 }
1311 
1312 static uint32_t
yds_get_lpfk(u_int sample_rate)1313 yds_get_lpfk(u_int sample_rate)
1314 {
1315 	int i;
1316 	static struct lpfkt {
1317 		u_int rate;
1318 		uint32_t lpfk;
1319 	} lpfkt[] = {
1320 		{8000,  0x18b20000},
1321 		{11025, 0x20930000},
1322 		{16000, 0x2b9a0000},
1323 		{22050, 0x35a10000},
1324 		{32000, 0x3eaa0000},
1325 		{48000, 0x40000000},
1326 		{0, 0}
1327 	};
1328 
1329 	if (sample_rate == 44100)		/* for P44 slot? */
1330 		return 0x46460000;
1331 
1332 	for (i = 0; lpfkt[i].rate != 0; i++)
1333 		if (sample_rate <= lpfkt[i].rate)
1334 			break;
1335 
1336 	return lpfkt[i].lpfk;
1337 }
1338 
1339 static int
yds_trigger_output(void * addr,void * start,void * end,int blksize,void (* intr)(void *),void * arg,const audio_params_t * param)1340 yds_trigger_output(void *addr, void *start, void *end, int blksize,
1341 		   void (*intr)(void *), void *arg, const audio_params_t *param)
1342 #define P44		(sc->sc_flags & YDS_CAP_HAS_P44)
1343 {
1344 	struct yds_softc *sc;
1345 	struct yds_dma *p;
1346 	struct play_slot_ctrl_bank *psb;
1347 	const u_int gain = 0x40000000;
1348 	bus_addr_t s;
1349 	size_t l;
1350 	int i;
1351 	int p44, channels;
1352 	uint32_t format;
1353 
1354 	sc = addr;
1355 #ifdef DIAGNOSTIC
1356 	if (sc->sc_play.intr)
1357 		panic("yds_trigger_output: already running");
1358 #endif
1359 
1360 	sc->sc_play.intr = intr;
1361 	sc->sc_play.intr_arg = arg;
1362 	sc->sc_play.offset = 0;
1363 	sc->sc_play.blksize = blksize;
1364 
1365 	DPRINTFN(1, ("yds_trigger_output: sc=%p start=%p end=%p "
1366 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
1367 
1368 	p = yds_find_dma(sc, start);
1369 	if (!p) {
1370 		printf("yds_trigger_output: bad addr %p\n", start);
1371 		return EINVAL;
1372 	}
1373 	sc->sc_play.dma = p;
1374 
1375 #ifdef YDS_USE_P44
1376 	/* The document says the P44 SRC supports only stereo, 16bit PCM. */
1377 	if (P44)
1378 		p44 = ((param->sample_rate == 44100) &&
1379 		       (param->channels == 2) &&
1380 		       (param->precision == 16));
1381 	else
1382 #endif
1383 		p44 = 0;
1384 	channels = p44 ? 1 : param->channels;
1385 
1386 	s = DMAADDR(p);
1387 	l = ((char *)end - (char *)start);
1388 	sc->sc_play.length = l;
1389 
1390 	*sc->ptbl = htole32(channels);	/* Num of play */
1391 
1392 	sc->sc_play.factor = 1;
1393 	if (param->channels == 2)
1394 		sc->sc_play.factor *= 2;
1395 	if (param->precision != 8)
1396 		sc->sc_play.factor *= 2;
1397 	l /= sc->sc_play.factor;
1398 
1399 	format = ((channels == 2 ? PSLT_FORMAT_STEREO : 0) |
1400 		  (param->precision == 8 ? PSLT_FORMAT_8BIT : 0) |
1401 		  (p44 ? PSLT_FORMAT_SRC441 : 0));
1402 
1403 	psb = sc->pbankp[0];
1404 	memset(psb, 0, sizeof(*psb));
1405 	psb->format = htole32(format);
1406 	psb->pgbase = htole32(s);
1407 	psb->pgloopend = htole32(l);
1408 	if (!p44) {
1409 		psb->pgdeltaend = htole32((param->sample_rate * 65536 / 48000) << 12);
1410 		psb->lpfkend = htole32(yds_get_lpfk(param->sample_rate));
1411 		psb->eggainend = htole32(gain);
1412 		psb->lpfq = htole32(yds_get_lpfq(param->sample_rate));
1413 		psb->pgdelta = htole32(psb->pgdeltaend);
1414 		psb->lpfk = htole32(yds_get_lpfk(param->sample_rate));
1415 		psb->eggain = htole32(gain);
1416 	}
1417 
1418 	for (i = 0; i < channels; i++) {
1419 		/* i == 0: left or mono, i == 1: right */
1420 		psb = sc->pbankp[i*2];
1421 		if (i)
1422 			/* copy from left */
1423 			*psb = *(sc->pbankp[0]);
1424 		if (channels == 2) {
1425 			/* stereo */
1426 			if (i == 0) {
1427 				psb->lchgain = psb->lchgainend = htole32(gain);
1428 			} else {
1429 				psb->lchgain = psb->lchgainend = 0;
1430 				psb->rchgain = psb->rchgainend = htole32(gain);
1431 				psb->format |= htole32(PSLT_FORMAT_RCH);
1432 			}
1433 		} else if (!p44) {
1434 			/* mono */
1435 			psb->lchgain = psb->rchgain = htole32(gain);
1436 			psb->lchgainend = psb->rchgainend = htole32(gain);
1437 		}
1438 		/* copy to the other bank */
1439 		*(sc->pbankp[i*2+1]) = *psb;
1440 	}
1441 
1442 	YDS_DUMP_PLAY_SLOT(5, sc, 0);
1443 	YDS_DUMP_PLAY_SLOT(5, sc, 1);
1444 
1445 	if (p44)
1446 		YWRITE4(sc, YDS_P44_OUT_VOLUME, 0x3fff3fff);
1447 	else
1448 		YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0x3fff3fff);
1449 
1450 	/* Now the play slot for the next frame is set up!! */
1451 	/* Sync play slot control data for both directions */
1452 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1453 			sc->pbankoff,
1454 			sizeof(struct play_slot_ctrl_bank) *
1455 			    channels * N_PLAY_SLOT_CTRL_BANK,
1456 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1457 	/* Sync ring buffer */
1458 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1459 			BUS_DMASYNC_PREWRITE);
1460 	/* HERE WE GO!! */
1461 	YWRITE4(sc, YDS_MODE,
1462 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1463 
1464 	return 0;
1465 }
1466 #undef P44
1467 
1468 static int
yds_trigger_input(void * addr,void * start,void * end,int blksize,void (* intr)(void *),void * arg,const audio_params_t * param)1469 yds_trigger_input(void *addr, void *start, void *end, int blksize,
1470 		  void (*intr)(void *), void *arg, const audio_params_t *param)
1471 {
1472 	struct yds_softc *sc;
1473 	struct yds_dma *p;
1474 	u_int srate, format;
1475 	struct rec_slot_ctrl_bank *rsb;
1476 	bus_addr_t s;
1477 	size_t l;
1478 
1479 	sc = addr;
1480 #ifdef DIAGNOSTIC
1481 	if (sc->sc_rec.intr)
1482 		panic("yds_trigger_input: already running");
1483 #endif
1484 	sc->sc_rec.intr = intr;
1485 	sc->sc_rec.intr_arg = arg;
1486 	sc->sc_rec.offset = 0;
1487 	sc->sc_rec.blksize = blksize;
1488 
1489 	DPRINTFN(1, ("yds_trigger_input: "
1490 	    "sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
1491 	    addr, start, end, blksize, intr, arg));
1492 	DPRINTFN(1, (" parameters: rate=%u, precision=%u, channels=%u\n",
1493 	    param->sample_rate, param->precision, param->channels));
1494 
1495 	p = yds_find_dma(sc, start);
1496 	if (!p) {
1497 		printf("yds_trigger_input: bad addr %p\n", start);
1498 		return EINVAL;
1499 	}
1500 	sc->sc_rec.dma = p;
1501 
1502 	s = DMAADDR(p);
1503 	l = ((char *)end - (char *)start);
1504 	sc->sc_rec.length = l;
1505 
1506 	sc->sc_rec.factor = 1;
1507 	if (param->channels == 2)
1508 		sc->sc_rec.factor *= 2;
1509 	if (param->precision != 8)
1510 		sc->sc_rec.factor *= 2;
1511 
1512 	rsb = &sc->rbank[0];
1513 	memset(rsb, 0, sizeof(*rsb));
1514 	rsb->pgbase = htole32(s);
1515 	rsb->pgloopendadr = htole32(l);
1516 	/* Seems all 4 banks must be set up... */
1517 	sc->rbank[1] = *rsb;
1518 	sc->rbank[2] = *rsb;
1519 	sc->rbank[3] = *rsb;
1520 
1521 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0x3fff3fff);
1522 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0x3fff3fff);
1523 	srate = 48000 * 4096 / param->sample_rate - 1;
1524 	format = ((param->precision == 8 ? YDS_FORMAT_8BIT : 0) |
1525 		  (param->channels == 2 ? YDS_FORMAT_STEREO : 0));
1526 	DPRINTF(("srate=%d, format=%08x\n", srate, format));
1527 #ifdef YDS_USE_REC_SLOT
1528 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0x3fff3fff);
1529 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0x3fff3fff);
1530 	YWRITE4(sc, YDS_MAPOF_REC, YDS_RECSLOT_VALID);
1531 	YWRITE4(sc, YDS_REC_SAMPLE_RATE, srate);
1532 	YWRITE4(sc, YDS_REC_FORMAT, format);
1533 #else
1534 	YWRITE4(sc, YDS_MAPOF_REC, YDS_ADCSLOT_VALID);
1535 	YWRITE4(sc, YDS_ADC_SAMPLE_RATE, srate);
1536 	YWRITE4(sc, YDS_ADC_FORMAT, format);
1537 #endif
1538 	/* Now the rec slot for the next frame is set up!! */
1539 	/* Sync record slot control data */
1540 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1541 			sc->rbankoff,
1542 			sizeof(struct rec_slot_ctrl_bank)*
1543 			    N_REC_SLOT_CTRL*
1544 			    N_REC_SLOT_CTRL_BANK,
1545 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1546 	/* Sync ring buffer */
1547 	bus_dmamap_sync(sc->sc_dmatag, p->map, 0, blksize,
1548 			BUS_DMASYNC_PREREAD);
1549 	/* HERE WE GO!! */
1550 	YWRITE4(sc, YDS_MODE,
1551 		YREAD4(sc, YDS_MODE) | YDS_MODE_ACTV | YDS_MODE_ACTV2);
1552 
1553 	return 0;
1554 }
1555 
1556 static int
yds_halt(struct yds_softc * sc)1557 yds_halt(struct yds_softc *sc)
1558 {
1559 	uint32_t mode;
1560 
1561 	/* Stop the DSP operation. */
1562 	mode = YREAD4(sc, YDS_MODE);
1563 	YWRITE4(sc, YDS_MODE, mode & ~(YDS_MODE_ACTV|YDS_MODE_ACTV2));
1564 
1565 	/* Paranoia...  mute all */
1566 	YWRITE4(sc, YDS_P44_OUT_VOLUME, 0);
1567 	YWRITE4(sc, YDS_DAC_OUT_VOLUME, 0);
1568 	YWRITE4(sc, YDS_ADC_IN_VOLUME, 0);
1569 	YWRITE4(sc, YDS_REC_IN_VOLUME, 0);
1570 	YWRITE4(sc, YDS_DAC_REC_VOLUME, 0);
1571 	YWRITE4(sc, YDS_P44_REC_VOLUME, 0);
1572 
1573 	return 0;
1574 }
1575 
1576 static int
yds_halt_output(void * addr)1577 yds_halt_output(void *addr)
1578 {
1579 	struct yds_softc *sc;
1580 
1581 	DPRINTF(("yds: yds_halt_output\n"));
1582 	sc = addr;
1583 	if (sc->sc_play.intr) {
1584 		sc->sc_play.intr = 0;
1585 		/* Sync play slot control data */
1586 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1587 				sc->pbankoff,
1588 				sizeof(struct play_slot_ctrl_bank)*
1589 				    (*sc->ptbl)*N_PLAY_SLOT_CTRL_BANK,
1590 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1591 		/* Stop the play slot operation */
1592 		sc->pbankp[0]->status =
1593 		sc->pbankp[1]->status =
1594 		sc->pbankp[2]->status =
1595 		sc->pbankp[3]->status = 1;
1596 		/* Sync ring buffer */
1597 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_play.dma->map,
1598 				0, sc->sc_play.length, BUS_DMASYNC_POSTWRITE);
1599 	}
1600 
1601 	return 0;
1602 }
1603 
1604 static int
yds_halt_input(void * addr)1605 yds_halt_input(void *addr)
1606 {
1607 	struct yds_softc *sc;
1608 
1609 	DPRINTF(("yds: yds_halt_input\n"));
1610 	sc = addr;
1611 	if (sc->sc_rec.intr) {
1612 		sc->sc_rec.intr = NULL;
1613 		/* Stop the rec slot operation */
1614 		YWRITE4(sc, YDS_MAPOF_REC, 0);
1615 		/* Sync rec slot control data */
1616 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_ctrldata.map,
1617 				sc->rbankoff,
1618 				sizeof(struct rec_slot_ctrl_bank)*
1619 				    N_REC_SLOT_CTRL*N_REC_SLOT_CTRL_BANK,
1620 				BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
1621 		/* Sync ring buffer */
1622 		bus_dmamap_sync(sc->sc_dmatag, sc->sc_rec.dma->map,
1623 				0, sc->sc_rec.length, BUS_DMASYNC_POSTREAD);
1624 	}
1625 
1626 	return 0;
1627 }
1628 
1629 static int
yds_getdev(void * addr,struct audio_device * retp)1630 yds_getdev(void *addr, struct audio_device *retp)
1631 {
1632 
1633 	*retp = yds_device;
1634 	return 0;
1635 }
1636 
1637 static int
yds_mixer_set_port(void * addr,mixer_ctrl_t * cp)1638 yds_mixer_set_port(void *addr, mixer_ctrl_t *cp)
1639 {
1640 	struct yds_softc *sc;
1641 
1642 	sc = addr;
1643 	return sc->sc_codec[0].codec_if->vtbl->mixer_set_port(
1644 	    sc->sc_codec[0].codec_if, cp);
1645 }
1646 
1647 static int
yds_mixer_get_port(void * addr,mixer_ctrl_t * cp)1648 yds_mixer_get_port(void *addr, mixer_ctrl_t *cp)
1649 {
1650 	struct yds_softc *sc;
1651 
1652 	sc = addr;
1653 	return sc->sc_codec[0].codec_if->vtbl->mixer_get_port(
1654 	    sc->sc_codec[0].codec_if, cp);
1655 }
1656 
1657 static int
yds_query_devinfo(void * addr,mixer_devinfo_t * dip)1658 yds_query_devinfo(void *addr, mixer_devinfo_t *dip)
1659 {
1660 	struct yds_softc *sc;
1661 
1662 	sc = addr;
1663 	return sc->sc_codec[0].codec_if->vtbl->query_devinfo(
1664 	    sc->sc_codec[0].codec_if, dip);
1665 }
1666 
1667 static void *
yds_malloc(void * addr,int direction,size_t size)1668 yds_malloc(void *addr, int direction, size_t size)
1669 {
1670 	struct yds_softc *sc;
1671 	struct yds_dma *p;
1672 	int error;
1673 
1674 	p = kmem_alloc(sizeof(*p), KM_SLEEP);
1675 	sc = addr;
1676 	error = yds_allocmem(sc, size, 16, p);
1677 	if (error) {
1678 		kmem_free(p, sizeof(*p));
1679 		return NULL;
1680 	}
1681 	p->next = sc->sc_dmas;
1682 	sc->sc_dmas = p;
1683 	return KERNADDR(p);
1684 }
1685 
1686 static void
yds_free(void * addr,void * ptr,size_t size)1687 yds_free(void *addr, void *ptr, size_t size)
1688 {
1689 	struct yds_softc *sc;
1690 	struct yds_dma **pp, *p;
1691 
1692 	sc = addr;
1693 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1694 		if (KERNADDR(p) == ptr) {
1695 			yds_freemem(sc, p);
1696 			*pp = p->next;
1697 			kmem_free(p, sizeof(*p));
1698 			return;
1699 		}
1700 	}
1701 }
1702 
1703 static struct yds_dma *
yds_find_dma(struct yds_softc * sc,void * addr)1704 yds_find_dma(struct yds_softc *sc, void *addr)
1705 {
1706 	struct yds_dma *p;
1707 
1708 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
1709 		continue;
1710 
1711 	return p;
1712 }
1713 
1714 static size_t
yds_round_buffersize(void * addr,int direction,size_t size)1715 yds_round_buffersize(void *addr, int direction, size_t size)
1716 {
1717 
1718 	/*
1719 	 * Buffer size should be at least twice as bigger as a frame.
1720 	 */
1721 	if (size < 1024 * 3)
1722 		size = 1024 * 3;
1723 	return size;
1724 }
1725 
1726 static int
yds_get_props(void * addr)1727 yds_get_props(void *addr)
1728 {
1729 
1730 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE |
1731 	    AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1732 }
1733 
1734 static void
yds_get_locks(void * addr,kmutex_t ** intr,kmutex_t ** thread)1735 yds_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
1736 {
1737 	struct yds_softc *sc;
1738 
1739 	sc = addr;
1740 	*intr = &sc->sc_intr_lock;
1741 	*thread = &sc->sc_lock;
1742 }
1743