Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.25
      1 /*	$NetBSD: vidcaudio.c,v 1.25 2003/12/31 15:40:31 bjh21 Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Melvin Tang-Richardson
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the RiscBSD team.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2003 Ben Harris
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. The name of the author may not be used to endorse or promote products
     45  *    derived from this software without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * audio driver for the RiscPC 8/16 bit sound
     61  *
     62  * Interfaces with the NetBSD generic audio driver to provide SUN
     63  * /dev/audio (partial) compatibility.
     64  */
     65 
     66 #include <sys/param.h>	/* proc.h */
     67 
     68 __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.25 2003/12/31 15:40:31 bjh21 Exp $");
     69 
     70 #include <sys/audioio.h>
     71 #include <sys/conf.h>   /* autoconfig functions */
     72 #include <sys/device.h> /* device calls */
     73 #include <sys/errno.h>
     74 #include <sys/proc.h>	/* device calls */
     75 #include <sys/systm.h>
     76 
     77 #include <uvm/uvm_extern.h>
     78 
     79 #include <dev/audio_if.h>
     80 #include <dev/mulaw.h>
     81 
     82 #include <machine/intr.h>
     83 #include <arm/arm32/katelib.h>
     84 
     85 #include <arm/iomd/vidcaudiovar.h>
     86 #include <arm/iomd/iomdreg.h>
     87 #include <arm/iomd/iomdvar.h>
     88 #include <arm/iomd/vidc.h>
     89 #include <arm/mainbus/mainbus.h>
     90 #include <arm/iomd/waveform.h>
     91 #include "vidcaudio.h"
     92 
     93 extern int *vidc_base;
     94 
     95 struct vidcaudio_softc {
     96 	struct	device sc_dev;
     97 
     98 	irqhandler_t	sc_ih;
     99 	int	sc_dma_intr;
    100 
    101 	u_int	sc_hw_encoding;
    102 	u_int	sc_hw_precision;
    103 
    104 	void	*sc_pstart;
    105 	void	*sc_pend;
    106 	size_t	sc_pblksize;
    107 	void	*sc_pnext;
    108 	void	(*sc_pintr)(void *);
    109 	void	*sc_parg;
    110 	int	sc_pcountdown;
    111 };
    112 
    113 static int  vidcaudio_probe(struct device *, struct cfdata *, void *);
    114 static void vidcaudio_attach(struct device *, struct device *, void *);
    115 static int  vidcaudio_open(void *, int);
    116 static void vidcaudio_close(void *);
    117 
    118 static int vidcaudio_intr(void *);
    119 static void vidcaudio_rate(int);
    120 static void vidcaudio_ctrl(int);
    121 static void vidcaudio_stereo(int, int);
    122 static void mulaw_to_vidc(void *, u_char *, int);
    123 static void mulaw_to_vidc_stereo(void *, u_char *, int);
    124 
    125 CFATTACH_DECL(vidcaudio, sizeof(struct vidcaudio_softc),
    126     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    127 
    128 static int    vidcaudio_query_encoding(void *, struct audio_encoding *);
    129 static int    vidcaudio_set_params(void *, int, int, struct audio_params *,
    130     struct audio_params *);
    131 static int    vidcaudio_round_blocksize(void *, int);
    132 static int    vidcaudio_trigger_output(void *, void *, void *, int,
    133     void (*)(void *), void *, struct audio_params *);
    134 static int    vidcaudio_trigger_input(void *, void *, void *, int,
    135     void (*)(void *), void *, struct audio_params *);
    136 static int    vidcaudio_halt_output(void *);
    137 static int    vidcaudio_halt_input(void *);
    138 static int    vidcaudio_getdev(void *, struct audio_device *);
    139 static int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    140 static int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    141 static int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    142 static int    vidcaudio_get_props(void *);
    143 
    144 static struct audio_device vidcaudio_device = {
    145 	"ARM VIDC",
    146 	"",
    147 	"vidcaudio"
    148 };
    149 
    150 static struct audio_hw_if vidcaudio_hw_if = {
    151 	vidcaudio_open,
    152 	vidcaudio_close,
    153 	NULL,
    154 	vidcaudio_query_encoding,
    155 	vidcaudio_set_params,
    156 	vidcaudio_round_blocksize,
    157 	NULL,
    158 	NULL,
    159 	NULL,
    160 	NULL,
    161 	NULL,
    162 	vidcaudio_halt_output,
    163 	vidcaudio_halt_input,
    164 	NULL,
    165 	vidcaudio_getdev,
    166 	NULL,
    167 	vidcaudio_set_port,
    168 	vidcaudio_get_port,
    169 	vidcaudio_query_devinfo,
    170 	NULL,
    171 	NULL,
    172 	NULL,
    173 	NULL,
    174 	vidcaudio_get_props,
    175 	vidcaudio_trigger_output,
    176 	vidcaudio_trigger_input,
    177 	NULL,
    178 };
    179 
    180 
    181 void
    182 vidcaudio_beep_generate(void)
    183 {
    184 
    185 }
    186 
    187 
    188 static int
    189 vidcaudio_probe(struct device *parent, struct cfdata *cf, void *aux)
    190 {
    191 	int id;
    192 
    193 	id = IOMD_ID;
    194 
    195 	/* So far I only know about this IOMD */
    196 	switch (id) {
    197 	case RPC600_IOMD_ID:
    198 	case ARM7500_IOC_ID:
    199 	case ARM7500FE_IOC_ID:
    200 		return 1;
    201 	default:
    202 		aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
    203 		return 0;
    204 	}
    205 }
    206 
    207 
    208 static void
    209 vidcaudio_attach(struct device *parent, struct device *self, void *aux)
    210 {
    211 	struct vidcaudio_softc *sc = (void *)self;
    212 
    213 	switch (IOMD_ID) {
    214 	case RPC600_IOMD_ID:
    215 		sc->sc_hw_encoding = AUDIO_ENCODING_ULAW;
    216 		sc->sc_hw_precision = 8;
    217 		sc->sc_dma_intr = IRQ_DMASCH0;
    218 		aprint_normal(": 8-bit\n");
    219 		break;
    220 	case ARM7500_IOC_ID:
    221 	case ARM7500FE_IOC_ID:
    222 		sc->sc_hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    223 		sc->sc_hw_precision = 16;
    224 		sc->sc_dma_intr = IRQ_SDMA;
    225 		aprint_normal(": 16-bit\n");
    226 		break;
    227 	default:
    228 		aprint_error(": strange IOMD\n");
    229 		return;
    230 	}
    231 
    232 	/* Install the irq handler for the DMA interrupt */
    233 	sc->sc_ih.ih_func = vidcaudio_intr;
    234 	sc->sc_ih.ih_arg = sc;
    235 	sc->sc_ih.ih_level = IPL_AUDIO;
    236 	sc->sc_ih.ih_name = self->dv_xname;
    237 
    238 	if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
    239 		aprint_error("%s: couldn't claim IRQ %d\n",
    240 		    self->dv_xname, sc->sc_dma_intr);
    241 		return;
    242 	}
    243 
    244 	disable_irq(sc->sc_dma_intr);
    245 
    246 	audio_attach_mi(&vidcaudio_hw_if, sc, self);
    247 }
    248 
    249 static int
    250 vidcaudio_open(void *addr, int flags)
    251 {
    252 
    253 #ifdef VIDCAUDIO_DEBUG
    254 	printf("DEBUG: vidcaudio_open called\n");
    255 #endif
    256 	return 0;
    257 }
    258 
    259 static void
    260 vidcaudio_close(void *addr)
    261 {
    262 
    263 #ifdef VIDCAUDIO_DEBUG
    264 	printf("DEBUG: vidcaudio_close called\n");
    265 #endif
    266 }
    267 
    268 /*
    269  * Interface to the generic audio driver
    270  */
    271 
    272 static int
    273 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
    274 {
    275 	struct vidcaudio_softc *sc = addr;
    276 
    277 	switch (fp->index) {
    278 	case 0:
    279 		strcpy(fp->name, AudioEmulaw);
    280 		fp->encoding = AUDIO_ENCODING_ULAW;
    281 		fp->precision = 8;
    282 		if (sc->sc_hw_encoding != AUDIO_ENCODING_ULAW)
    283 			fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    284 		else
    285 			fp->flags = 0;
    286 		break;
    287 
    288 	case 1:
    289 		if (sc->sc_hw_encoding == AUDIO_ENCODING_SLINEAR_LE) {
    290 			strcpy(fp->name, AudioEslinear_le);
    291 			fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    292 			fp->precision = 16;
    293 			fp->flags = 0;
    294 			break;
    295 		}
    296 		/* FALLTHROUGH */
    297 	default:
    298 		return EINVAL;
    299 	}
    300 	return 0;
    301 }
    302 
    303 #define MULAW_TO_VIDC(m) (~(m) << 1 | (m) >> 7)
    304 
    305 static void
    306 mulaw_to_vidc(void *v, u_char *p, int cc)
    307 {
    308 
    309 	while (--cc >= 0) {
    310 		*p = MULAW_TO_VIDC(*p);
    311 		++p;
    312 	}
    313 }
    314 
    315 static void
    316 mulaw_to_vidc_stereo(void *v, u_char *p, int cc)
    317 {
    318 	u_char *q = p;
    319 
    320 	p += cc;
    321 	q += cc * 2;
    322 	while (--cc >= 0) {
    323 		q -= 2;
    324 		--p;
    325 		q[0] = q[1] = MULAW_TO_VIDC(*p);
    326 	}
    327 }
    328 
    329 static int
    330 vidcaudio_set_params(void *addr, int setmode, int usemode,
    331     struct audio_params *p, struct audio_params *r)
    332 {
    333 	struct vidcaudio_softc *sc = addr;
    334 	int sample_period, ch;
    335 
    336 	if (setmode & AUMODE_PLAY) {
    337 		switch (sc->sc_hw_encoding) {
    338 		case AUDIO_ENCODING_ULAW:
    339 			/* VIDC20ish, u-law, 8-channel */
    340 			if (p->encoding != AUDIO_ENCODING_ULAW ||
    341 			    p->precision != 8)
    342 				return EINVAL;
    343 			/*
    344 			 * We always use two hardware channels,
    345 			 * because using one at 8kHz gives a nasty
    346 			 * whining sound from the speaker.  The
    347 			 * aurateconv mechanism doesn't support ulaw,
    348 			 * so we do the channel duplication ourselves,
    349 			 * and don't try to do rate conversion.
    350 			 */
    351 			switch (p->channels) {
    352 			case 1:
    353 				p->sw_code = mulaw_to_vidc_stereo;
    354 				p->factor = 2; p->factor_denom = 1;
    355 				break;
    356 			case 2:
    357 				p->sw_code = mulaw_to_vidc;
    358 				p->factor = 1; p->factor_denom = 1;
    359 				break;
    360 			default:
    361 				return EINVAL;
    362 			}
    363 			p->hw_channels = p->channels;
    364 			sample_period =
    365 			    1000000 / 2 / p->sample_rate;
    366 			if (sample_period < 3) sample_period = 3;
    367 			p->hw_sample_rate = p->sample_rate =
    368 			    1000000 / 2 / sample_period;
    369 			p->hw_encoding = AUDIO_ENCODING_NONE;
    370 			p->hw_precision = 8;
    371 			vidcaudio_rate(sample_period - 2);
    372 			vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
    373 			if (p->hw_channels == 1)
    374 				for (ch = 0; ch < 8; ch++)
    375 					vidcaudio_stereo(ch, SIR_CENTRE);
    376 			else
    377 				for (ch = 0; ch < 8; ch++)
    378 					vidcaudio_stereo(ch, ch & 1 ?
    379 					    SIR_LEFT_100 : SIR_RIGHT_100);
    380 			break;
    381 
    382 		case AUDIO_ENCODING_SLINEAR_LE:
    383 			/* ARM7500ish, 16-bit, two-channel */
    384 			if (p->encoding == AUDIO_ENCODING_ULAW &&
    385 			    p->precision == 8) {
    386 				p->sw_code = mulaw_to_slinear16_le;
    387 				p->factor = 2; p->factor_denom = 1;
    388 			} else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
    389 			    p->precision != 16)
    390 				return EINVAL;
    391 			p->hw_channels = 2;
    392 			sample_period = 705600 / 4 / p->sample_rate;
    393 			if (sample_period < 3) sample_period = 3;
    394 			p->hw_sample_rate =
    395 			    1000000 / 4 / sample_period;
    396 			vidcaudio_rate(sample_period - 2);
    397 			vidcaudio_ctrl(SCR_SERIAL);
    398 			p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    399 			p->hw_precision = 16;
    400 			break;
    401 		}
    402 	}
    403 	return 0;
    404 }
    405 
    406 static int
    407 vidcaudio_round_blocksize(void *addr, int wantblk)
    408 {
    409 	int blk;
    410 
    411 	/*
    412 	 * Find the smallest power of two that's larger than the
    413 	 * requested block size, but don't allow < 32 (DMA burst is 16
    414 	 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
    415 	 * confined to a page).
    416 	 */
    417 
    418 	for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
    419 		if (blk >= wantblk) return blk;
    420 	return blk;
    421 }
    422 
    423 static int
    424 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    425     void (*intr)(void *), void *arg, struct audio_params *params)
    426 {
    427 	struct vidcaudio_softc *sc = addr;
    428 
    429 	KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize));
    430 
    431 #ifdef VIDCAUDIO_DEBUG
    432 	printf("vidcaudio_trigger_output %p-%p/0x%x\n",
    433 	    start, end, blksize);
    434 #endif
    435 
    436 	sc->sc_pstart = start;
    437 	sc->sc_pend = end;
    438 	sc->sc_pblksize = blksize;
    439 	sc->sc_pnext = start;
    440 	sc->sc_pintr = intr;
    441 	sc->sc_parg = arg;
    442 
    443 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    444 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
    445 
    446 	/*
    447 	 * Queue up the first two blocks, but don't tell audio code
    448 	 * we're finished with them yet.
    449 	 */
    450 	sc->sc_pcountdown = 2;
    451 
    452 	enable_irq(sc->sc_dma_intr);
    453 
    454 	return 0;
    455 }
    456 
    457 static int
    458 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
    459     void (*intr)(void *), void *arg, struct audio_params *params)
    460 {
    461 
    462 	return ENODEV;
    463 }
    464 
    465 static int
    466 vidcaudio_halt_output(void *addr)
    467 {
    468 	struct vidcaudio_softc *sc = addr;
    469 
    470 #ifdef VIDCAUDIO_DEBUG
    471 	printf("vidcaudio_halt_output\n");
    472 #endif
    473 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    474 	disable_irq(sc->sc_dma_intr);
    475 	return 0;
    476 }
    477 
    478 static int
    479 vidcaudio_halt_input(void *addr)
    480 {
    481 
    482 	return ENODEV;
    483 }
    484 
    485 static int
    486 vidcaudio_getdev(void *addr, struct audio_device *retp)
    487 {
    488 
    489 	*retp = vidcaudio_device;
    490 	return 0;
    491 }
    492 
    493 
    494 static int
    495 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    496 {
    497 
    498 	return EINVAL;
    499 }
    500 
    501 static int
    502 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    503 {
    504 
    505 	return EINVAL;
    506 }
    507 
    508 static int
    509 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    510 {
    511 
    512 	return ENXIO;
    513 }
    514 
    515 static int
    516 vidcaudio_get_props(void *addr)
    517 {
    518 
    519 	return 0;
    520 }
    521 
    522 static void
    523 vidcaudio_rate(int rate)
    524 {
    525 
    526 	WriteWord(vidc_base, VIDC_SFR | rate);
    527 }
    528 
    529 static void
    530 vidcaudio_ctrl(int ctrl)
    531 {
    532 
    533 	WriteWord(vidc_base, VIDC_SCR | ctrl);
    534 }
    535 
    536 static void
    537 vidcaudio_stereo(int channel, int position)
    538 {
    539 
    540 	channel = channel << 24 | VIDC_SIR0;
    541 	WriteWord(vidc_base, channel | position);
    542 }
    543 
    544 static int
    545 vidcaudio_intr(void *arg)
    546 {
    547 	struct vidcaudio_softc *sc = arg;
    548 	int status;
    549 	paddr_t pnext, pend;
    550 
    551 	status = IOMD_READ_BYTE(IOMD_SD0ST);
    552 #ifdef VIDCAUDIO_DEBUG
    553 	printf ( "I[%x]", status );
    554 #endif
    555 	if ((status & IOMD_DMAST_INT) == 0)
    556 		return 0;
    557 
    558 	/* FIXME: Not really allowed to use pmap here. */
    559 	pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_pnext, &pnext);
    560 	pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
    561 
    562 	switch (status &
    563 	    (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
    564 
    565 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    566 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    567 #ifdef VIDCAUDIO_DEBUG
    568 		printf("B<0x%08lx,0x%03lx>", pnext, pend);
    569 #endif
    570 		IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
    571 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
    572 		break;
    573 
    574 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    575 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    576 #ifdef VIDCAUDIO_DEBUG
    577 		printf("A<0x%08lx,0x%03lx>", pnext, pend);
    578 #endif
    579 		IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
    580 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
    581 		break;
    582 	}
    583 
    584 	sc->sc_pnext = (char *)sc->sc_pnext + sc->sc_pblksize;
    585 	if (sc->sc_pnext >= sc->sc_pend)
    586 		sc->sc_pnext = sc->sc_pstart;
    587 
    588 	if (sc->sc_pcountdown > 0)
    589 		sc->sc_pcountdown--;
    590 	else
    591 		(*sc->sc_pintr)(sc->sc_parg);
    592 
    593 	return 1;
    594 }
    595