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