Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.27
      1 /*	$NetBSD: vidcaudio.c,v 1.27 2004/01/01 16:23:15 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.27 2004/01/01 16:23:15 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 <machine/machdep.h>
     84 #include <arm/arm32/katelib.h>
     85 
     86 #include <arm/iomd/vidcaudiovar.h>
     87 #include <arm/iomd/iomdreg.h>
     88 #include <arm/iomd/iomdvar.h>
     89 #include <arm/iomd/vidc.h>
     90 #include <arm/mainbus/mainbus.h>
     91 #include <arm/iomd/waveform.h>
     92 #include "vidcaudio.h"
     93 
     94 extern int *vidc_base;
     95 
     96 struct vidcaudio_softc {
     97 	struct	device sc_dev;
     98 
     99 	irqhandler_t	sc_ih;
    100 	int	sc_dma_intr;
    101 
    102 	int	sc_is16bit;
    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_is16bit = (cmos_read(0xc4) >> 5) & 1;
    216 		sc->sc_dma_intr = IRQ_DMASCH0;
    217 		break;
    218 	case ARM7500_IOC_ID:
    219 	case ARM7500FE_IOC_ID:
    220 		sc->sc_is16bit = TRUE;
    221 		sc->sc_dma_intr = IRQ_SDMA;
    222 		break;
    223 	default:
    224 		aprint_error(": strange IOMD\n");
    225 		return;
    226 	}
    227 
    228 	if (sc->sc_is16bit)
    229 		aprint_normal(": 16-bit external DAC\n");
    230 	else
    231 		aprint_normal(": 8-bit internal DAC\n");
    232 
    233 	/* Install the irq handler for the DMA interrupt */
    234 	sc->sc_ih.ih_func = vidcaudio_intr;
    235 	sc->sc_ih.ih_arg = sc;
    236 	sc->sc_ih.ih_level = IPL_AUDIO;
    237 	sc->sc_ih.ih_name = self->dv_xname;
    238 
    239 	if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
    240 		aprint_error("%s: couldn't claim IRQ %d\n",
    241 		    self->dv_xname, sc->sc_dma_intr);
    242 		return;
    243 	}
    244 
    245 	disable_irq(sc->sc_dma_intr);
    246 
    247 	audio_attach_mi(&vidcaudio_hw_if, sc, self);
    248 }
    249 
    250 static int
    251 vidcaudio_open(void *addr, int flags)
    252 {
    253 
    254 #ifdef VIDCAUDIO_DEBUG
    255 	printf("DEBUG: vidcaudio_open called\n");
    256 #endif
    257 	return 0;
    258 }
    259 
    260 static void
    261 vidcaudio_close(void *addr)
    262 {
    263 
    264 #ifdef VIDCAUDIO_DEBUG
    265 	printf("DEBUG: vidcaudio_close called\n");
    266 #endif
    267 }
    268 
    269 /*
    270  * Interface to the generic audio driver
    271  */
    272 
    273 static int
    274 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
    275 {
    276 	struct vidcaudio_softc *sc = addr;
    277 
    278 	switch (fp->index) {
    279 	case 0:
    280 		strcpy(fp->name, AudioEmulaw);
    281 		fp->encoding = AUDIO_ENCODING_ULAW;
    282 		fp->precision = 8;
    283 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    284 		break;
    285 
    286 	case 1:
    287 		if (sc->sc_is16bit) {
    288 			strcpy(fp->name, AudioEslinear_le);
    289 			fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    290 			fp->precision = 16;
    291 			fp->flags = 0;
    292 			break;
    293 		}
    294 		/* FALLTHROUGH */
    295 	default:
    296 		return EINVAL;
    297 	}
    298 	return 0;
    299 }
    300 
    301 #define MULAW_TO_VIDC(m) (~(m) << 1 | (m) >> 7)
    302 
    303 static void
    304 mulaw_to_vidc(void *v, u_char *p, int cc)
    305 {
    306 
    307 	while (--cc >= 0) {
    308 		*p = MULAW_TO_VIDC(*p);
    309 		++p;
    310 	}
    311 }
    312 
    313 static void
    314 mulaw_to_vidc_stereo(void *v, u_char *p, int cc)
    315 {
    316 	u_char *q = p;
    317 
    318 	p += cc;
    319 	q += cc * 2;
    320 	while (--cc >= 0) {
    321 		q -= 2;
    322 		--p;
    323 		q[0] = q[1] = MULAW_TO_VIDC(*p);
    324 	}
    325 }
    326 
    327 static int
    328 vidcaudio_set_params(void *addr, int setmode, int usemode,
    329     struct audio_params *p, struct audio_params *r)
    330 {
    331 	struct vidcaudio_softc *sc = addr;
    332 	int sample_period, ch;
    333 
    334 	if (setmode & AUMODE_PLAY) {
    335 		if (sc->sc_is16bit) {
    336 			/* ARM7500ish, 16-bit, two-channel */
    337 			if (p->encoding == AUDIO_ENCODING_ULAW &&
    338 			    p->precision == 8) {
    339 				p->sw_code = mulaw_to_slinear16_le;
    340 				p->factor = 2; p->factor_denom = 1;
    341 			} else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
    342 			    p->precision != 16)
    343 				return EINVAL;
    344 			p->hw_channels = 2;
    345 			sample_period = 705600 / 4 / p->sample_rate;
    346 			if (sample_period < 3) sample_period = 3;
    347 			p->hw_sample_rate =
    348 			    705600 / 4 / sample_period;
    349 			vidcaudio_rate(sample_period - 2);
    350 			p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    351 			p->hw_precision = 16;
    352 		} else {
    353 			/* VIDC20ish, u-law, 8-channel */
    354 			if (p->encoding != AUDIO_ENCODING_ULAW ||
    355 			    p->precision != 8)
    356 				return EINVAL;
    357 			/*
    358 			 * We always use two hardware channels,
    359 			 * because using one at 8kHz gives a nasty
    360 			 * whining sound from the speaker.  The
    361 			 * aurateconv mechanism doesn't support ulaw,
    362 			 * so we do the channel duplication ourselves,
    363 			 * and don't try to do rate conversion.
    364 			 */
    365 			switch (p->channels) {
    366 			case 1:
    367 				p->sw_code = mulaw_to_vidc_stereo;
    368 				p->factor = 2; p->factor_denom = 1;
    369 				break;
    370 			case 2:
    371 				p->sw_code = mulaw_to_vidc;
    372 				p->factor = 1; p->factor_denom = 1;
    373 				break;
    374 			default:
    375 				return EINVAL;
    376 			}
    377 			p->hw_channels = p->channels;
    378 			sample_period =
    379 			    1000000 / 2 / p->sample_rate;
    380 			if (sample_period < 3) sample_period = 3;
    381 			p->hw_sample_rate = p->sample_rate =
    382 			    1000000 / 2 / sample_period;
    383 			p->hw_encoding = AUDIO_ENCODING_NONE;
    384 			p->hw_precision = 8;
    385 			vidcaudio_rate(sample_period - 2);
    386 			vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
    387 			if (p->hw_channels == 1)
    388 				for (ch = 0; ch < 8; ch++)
    389 					vidcaudio_stereo(ch, SIR_CENTRE);
    390 			else
    391 				for (ch = 0; ch < 8; ch++)
    392 					vidcaudio_stereo(ch, ch & 1 ?
    393 					    SIR_LEFT_100 : SIR_RIGHT_100);
    394 		}
    395 	}
    396 	return 0;
    397 }
    398 
    399 static int
    400 vidcaudio_round_blocksize(void *addr, int wantblk)
    401 {
    402 	int blk;
    403 
    404 	/*
    405 	 * Find the smallest power of two that's larger than the
    406 	 * requested block size, but don't allow < 32 (DMA burst is 16
    407 	 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
    408 	 * confined to a page).
    409 	 */
    410 
    411 	for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
    412 		if (blk >= wantblk) return blk;
    413 	return blk;
    414 }
    415 
    416 static int
    417 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    418     void (*intr)(void *), void *arg, struct audio_params *params)
    419 {
    420 	struct vidcaudio_softc *sc = addr;
    421 
    422 	KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize));
    423 
    424 #ifdef VIDCAUDIO_DEBUG
    425 	printf("vidcaudio_trigger_output %p-%p/0x%x\n",
    426 	    start, end, blksize);
    427 #endif
    428 
    429 	sc->sc_pstart = start;
    430 	sc->sc_pend = end;
    431 	sc->sc_pblksize = blksize;
    432 	sc->sc_pnext = start;
    433 	sc->sc_pintr = intr;
    434 	sc->sc_parg = arg;
    435 
    436 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    437 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
    438 
    439 	/*
    440 	 * Queue up the first two blocks, but don't tell audio code
    441 	 * we're finished with them yet.
    442 	 */
    443 	sc->sc_pcountdown = 2;
    444 
    445 	enable_irq(sc->sc_dma_intr);
    446 
    447 	return 0;
    448 }
    449 
    450 static int
    451 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
    452     void (*intr)(void *), void *arg, struct audio_params *params)
    453 {
    454 
    455 	return ENODEV;
    456 }
    457 
    458 static int
    459 vidcaudio_halt_output(void *addr)
    460 {
    461 	struct vidcaudio_softc *sc = addr;
    462 
    463 #ifdef VIDCAUDIO_DEBUG
    464 	printf("vidcaudio_halt_output\n");
    465 #endif
    466 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    467 	disable_irq(sc->sc_dma_intr);
    468 	return 0;
    469 }
    470 
    471 static int
    472 vidcaudio_halt_input(void *addr)
    473 {
    474 
    475 	return ENODEV;
    476 }
    477 
    478 static int
    479 vidcaudio_getdev(void *addr, struct audio_device *retp)
    480 {
    481 
    482 	*retp = vidcaudio_device;
    483 	return 0;
    484 }
    485 
    486 
    487 static int
    488 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    489 {
    490 
    491 	return EINVAL;
    492 }
    493 
    494 static int
    495 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    496 {
    497 
    498 	return EINVAL;
    499 }
    500 
    501 static int
    502 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    503 {
    504 
    505 	return ENXIO;
    506 }
    507 
    508 static int
    509 vidcaudio_get_props(void *addr)
    510 {
    511 
    512 	return 0;
    513 }
    514 
    515 static void
    516 vidcaudio_rate(int rate)
    517 {
    518 
    519 	WriteWord(vidc_base, VIDC_SFR | rate);
    520 }
    521 
    522 static void
    523 vidcaudio_ctrl(int ctrl)
    524 {
    525 
    526 	WriteWord(vidc_base, VIDC_SCR | ctrl);
    527 }
    528 
    529 static void
    530 vidcaudio_stereo(int channel, int position)
    531 {
    532 
    533 	channel = channel << 24 | VIDC_SIR0;
    534 	WriteWord(vidc_base, channel | position);
    535 }
    536 
    537 static int
    538 vidcaudio_intr(void *arg)
    539 {
    540 	struct vidcaudio_softc *sc = arg;
    541 	int status;
    542 	paddr_t pnext, pend;
    543 
    544 	status = IOMD_READ_BYTE(IOMD_SD0ST);
    545 #ifdef VIDCAUDIO_DEBUG
    546 	printf ( "I[%x]", status );
    547 #endif
    548 	if ((status & IOMD_DMAST_INT) == 0)
    549 		return 0;
    550 
    551 	/* FIXME: Not really allowed to use pmap here. */
    552 	pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_pnext, &pnext);
    553 	pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
    554 
    555 	switch (status &
    556 	    (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
    557 
    558 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    559 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    560 #ifdef VIDCAUDIO_DEBUG
    561 		printf("B<0x%08lx,0x%03lx>", pnext, pend);
    562 #endif
    563 		IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
    564 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
    565 		break;
    566 
    567 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    568 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    569 #ifdef VIDCAUDIO_DEBUG
    570 		printf("A<0x%08lx,0x%03lx>", pnext, pend);
    571 #endif
    572 		IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
    573 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
    574 		break;
    575 	}
    576 
    577 	sc->sc_pnext = (char *)sc->sc_pnext + sc->sc_pblksize;
    578 	if (sc->sc_pnext >= sc->sc_pend)
    579 		sc->sc_pnext = sc->sc_pstart;
    580 
    581 	if (sc->sc_pcountdown > 0)
    582 		sc->sc_pcountdown--;
    583 	else
    584 		(*sc->sc_pintr)(sc->sc_parg);
    585 
    586 	return 1;
    587 }
    588