Home | History | Annotate | Line # | Download | only in g2
aica.c revision 1.2
      1 /*	$NetBSD: aica.c,v 1.2 2003/08/24 19:44:29 marcus 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.2 2003/08/24 19:44:29 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:
    493 		break;
    494 	case AUDIO_ENCODING_ULINEAR:
    495 		play->sw_code = change_sign8;
    496 		break;
    497 
    498 	case AUDIO_ENCODING_SLINEAR_BE:
    499 		if (play->precision == 16)
    500 			play->sw_code = swap_bytes;
    501 		break;
    502 	case AUDIO_ENCODING_SLINEAR_LE:
    503 		break;
    504 	case AUDIO_ENCODING_ULINEAR_BE:
    505 		if (play->precision == 16)
    506 			play->sw_code = swap_bytes_change_sign16_le;
    507 		break;
    508 	case AUDIO_ENCODING_ULINEAR_LE:
    509 		if (play->precision == 16)
    510 			play->sw_code = change_sign16_le;
    511 		break;
    512 
    513 	case AUDIO_ENCODING_ULAW:
    514 		play->sw_code = mulaw_to_slinear16_le;
    515 		play->hw_precision = 16;
    516 		sc->sc_precision = play->hw_precision;
    517 		break;
    518 	case AUDIO_ENCODING_ALAW:
    519 		play->sw_code = alaw_to_slinear16_le;
    520 		play->hw_precision = 16;
    521 		sc->sc_precision = play->hw_precision;
    522 		break;
    523 
    524 	default:
    525 		return EINVAL;
    526 	}
    527 
    528 	return 0;
    529 }
    530 
    531 int
    532 aica_round_blocksize(void *addr, int blk)
    533 {
    534 	struct aica_softc *sc = addr;
    535 
    536 	switch (sc->sc_precision) {
    537 	case 4:
    538 		if (sc->sc_channels == 1)
    539 			return AICA_DMABUF_SIZE / 4;
    540 		else
    541 			return AICA_DMABUF_SIZE / 2;
    542 		break;
    543 	case 8:
    544 		if (sc->sc_channels == 1)
    545 			return AICA_DMABUF_SIZE / 2;
    546 		else
    547 			return AICA_DMABUF_SIZE;
    548 		break;
    549 	case 16:
    550 		if (sc->sc_channels == 1)
    551 			return AICA_DMABUF_SIZE;
    552 		else
    553 			return AICA_DMABUF_SIZE * 2;
    554 		break;
    555 	default:
    556 		break;
    557 	}
    558 
    559 	return AICA_DMABUF_SIZE / 4;
    560 }
    561 
    562 size_t
    563 aica_round_buffersize(void *addr, int dir, size_t bufsize)
    564 {
    565 
    566 	if (dir == AUMODE_PLAY)
    567 		return 65536;
    568 
    569 	return 512;	/* XXX: AUMINBUF */
    570 }
    571 
    572 void
    573 aica_command(struct aica_softc *sc, u_int32_t command)
    574 {
    575 
    576 	bus_space_write_4(sc->sc_memt, sc->sc_aica_memh,
    577 	    AICA_ARM_CMD_COMMAND, command);
    578 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh, AICA_ARM_CMD_SERIAL,
    579 	    bus_space_read_4(sc->sc_memt, sc->sc_aica_memh,
    580 	    AICA_ARM_CMD_SERIAL) + 1);
    581 }
    582 
    583 void
    584 aica_sendparam(struct aica_softc *sc, u_int32_t command,
    585     int32_t lparam, int32_t rparam)
    586 {
    587 
    588 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    589 	    AICA_ARM_CMD_LPARAM, lparam);
    590 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    591 	    AICA_ARM_CMD_RPARAM, rparam);
    592 
    593 	aica_command(sc, command);
    594 }
    595 
    596 void
    597 aica_play(struct aica_softc *sc, int blksize, int channel, int rate, int prec)
    598 {
    599 
    600 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    601 	    AICA_ARM_CMD_BLOCKSIZE, blksize);
    602 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    603 	    AICA_ARM_CMD_CHANNEL, channel);
    604 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    605 	    AICA_ARM_CMD_RATE, rate);
    606 	bus_space_write_4(sc->sc_memt,sc->sc_aica_memh,
    607 	    AICA_ARM_CMD_PRECISION, prec);
    608 
    609 	aica_command(sc, AICA_COMMAND_PLAY);
    610 }
    611 
    612 void
    613 aica_fillbuffer(struct aica_softc *sc)
    614 {
    615 
    616 	if (sc->sc_channels == 2) {
    617 		if (sc->sc_precision == 16) {
    618 			aica_ch2p16write(sc,
    619 			    AICA_DMABUF_LEFT + sc->sc_nextfill,
    620 			    (u_int16_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
    621 			aica_ch2p16write(sc,
    622 			    AICA_DMABUF_RIGHT + sc->sc_nextfill,
    623 			    (u_int16_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
    624 		} else if (sc->sc_precision == 8) {
    625 			aica_ch2p8write(sc, AICA_DMABUF_LEFT + sc->sc_nextfill,
    626 			    (u_int8_t *)sc->sc_buffer + 0, sc->sc_blksize / 2);
    627 			aica_ch2p8write(sc, AICA_DMABUF_RIGHT + sc->sc_nextfill,
    628 			    (u_int8_t *)sc->sc_buffer + 1, sc->sc_blksize / 2);
    629 		}
    630 	} else {
    631 		aica_memwrite(sc, AICA_DMABUF_MONO + sc->sc_nextfill,
    632 		    sc->sc_buffer, sc->sc_blksize);
    633 	}
    634 
    635 	(int8_t *)sc->sc_buffer += sc->sc_blksize;
    636 	if (sc->sc_buffer >= sc->sc_buffer_end)
    637 		sc->sc_buffer = sc->sc_buffer_start;
    638 
    639 	sc->sc_nextfill ^= sc->sc_blksize / sc->sc_channels;
    640 }
    641 
    642 int
    643 aica_intr(void *arg)
    644 {
    645 	struct aica_softc *sc = arg;
    646 
    647 	aica_fillbuffer(sc);
    648 
    649 	/* call audio interrupt handler (audio_pint()) */
    650 	if (sc->sc_open && sc->sc_intr != NULL) {
    651 		(*(sc->sc_intr))(sc->sc_intr_arg);
    652 	}
    653 
    654 	/* clear SPU interrupt */
    655 	bus_space_write_4(sc->sc_memt, sc->sc_aica_regh, 0x28bc, 0x20);
    656 	return 1;
    657 }
    658 
    659 int
    660 aica_trigger_output(void *addr, void *start, void *end, int blksize,
    661     void (*intr)(void *), void *arg, struct audio_params *param)
    662 {
    663 	struct aica_softc *sc = addr;
    664 
    665 	aica_command(sc, AICA_COMMAND_INIT);
    666 	tsleep(aica_trigger_output, PWAIT, "aicawait", hz / 20);
    667 
    668 	sc->sc_buffer_start = sc->sc_buffer = start;
    669 	sc->sc_buffer_end = end;
    670 	sc->sc_blksize = blksize;
    671 	sc->sc_nextfill = 0;
    672 
    673 	sc->sc_intr = intr;
    674 	sc->sc_intr_arg = arg;
    675 
    676 	/* fill buffers in advance */
    677 	aica_intr(sc);
    678 	aica_intr(sc);
    679 
    680 	/* ...and start playing */
    681 	aica_play(sc, blksize / sc->sc_channels, sc->sc_channels, sc->sc_rate,
    682 	    sc->sc_precision);
    683 
    684 	return 0;
    685 }
    686 
    687 int
    688 aica_trigger_input(void *addr, void *start, void *end, int blksize,
    689     void (*intr)(void *), void *arg, struct audio_params *param)
    690 {
    691 
    692 	return ENODEV;
    693 }
    694 
    695 int
    696 aica_halt_output(void *addr)
    697 {
    698 	struct aica_softc *sc = addr;
    699 
    700 	aica_command(sc, AICA_COMMAND_STOP);
    701 
    702 	return 0;
    703 }
    704 
    705 int
    706 aica_halt_input(void *addr)
    707 {
    708 
    709 	return ENODEV;
    710 }
    711 
    712 int
    713 aica_getdev(void *addr, struct audio_device *ret)
    714 {
    715 
    716 	*ret = aica_device;
    717 	return 0;
    718 }
    719 
    720 int
    721 aica_set_port(void *addr, mixer_ctrl_t *mc)
    722 {
    723 	struct aica_softc *sc = addr;
    724 
    725 	switch (mc->dev) {
    726 	case AICA_MASTER_VOL:
    727 		sc->sc_output_master =
    728 		    mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] & 0xff;
    729 		aica_sendparam(sc, AICA_COMMAND_MVOL,
    730 		    sc->sc_output_master, sc->sc_output_master);
    731 		break;
    732 	case AICA_OUTPUT_GAIN:
    733 		sc->sc_output_gain[AICA_VOLUME_LEFT] =
    734 		    mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]  & 0xff;
    735 		sc->sc_output_gain[AICA_VOLUME_RIGHT] =
    736 		    mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] & 0xff;
    737 		aica_sendparam(sc, AICA_COMMAND_VOL,
    738 		    sc->sc_output_gain[AICA_VOLUME_LEFT],
    739 		    sc->sc_output_gain[AICA_VOLUME_RIGHT]);
    740 		break;
    741 	default:
    742 		return EINVAL;
    743 	}
    744 
    745 	return 0;
    746 }
    747 
    748 int
    749 aica_get_port(void *addr, mixer_ctrl_t *mc)
    750 {
    751 	struct aica_softc *sc = addr;
    752 
    753 	switch (mc->dev) {
    754 	case AICA_MASTER_VOL:
    755 		if (mc->un.value.num_channels != 1)
    756 			return EINVAL;
    757 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
    758 		    L16TO256(L256TO16(sc->sc_output_master));
    759 		break;
    760 	case AICA_OUTPUT_GAIN:
    761 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
    762 		    sc->sc_output_gain[AICA_VOLUME_LEFT];
    763 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
    764 		    sc->sc_output_gain[AICA_VOLUME_RIGHT];
    765 		break;
    766 	default:
    767 		return EINVAL;
    768 	}
    769 	return 0;
    770 }
    771 
    772 int
    773 aica_query_devinfo(void *addr, mixer_devinfo_t *md)
    774 {
    775 
    776 	switch (md->index) {
    777 	case AICA_MASTER_VOL:
    778 		md->type = AUDIO_MIXER_VALUE;
    779 		md->mixer_class = AICA_OUTPUT_CLASS;
    780 		md->prev = md->next = AUDIO_MIXER_LAST;
    781 		strcpy(md->label.name, AudioNmaster);
    782 		md->un.v.num_channels = 1;
    783 		strcpy(md->un.v.units.name, AudioNvolume);
    784 		return 0;
    785 	case AICA_OUTPUT_GAIN:
    786 		md->type = AUDIO_MIXER_VALUE;
    787 		md->mixer_class = AICA_OUTPUT_CLASS;
    788 		md->prev = md->next = AUDIO_MIXER_LAST;
    789 		strcpy(md->label.name, AudioNoutput);
    790 		md->un.v.num_channels = 2;
    791 		strcpy(md->label.name, AudioNvolume);
    792 		return 0;
    793 	case AICA_OUTPUT_CLASS:
    794 		md->type = AUDIO_MIXER_CLASS;
    795 		md->mixer_class = AICA_OUTPUT_CLASS;
    796 		md->next = md->prev = AUDIO_MIXER_LAST;
    797 		strcpy(md->label.name, AudioCoutputs);
    798 		return 0;
    799 	}
    800 
    801 	return ENXIO;
    802 }
    803 
    804 int
    805 aica_get_props(void *addr)
    806 {
    807 
    808 	return 0;
    809 }
    810