Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.23
      1 /*	$NetBSD: vidcaudio.c,v 1.23 2003/12/29 16:49: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  * audio driver for the RiscPC 16 bit sound
     34  *
     35  * Interfaces with the NetBSD generic audio driver to provide SUN
     36  * /dev/audio (partial) compatibility.
     37  */
     38 
     39 #include <sys/param.h>	/* proc.h */
     40 
     41 __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.23 2003/12/29 16:49:31 bjh21 Exp $");
     42 
     43 #include <sys/audioio.h>
     44 #include <sys/conf.h>   /* autoconfig functions */
     45 #include <sys/device.h> /* device calls */
     46 #include <sys/errno.h>
     47 #include <sys/proc.h>	/* device calls */
     48 #include <sys/systm.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <dev/audio_if.h>
     53 
     54 #include <machine/intr.h>
     55 #include <arm/arm32/katelib.h>
     56 
     57 #include <arm/iomd/vidcaudiovar.h>
     58 #include <arm/iomd/iomdreg.h>
     59 #include <arm/iomd/iomdvar.h>
     60 #include <arm/iomd/vidc.h>
     61 #include <arm/mainbus/mainbus.h>
     62 #include <arm/iomd/waveform.h>
     63 #include "vidcaudio.h"
     64 
     65 extern int *vidc_base;
     66 
     67 struct audio_general {
     68 	vaddr_t	silence;
     69 	irqhandler_t	ih;
     70 
     71 	void	(*intr)(void *);
     72 	void	*arg;
     73 
     74 	paddr_t	next_cur;
     75 	paddr_t	next_end;
     76 	void	(*next_intr)(void *);
     77 	void	*next_arg;
     78 
     79 	int	buffer;
     80 	int	in_progress;
     81 
     82 	int	open;
     83 } ag;
     84 
     85 struct vidcaudio_softc {
     86 	struct	device device;
     87 
     88 	int	open;
     89 };
     90 
     91 int  vidcaudio_probe(struct device *, struct cfdata *, void *);
     92 void vidcaudio_attach(struct device *, struct device *, void *);
     93 int  vidcaudio_open(void *, int);
     94 void vidcaudio_close(void *);
     95 
     96 int vidcaudio_intr(void *);
     97 int vidcaudio_dma_program(vaddr_t, vaddr_t, void (*)(void *), void *);
     98 void vidcaudio_dummy_routine(void *);
     99 int vidcaudio_stereo(int, int);
    100 int vidcaudio_rate(int);
    101 void vidcaudio_shutdown(void);
    102 
    103 static int sound_dma_intr;
    104 
    105 CFATTACH_DECL(vidcaudio, sizeof(struct vidcaudio_softc),
    106     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    107 
    108 int    vidcaudio_query_encoding(void *, struct audio_encoding *);
    109 int    vidcaudio_set_params(void *, int, int, struct audio_params *,
    110     struct audio_params *);
    111 int    vidcaudio_round_blocksize(void *, int);
    112 int    vidcaudio_start_output(void *, void *, int, void (*)(void *), void *);
    113 int    vidcaudio_start_input(void *, void *, int, void (*)(void *), void *);
    114 int    vidcaudio_halt_output(void *);
    115 int    vidcaudio_halt_input(void *);
    116 int    vidcaudio_getdev(void *, struct audio_device *);
    117 int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    118 int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    119 int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    120 int    vidcaudio_get_props(void *);
    121 
    122 struct audio_device vidcaudio_device = {
    123 	"VidcAudio 8-bit",
    124 	"x",
    125 	"vidcaudio"
    126 };
    127 
    128 struct audio_hw_if vidcaudio_hw_if = {
    129 	vidcaudio_open,
    130 	vidcaudio_close,
    131 	NULL,
    132 	vidcaudio_query_encoding,
    133 	vidcaudio_set_params,
    134 	vidcaudio_round_blocksize,
    135 	NULL,
    136 	NULL,
    137 	NULL,
    138 	vidcaudio_start_output,
    139 	vidcaudio_start_input,
    140 	vidcaudio_halt_output,
    141 	vidcaudio_halt_input,
    142 	NULL,
    143 	vidcaudio_getdev,
    144 	NULL,
    145 	vidcaudio_set_port,
    146 	vidcaudio_get_port,
    147 	vidcaudio_query_devinfo,
    148 	NULL,
    149 	NULL,
    150 	NULL,
    151 	NULL,
    152 	vidcaudio_get_props,
    153 	NULL,
    154 	NULL,
    155 	NULL,
    156 };
    157 
    158 
    159 void
    160 vidcaudio_beep_generate(void)
    161 {
    162 
    163 	vidcaudio_dma_program(ag.silence, ag.silence+sizeof(beep_waveform)-16,
    164 	    vidcaudio_dummy_routine, NULL);
    165 }
    166 
    167 
    168 int
    169 vidcaudio_probe(struct device *parent, struct cfdata *cf, void *aux)
    170 {
    171 	int id;
    172 
    173 	id = IOMD_ID;
    174 
    175 	/* So far I only know about this IOMD */
    176 	switch (id) {
    177 	case RPC600_IOMD_ID:
    178 		return 1;
    179 		break;
    180 	case ARM7500_IOC_ID:
    181 	case ARM7500FE_IOC_ID:
    182 		return 1;
    183 		break;
    184 	default:
    185 		printf("vidcaudio: Unknown IOMD id=%04x", id);
    186 		break;
    187 	}
    188 
    189 	return 0;
    190 }
    191 
    192 
    193 void
    194 vidcaudio_attach(struct device *parent, struct device *self, void *aux)
    195 {
    196 	struct vidcaudio_softc *sc = (void *)self;
    197 	int id;
    198 
    199 	sc->open = 0;
    200 	ag.in_progress = 0;
    201 
    202 	ag.next_cur = 0;
    203 	ag.next_end = 0;
    204 	ag.next_intr = NULL;
    205 	ag.next_arg = NULL;
    206 
    207 	vidcaudio_rate(32); /* 24*1024 */
    208 
    209 	/* Program the silence buffer and reset the DMA channel */
    210 	ag.silence = uvm_km_alloc(kernel_map, PAGE_SIZE);
    211 	if (ag.silence == 0)
    212 		panic("vidcaudio: Cannot allocate memory");
    213 
    214 	memset((char *)ag.silence, 0, PAGE_SIZE);
    215 	memcpy((char *)ag.silence, (char *)beep_waveform, sizeof(beep_waveform));
    216 
    217 	ag.buffer = 0;
    218 
    219 	/* Install the irq handler for the DMA interrupt */
    220 	ag.ih.ih_func = vidcaudio_intr;
    221 	ag.ih.ih_arg = NULL;
    222 	ag.ih.ih_level = IPL_AUDIO;
    223 	ag.ih.ih_name = "vidcaudio";
    224 
    225 	ag.intr = NULL;
    226 /*	ag.nextintr = NULL;*/
    227 
    228 	id = IOMD_ID;
    229 
    230 	switch (id) {
    231 	case RPC600_IOMD_ID:
    232 		sound_dma_intr = IRQ_DMASCH0;
    233 		break;
    234 	case ARM7500_IOC_ID:
    235 	case ARM7500FE_IOC_ID:
    236 		sound_dma_intr = IRQ_SDMA;
    237 		break;
    238 	}
    239 
    240 	disable_irq(sound_dma_intr);
    241 
    242 	if (irq_claim(sound_dma_intr, &(ag.ih)))
    243 		panic("vidcaudio: couldn't claim IRQ %d", sound_dma_intr);
    244 
    245 	disable_irq(sound_dma_intr);
    246 
    247 	printf("\n");
    248 
    249 	vidcaudio_dma_program(ag.silence, ag.silence + PAGE_SIZE - 16,
    250 	    vidcaudio_dummy_routine, NULL);
    251 
    252 	audio_attach_mi(&vidcaudio_hw_if, sc, &sc->device);
    253 
    254 #ifdef VIDCAUDIO_DEBUG
    255 	printf(" UNDER DEVELOPMENT (nuts)\n");
    256 #endif
    257 }
    258 
    259 int
    260 vidcaudio_open(void *addr, int flags)
    261 {
    262 	struct vidcaudio_softc *sc = addr;
    263 
    264 #ifdef VIDCAUDIO_DEBUG
    265 	printf("DEBUG: vidcaudio_open called\n");
    266 #endif
    267 
    268 	if (sc->open)
    269 		return EBUSY;
    270 
    271 	sc->open = 1;
    272 	ag.open = 1;
    273 
    274 	return 0;
    275 }
    276 
    277 void
    278 vidcaudio_close(void *addr)
    279 {
    280 	struct vidcaudio_softc *sc = addr;
    281 
    282 	vidcaudio_shutdown();
    283 
    284 #ifdef VIDCAUDIO_DEBUG
    285 	printf("DEBUG: vidcaudio_close called\n");
    286 #endif
    287 
    288 	sc->open = 0;
    289 	ag.open = 0;
    290 }
    291 
    292 /*
    293  * Interface to the generic audio driver
    294  */
    295 
    296 int
    297 vidcaudio_query_encoding(void *addr, struct audio_encoding *fp)
    298 {
    299 
    300 	switch (fp->index) {
    301 	case 0:
    302 		strcpy(fp->name, "vidc");
    303 		fp->encoding = AUDIO_ENCODING_ULAW;
    304 		fp->precision = 8;
    305 		fp->flags = 0;
    306 		break;
    307 
    308 	default:
    309 		return EINVAL;
    310 	}
    311 	return 0;
    312 }
    313 
    314 int
    315 vidcaudio_set_params(void *addr, int setmode, int usemode,
    316     struct audio_params *p, struct audio_params *r)
    317 {
    318 
    319 	if (p->encoding != AUDIO_ENCODING_ULAW)
    320 		return EINVAL;
    321 	/* FIXME Handle number of channels properly. */
    322 	vidcaudio_rate(4 * p->sample_rate / (3 * 1024)); /* XXX probably wrong */
    323 
    324 	return 0;
    325 }
    326 
    327 int
    328 vidcaudio_round_blocksize(void *addr, int blk)
    329 {
    330 
    331 	if (blk > PAGE_SIZE)
    332 		blk = PAGE_SIZE;
    333 	return (blk);
    334 }
    335 
    336 #define ROUND(s)  ( ((int)s) & (~(PAGE_SIZE-1)) )
    337 
    338 int
    339 vidcaudio_start_output(void *addr, void *p, int cc, void (*intr)(void *),
    340     void *arg)
    341 {
    342 
    343 	/* I can only DMA inside 1 page */
    344 
    345 #ifdef VIDCAUDIO_DEBUG
    346 	printf("vidcaudio_start_output (%d) %p %p\n", cc, intr, arg);
    347 #endif
    348 
    349 	if (ROUND(p) != ROUND(p+cc)) {
    350 		/*
    351 		 * If it's over a page I can fix it up by copying it into
    352 		 * my buffer
    353 		 */
    354 
    355 #ifdef VIDCAUDIO_DEBUG
    356 		printf("vidcaudio: DMA over page boundary requested."
    357 		    "  Fixing up\n");
    358 #endif
    359 		memcpy(p, (char *)ag.silence, cc > PAGE_SIZE ? PAGE_SIZE : cc);
    360 		p = (void *)ag.silence;
    361 
    362 		/*
    363 		 * I can't DMA any more than that, but it is possible to
    364 		 * fix it up by handling multiple buffers and only
    365 		 * interrupting the audio driver after sending out all the
    366 		 * stuff it gave me.  That it more than I can be bothered
    367 		 * to do right now and it probablly wont happen so I'll just
    368 		 * truncate the buffer and tell the user.
    369 		 */
    370 
    371 		if (cc > PAGE_SIZE) {
    372 			printf("vidcaudio: DMA buffer truncated. I could fix this up\n");
    373 			cc = PAGE_SIZE;
    374 		}
    375 	}
    376 	vidcaudio_dma_program((vaddr_t)p, (vaddr_t)((char *)p+cc),
    377 	    intr, arg);
    378 	return 0;
    379 }
    380 
    381 int
    382 vidcaudio_start_input(void *addr, void *p, int cc, void (*intr)(void *),
    383     void *arg)
    384 {
    385 	return EIO;
    386 }
    387 
    388 int
    389 vidcaudio_halt_output(void *addr)
    390 {
    391 
    392 #ifdef VIDCAUDIO_DEBUG
    393 	printf("DEBUG: vidcaudio_halt_output\n");
    394 #endif
    395 	return EIO;
    396 }
    397 
    398 int
    399 vidcaudio_halt_input(void *addr)
    400 {
    401 
    402 #ifdef VIDCAUDIO_DEBUG
    403 	printf("DEBUG: vidcaudio_halt_input\n");
    404 #endif
    405 	return EIO;
    406 }
    407 
    408 int
    409 vidcaudio_getdev(void *addr, struct audio_device *retp)
    410 {
    411 
    412 	*retp = vidcaudio_device;
    413 	return 0;
    414 }
    415 
    416 
    417 int
    418 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    419 {
    420 
    421 	return EINVAL;
    422 }
    423 
    424 int
    425 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    426 {
    427 
    428 	return EINVAL;
    429 }
    430 
    431 int
    432 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    433 {
    434 
    435 	return ENXIO;
    436 }
    437 
    438 int
    439 vidcaudio_get_props(void *addr)
    440 {
    441 
    442 	return 0;
    443 }
    444 void
    445 vidcaudio_dummy_routine(void *arg)
    446 {
    447 
    448 #ifdef VIDCAUDIO_DEBUG
    449 	printf("vidcaudio_dummy_routine\n");
    450 #endif
    451 }
    452 
    453 int
    454 vidcaudio_rate(int rate)
    455 {
    456 
    457 	WriteWord(vidc_base, VIDC_SFR | rate);
    458 	return 0;
    459 }
    460 
    461 int
    462 vidcaudio_stereo(int channel, int position)
    463 {
    464 
    465 	if (channel < 0) return EINVAL;
    466 	if (channel > 7) return EINVAL;
    467 	channel = channel << 24 | VIDC_SIR0;
    468 	WriteWord(vidc_base, channel | position);
    469 	return 0;
    470 }
    471 
    472 #define PHYS(x, y) pmap_extract(pmap_kernel(), ((x)&L2_S_FRAME), (paddr_t *)(y))
    473 
    474 /*
    475  * Program the next buffer to be used
    476  * This function must be re-entrant, maximum re-entrancy of 2
    477  */
    478 
    479 #define FLAGS (0)
    480 
    481 int
    482 vidcaudio_dma_program(vaddr_t cur, vaddr_t end, void (*intr)(void *),
    483     void *arg)
    484 {
    485 	paddr_t pa1, pa2;
    486 
    487 	/* If there isn't a transfer in progress then start a new one */
    488 	if (ag.in_progress == 0) {
    489 		ag.buffer = 0;
    490 		IOMD_WRITE_WORD(IOMD_SD0CR, 0x90);	/* Reset State Machine */
    491 		IOMD_WRITE_WORD(IOMD_SD0CR, 0x30);	/* Reset State Machine */
    492 
    493 		PHYS(cur, &pa1);
    494 		PHYS(end - 16, &pa2);
    495 
    496 		IOMD_WRITE_WORD(IOMD_SD0CURB, pa1);
    497 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pa2|FLAGS);
    498 		IOMD_WRITE_WORD(IOMD_SD0CURA, pa1);
    499 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pa2|FLAGS);
    500 
    501 		ag.in_progress = 1;
    502 
    503 		ag.next_cur = ag.next_end = 0;
    504 		ag.next_intr = ag.next_arg = 0;
    505 
    506 		ag.intr = intr;
    507 		ag.arg = arg;
    508 
    509 		/*
    510 		 * The driver 'clicks' between buffer swaps, leading me
    511 		 * to think  that the fifo is much small than on other
    512 		 * sound cards so I'm going to have to do some tricks here
    513 		 */
    514 
    515 		(*ag.intr)(ag.arg);		/* Schedule the next buffer */
    516 		ag.intr = vidcaudio_dummy_routine; /* Already done this     */
    517 		ag.arg = NULL;
    518 
    519 #ifdef PRINT
    520 		printf("vidcaudio: start output\n");
    521 #endif
    522 #ifdef VIDCAUDIO_DEBUG
    523 		printf("SE");
    524 #endif
    525 		enable_irq(sound_dma_intr);
    526 	} else {
    527 		/* Otherwise schedule the next one */
    528 		if (ag.next_cur != 0) {
    529 			/* If there's one scheduled then complain */
    530 			printf("vidcaudio: Buffer already Q'ed\n");
    531 			return EIO;
    532 		} else {
    533 			/* We're OK to schedule it now */
    534 			ag.buffer = (++ag.buffer) & 1;
    535 			PHYS(cur, &ag.next_cur);
    536 			PHYS(end - 16, &ag.next_end);
    537 			ag.next_intr = intr;
    538 			ag.next_arg = arg;
    539 #ifdef VIDCAUDIO_DEBUG
    540 			printf("s");
    541 #endif
    542 		}
    543 	}
    544 	return 0;
    545 }
    546 
    547 void
    548 vidcaudio_shutdown(void)
    549 {
    550 
    551 	/* Shut down the channel */
    552 	ag.intr = NULL;
    553 	ag.in_progress = 0;
    554 #ifdef PRINT
    555 	printf("vidcaudio: stop output\n");
    556 #endif
    557 	IOMD_WRITE_WORD(IOMD_SD0CURB, ag.silence);
    558 	IOMD_WRITE_WORD(IOMD_SD0ENDB,
    559 	    (ag.silence + PAGE_SIZE - 16) | (1 << 30));
    560 	disable_irq(sound_dma_intr);
    561 }
    562 
    563 int
    564 vidcaudio_intr(void *arg)
    565 {
    566 	int status = IOMD_READ_BYTE(IOMD_SD0ST);
    567 	void (*nintr)(void *);
    568 	void *narg;
    569 	void (*xintr)(void *);
    570 	void *xarg;
    571 	int xcur, xend;
    572 
    573 	IOMD_WRITE_WORD(IOMD_DMARQ, 0x10);
    574 
    575 #ifdef PRINT
    576 	printf ( "I" );
    577 #endif
    578 
    579 	if (ag.open == 0) {
    580 		vidcaudio_shutdown();
    581 		return 0;
    582 	}
    583 
    584 	/* Have I got the generic audio device attached */
    585 
    586 #ifdef VIDCAUDIO_DEBUG
    587 	printf ( "[B%01x]", status );
    588 #endif
    589 
    590 	nintr = ag.intr;
    591 	narg = ag.arg;
    592 	ag.intr = NULL;
    593 
    594 	xintr = ag.next_intr;
    595 	xarg = ag.next_arg;
    596 	xcur = ag.next_cur;
    597 	xend = ag.next_end;
    598 	ag.next_cur = 0;
    599 	ag.intr = xintr;
    600 	ag.arg = xarg;
    601 
    602 	if (nintr) {
    603 #ifdef VIDCAUDIO_DEBUG
    604 		printf("i");
    605 #endif
    606 		(*nintr)(narg);
    607 	}
    608 
    609 	if (xcur == 0) {
    610 		vidcaudio_shutdown();
    611 	} else {
    612 #define OVERRUN 	(0x04)
    613 #define INTERRUPT	(0x02)
    614 #define BANK_A		(0x00)
    615 #define BANK_B		(0x01)
    616 		switch (status & 0x7) {
    617 		case (INTERRUPT|BANK_A):
    618 #ifdef PRINT
    619 			printf("B");
    620 #endif
    621 			IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
    622 			IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
    623 			break;
    624 
    625 		case (INTERRUPT|BANK_B):
    626 #ifdef PRINT
    627 			printf("A");
    628 #endif
    629 			IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
    630 			IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
    631 			break;
    632 
    633 		case (OVERRUN|INTERRUPT|BANK_A):
    634 #ifdef PRINT
    635 			printf("A");
    636 #endif
    637 			IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
    638 			IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
    639 			break;
    640 
    641 		case (OVERRUN|INTERRUPT|BANK_B):
    642 #ifdef PRINT
    643 			printf("B");
    644 #endif
    645 			IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
    646 			IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
    647 			break;
    648 		}
    649 /*
    650 	ag.next_cur = 0;
    651 	ag.intr = xintr;
    652 	ag.arg = xarg;
    653 */
    654 	}
    655 #ifdef PRINT
    656 	printf ( "i" );
    657 #endif
    658 
    659 	if (ag.next_cur == 0) {
    660 		(*ag.intr)(ag.arg);		/* Schedule the next buffer */
    661 		ag.intr = vidcaudio_dummy_routine; /* Already done this     */
    662 		ag.arg = NULL;
    663 	}
    664 	return 0 ;	/* Pass interrupt on down the chain */
    665 }
    666