Home | History | Annotate | Line # | Download | only in g2
aica.c revision 1.6
      1 /*	$NetBSD: aica.c,v 1.6 2005/01/10 22:01:36 kent Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 SHIMIZU Ryo <ryo (at) 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.6 2005/01/10 22:01:36 kent 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 const 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 #define AICA_NFORMATS	5
    102 static const struct audio_format aica_formats[AICA_NFORMATS] = {
    103 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_ADPCM, 4, 4,
    104 	 1, AUFMT_MONAURAL, 0, {1, 65536}},
    105 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    106 	 1, AUFMT_MONAURAL, 0, {1, 65536}},
    107 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    108 	 2, AUFMT_STEREO, 0, {1, 65536}},
    109 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    110 	 1, AUFMT_MONAURAL, 0, {1, 65536}},
    111 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    112 	 2, AUFMT_STEREO, 0, {1, 65536}},
    113 };
    114 
    115 int aica_match(struct device *, struct cfdata *, void *);
    116 void aica_attach(struct device *, struct device *, void *);
    117 int aica_print(void *, const char *);
    118 
    119 CFATTACH_DECL(aica, sizeof(struct aica_softc), aica_match, aica_attach,
    120     NULL, NULL);
    121 
    122 const struct audio_device aica_device = {
    123 	"Dreamcast Sound",
    124 	"",
    125 	"aica"
    126 };
    127 
    128 __inline static void aica_g2fifo_wait(void);
    129 void aica_enable(struct aica_softc *);
    130 void aica_disable(struct aica_softc *);
    131 void aica_memwrite(struct aica_softc *, bus_size_t, u_int32_t *, int);
    132 void aica_ch2p16write(struct aica_softc *, bus_size_t, u_int16_t *, int);
    133 void aica_ch2p8write(struct aica_softc *, bus_size_t, u_int8_t *, int);
    134 void aica_command(struct aica_softc *, u_int32_t);
    135 void aica_sendparam(struct aica_softc *, u_int32_t, int, int);
    136 void aica_play(struct aica_softc *, int, int, int, int);
    137 void aica_fillbuffer(struct aica_softc *);
    138 
    139 /* intr */
    140 int aica_intr(void *);
    141 
    142 /* for audio */
    143 int aica_open(void *, int);
    144 void aica_close(void *);
    145 int aica_query_encoding(void *, struct audio_encoding *);
    146 int aica_set_params(void *, int, int, audio_params_t *,
    147     audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
    148 int aica_round_blocksize(void *, int, int, const audio_params_t *);
    149 size_t aica_round_buffersize(void *, int, size_t);
    150 int aica_trigger_output(void *, void *, void *, int, void (*)(void *), void *,
    151     const audio_params_t *);
    152 int aica_trigger_input(void *, void *, void *, int, void (*)(void *), void *,
    153     const audio_params_t *);
    154 int aica_halt_output(void *);
    155 int aica_halt_input(void *);
    156 int aica_getdev(void *, struct audio_device *);
    157 int aica_set_port(void *, mixer_ctrl_t *);
    158 int aica_get_port(void *, mixer_ctrl_t *);
    159 int aica_query_devinfo(void *, mixer_devinfo_t *);
    160 void aica_encode(int, int, int, int, u_char *, u_short **);
    161 int aica_get_props(void *);
    162 
    163 const struct audio_hw_if aica_hw_if = {
    164 	aica_open,
    165 	aica_close,
    166 	NULL,				/* aica_drain */
    167 	aica_query_encoding,
    168 	aica_set_params,
    169 	aica_round_blocksize,
    170 	NULL,				/* aica_commit_setting */
    171 	NULL,				/* aica_init_output */
    172 	NULL,				/* aica_init_input */
    173 	NULL,				/* aica_start_output */
    174 	NULL,				/* aica_start_input */
    175 	aica_halt_output,
    176 	aica_halt_input,
    177 	NULL,				/* aica_speaker_ctl */
    178 	aica_getdev,
    179 	NULL,				/* aica_setfd */
    180 	aica_set_port,
    181 	aica_get_port,
    182 	aica_query_devinfo,
    183 	NULL,				/* aica_allocm */
    184 	NULL,				/* aica_freem */
    185 
    186 	aica_round_buffersize,		/* aica_round_buffersize */
    187 
    188 	NULL,				/* aica_mappage */
    189 	aica_get_props,
    190 	aica_trigger_output,
    191 	aica_trigger_input,
    192 	NULL,				/* aica_dev_ioctl */
    193 };
    194 
    195 int
    196 aica_match(struct device *parent, struct cfdata *cf, void *aux)
    197 {
    198 	static int aica_matched = 0;
    199 
    200 	if (aica_matched)
    201 		return 0;
    202 
    203 	aica_matched = 1;
    204 	return 1;
    205 }
    206 
    207 void
    208 aica_attach(struct device *parent, struct device *self, void *aux)
    209 {
    210 	struct aica_softc *sc = (struct aica_softc *)self;
    211 	struct g2bus_attach_args *ga = aux;
    212 	int i;
    213 
    214 	sc->sc_memt = ga->ga_memt;
    215 
    216 	if (bus_space_map(sc->sc_memt, AICA_REG_ADDR, 0x3000, 0,
    217 	    &sc->sc_aica_regh) != 0) {
    218 		printf(": can't map AICA register space\n");
    219 		return;
    220 	}
    221 
    222 	if (bus_space_map(sc->sc_memt, AICA_RAM_START, AICA_RAM_SIZE, 0,
    223 	    &sc->sc_aica_memh) != 0) {
    224 		printf(": can't map AICA memory space\n");
    225 		return;
    226 	}
    227 
    228 	printf(": ARM7 Sound Processing Unit\n");
    229 
    230 	aica_disable(sc);
    231 
    232 	for (i = 0; i < AICA_NCHAN; i++)
    233 		bus_space_write_4(sc->sc_memt,sc->sc_aica_regh, i << 7,
    234 		    ((bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, i << 7)
    235 		    & ~0x4000) | 0x8000));
    236 
    237 	/* load microcode, and clear memory */
    238 	bus_space_set_region_4(sc->sc_memt, sc->sc_aica_memh,
    239 	    0, 0, AICA_RAM_SIZE);
    240 
    241 	aica_memwrite(sc, 0, aica_armcode, sizeof(aica_armcode));
    242 
    243 	aica_enable(sc);
    244 
    245 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    246 	    sysasic_intr_string(IPL_BIO));
    247 	sysasic_intr_establish(SYSASIC_EVENT_AICA, IPL_BIO, aica_intr, sc);
    248 
    249 	audio_attach_mi(&aica_hw_if, sc, &sc->sc_dev);
    250 
    251 	/* init parameters */
    252 	sc->sc_output_master = 255;
    253 	sc->sc_output_gain[AICA_VOLUME_LEFT]  = 255;
    254 	sc->sc_output_gain[AICA_VOLUME_RIGHT] = 255;
    255 }
    256 
    257 void
    258 aica_enable(struct aica_softc *sc)
    259 {
    260 
    261 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28a8, 24);
    262 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00,
    263 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00) & ~1);
    264 }
    265 
    266 void
    267 aica_disable(struct aica_softc *sc)
    268 {
    269 
    270 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00,
    271 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_regh, 0x2c00) | 1);
    272 }
    273 
    274 inline static void
    275 aica_g2fifo_wait()
    276 {
    277 	int i;
    278 
    279 	i = AICA_TIMEOUT;
    280 	while (--i > 0)
    281 		if ((*(volatile u_int32_t *)0xa05f688c) & 0x11)
    282 			break;
    283 }
    284 
    285 void
    286 aica_memwrite(struct aica_softc *sc, bus_size_t offset, u_int32_t *src, int len)
    287 {
    288 	int n;
    289 
    290 	KASSERT((offset & 3) == 0);
    291 	n = (len + 3) / 4;	/* u_int32_t * n (aligned) */
    292 
    293 	aica_g2fifo_wait();
    294 	bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
    295 	    offset, src, n);
    296 }
    297 
    298 void
    299 aica_ch2p16write(struct aica_softc *sc, bus_size_t offset, u_int16_t *src,
    300     int len)
    301 {
    302 	union {
    303 		u_int32_t w[8];
    304 		u_int16_t s[16];
    305 	} buf;
    306 	u_int16_t *p;
    307 	int i;
    308 
    309 	KASSERT((offset & 3) == 0);
    310 
    311 	while (len >= 32) {
    312 		p = buf.s;
    313 		*p++ = *src++; src++;
    314 		*p++ = *src++; src++;
    315 		*p++ = *src++; src++;
    316 		*p++ = *src++; src++;
    317 		*p++ = *src++; src++;
    318 		*p++ = *src++; src++;
    319 		*p++ = *src++; src++;
    320 		*p++ = *src++; src++;
    321 		*p++ = *src++; src++;
    322 		*p++ = *src++; src++;
    323 		*p++ = *src++; src++;
    324 		*p++ = *src++; src++;
    325 		*p++ = *src++; src++;
    326 		*p++ = *src++; src++;
    327 		*p++ = *src++; src++;
    328 		*p++ = *src++; src++;
    329 
    330 		aica_g2fifo_wait();
    331 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
    332 		    offset, buf.w , 32 / 4);
    333 
    334 		offset += sizeof(u_int16_t) * 16;
    335 		len -= 32;
    336 	}
    337 
    338 	if (len / 2 > 0) {
    339 		p = buf.s;
    340 		for (i = 0; i < len / 2; i++) {
    341 			*p++ = *src++; src++;
    342 		}
    343 
    344 		aica_g2fifo_wait();
    345 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
    346 		    offset, buf.w, len / 4);
    347 	}
    348 }
    349 
    350 void
    351 aica_ch2p8write(struct aica_softc *sc, bus_size_t offset, u_int8_t *src,
    352     int len)
    353 {
    354 	u_int32_t buf[8];
    355 	u_int8_t *p;
    356 	int i;
    357 
    358 	KASSERT((offset & 3) == 0);
    359 	while (len >= 32) {
    360 		p = (u_int8_t *)buf;
    361 
    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 		*p++ = *src++; src++;
    381 		*p++ = *src++; src++;
    382 		*p++ = *src++; src++;
    383 		*p++ = *src++; src++;
    384 		*p++ = *src++; src++;
    385 		*p++ = *src++; src++;
    386 		*p++ = *src++; src++;
    387 		*p++ = *src++; src++;
    388 		*p++ = *src++; src++;
    389 		*p++ = *src++; src++;
    390 		*p++ = *src++; src++;
    391 		*p++ = *src++; src++;
    392 		*p++ = *src++; src++;
    393 		*p++ = *src++; src++;
    394 
    395 		aica_g2fifo_wait();
    396 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
    397 		    offset, buf, 32 / 4);
    398 
    399 		offset += 32;
    400 		len -= 32;
    401 	}
    402 
    403 	if (len) {
    404 		p = (u_int8_t *)buf;
    405 		for (i = 0; i < len; i++)
    406 			*p++ = *src++; src++;
    407 
    408 		aica_g2fifo_wait();
    409 		bus_space_write_region_4(sc->sc_memt, sc->sc_aica_memh,
    410 		    offset, buf, len / 4);
    411 	}
    412 }
    413 
    414 int
    415 aica_open(void *addr, int flags)
    416 {
    417 	struct aica_softc *sc = addr;
    418 
    419 	if (sc->sc_open)
    420 		return EBUSY;
    421 
    422 	sc->sc_intr = NULL;
    423 	sc->sc_open = 1;
    424 
    425 	return 0;
    426 }
    427 
    428 void
    429 aica_close(void *addr)
    430 {
    431 	struct aica_softc *sc = addr;
    432 
    433 	sc->sc_open = 0;
    434 	sc->sc_intr = NULL;
    435 }
    436 
    437 int
    438 aica_query_encoding(void *addr, struct audio_encoding *fp)
    439 {
    440 	if (fp->index >= sizeof(aica_encodings) / sizeof(aica_encodings[0]))
    441 		return EINVAL;
    442 
    443 	strcpy(fp->name, aica_encodings[fp->index].name);
    444 	fp->encoding = aica_encodings[fp->index].encoding;
    445 	fp->precision = aica_encodings[fp->index].precision;
    446 	fp->flags = 0;
    447 
    448 	return 0;
    449 }
    450 
    451 int
    452 aica_set_params(void *addr, int setmode, int usemode,
    453     audio_params_t *play, audio_params_t *rec,
    454     stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    455 {
    456 	struct aica_softc *sc = addr;
    457 	const audio_params_t *hw;
    458 	int i;
    459 
    460 	i = auconv_set_converter(aica_formats, AICA_NFORMATS,
    461 				 AUMODE_PLAY, play, FALSE, pfil);
    462 	if (i < 0)
    463 		return EINVAL;
    464 	hw = pfil->req_size > 0 ? &pfil->filters[0].param : play;
    465 	sc->sc_precision = hw->precision;
    466 	sc->sc_channels = hw->channels;
    467 	sc->sc_rate = hw->sample_rate;
    468 	sc->sc_encodings = hw->encoding;
    469 	return 0;
    470 }
    471 
    472 int
    473 aica_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
    474 {
    475 	struct aica_softc *sc = addr;
    476 
    477 	switch (sc->sc_precision) {
    478 	case 4:
    479 		if (sc->sc_channels == 1)
    480 			return AICA_DMABUF_SIZE / 4;
    481 		else
    482 			return AICA_DMABUF_SIZE / 2;
    483 		break;
    484 	case 8:
    485 		if (sc->sc_channels == 1)
    486 			return AICA_DMABUF_SIZE / 2;
    487 		else
    488 			return AICA_DMABUF_SIZE;
    489 		break;
    490 	case 16:
    491 		if (sc->sc_channels == 1)
    492 			return AICA_DMABUF_SIZE;
    493 		else
    494 			return AICA_DMABUF_SIZE * 2;
    495 		break;
    496 	default:
    497 		break;
    498 	}
    499 
    500 	return AICA_DMABUF_SIZE / 4;
    501 }
    502 
    503 size_t
    504 aica_round_buffersize(void *addr, int dir, size_t bufsize)
    505 {
    506 
    507 	if (dir == AUMODE_PLAY)
    508 		return 65536;
    509 
    510 	return 512;	/* XXX: AUMINBUF */
    511 }
    512 
    513 void
    514 aica_command(struct aica_softc *sc, u_int32_t command)
    515 {
    516 
    517 	bus_space_write_4(sc->sc_memt, sc->sc_aica_memh,
    518 	    AICA_ARM_CMD_COMMAND, command);
    519 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh, AICA_ARM_CMD_SERIAL,
    520 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_memh,
    521 	    AICA_ARM_CMD_SERIAL) + 1);
    522 }
    523 
    524 void
    525 aica_sendparam(struct aica_softc *sc, u_int32_t command,
    526     int32_t lparam, int32_t rparam)
    527 {
    528 
    529 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    530 	    AICA_ARM_CMD_LPARAM, lparam);
    531 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    532 	    AICA_ARM_CMD_RPARAM, rparam);
    533 
    534 	aica_command(sc, command);
    535 }
    536 
    537 void
    538 aica_play(struct aica_softc *sc, int blksize, int channel, int rate, int prec)
    539 {
    540 
    541 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    542 	    AICA_ARM_CMD_BLOCKSIZE, blksize);
    543 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    544 	    AICA_ARM_CMD_CHANNEL, channel);
    545 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    546 	    AICA_ARM_CMD_RATE, rate);
    547 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    548 	    AICA_ARM_CMD_PRECISION, prec);
    549 
    550 	aica_command(sc, AICA_COMMAND_PLAY);
    551 }
    552 
    553 void
    554 aica_fillbuffer(struct aica_softc *sc)
    555 {
    556 
    557 	if (sc->sc_channels == 2) {
    558 		if (sc->sc_precision == 16) {
    559 			aica_ch2p16write(sc,
    560 			    AICA_DMABUF_LEFT + sc->sc_nextfill,
    561 			    (u_int16_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
    562 			aica_ch2p16write(sc,
    563 			    AICA_DMABUF_RIGHT + sc->sc_nextfill,
    564 			    (u_int16_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
    565 		} else if (sc->sc_precision == 8) {
    566 			aica_ch2p8write(sc, AICA_DMABUF_LEFT + sc->sc_nextfill,
    567 			    (u_int8_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
    568 			aica_ch2p8write(sc, AICA_DMABUF_RIGHT + sc->sc_nextfill,
    569 			    (u_int8_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
    570 		}
    571 	} else {
    572 		aica_memwrite(sc, AICA_DMABUF_MONO + sc->sc_nextfill,
    573 		    sc->sc_buffer, sc->sc_blksize);
    574 	}
    575 
    576 	(int8_t *)sc->sc_buffer += sc->sc_blksize;
    577 	if (sc->sc_buffer >= sc->sc_buffer_end)
    578 		sc->sc_buffer = sc->sc_buffer_start;
    579 
    580 	sc->sc_nextfill ^= sc->sc_blksize / sc->sc_channels;
    581 }
    582 
    583 int
    584 aica_intr(void *arg)
    585 {
    586 	struct aica_softc *sc = arg;
    587 
    588 	aica_fillbuffer(sc);
    589 
    590 	/* call audio interrupt handler (audio_pint()) */
    591 	if (sc->sc_open && sc->sc_intr != NULL) {
    592 		(*(sc->sc_intr))(sc->sc_intr_arg);
    593 	}
    594 
    595 	/* clear SPU interrupt */
    596 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28bc, 0x20);
    597 	return 1;
    598 }
    599 
    600 int
    601 aica_trigger_output(void *addr, void *start, void *end, int blksize,
    602     void (*intr)(void *), void *arg, const audio_params_t *param)
    603 {
    604 	struct aica_softc *sc = addr;
    605 
    606 	aica_command(sc, AICA_COMMAND_INIT);
    607 	tsleep(aica_trigger_output, PWAIT, "aicawait", hz / 20);
    608 
    609 	sc->sc_buffer_start = sc->sc_buffer = start;
    610 	sc->sc_buffer_end = end;
    611 	sc->sc_blksize = blksize;
    612 	sc->sc_nextfill = 0;
    613 
    614 	sc->sc_intr = intr;
    615 	sc->sc_intr_arg = arg;
    616 
    617 	/* fill buffers in advance */
    618 	aica_intr(sc);
    619 	aica_intr(sc);
    620 
    621 	/* ...and start playing */
    622 	aica_play(sc, blksize / sc->sc_channels, sc->sc_channels, sc->sc_rate,
    623 	    sc->sc_precision);
    624 
    625 	return 0;
    626 }
    627 
    628 int
    629 aica_trigger_input(void *addr, void *start, void *end, int blksize,
    630     void (*intr)(void *), void *arg, const audio_params_t *param)
    631 {
    632 
    633 	return ENODEV;
    634 }
    635 
    636 int
    637 aica_halt_output(void *addr)
    638 {
    639 	struct aica_softc *sc = addr;
    640 
    641 	aica_command(sc, AICA_COMMAND_STOP);
    642 
    643 	return 0;
    644 }
    645 
    646 int
    647 aica_halt_input(void *addr)
    648 {
    649 
    650 	return ENODEV;
    651 }
    652 
    653 int
    654 aica_getdev(void *addr, struct audio_device *ret)
    655 {
    656 
    657 	*ret = aica_device;
    658 	return 0;
    659 }
    660 
    661 int
    662 aica_set_port(void *addr, mixer_ctrl_t *mc)
    663 {
    664 	struct aica_softc *sc = addr;
    665 
    666 	switch (mc->dev) {
    667 	case AICA_MASTER_VOL:
    668 		sc->sc_output_master =
    669 		    mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] & 0xff;
    670 		aica_sendparam(sc, AICA_COMMAND_MVOL,
    671 		    sc->sc_output_master, sc->sc_output_master);
    672 		break;
    673 	case AICA_OUTPUT_GAIN:
    674 		sc->sc_output_gain[AICA_VOLUME_LEFT] =
    675 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  & 0xff;
    676 		sc->sc_output_gain[AICA_VOLUME_RIGHT] =
    677 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] & 0xff;
    678 		aica_sendparam(sc, AICA_COMMAND_VOL,
    679 		    sc->sc_output_gain[AICA_VOLUME_LEFT],
    680 		    sc->sc_output_gain[AICA_VOLUME_RIGHT]);
    681 		break;
    682 	default:
    683 		return EINVAL;
    684 	}
    685 
    686 	return 0;
    687 }
    688 
    689 int
    690 aica_get_port(void *addr, mixer_ctrl_t *mc)
    691 {
    692 	struct aica_softc *sc = addr;
    693 
    694 	switch (mc->dev) {
    695 	case AICA_MASTER_VOL:
    696 		if (mc->un.value.num_channels != 1)
    697 			return EINVAL;
    698 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
    699 		    L16TO256(L256TO16(sc->sc_output_master));
    700 		break;
    701 	case AICA_OUTPUT_GAIN:
    702 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    703 		    sc->sc_output_gain[AICA_VOLUME_LEFT];
    704 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    705 		    sc->sc_output_gain[AICA_VOLUME_RIGHT];
    706 		break;
    707 	default:
    708 		return EINVAL;
    709 	}
    710 	return 0;
    711 }
    712 
    713 int
    714 aica_query_devinfo(void *addr, mixer_devinfo_t *md)
    715 {
    716 
    717 	switch (md->index) {
    718 	case AICA_MASTER_VOL:
    719 		md->type = AUDIO_MIXER_VALUE;
    720 		md->mixer_class = AICA_OUTPUT_CLASS;
    721 		md->prev = md->next = AUDIO_MIXER_LAST;
    722 		strcpy(md->label.name, AudioNmaster);
    723 		md->un.v.num_channels = 1;
    724 		strcpy(md->un.v.units.name, AudioNvolume);
    725 		return 0;
    726 	case AICA_OUTPUT_GAIN:
    727 		md->type = AUDIO_MIXER_VALUE;
    728 		md->mixer_class = AICA_OUTPUT_CLASS;
    729 		md->prev = md->next = AUDIO_MIXER_LAST;
    730 		strcpy(md->label.name, AudioNoutput);
    731 		md->un.v.num_channels = 2;
    732 		strcpy(md->label.name, AudioNvolume);
    733 		return 0;
    734 	case AICA_OUTPUT_CLASS:
    735 		md->type = AUDIO_MIXER_CLASS;
    736 		md->mixer_class = AICA_OUTPUT_CLASS;
    737 		md->next = md->prev = AUDIO_MIXER_LAST;
    738 		strcpy(md->label.name, AudioCoutputs);
    739 		return 0;
    740 	}
    741 
    742 	return ENXIO;
    743 }
    744 
    745 int
    746 aica_get_props(void *addr)
    747 {
    748 
    749 	return 0;
    750 }
    751