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