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