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