Home | History | Annotate | Line # | Download | only in iomd
vidcaudio.c revision 1.60.6.1
      1 /*	$NetBSD: vidcaudio.c,v 1.60.6.1 2021/04/03 22:28:17 thorpej 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.60.6.1 2021/04/03 22:28:17 thorpej 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/malloc.h>
     75 #include <sys/proc.h>	/* device calls */
     76 #include <sys/systm.h>
     77 
     78 #include <uvm/uvm_extern.h>
     79 
     80 #include <dev/audio/audio_if.h>
     81 #include <dev/audio/audiobellvar.h>
     82 #include <dev/audio/mulaw.h>
     83 
     84 #include <machine/intr.h>
     85 #include <machine/machdep.h>
     86 
     87 #include <arm/iomd/vidcaudiovar.h>
     88 #include <arm/iomd/iomdreg.h>
     89 #include <arm/iomd/iomdvar.h>
     90 #include <arm/iomd/vidc.h>
     91 #include <arm/mainbus/mainbus.h>
     92 
     93 #include "pckbd.h"
     94 #if NPCKBD > 0
     95 #include <dev/pckbport/pckbdvar.h>
     96 #endif
     97 
     98 extern int *vidc_base;
     99 
    100 #ifdef VIDCAUDIO_DEBUG
    101 #define DPRINTF(x)	printf x
    102 #else
    103 #define DPRINTF(x)
    104 #endif
    105 
    106 #define WriteWord(a, b) \
    107 *((volatile unsigned int *)(a)) = (b)
    108 
    109 #define ReadWord(a) \
    110 (*((volatile unsigned int *)(a)))
    111 
    112 struct vidcaudio_softc {
    113 	device_t	sc_dev;
    114 
    115 	irqhandler_t	sc_ih;
    116 	int	sc_dma_intr;
    117 
    118 	int	sc_is16bit;
    119 
    120 	size_t	sc_pblksize;
    121 	vaddr_t	sc_poffset;
    122 	vaddr_t	sc_pbufsize;
    123 	paddr_t	*sc_ppages;
    124 	void	(*sc_pintr)(void *);
    125 	void	*sc_parg;
    126 	int	sc_pcountdown;
    127 
    128 	kmutex_t	sc_lock;
    129 	kmutex_t	sc_intr_lock;
    130 };
    131 
    132 static int  vidcaudio_probe(device_t , cfdata_t , void *);
    133 static void vidcaudio_attach(device_t , device_t , void *);
    134 static void vidcaudio_close(void *);
    135 
    136 static int vidcaudio_intr(void *);
    137 static void vidcaudio_rate(int);
    138 static void vidcaudio_ctrl(int);
    139 static void vidcaudio_stereo(int, int);
    140 
    141 CFATTACH_DECL_NEW(vidcaudio, sizeof(struct vidcaudio_softc),
    142     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    143 
    144 static int    vidcaudio_query_format(void *, audio_format_query_t *);
    145 static int    vidcaudio_set_format(void *, int,
    146     const audio_params_t *, const audio_params_t *,
    147     audio_filter_reg_t *, audio_filter_reg_t *);
    148 static int    vidcaudio_round_blocksize(void *, int, int, const audio_params_t *);
    149 static int    vidcaudio_trigger_output(void *, void *, void *, int,
    150     void (*)(void *), void *, const audio_params_t *);
    151 static int    vidcaudio_halt_output(void *);
    152 static int    vidcaudio_getdev(void *, struct audio_device *);
    153 static int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    154 static int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    155 static int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    156 static int    vidcaudio_get_props(void *);
    157 static void   vidcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    158 
    159 static struct audio_device vidcaudio_device = {
    160 	"ARM VIDC",
    161 	"",
    162 	"vidcaudio"
    163 };
    164 
    165 static const struct audio_hw_if vidcaudio_hw_if = {
    166 	.close			= vidcaudio_close,
    167 	.query_format		= vidcaudio_query_format,
    168 	.set_format		= vidcaudio_set_format,
    169 	.round_blocksize	= vidcaudio_round_blocksize,
    170 	.halt_output		= vidcaudio_halt_output,
    171 	.getdev			= vidcaudio_getdev,
    172 	.set_port		= vidcaudio_set_port,
    173 	.get_port		= vidcaudio_get_port,
    174 	.query_devinfo		= vidcaudio_query_devinfo,
    175 	.get_props		= vidcaudio_get_props,
    176 	.trigger_output		= vidcaudio_trigger_output,
    177 	.get_locks		= vidcaudio_get_locks,
    178 };
    179 
    180 static const struct audio_format vidcaudio_formats_16bit = {
    181 	.mode		= AUMODE_PLAY,
    182 	.encoding	= AUDIO_ENCODING_SLINEAR_LE,
    183 	.validbits	= 16,
    184 	.precision	= 16,
    185 	.channels	= 2,
    186 	.channel_mask	= AUFMT_STEREO,
    187 	/* There are more selectable frequencies but these should be enough. */
    188 	.frequency_type	= 6,
    189 	.frequency	= {
    190 		19600,	/* /9 */
    191 		22050,	/* /8 */
    192 		25200,	/* /7 */
    193 		29400,	/* /6 */
    194 		35280,	/* /5 */
    195 		44100,	/* /4 */
    196 	},
    197 };
    198 static const struct audio_format vidcaudio_formats_8bit = {
    199 	.mode		= AUMODE_PLAY,
    200 	.encoding	= AUDIO_ENCODING_ULAW,
    201 	.validbits	= 8,
    202 	.precision	= 8,
    203 	.channels	= 2,	/* we use stereo always */
    204 	.channel_mask	= AUFMT_STEREO,
    205 	/* frequency is preferably an integer. */
    206 	.frequency_type	= 6,
    207 	.frequency	= {
    208 		10000,	/* 50us */
    209 		12500,	/* 40us */
    210 		20000,	/* 25us */
    211 		25000,	/* 20us */
    212 		31250,	/* 16us */
    213 		50000,	/* 10us */
    214 	},
    215 };
    216 
    217 static int
    218 vidcaudio_probe(device_t parent, cfdata_t cf, void *aux)
    219 {
    220 	int id;
    221 
    222 	id = IOMD_ID;
    223 
    224 	/* So far I only know about this IOMD */
    225 	switch (id) {
    226 	case RPC600_IOMD_ID:
    227 	case ARM7500_IOC_ID:
    228 	case ARM7500FE_IOC_ID:
    229 		return 1;
    230 	default:
    231 		aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
    232 		return 0;
    233 	}
    234 }
    235 
    236 
    237 static void
    238 vidcaudio_attach(device_t parent, device_t self, void *aux)
    239 {
    240 	struct vidcaudio_softc *sc = device_private(self);
    241 	device_t beepdev;
    242 
    243 	sc->sc_dev = self;
    244 	switch (IOMD_ID) {
    245 #ifndef EB7500ATX
    246 	case RPC600_IOMD_ID:
    247 		sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
    248 		sc->sc_dma_intr = IRQ_DMASCH0;
    249 		break;
    250 #endif
    251 	case ARM7500_IOC_ID:
    252 	case ARM7500FE_IOC_ID:
    253 		sc->sc_is16bit = true;
    254 		sc->sc_dma_intr = IRQ_SDMA;
    255 		break;
    256 	default:
    257 		aprint_error(": strange IOMD\n");
    258 		return;
    259 	}
    260 
    261 	if (sc->sc_is16bit)
    262 		aprint_normal(": 16-bit external DAC\n");
    263 	else
    264 		aprint_normal(": 8-bit internal DAC\n");
    265 
    266 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    267 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    268 
    269 	/* Install the irq handler for the DMA interrupt */
    270 	sc->sc_ih.ih_func = vidcaudio_intr;
    271 	sc->sc_ih.ih_arg = sc;
    272 	sc->sc_ih.ih_level = IPL_AUDIO;
    273 	sc->sc_ih.ih_name = device_xname(self);
    274 
    275 	if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
    276 		aprint_error("%s: couldn't claim IRQ %d\n",
    277 		    device_xname(self), sc->sc_dma_intr);
    278 		return;
    279 	}
    280 
    281 	disable_irq(sc->sc_dma_intr);
    282 
    283 	beepdev = audio_attach_mi(&vidcaudio_hw_if, sc, self);
    284 #if NPCKBD > 0
    285 	pckbd_hookup_bell(audiobell, beepdev);
    286 #endif
    287 }
    288 
    289 static void
    290 vidcaudio_close(void *addr)
    291 {
    292 	struct vidcaudio_softc *sc;
    293 
    294 	DPRINTF(("DEBUG: vidcaudio_close called\n"));
    295 	sc = addr;
    296 	if (sc->sc_ppages != NULL) {
    297 		free(sc->sc_ppages, M_DEVBUF);
    298 		sc->sc_ppages = NULL;
    299 	}
    300 }
    301 
    302 /*
    303  * Interface to the generic audio driver
    304  */
    305 
    306 static int
    307 vidcaudio_query_format(void *addr, audio_format_query_t *afp)
    308 {
    309 	struct vidcaudio_softc *sc;
    310 
    311 	sc = addr;
    312 	if (sc->sc_is16bit)
    313 		return audio_query_format(&vidcaudio_formats_16bit, 1, afp);
    314 	else
    315 		return audio_query_format(&vidcaudio_formats_8bit, 1, afp);
    316 }
    317 
    318 static int
    319 vidcaudio_set_format(void *addr, int setmode,
    320     const audio_params_t *play, const audio_params_t *rec,
    321     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    322 {
    323 	struct vidcaudio_softc *sc;
    324 	int sample_period, ch;
    325 
    326 	sc = addr;
    327 	if (sc->sc_is16bit) {
    328 		/* ARM7500ish, 16-bit, two-channel */
    329 		sample_period = 705600 / 4 / play->sample_rate;
    330 		if (sample_period < 3)
    331 			sample_period = 3;
    332 		vidcaudio_rate(sample_period - 2);
    333 		vidcaudio_ctrl(SCR_SERIAL);
    334 	} else {
    335 		/* VIDC20ish, u-law, 8-channel */
    336 		/*
    337 		 * We always use two hardware channels, because using
    338 		 * one at 8kHz gives a nasty whining sound from the
    339 		 * speaker.
    340 		 */
    341 		sample_period = 1000000 / 2 / play->sample_rate;
    342 		if (sample_period < 3)
    343 			sample_period = 3;
    344 		vidcaudio_rate(sample_period - 2);
    345 		vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
    346 		for (ch = 0; ch < 8; ch += 2)
    347 			vidcaudio_stereo(ch, SIR_LEFT_100);
    348 		for (ch = 1; ch < 8; ch += 2)
    349 			vidcaudio_stereo(ch, SIR_RIGHT_100);
    350 
    351 		pfil->codec = audio_internal_to_mulaw;
    352 	}
    353 	return 0;
    354 }
    355 
    356 static int
    357 vidcaudio_round_blocksize(void *addr, int wantblk,
    358 			  int mode, const audio_params_t *param)
    359 {
    360 	int blk;
    361 
    362 	/*
    363 	 * Find the smallest power of two that's larger than the
    364 	 * requested block size, but don't allow < 32 (DMA burst is 16
    365 	 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
    366 	 * confined to a page).
    367 	 */
    368 
    369 	for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
    370 		if (blk >= wantblk)
    371 			return blk;
    372 	return blk;
    373 }
    374 
    375 static int
    376 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    377     void (*intr)(void *), void *arg, const audio_params_t *params)
    378 {
    379 	struct vidcaudio_softc *sc;
    380 	size_t npages, i;
    381 
    382 	DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
    383 	    start, end, blksize));
    384 
    385 	sc = addr;
    386 	KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize, 0, NULL));
    387 	KASSERT((vaddr_t)start % blksize == 0);
    388 
    389 	sc->sc_pblksize = blksize;
    390 	sc->sc_pbufsize = (char *)end - (char *)start;
    391 	npages = sc->sc_pbufsize >> PGSHIFT;
    392 	if (sc->sc_ppages != NULL)
    393 		free(sc->sc_ppages, M_DEVBUF);
    394 	sc->sc_ppages = malloc(npages * sizeof(paddr_t), M_DEVBUF, M_WAITOK);
    395 	if (sc->sc_ppages == NULL)
    396 		return ENOMEM;
    397 	for (i = 0; i < npages; i++)
    398 		if (!pmap_extract(pmap_kernel(),
    399 		    (vaddr_t)start + i * PAGE_SIZE, &sc->sc_ppages[i]))
    400 			return EIO;
    401 	sc->sc_poffset = 0;
    402 	sc->sc_pintr = intr;
    403 	sc->sc_parg = arg;
    404 
    405 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    406 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
    407 
    408 	/*
    409 	 * Queue up the first two blocks, but don't tell audio code
    410 	 * we're finished with them yet.
    411 	 */
    412 	sc->sc_pcountdown = 2;
    413 
    414 	enable_irq(sc->sc_dma_intr);
    415 
    416 	return 0;
    417 }
    418 
    419 static int
    420 vidcaudio_halt_output(void *addr)
    421 {
    422 	struct vidcaudio_softc *sc;
    423 
    424 	DPRINTF(("vidcaudio_halt_output\n"));
    425 	sc = addr;
    426 	disable_irq(sc->sc_dma_intr);
    427 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    428 	return 0;
    429 }
    430 
    431 static int
    432 vidcaudio_getdev(void *addr, struct audio_device *retp)
    433 {
    434 
    435 	*retp = vidcaudio_device;
    436 	return 0;
    437 }
    438 
    439 
    440 static int
    441 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    442 {
    443 
    444 	return EINVAL;
    445 }
    446 
    447 static int
    448 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    449 {
    450 
    451 	return EINVAL;
    452 }
    453 
    454 static int
    455 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    456 {
    457 
    458 	return ENXIO;
    459 }
    460 
    461 static int
    462 vidcaudio_get_props(void *addr)
    463 {
    464 
    465 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
    466 }
    467 
    468 static void
    469 vidcaudio_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    470 {
    471 	struct vidcaudio_softc *sc = opaque;
    472 
    473 	*intr = &sc->sc_intr_lock;
    474 	*thread = &sc->sc_lock;
    475 }
    476 
    477 static void
    478 vidcaudio_rate(int rate)
    479 {
    480 
    481 	WriteWord(vidc_base, VIDC_SFR | rate);
    482 }
    483 
    484 static void
    485 vidcaudio_ctrl(int ctrl)
    486 {
    487 
    488 	WriteWord(vidc_base, VIDC_SCR | ctrl);
    489 }
    490 
    491 static void
    492 vidcaudio_stereo(int channel, int position)
    493 {
    494 
    495 	channel = channel << 24 | VIDC_SIR0;
    496 	WriteWord(vidc_base, channel | position);
    497 }
    498 
    499 static int
    500 vidcaudio_intr(void *arg)
    501 {
    502 	struct vidcaudio_softc *sc;
    503 	int status;
    504 	paddr_t pnext, pend;
    505 
    506 	sc = arg;
    507 	mutex_spin_enter(&sc->sc_intr_lock);
    508 
    509 	status = IOMD_READ_BYTE(IOMD_SD0ST);
    510 	DPRINTF(("I[%x]", status));
    511 	if ((status & IOMD_DMAST_INT) == 0) {
    512 		mutex_spin_exit(&sc->sc_intr_lock);
    513 		return 0;
    514 	}
    515 
    516 	pnext = sc->sc_ppages[sc->sc_poffset >> PGSHIFT] |
    517 	    (sc->sc_poffset & PGOFSET);
    518 	pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
    519 
    520 	switch (status &
    521 	    (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
    522 
    523 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    524 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    525 		DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
    526 		IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
    527 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
    528 		break;
    529 
    530 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    531 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    532 		DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
    533 		IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
    534 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
    535 		break;
    536 	}
    537 
    538 	sc->sc_poffset += sc->sc_pblksize;
    539 	if (sc->sc_poffset >= sc->sc_pbufsize)
    540 		sc->sc_poffset = 0;
    541 
    542 	if (sc->sc_pcountdown > 0)
    543 		sc->sc_pcountdown--;
    544 	else
    545 		(*sc->sc_pintr)(sc->sc_parg);
    546 
    547 	mutex_spin_exit(&sc->sc_intr_lock);
    548 	return 1;
    549 }
    550