Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.29
      1 /*	$NetBSD: vidcaudio.c,v 1.29 2004/01/01 16:42:36 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.29 2004/01/01 16:42:36 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 #ifdef VIDCAUDIO_DEBUG
     97 #define DPRINTF(x)	printf x
     98 #else
     99 #define DPRINTF(x)
    100 #endif
    101 
    102 struct vidcaudio_softc {
    103 	struct	device sc_dev;
    104 
    105 	irqhandler_t	sc_ih;
    106 	int	sc_dma_intr;
    107 
    108 	int	sc_is16bit;
    109 
    110 	void	*sc_pstart;
    111 	void	*sc_pend;
    112 	size_t	sc_pblksize;
    113 	void	*sc_pnext;
    114 	void	(*sc_pintr)(void *);
    115 	void	*sc_parg;
    116 	int	sc_pcountdown;
    117 };
    118 
    119 static int  vidcaudio_probe(struct device *, struct cfdata *, void *);
    120 static void vidcaudio_attach(struct device *, struct device *, void *);
    121 static int  vidcaudio_open(void *, int);
    122 static void vidcaudio_close(void *);
    123 
    124 static int vidcaudio_intr(void *);
    125 static void vidcaudio_rate(int);
    126 static void vidcaudio_ctrl(int);
    127 static void vidcaudio_stereo(int, int);
    128 static void mulaw_to_vidc(void *, u_char *, int);
    129 static void mulaw_to_vidc_stereo(void *, u_char *, int);
    130 
    131 CFATTACH_DECL(vidcaudio, sizeof(struct vidcaudio_softc),
    132     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    133 
    134 static int    vidcaudio_query_encoding(void *, struct audio_encoding *);
    135 static int    vidcaudio_set_params(void *, int, int, struct audio_params *,
    136     struct audio_params *);
    137 static int    vidcaudio_round_blocksize(void *, int);
    138 static int    vidcaudio_trigger_output(void *, void *, void *, int,
    139     void (*)(void *), void *, struct audio_params *);
    140 static int    vidcaudio_trigger_input(void *, void *, void *, int,
    141     void (*)(void *), void *, struct audio_params *);
    142 static int    vidcaudio_halt_output(void *);
    143 static int    vidcaudio_halt_input(void *);
    144 static int    vidcaudio_getdev(void *, struct audio_device *);
    145 static int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    146 static int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    147 static int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    148 static int    vidcaudio_get_props(void *);
    149 
    150 static struct audio_device vidcaudio_device = {
    151 	"ARM VIDC",
    152 	"",
    153 	"vidcaudio"
    154 };
    155 
    156 static struct audio_hw_if vidcaudio_hw_if = {
    157 	vidcaudio_open,
    158 	vidcaudio_close,
    159 	NULL,
    160 	vidcaudio_query_encoding,
    161 	vidcaudio_set_params,
    162 	vidcaudio_round_blocksize,
    163 	NULL,
    164 	NULL,
    165 	NULL,
    166 	NULL,
    167 	NULL,
    168 	vidcaudio_halt_output,
    169 	vidcaudio_halt_input,
    170 	NULL,
    171 	vidcaudio_getdev,
    172 	NULL,
    173 	vidcaudio_set_port,
    174 	vidcaudio_get_port,
    175 	vidcaudio_query_devinfo,
    176 	NULL,
    177 	NULL,
    178 	NULL,
    179 	NULL,
    180 	vidcaudio_get_props,
    181 	vidcaudio_trigger_output,
    182 	vidcaudio_trigger_input,
    183 	NULL,
    184 };
    185 
    186 
    187 void
    188 vidcaudio_beep_generate(void)
    189 {
    190 
    191 }
    192 
    193 
    194 static int
    195 vidcaudio_probe(struct device *parent, struct cfdata *cf, void *aux)
    196 {
    197 	int id;
    198 
    199 	id = IOMD_ID;
    200 
    201 	/* So far I only know about this IOMD */
    202 	switch (id) {
    203 	case RPC600_IOMD_ID:
    204 	case ARM7500_IOC_ID:
    205 	case ARM7500FE_IOC_ID:
    206 		return 1;
    207 	default:
    208 		aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
    209 		return 0;
    210 	}
    211 }
    212 
    213 
    214 static void
    215 vidcaudio_attach(struct device *parent, struct device *self, void *aux)
    216 {
    217 	struct vidcaudio_softc *sc = (void *)self;
    218 
    219 	switch (IOMD_ID) {
    220 	case RPC600_IOMD_ID:
    221 		sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
    222 		sc->sc_dma_intr = IRQ_DMASCH0;
    223 		break;
    224 	case ARM7500_IOC_ID:
    225 	case ARM7500FE_IOC_ID:
    226 		sc->sc_is16bit = TRUE;
    227 		sc->sc_dma_intr = IRQ_SDMA;
    228 		break;
    229 	default:
    230 		aprint_error(": strange IOMD\n");
    231 		return;
    232 	}
    233 
    234 	if (sc->sc_is16bit)
    235 		aprint_normal(": 16-bit external DAC\n");
    236 	else
    237 		aprint_normal(": 8-bit internal DAC\n");
    238 
    239 	/* Install the irq handler for the DMA interrupt */
    240 	sc->sc_ih.ih_func = vidcaudio_intr;
    241 	sc->sc_ih.ih_arg = sc;
    242 	sc->sc_ih.ih_level = IPL_AUDIO;
    243 	sc->sc_ih.ih_name = self->dv_xname;
    244 
    245 	if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
    246 		aprint_error("%s: couldn't claim IRQ %d\n",
    247 		    self->dv_xname, sc->sc_dma_intr);
    248 		return;
    249 	}
    250 
    251 	disable_irq(sc->sc_dma_intr);
    252 
    253 	audio_attach_mi(&vidcaudio_hw_if, sc, self);
    254 }
    255 
    256 static int
    257 vidcaudio_open(void *addr, int flags)
    258 {
    259 
    260 	DPRINTF(("DEBUG: vidcaudio_open called\n"));
    261 	return 0;
    262 }
    263 
    264 static void
    265 vidcaudio_close(void *addr)
    266 {
    267 
    268 	DPRINTF(("DEBUG: vidcaudio_close called\n"));
    269 }
    270 
    271 /*
    272  * Interface to the generic audio driver
    273  */
    274 
    275 static int
    276 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
    277 {
    278 	struct vidcaudio_softc *sc = addr;
    279 
    280 	switch (fp->index) {
    281 	case 0:
    282 		strcpy(fp->name, AudioEmulaw);
    283 		fp->encoding = AUDIO_ENCODING_ULAW;
    284 		fp->precision = 8;
    285 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    286 		break;
    287 
    288 	case 1:
    289 		if (sc->sc_is16bit) {
    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 		if (sc->sc_is16bit) {
    338 			/* ARM7500ish, 16-bit, two-channel */
    339 			if (p->encoding == AUDIO_ENCODING_ULAW &&
    340 			    p->precision == 8) {
    341 				p->sw_code = mulaw_to_slinear16_le;
    342 				p->factor = 2; p->factor_denom = 1;
    343 			} else if (p->encoding != AUDIO_ENCODING_SLINEAR_LE ||
    344 			    p->precision != 16)
    345 				return EINVAL;
    346 			p->hw_channels = 2;
    347 			sample_period = 705600 / 4 / p->sample_rate;
    348 			if (sample_period < 3) sample_period = 3;
    349 			p->hw_sample_rate =
    350 			    705600 / 4 / sample_period;
    351 			vidcaudio_rate(sample_period - 2);
    352 			vidcaudio_ctrl(SCR_SERIAL);
    353 			p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
    354 			p->hw_precision = 16;
    355 		} else {
    356 			/* VIDC20ish, u-law, 8-channel */
    357 			if (p->encoding != AUDIO_ENCODING_ULAW ||
    358 			    p->precision != 8)
    359 				return EINVAL;
    360 			/*
    361 			 * We always use two hardware channels,
    362 			 * because using one at 8kHz gives a nasty
    363 			 * whining sound from the speaker.  The
    364 			 * aurateconv mechanism doesn't support ulaw,
    365 			 * so we do the channel duplication ourselves,
    366 			 * and don't try to do rate conversion.
    367 			 */
    368 			switch (p->channels) {
    369 			case 1:
    370 				p->sw_code = mulaw_to_vidc_stereo;
    371 				p->factor = 2; p->factor_denom = 1;
    372 				break;
    373 			case 2:
    374 				p->sw_code = mulaw_to_vidc;
    375 				p->factor = 1; p->factor_denom = 1;
    376 				break;
    377 			default:
    378 				return EINVAL;
    379 			}
    380 			p->hw_channels = p->channels;
    381 			sample_period =
    382 			    1000000 / 2 / p->sample_rate;
    383 			if (sample_period < 3) sample_period = 3;
    384 			p->hw_sample_rate = p->sample_rate =
    385 			    1000000 / 2 / sample_period;
    386 			p->hw_encoding = AUDIO_ENCODING_NONE;
    387 			p->hw_precision = 8;
    388 			vidcaudio_rate(sample_period - 2);
    389 			vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
    390 			if (p->hw_channels == 1)
    391 				for (ch = 0; ch < 8; ch++)
    392 					vidcaudio_stereo(ch, SIR_CENTRE);
    393 			else
    394 				for (ch = 0; ch < 8; ch++)
    395 					vidcaudio_stereo(ch, ch & 1 ?
    396 					    SIR_LEFT_100 : SIR_RIGHT_100);
    397 		}
    398 	}
    399 	return 0;
    400 }
    401 
    402 static int
    403 vidcaudio_round_blocksize(void *addr, int wantblk)
    404 {
    405 	int blk;
    406 
    407 	/*
    408 	 * Find the smallest power of two that's larger than the
    409 	 * requested block size, but don't allow < 32 (DMA burst is 16
    410 	 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
    411 	 * confined to a page).
    412 	 */
    413 
    414 	for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
    415 		if (blk >= wantblk) return blk;
    416 	return blk;
    417 }
    418 
    419 static int
    420 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    421     void (*intr)(void *), void *arg, struct audio_params *params)
    422 {
    423 	struct vidcaudio_softc *sc = addr;
    424 
    425 	KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize));
    426 
    427 	DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
    428 	    start, end, blksize));
    429 
    430 	sc->sc_pstart = start;
    431 	sc->sc_pend = end;
    432 	sc->sc_pblksize = blksize;
    433 	sc->sc_pnext = start;
    434 	sc->sc_pintr = intr;
    435 	sc->sc_parg = arg;
    436 
    437 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    438 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
    439 
    440 	/*
    441 	 * Queue up the first two blocks, but don't tell audio code
    442 	 * we're finished with them yet.
    443 	 */
    444 	sc->sc_pcountdown = 2;
    445 
    446 	enable_irq(sc->sc_dma_intr);
    447 
    448 	return 0;
    449 }
    450 
    451 static int
    452 vidcaudio_trigger_input(void *addr, void *start, void *end, int blksize,
    453     void (*intr)(void *), void *arg, struct audio_params *params)
    454 {
    455 
    456 	return ENODEV;
    457 }
    458 
    459 static int
    460 vidcaudio_halt_output(void *addr)
    461 {
    462 	struct vidcaudio_softc *sc = addr;
    463 
    464 	DPRINTF(("vidcaudio_halt_output\n"));
    465 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    466 	disable_irq(sc->sc_dma_intr);
    467 	return 0;
    468 }
    469 
    470 static int
    471 vidcaudio_halt_input(void *addr)
    472 {
    473 
    474 	return ENODEV;
    475 }
    476 
    477 static int
    478 vidcaudio_getdev(void *addr, struct audio_device *retp)
    479 {
    480 
    481 	*retp = vidcaudio_device;
    482 	return 0;
    483 }
    484 
    485 
    486 static int
    487 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    488 {
    489 
    490 	return EINVAL;
    491 }
    492 
    493 static int
    494 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    495 {
    496 
    497 	return EINVAL;
    498 }
    499 
    500 static int
    501 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    502 {
    503 
    504 	return ENXIO;
    505 }
    506 
    507 static int
    508 vidcaudio_get_props(void *addr)
    509 {
    510 
    511 	return 0;
    512 }
    513 
    514 static void
    515 vidcaudio_rate(int rate)
    516 {
    517 
    518 	WriteWord(vidc_base, VIDC_SFR | rate);
    519 }
    520 
    521 static void
    522 vidcaudio_ctrl(int ctrl)
    523 {
    524 
    525 	WriteWord(vidc_base, VIDC_SCR | ctrl);
    526 }
    527 
    528 static void
    529 vidcaudio_stereo(int channel, int position)
    530 {
    531 
    532 	channel = channel << 24 | VIDC_SIR0;
    533 	WriteWord(vidc_base, channel | position);
    534 }
    535 
    536 static int
    537 vidcaudio_intr(void *arg)
    538 {
    539 	struct vidcaudio_softc *sc = arg;
    540 	int status;
    541 	paddr_t pnext, pend;
    542 
    543 	status = IOMD_READ_BYTE(IOMD_SD0ST);
    544 	DPRINTF(("I[%x]", status));
    545 	if ((status & IOMD_DMAST_INT) == 0)
    546 		return 0;
    547 
    548 	/* FIXME: Not really allowed to use pmap here. */
    549 	pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_pnext, &pnext);
    550 	pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
    551 
    552 	switch (status &
    553 	    (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
    554 
    555 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    556 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    557 		DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
    558 		IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
    559 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
    560 		break;
    561 
    562 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    563 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    564 		DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
    565 		IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
    566 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
    567 		break;
    568 	}
    569 
    570 	sc->sc_pnext = (char *)sc->sc_pnext + sc->sc_pblksize;
    571 	if (sc->sc_pnext >= sc->sc_pend)
    572 		sc->sc_pnext = sc->sc_pstart;
    573 
    574 	if (sc->sc_pcountdown > 0)
    575 		sc->sc_pcountdown--;
    576 	else
    577 		(*sc->sc_pintr)(sc->sc_parg);
    578 
    579 	return 1;
    580 }
    581