xref: /netbsd-src/sys/arch/dreamcast/dev/g2/aica.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: aica.c,v 1.4 2003/08/25 18:48:31 marcus Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 SHIMIZU Ryo <ryo@misakimix.org>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: aica.c,v 1.4 2003/08/25 18:48:31 marcus Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/audioio.h>
39 
40 #include <dev/audio_if.h>
41 #include <dev/mulaw.h>
42 #include <dev/auconv.h>
43 
44 #include <machine/bus.h>
45 #include <machine/sysasicvar.h>
46 
47 #include <dreamcast/dev/g2/g2busvar.h>
48 #include <dreamcast/dev/g2/aicavar.h>
49 #include <dreamcast/dev/microcode/aica_armcode.h>
50 
51 #define	AICA_REG_ADDR	0x00700000
52 #define	AICA_RAM_START	0x00800000
53 #define	AICA_RAM_SIZE	0x00200000
54 #define	AICA_NCHAN	64
55 #define	AICA_TIMEOUT	0x1800
56 
57 struct aica_softc {
58 	struct device		sc_dev;		/* base device */
59 	bus_space_tag_t		sc_memt;
60 	bus_space_handle_t	sc_aica_regh;
61 	bus_space_handle_t	sc_aica_memh;
62 
63 	/* audio property */
64 	int			sc_open;
65 	int			sc_encodings;
66 	int			sc_precision;
67 	int			sc_channels;
68 	int			sc_rate;
69 	void			(*sc_intr)(void *);
70 	void			*sc_intr_arg;
71 
72 	int			sc_output_master;
73 	int			sc_output_gain[2];
74 #define	AICA_VOLUME_LEFT	0
75 #define	AICA_VOLUME_RIGHT	1
76 
77 	/* work for output */
78 	void			*sc_buffer;
79 	void			*sc_buffer_start;
80 	void			*sc_buffer_end;
81 	int			sc_blksize;
82 	int			sc_nextfill;
83 };
84 
85 struct {
86 	char	*name;
87 	int 	encoding;
88 	int 	precision;
89 } aica_encodings[] = {
90 	{AudioEadpcm,		AUDIO_ENCODING_ADPCM,		4},
91 	{AudioEslinear,		AUDIO_ENCODING_SLINEAR,		8},
92 	{AudioEulinear,		AUDIO_ENCODING_ULINEAR,		8},
93 	{AudioEmulaw,		AUDIO_ENCODING_ULAW,		8},
94 	{AudioEalaw,		AUDIO_ENCODING_ALAW,		8},
95 	{AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	16},
96 	{AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	16},
97 	{AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	16},
98 	{AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	16},
99 };
100 
101 int aica_match(struct device *, struct cfdata *, void *);
102 void aica_attach(struct device *, struct device *, void *);
103 int aica_print(void *, const char *);
104 
105 CFATTACH_DECL(aica, sizeof(struct aica_softc), aica_match, aica_attach,
106     NULL, NULL);
107 
108 struct audio_device aica_device = {
109 	"Dreamcast Sound",
110 	"",
111 	"aica"
112 };
113 
114 __inline static void aica_g2fifo_wait(void);
115 void aica_enable(struct aica_softc *);
116 void aica_disable(struct aica_softc *);
117 void aica_memwrite(struct aica_softc *, bus_size_t, u_int32_t *, int);
118 void aica_ch2p16write(struct aica_softc *, bus_size_t, u_int16_t *, int);
119 void aica_ch2p8write(struct aica_softc *, bus_size_t, u_int8_t *, int);
120 void aica_command(struct aica_softc *, u_int32_t);
121 void aica_sendparam(struct aica_softc *, u_int32_t, int, int);
122 void aica_play(struct aica_softc *, int, int, int, int);
123 void aica_fillbuffer(struct aica_softc *);
124 
125 /* intr */
126 int aica_intr(void *);
127 
128 /* for audio */
129 int aica_open(void *, int);
130 void aica_close(void *);
131 int aica_query_encoding(void *, struct audio_encoding *);
132 int aica_set_params(void *, int, int, struct audio_params *,
133     struct audio_params *);
134 int aica_round_blocksize(void *, int);
135 size_t aica_round_buffersize(void *, int, size_t);
136 int aica_trigger_output(void *, void *, void *, int, void (*)(void *), void *,
137     struct audio_params *);
138 int aica_trigger_input(void *, void *, void *, int, void (*)(void *), void *,
139     struct audio_params *);
140 int aica_halt_output(void *);
141 int aica_halt_input(void *);
142 int aica_getdev(void *, struct audio_device *);
143 int aica_set_port(void *, mixer_ctrl_t *);
144 int aica_get_port(void *, mixer_ctrl_t *);
145 int aica_query_devinfo(void *, mixer_devinfo_t *);
146 void aica_encode(int, int, int, int, u_char *, u_short **);
147 int aica_get_props(void *);
148 
149 struct audio_hw_if aica_hw_if = {
150 	aica_open,
151 	aica_close,
152 	NULL,				/* aica_drain */
153 	aica_query_encoding,
154 	aica_set_params,
155 	aica_round_blocksize,
156 	NULL,				/* aica_commit_setting */
157 	NULL,				/* aica_init_output */
158 	NULL,				/* aica_init_input */
159 	NULL,				/* aica_start_output */
160 	NULL,				/* aica_start_input */
161 	aica_halt_output,
162 	aica_halt_input,
163 	NULL,				/* aica_speaker_ctl */
164 	aica_getdev,
165 	NULL,				/* aica_setfd */
166 	aica_set_port,
167 	aica_get_port,
168 	aica_query_devinfo,
169 	NULL,				/* aica_allocm */
170 	NULL,				/* aica_freem */
171 
172 	aica_round_buffersize,		/* aica_round_buffersize */
173 
174 	NULL,				/* aica_mappage */
175 	aica_get_props,
176 	aica_trigger_output,
177 	aica_trigger_input,
178 	NULL,				/* aica_dev_ioctl */
179 };
180 
181 int
182 aica_match(struct device *parent, struct cfdata *cf, void *aux)
183 {
184 	static int aica_matched = 0;
185 
186 	if (aica_matched)
187 		return 0;
188 
189 	aica_matched = 1;
190 	return 1;
191 }
192 
193 void
194 aica_attach(struct device *parent, struct device *self, void *aux)
195 {
196 	struct aica_softc *sc = (struct aica_softc *)self;
197 	struct g2bus_attach_args *ga = aux;
198 	int i;
199 
200 	sc->sc_memt = ga->ga_memt;
201 
202 	if (bus_space_map(sc->sc_memt, AICA_REG_ADDR, 0x3000, 0,
203 	    &sc->sc_aica_regh) != 0) {
204 		printf(": can't map AICA register space\n");
205 		return;
206 	}
207 
208 	if (bus_space_map(sc->sc_memt, AICA_RAM_START, AICA_RAM_SIZE, 0,
209 	    &sc->sc_aica_memh) != 0) {
210 		printf(": can't map AICA memory space\n");
211 		return;
212 	}
213 
214 	printf(": ARM7 Sound Processing Unit\n");
215 
216 	aica_disable(sc);
217 
218 	for (i = 0; i < AICA_NCHAN; i++)
219 		bus_space_write_4(sc->sc_memt,sc->sc_aica_regh, i << 7,
220 		    ((bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, i << 7)
221 		    & ~0x4000) | 0x8000));
222 
223 	/* load microcode, and clear memory */
224 	bus_space_set_region_4(sc->sc_memt, sc->sc_aica_memh,
225 	    0, 0, AICA_RAM_SIZE);
226 
227 	aica_memwrite(sc, 0, aica_armcode, sizeof(aica_armcode));
228 
229 	aica_enable(sc);
230 
231 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
232 	    sysasic_intr_string(IPL_BIO));
233 	sysasic_intr_establish(SYSASIC_EVENT_AICA, IPL_BIO, aica_intr, sc);
234 
235 	audio_attach_mi(&aica_hw_if, sc, &sc->sc_dev);
236 
237 	/* init parameters */
238 	sc->sc_output_master = 255;
239 	sc->sc_output_gain[AICA_VOLUME_LEFT]  = 255;
240 	sc->sc_output_gain[AICA_VOLUME_RIGHT] = 255;
241 }
242 
243 void
244 aica_enable(struct aica_softc *sc)
245 {
246 
247 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28a8, 24);
248 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00,
249 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00) & ~1);
250 }
251 
252 void
253 aica_disable(struct aica_softc *sc)
254 {
255 
256 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00,
257 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00) | 1);
258 }
259 
260 inline static void
261 aica_g2fifo_wait()
262 {
263 	int i;
264 
265 	i = AICA_TIMEOUT;
266 	while (--i > 0)
267 		if ((*(volatile u_int32_t *)0xa05f688c) & 0x11)
268 			break;
269 }
270 
271 void
272 aica_memwrite(struct aica_softc *sc, bus_size_t offset, u_int32_t *src, int len)
273 {
274 	int n;
275 
276 	KASSERT((offset & 3) == 0);
277 	n = (len + 3) / 4;	/* u_int32_t * n (aligned) */
278 
279 	aica_g2fifo_wait();
280 	bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
281 	    offset, src, n);
282 }
283 
284 void
285 aica_ch2p16write(struct aica_softc *sc, bus_size_t offset, u_int16_t *src,
286     int len)
287 {
288 	union {
289 		u_int32_t w[8];
290 		u_int16_t s[16];
291 	} buf;
292 	u_int16_t *p;
293 	int i;
294 
295 	KASSERT((offset & 3) == 0);
296 
297 	while (len >= 32) {
298 		p = buf.s;
299 		*p++ = *src++; src++;
300 		*p++ = *src++; src++;
301 		*p++ = *src++; src++;
302 		*p++ = *src++; src++;
303 		*p++ = *src++; src++;
304 		*p++ = *src++; src++;
305 		*p++ = *src++; src++;
306 		*p++ = *src++; src++;
307 		*p++ = *src++; src++;
308 		*p++ = *src++; src++;
309 		*p++ = *src++; src++;
310 		*p++ = *src++; src++;
311 		*p++ = *src++; src++;
312 		*p++ = *src++; src++;
313 		*p++ = *src++; src++;
314 		*p++ = *src++; src++;
315 
316 		aica_g2fifo_wait();
317 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
318 		    offset, buf.w , 32 / 4);
319 
320 		offset += sizeof(u_int16_t) * 16;
321 		len -= 32;
322 	}
323 
324 	if (len / 2 > 0) {
325 		p = buf.s;
326 		for (i = 0; i < len / 2; i++) {
327 			*p++ = *src++; src++;
328 		}
329 
330 		aica_g2fifo_wait();
331 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
332 		    offset, buf.w, len / 4);
333 	}
334 }
335 
336 void
337 aica_ch2p8write(struct aica_softc *sc, bus_size_t offset, u_int8_t *src,
338     int len)
339 {
340 	u_int32_t buf[8];
341 	u_int8_t *p;
342 	int i;
343 
344 	KASSERT((offset & 3) == 0);
345 	while (len >= 32) {
346 		p = (u_int8_t *)buf;
347 
348 		*p++ = *src++; src++;
349 		*p++ = *src++; src++;
350 		*p++ = *src++; src++;
351 		*p++ = *src++; src++;
352 		*p++ = *src++; src++;
353 		*p++ = *src++; src++;
354 		*p++ = *src++; src++;
355 		*p++ = *src++; src++;
356 		*p++ = *src++; src++;
357 		*p++ = *src++; src++;
358 		*p++ = *src++; src++;
359 		*p++ = *src++; src++;
360 		*p++ = *src++; src++;
361 		*p++ = *src++; src++;
362 		*p++ = *src++; src++;
363 		*p++ = *src++; src++;
364 		*p++ = *src++; src++;
365 		*p++ = *src++; src++;
366 		*p++ = *src++; src++;
367 		*p++ = *src++; src++;
368 		*p++ = *src++; src++;
369 		*p++ = *src++; src++;
370 		*p++ = *src++; src++;
371 		*p++ = *src++; src++;
372 		*p++ = *src++; src++;
373 		*p++ = *src++; src++;
374 		*p++ = *src++; src++;
375 		*p++ = *src++; src++;
376 		*p++ = *src++; src++;
377 		*p++ = *src++; src++;
378 		*p++ = *src++; src++;
379 		*p++ = *src++; src++;
380 
381 		aica_g2fifo_wait();
382 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
383 		    offset, buf, 32 / 4);
384 
385 		offset += 32;
386 		len -= 32;
387 	}
388 
389 	if (len) {
390 		p = (u_int8_t *)buf;
391 		for (i = 0; i < len; i++)
392 			*p++ = *src++; src++;
393 
394 		aica_g2fifo_wait();
395 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
396 		    offset, buf, len / 4);
397 	}
398 }
399 
400 int
401 aica_open(void *addr, int flags)
402 {
403 	struct aica_softc *sc = addr;
404 
405 	if (sc->sc_open)
406 		return EBUSY;
407 
408 	sc->sc_intr = NULL;
409 	sc->sc_open = 1;
410 
411 	return 0;
412 }
413 
414 void
415 aica_close(void *addr)
416 {
417 	struct aica_softc *sc = addr;
418 
419 	sc->sc_open = 0;
420 	sc->sc_intr = NULL;
421 }
422 
423 int
424 aica_query_encoding(void *addr, struct audio_encoding *fp)
425 {
426 	if (fp->index >= sizeof(aica_encodings) / sizeof(aica_encodings[0]))
427 		return EINVAL;
428 
429 	strcpy(fp->name, aica_encodings[fp->index].name);
430 	fp->encoding = aica_encodings[fp->index].encoding;
431 	fp->precision = aica_encodings[fp->index].precision;
432 	fp->flags = 0;
433 
434 	return 0;
435 }
436 
437 int
438 aica_set_params(void *addr, int setmode, int usemode,
439     struct audio_params *play, struct audio_params *rec)
440 {
441 	struct aica_softc *sc = addr;
442 
443 	if ((play->channels != 1) &&
444 	    (play->channels != 2))
445 		return EINVAL;
446 
447 	if ((play->precision != 4) &&
448 	    (play->precision != 8) &&
449 	    (play->precision != 16))
450 		return EINVAL;
451 
452 	play->factor = 1;
453 	play->factor_denom = 1;
454 
455 	play->hw_precision = play->precision;
456 	play->hw_channels = play->channels;
457 	play->hw_sample_rate = play->sample_rate;
458 	play->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
459 
460 	play->sw_code = NULL;
461 
462 	sc->sc_precision = play->hw_precision;
463 	sc->sc_channels = play->hw_channels;
464 	sc->sc_rate = play->hw_sample_rate;
465 	sc->sc_encodings = play->hw_encoding;
466 
467 #if 1
468 	/* XXX: limit check */
469 	if ((play->precision == 4) &&
470 	    (play->channels == 1) &&
471 	    (play->sample_rate >= 65536))
472 		return EINVAL;
473 
474 	if ((play->precision == 8) &&
475 	    (play->channels == 1) &&
476 	    (play->sample_rate >= 65536))
477 		return EINVAL;
478 #endif
479 
480 	switch (play->encoding) {
481 	case AUDIO_ENCODING_ADPCM:
482 		if (play->precision != 4)
483 			return EINVAL;
484 		if (play->channels != 1)
485 			return EINVAL;
486 
487 		play->hw_encoding = AUDIO_ENCODING_ADPCM;
488 		play->hw_precision = 8;	/* 4? XXX */
489 		sc->sc_precision = 4;
490 		break;
491 
492 	case AUDIO_ENCODING_SLINEAR_BE:
493 		if (play->precision == 16)
494 			play->sw_code = swap_bytes;
495 		break;
496 	case AUDIO_ENCODING_SLINEAR_LE:
497 	case AUDIO_ENCODING_SLINEAR:
498 		break;
499 	case AUDIO_ENCODING_ULINEAR_BE:
500 		if (play->precision == 16)
501 			play->sw_code = swap_bytes_change_sign16_le;
502 		else if(play->precision == 8)
503 			play->sw_code = change_sign8;
504 		break;
505 	case AUDIO_ENCODING_ULINEAR_LE:
506 	case AUDIO_ENCODING_ULINEAR:
507 		if (play->precision == 16)
508 			play->sw_code = change_sign16_le;
509 		else if(play->precision == 8)
510 			play->sw_code = change_sign8;
511 		break;
512 
513 	case AUDIO_ENCODING_ULAW:
514 		play->factor = 2;
515 		play->sw_code = mulaw_to_slinear16_le;
516 		play->hw_precision = 16;
517 		sc->sc_precision = play->hw_precision;
518 		break;
519 	case AUDIO_ENCODING_ALAW:
520 		play->factor = 2;
521 		play->sw_code = alaw_to_slinear16_le;
522 		play->hw_precision = 16;
523 		sc->sc_precision = play->hw_precision;
524 		break;
525 
526 	default:
527 		return EINVAL;
528 	}
529 
530 	return 0;
531 }
532 
533 int
534 aica_round_blocksize(void *addr, int blk)
535 {
536 	struct aica_softc *sc = addr;
537 
538 	switch (sc->sc_precision) {
539 	case 4:
540 		if (sc->sc_channels == 1)
541 			return AICA_DMABUF_SIZE / 4;
542 		else
543 			return AICA_DMABUF_SIZE / 2;
544 		break;
545 	case 8:
546 		if (sc->sc_channels == 1)
547 			return AICA_DMABUF_SIZE / 2;
548 		else
549 			return AICA_DMABUF_SIZE;
550 		break;
551 	case 16:
552 		if (sc->sc_channels == 1)
553 			return AICA_DMABUF_SIZE;
554 		else
555 			return AICA_DMABUF_SIZE * 2;
556 		break;
557 	default:
558 		break;
559 	}
560 
561 	return AICA_DMABUF_SIZE / 4;
562 }
563 
564 size_t
565 aica_round_buffersize(void *addr, int dir, size_t bufsize)
566 {
567 
568 	if (dir == AUMODE_PLAY)
569 		return 65536;
570 
571 	return 512;	/* XXX: AUMINBUF */
572 }
573 
574 void
575 aica_command(struct aica_softc *sc, u_int32_t command)
576 {
577 
578 	bus_space_write_4(sc->sc_memt, sc->sc_aica_memh,
579 	    AICA_ARM_CMD_COMMAND, command);
580 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh, AICA_ARM_CMD_SERIAL,
581 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_memh,
582 	    AICA_ARM_CMD_SERIAL) + 1);
583 }
584 
585 void
586 aica_sendparam(struct aica_softc *sc, u_int32_t command,
587     int32_t lparam, int32_t rparam)
588 {
589 
590 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
591 	    AICA_ARM_CMD_LPARAM, lparam);
592 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
593 	    AICA_ARM_CMD_RPARAM, rparam);
594 
595 	aica_command(sc, command);
596 }
597 
598 void
599 aica_play(struct aica_softc *sc, int blksize, int channel, int rate, int prec)
600 {
601 
602 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
603 	    AICA_ARM_CMD_BLOCKSIZE, blksize);
604 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
605 	    AICA_ARM_CMD_CHANNEL, channel);
606 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
607 	    AICA_ARM_CMD_RATE, rate);
608 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
609 	    AICA_ARM_CMD_PRECISION, prec);
610 
611 	aica_command(sc, AICA_COMMAND_PLAY);
612 }
613 
614 void
615 aica_fillbuffer(struct aica_softc *sc)
616 {
617 
618 	if (sc->sc_channels == 2) {
619 		if (sc->sc_precision == 16) {
620 			aica_ch2p16write(sc,
621 			    AICA_DMABUF_LEFT + sc->sc_nextfill,
622 			    (u_int16_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
623 			aica_ch2p16write(sc,
624 			    AICA_DMABUF_RIGHT + sc->sc_nextfill,
625 			    (u_int16_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
626 		} else if (sc->sc_precision == 8) {
627 			aica_ch2p8write(sc, AICA_DMABUF_LEFT + sc->sc_nextfill,
628 			    (u_int8_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
629 			aica_ch2p8write(sc, AICA_DMABUF_RIGHT + sc->sc_nextfill,
630 			    (u_int8_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
631 		}
632 	} else {
633 		aica_memwrite(sc, AICA_DMABUF_MONO + sc->sc_nextfill,
634 		    sc->sc_buffer, sc->sc_blksize);
635 	}
636 
637 	(int8_t *)sc->sc_buffer += sc->sc_blksize;
638 	if (sc->sc_buffer >= sc->sc_buffer_end)
639 		sc->sc_buffer = sc->sc_buffer_start;
640 
641 	sc->sc_nextfill ^= sc->sc_blksize / sc->sc_channels;
642 }
643 
644 int
645 aica_intr(void *arg)
646 {
647 	struct aica_softc *sc = arg;
648 
649 	aica_fillbuffer(sc);
650 
651 	/* call audio interrupt handler (audio_pint()) */
652 	if (sc->sc_open && sc->sc_intr != NULL) {
653 		(*(sc->sc_intr))(sc->sc_intr_arg);
654 	}
655 
656 	/* clear SPU interrupt */
657 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28bc, 0x20);
658 	return 1;
659 }
660 
661 int
662 aica_trigger_output(void *addr, void *start, void *end, int blksize,
663     void (*intr)(void *), void *arg, struct audio_params *param)
664 {
665 	struct aica_softc *sc = addr;
666 
667 	aica_command(sc, AICA_COMMAND_INIT);
668 	tsleep(aica_trigger_output, PWAIT, "aicawait", hz / 20);
669 
670 	sc->sc_buffer_start = sc->sc_buffer = start;
671 	sc->sc_buffer_end = end;
672 	sc->sc_blksize = blksize;
673 	sc->sc_nextfill = 0;
674 
675 	sc->sc_intr = intr;
676 	sc->sc_intr_arg = arg;
677 
678 	/* fill buffers in advance */
679 	aica_intr(sc);
680 	aica_intr(sc);
681 
682 	/* ...and start playing */
683 	aica_play(sc, blksize / sc->sc_channels, sc->sc_channels, sc->sc_rate,
684 	    sc->sc_precision);
685 
686 	return 0;
687 }
688 
689 int
690 aica_trigger_input(void *addr, void *start, void *end, int blksize,
691     void (*intr)(void *), void *arg, struct audio_params *param)
692 {
693 
694 	return ENODEV;
695 }
696 
697 int
698 aica_halt_output(void *addr)
699 {
700 	struct aica_softc *sc = addr;
701 
702 	aica_command(sc, AICA_COMMAND_STOP);
703 
704 	return 0;
705 }
706 
707 int
708 aica_halt_input(void *addr)
709 {
710 
711 	return ENODEV;
712 }
713 
714 int
715 aica_getdev(void *addr, struct audio_device *ret)
716 {
717 
718 	*ret = aica_device;
719 	return 0;
720 }
721 
722 int
723 aica_set_port(void *addr, mixer_ctrl_t *mc)
724 {
725 	struct aica_softc *sc = addr;
726 
727 	switch (mc->dev) {
728 	case AICA_MASTER_VOL:
729 		sc->sc_output_master =
730 		    mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] & 0xff;
731 		aica_sendparam(sc, AICA_COMMAND_MVOL,
732 		    sc->sc_output_master, sc->sc_output_master);
733 		break;
734 	case AICA_OUTPUT_GAIN:
735 		sc->sc_output_gain[AICA_VOLUME_LEFT] =
736 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  & 0xff;
737 		sc->sc_output_gain[AICA_VOLUME_RIGHT] =
738 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] & 0xff;
739 		aica_sendparam(sc, AICA_COMMAND_VOL,
740 		    sc->sc_output_gain[AICA_VOLUME_LEFT],
741 		    sc->sc_output_gain[AICA_VOLUME_RIGHT]);
742 		break;
743 	default:
744 		return EINVAL;
745 	}
746 
747 	return 0;
748 }
749 
750 int
751 aica_get_port(void *addr, mixer_ctrl_t *mc)
752 {
753 	struct aica_softc *sc = addr;
754 
755 	switch (mc->dev) {
756 	case AICA_MASTER_VOL:
757 		if (mc->un.value.num_channels != 1)
758 			return EINVAL;
759 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
760 		    L16TO256(L256TO16(sc->sc_output_master));
761 		break;
762 	case AICA_OUTPUT_GAIN:
763 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
764 		    sc->sc_output_gain[AICA_VOLUME_LEFT];
765 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
766 		    sc->sc_output_gain[AICA_VOLUME_RIGHT];
767 		break;
768 	default:
769 		return EINVAL;
770 	}
771 	return 0;
772 }
773 
774 int
775 aica_query_devinfo(void *addr, mixer_devinfo_t *md)
776 {
777 
778 	switch (md->index) {
779 	case AICA_MASTER_VOL:
780 		md->type = AUDIO_MIXER_VALUE;
781 		md->mixer_class = AICA_OUTPUT_CLASS;
782 		md->prev = md->next = AUDIO_MIXER_LAST;
783 		strcpy(md->label.name, AudioNmaster);
784 		md->un.v.num_channels = 1;
785 		strcpy(md->un.v.units.name, AudioNvolume);
786 		return 0;
787 	case AICA_OUTPUT_GAIN:
788 		md->type = AUDIO_MIXER_VALUE;
789 		md->mixer_class = AICA_OUTPUT_CLASS;
790 		md->prev = md->next = AUDIO_MIXER_LAST;
791 		strcpy(md->label.name, AudioNoutput);
792 		md->un.v.num_channels = 2;
793 		strcpy(md->label.name, AudioNvolume);
794 		return 0;
795 	case AICA_OUTPUT_CLASS:
796 		md->type = AUDIO_MIXER_CLASS;
797 		md->mixer_class = AICA_OUTPUT_CLASS;
798 		md->next = md->prev = AUDIO_MIXER_LAST;
799 		strcpy(md->label.name, AudioCoutputs);
800 		return 0;
801 	}
802 
803 	return ENXIO;
804 }
805 
806 int
807 aica_get_props(void *addr)
808 {
809 
810 	return 0;
811 }
812