Home | History | Annotate | Line # | Download | only in iomd
      1  1.62     skrll /*	$NetBSD: vidcaudio.c,v 1.62 2022/09/27 06:14:41 skrll Exp $	*/
      2   1.1   reinoud 
      3   1.1   reinoud /*
      4   1.1   reinoud  * Copyright (c) 1995 Melvin Tang-Richardson
      5   1.1   reinoud  *
      6   1.1   reinoud  * Redistribution and use in source and binary forms, with or without
      7   1.1   reinoud  * modification, are permitted provided that the following conditions
      8   1.1   reinoud  * are met:
      9   1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     10   1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     11   1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     13   1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     14   1.1   reinoud  * 3. All advertising materials mentioning features or use of this software
     15   1.1   reinoud  *    must display the following acknowledgement:
     16   1.1   reinoud  *	This product includes software developed by the RiscBSD team.
     17   1.1   reinoud  * 4. The name of the author may not be used to endorse or promote products
     18   1.1   reinoud  *    derived from this software without specific prior written permission.
     19   1.1   reinoud  *
     20   1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1   reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1   reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.1   reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1   reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.1   reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.1   reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.1   reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.1   reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.1   reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1   reinoud  */
     31   1.1   reinoud 
     32  1.24     bjh21 /*-
     33  1.24     bjh21  * Copyright (c) 2003 Ben Harris
     34  1.24     bjh21  * All rights reserved.
     35  1.24     bjh21  *
     36  1.24     bjh21  * Redistribution and use in source and binary forms, with or without
     37  1.24     bjh21  * modification, are permitted provided that the following conditions
     38  1.24     bjh21  * are met:
     39  1.24     bjh21  * 1. Redistributions of source code must retain the above copyright
     40  1.24     bjh21  *    notice, this list of conditions and the following disclaimer.
     41  1.24     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.24     bjh21  *    notice, this list of conditions and the following disclaimer in the
     43  1.24     bjh21  *    documentation and/or other materials provided with the distribution.
     44  1.24     bjh21  * 3. The name of the author may not be used to endorse or promote products
     45  1.24     bjh21  *    derived from this software without specific prior written permission.
     46  1.24     bjh21  *
     47  1.24     bjh21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  1.24     bjh21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  1.24     bjh21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  1.24     bjh21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  1.24     bjh21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  1.24     bjh21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  1.24     bjh21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  1.24     bjh21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  1.24     bjh21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  1.24     bjh21  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  1.24     bjh21  */
     58  1.24     bjh21 
     59   1.1   reinoud /*
     60  1.24     bjh21  * audio driver for the RiscPC 8/16 bit sound
     61   1.1   reinoud  *
     62   1.1   reinoud  * Interfaces with the NetBSD generic audio driver to provide SUN
     63   1.1   reinoud  * /dev/audio (partial) compatibility.
     64   1.1   reinoud  */
     65   1.1   reinoud 
     66   1.1   reinoud #include <sys/param.h>	/* proc.h */
     67   1.5     bjh21 
     68  1.62     skrll __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.62 2022/09/27 06:14:41 skrll Exp $");
     69   1.5     bjh21 
     70  1.18     bjh21 #include <sys/audioio.h>
     71   1.1   reinoud #include <sys/conf.h>   /* autoconfig functions */
     72   1.1   reinoud #include <sys/device.h> /* device calls */
     73  1.18     bjh21 #include <sys/errno.h>
     74  1.62     skrll #include <sys/kmem.h>
     75   1.1   reinoud #include <sys/proc.h>	/* device calls */
     76   1.1   reinoud #include <sys/systm.h>
     77   1.1   reinoud 
     78   1.1   reinoud #include <uvm/uvm_extern.h>
     79   1.1   reinoud 
     80  1.58     isaki #include <dev/audio/audio_if.h>
     81  1.58     isaki #include <dev/audio/audiobellvar.h>
     82  1.58     isaki #include <dev/audio/mulaw.h>
     83   1.1   reinoud 
     84   1.4   thorpej #include <machine/intr.h>
     85  1.27     bjh21 #include <machine/machdep.h>
     86   1.1   reinoud 
     87   1.5     bjh21 #include <arm/iomd/vidcaudiovar.h>
     88   1.1   reinoud #include <arm/iomd/iomdreg.h>
     89   1.1   reinoud #include <arm/iomd/iomdvar.h>
     90   1.1   reinoud #include <arm/iomd/vidc.h>
     91   1.1   reinoud #include <arm/mainbus/mainbus.h>
     92   1.1   reinoud 
     93  1.39     bjh21 #include "pckbd.h"
     94  1.39     bjh21 #if NPCKBD > 0
     95  1.39     bjh21 #include <dev/pckbport/pckbdvar.h>
     96  1.39     bjh21 #endif
     97  1.39     bjh21 
     98   1.1   reinoud extern int *vidc_base;
     99   1.1   reinoud 
    100  1.28     bjh21 #ifdef VIDCAUDIO_DEBUG
    101  1.28     bjh21 #define DPRINTF(x)	printf x
    102  1.28     bjh21 #else
    103  1.28     bjh21 #define DPRINTF(x)
    104  1.28     bjh21 #endif
    105  1.28     bjh21 
    106  1.53     skrll #define WriteWord(a, b) \
    107  1.53     skrll *((volatile unsigned int *)(a)) = (b)
    108  1.53     skrll 
    109  1.53     skrll #define ReadWord(a) \
    110  1.53     skrll (*((volatile unsigned int *)(a)))
    111  1.53     skrll 
    112  1.24     bjh21 struct vidcaudio_softc {
    113  1.50     skrll 	device_t	sc_dev;
    114   1.1   reinoud 
    115  1.24     bjh21 	irqhandler_t	sc_ih;
    116  1.24     bjh21 	int	sc_dma_intr;
    117   1.1   reinoud 
    118  1.27     bjh21 	int	sc_is16bit;
    119   1.1   reinoud 
    120  1.24     bjh21 	size_t	sc_pblksize;
    121  1.52      matt 	vaddr_t	sc_poffset;
    122  1.52      matt 	vaddr_t	sc_pbufsize;
    123  1.30     bjh21 	paddr_t	*sc_ppages;
    124  1.62     skrll 	size_t	sc_szppages;
    125  1.24     bjh21 	void	(*sc_pintr)(void *);
    126  1.24     bjh21 	void	*sc_parg;
    127  1.24     bjh21 	int	sc_pcountdown;
    128  1.47  jmcneill 
    129  1.47  jmcneill 	kmutex_t	sc_lock;
    130  1.47  jmcneill 	kmutex_t	sc_intr_lock;
    131   1.1   reinoud };
    132   1.1   reinoud 
    133  1.50     skrll static int  vidcaudio_probe(device_t , cfdata_t , void *);
    134  1.50     skrll static void vidcaudio_attach(device_t , device_t , void *);
    135  1.25     bjh21 static void vidcaudio_close(void *);
    136  1.25     bjh21 
    137  1.25     bjh21 static int vidcaudio_intr(void *);
    138  1.25     bjh21 static void vidcaudio_rate(int);
    139  1.25     bjh21 static void vidcaudio_ctrl(int);
    140  1.25     bjh21 static void vidcaudio_stereo(int, int);
    141   1.1   reinoud 
    142  1.50     skrll CFATTACH_DECL_NEW(vidcaudio, sizeof(struct vidcaudio_softc),
    143  1.13   thorpej     vidcaudio_probe, vidcaudio_attach, NULL, NULL);
    144   1.1   reinoud 
    145  1.58     isaki static int    vidcaudio_query_format(void *, audio_format_query_t *);
    146  1.58     isaki static int    vidcaudio_set_format(void *, int,
    147  1.58     isaki     const audio_params_t *, const audio_params_t *,
    148  1.58     isaki     audio_filter_reg_t *, audio_filter_reg_t *);
    149  1.41      kent static int    vidcaudio_round_blocksize(void *, int, int, const audio_params_t *);
    150  1.25     bjh21 static int    vidcaudio_trigger_output(void *, void *, void *, int,
    151  1.41      kent     void (*)(void *), void *, const audio_params_t *);
    152  1.25     bjh21 static int    vidcaudio_halt_output(void *);
    153  1.25     bjh21 static int    vidcaudio_getdev(void *, struct audio_device *);
    154  1.25     bjh21 static int    vidcaudio_set_port(void *, mixer_ctrl_t *);
    155  1.25     bjh21 static int    vidcaudio_get_port(void *, mixer_ctrl_t *);
    156  1.25     bjh21 static int    vidcaudio_query_devinfo(void *, mixer_devinfo_t *);
    157  1.25     bjh21 static int    vidcaudio_get_props(void *);
    158  1.47  jmcneill static void   vidcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
    159   1.1   reinoud 
    160  1.25     bjh21 static struct audio_device vidcaudio_device = {
    161  1.24     bjh21 	"ARM VIDC",
    162  1.24     bjh21 	"",
    163   1.1   reinoud 	"vidcaudio"
    164   1.1   reinoud };
    165   1.1   reinoud 
    166  1.40      yamt static const struct audio_hw_if vidcaudio_hw_if = {
    167  1.57     isaki 	.close			= vidcaudio_close,
    168  1.58     isaki 	.query_format		= vidcaudio_query_format,
    169  1.58     isaki 	.set_format		= vidcaudio_set_format,
    170  1.57     isaki 	.round_blocksize	= vidcaudio_round_blocksize,
    171  1.57     isaki 	.halt_output		= vidcaudio_halt_output,
    172  1.57     isaki 	.getdev			= vidcaudio_getdev,
    173  1.57     isaki 	.set_port		= vidcaudio_set_port,
    174  1.57     isaki 	.get_port		= vidcaudio_get_port,
    175  1.57     isaki 	.query_devinfo		= vidcaudio_query_devinfo,
    176  1.57     isaki 	.get_props		= vidcaudio_get_props,
    177  1.57     isaki 	.trigger_output		= vidcaudio_trigger_output,
    178  1.57     isaki 	.get_locks		= vidcaudio_get_locks,
    179   1.1   reinoud };
    180   1.1   reinoud 
    181  1.58     isaki static const struct audio_format vidcaudio_formats_16bit = {
    182  1.58     isaki 	.mode		= AUMODE_PLAY,
    183  1.58     isaki 	.encoding	= AUDIO_ENCODING_SLINEAR_LE,
    184  1.58     isaki 	.validbits	= 16,
    185  1.58     isaki 	.precision	= 16,
    186  1.58     isaki 	.channels	= 2,
    187  1.58     isaki 	.channel_mask	= AUFMT_STEREO,
    188  1.58     isaki 	/* There are more selectable frequencies but these should be enough. */
    189  1.58     isaki 	.frequency_type	= 6,
    190  1.58     isaki 	.frequency	= {
    191  1.58     isaki 		19600,	/* /9 */
    192  1.58     isaki 		22050,	/* /8 */
    193  1.58     isaki 		25200,	/* /7 */
    194  1.58     isaki 		29400,	/* /6 */
    195  1.58     isaki 		35280,	/* /5 */
    196  1.58     isaki 		44100,	/* /4 */
    197  1.58     isaki 	},
    198  1.58     isaki };
    199  1.58     isaki static const struct audio_format vidcaudio_formats_8bit = {
    200  1.58     isaki 	.mode		= AUMODE_PLAY,
    201  1.58     isaki 	.encoding	= AUDIO_ENCODING_ULAW,
    202  1.58     isaki 	.validbits	= 8,
    203  1.58     isaki 	.precision	= 8,
    204  1.58     isaki 	.channels	= 2,	/* we use stereo always */
    205  1.58     isaki 	.channel_mask	= AUFMT_STEREO,
    206  1.58     isaki 	/* frequency is preferably an integer. */
    207  1.58     isaki 	.frequency_type	= 6,
    208  1.58     isaki 	.frequency	= {
    209  1.58     isaki 		10000,	/* 50us */
    210  1.58     isaki 		12500,	/* 40us */
    211  1.58     isaki 		20000,	/* 25us */
    212  1.58     isaki 		25000,	/* 20us */
    213  1.58     isaki 		31250,	/* 16us */
    214  1.58     isaki 		50000,	/* 10us */
    215  1.58     isaki 	},
    216  1.58     isaki };
    217  1.58     isaki 
    218  1.25     bjh21 static int
    219  1.50     skrll vidcaudio_probe(device_t parent, cfdata_t cf, void *aux)
    220   1.1   reinoud {
    221   1.1   reinoud 	int id;
    222   1.1   reinoud 
    223   1.1   reinoud 	id = IOMD_ID;
    224   1.1   reinoud 
    225   1.1   reinoud 	/* So far I only know about this IOMD */
    226   1.1   reinoud 	switch (id) {
    227   1.1   reinoud 	case RPC600_IOMD_ID:
    228   1.1   reinoud 	case ARM7500_IOC_ID:
    229   1.1   reinoud 	case ARM7500FE_IOC_ID:
    230  1.18     bjh21 		return 1;
    231   1.1   reinoud 	default:
    232  1.24     bjh21 		aprint_error("vidcaudio: Unknown IOMD id=%04x", id);
    233  1.24     bjh21 		return 0;
    234   1.1   reinoud 	}
    235   1.1   reinoud }
    236   1.1   reinoud 
    237   1.1   reinoud 
    238  1.25     bjh21 static void
    239  1.50     skrll vidcaudio_attach(device_t parent, device_t self, void *aux)
    240   1.1   reinoud {
    241  1.50     skrll 	struct vidcaudio_softc *sc = device_private(self);
    242  1.50     skrll 	device_t beepdev;
    243   1.1   reinoud 
    244  1.50     skrll 	sc->sc_dev = self;
    245  1.24     bjh21 	switch (IOMD_ID) {
    246  1.32     chris #ifndef EB7500ATX
    247   1.1   reinoud 	case RPC600_IOMD_ID:
    248  1.27     bjh21 		sc->sc_is16bit = (cmos_read(0xc4) >> 5) & 1;
    249  1.24     bjh21 		sc->sc_dma_intr = IRQ_DMASCH0;
    250   1.1   reinoud 		break;
    251  1.32     chris #endif
    252   1.1   reinoud 	case ARM7500_IOC_ID:
    253   1.1   reinoud 	case ARM7500FE_IOC_ID:
    254  1.45   thorpej 		sc->sc_is16bit = true;
    255  1.24     bjh21 		sc->sc_dma_intr = IRQ_SDMA;
    256   1.1   reinoud 		break;
    257  1.24     bjh21 	default:
    258  1.24     bjh21 		aprint_error(": strange IOMD\n");
    259  1.24     bjh21 		return;
    260   1.1   reinoud 	}
    261   1.1   reinoud 
    262  1.27     bjh21 	if (sc->sc_is16bit)
    263  1.27     bjh21 		aprint_normal(": 16-bit external DAC\n");
    264  1.27     bjh21 	else
    265  1.27     bjh21 		aprint_normal(": 8-bit internal DAC\n");
    266  1.27     bjh21 
    267  1.47  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    268  1.48       mrg 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    269  1.47  jmcneill 
    270  1.24     bjh21 	/* Install the irq handler for the DMA interrupt */
    271  1.24     bjh21 	sc->sc_ih.ih_func = vidcaudio_intr;
    272  1.24     bjh21 	sc->sc_ih.ih_arg = sc;
    273  1.48       mrg 	sc->sc_ih.ih_level = IPL_AUDIO;
    274  1.51     skrll 	sc->sc_ih.ih_name = device_xname(self);
    275  1.24     bjh21 
    276  1.24     bjh21 	if (irq_claim(sc->sc_dma_intr, &sc->sc_ih) != 0) {
    277  1.24     bjh21 		aprint_error("%s: couldn't claim IRQ %d\n",
    278  1.51     skrll 		    device_xname(self), sc->sc_dma_intr);
    279  1.24     bjh21 		return;
    280  1.24     bjh21 	}
    281   1.1   reinoud 
    282  1.24     bjh21 	disable_irq(sc->sc_dma_intr);
    283   1.1   reinoud 
    284  1.34     bjh21 	beepdev = audio_attach_mi(&vidcaudio_hw_if, sc, self);
    285  1.39     bjh21 #if NPCKBD > 0
    286  1.55  christos 	pckbd_hookup_bell(audiobell, beepdev);
    287  1.39     bjh21 #endif
    288   1.1   reinoud }
    289   1.1   reinoud 
    290  1.25     bjh21 static void
    291  1.17     bjh21 vidcaudio_close(void *addr)
    292   1.1   reinoud {
    293  1.42      kent 	struct vidcaudio_softc *sc;
    294   1.1   reinoud 
    295  1.28     bjh21 	DPRINTF(("DEBUG: vidcaudio_close called\n"));
    296  1.42      kent 	sc = addr;
    297  1.30     bjh21 	if (sc->sc_ppages != NULL) {
    298  1.62     skrll 		kmem_free(sc->sc_ppages, sc->sc_szppages * sizeof(paddr_t));
    299  1.30     bjh21 		sc->sc_ppages = NULL;
    300  1.62     skrll 		sc->sc_szppages = 0;
    301  1.30     bjh21 	}
    302   1.1   reinoud }
    303   1.1   reinoud 
    304  1.18     bjh21 /*
    305  1.18     bjh21  * Interface to the generic audio driver
    306  1.18     bjh21  */
    307   1.1   reinoud 
    308  1.25     bjh21 static int
    309  1.58     isaki vidcaudio_query_format(void *addr, audio_format_query_t *afp)
    310   1.1   reinoud {
    311  1.42      kent 	struct vidcaudio_softc *sc;
    312  1.18     bjh21 
    313  1.42      kent 	sc = addr;
    314  1.58     isaki 	if (sc->sc_is16bit)
    315  1.58     isaki 		return audio_query_format(&vidcaudio_formats_16bit, 1, afp);
    316  1.58     isaki 	else
    317  1.58     isaki 		return audio_query_format(&vidcaudio_formats_8bit, 1, afp);
    318  1.24     bjh21 }
    319  1.24     bjh21 
    320  1.41      kent static int
    321  1.58     isaki vidcaudio_set_format(void *addr, int setmode,
    322  1.58     isaki     const audio_params_t *play, const audio_params_t *rec,
    323  1.58     isaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    324  1.24     bjh21 {
    325  1.42      kent 	struct vidcaudio_softc *sc;
    326  1.24     bjh21 	int sample_period, ch;
    327  1.18     bjh21 
    328  1.42      kent 	sc = addr;
    329  1.36     bjh21 	if (sc->sc_is16bit) {
    330  1.36     bjh21 		/* ARM7500ish, 16-bit, two-channel */
    331  1.58     isaki 		sample_period = 705600 / 4 / play->sample_rate;
    332  1.58     isaki 		if (sample_period < 3)
    333  1.58     isaki 			sample_period = 3;
    334  1.36     bjh21 		vidcaudio_rate(sample_period - 2);
    335  1.36     bjh21 		vidcaudio_ctrl(SCR_SERIAL);
    336  1.36     bjh21 	} else {
    337  1.36     bjh21 		/* VIDC20ish, u-law, 8-channel */
    338  1.36     bjh21 		/*
    339  1.36     bjh21 		 * We always use two hardware channels, because using
    340  1.36     bjh21 		 * one at 8kHz gives a nasty whining sound from the
    341  1.58     isaki 		 * speaker.
    342  1.36     bjh21 		 */
    343  1.58     isaki 		sample_period = 1000000 / 2 / play->sample_rate;
    344  1.58     isaki 		if (sample_period < 3)
    345  1.58     isaki 			sample_period = 3;
    346  1.36     bjh21 		vidcaudio_rate(sample_period - 2);
    347  1.36     bjh21 		vidcaudio_ctrl(SCR_SDAC | SCR_CLKSEL);
    348  1.58     isaki 		for (ch = 0; ch < 8; ch += 2)
    349  1.58     isaki 			vidcaudio_stereo(ch, SIR_LEFT_100);
    350  1.58     isaki 		for (ch = 1; ch < 8; ch += 2)
    351  1.58     isaki 			vidcaudio_stereo(ch, SIR_RIGHT_100);
    352  1.58     isaki 
    353  1.58     isaki 		pfil->codec = audio_internal_to_mulaw;
    354  1.24     bjh21 	}
    355   1.1   reinoud 	return 0;
    356   1.1   reinoud }
    357   1.1   reinoud 
    358  1.25     bjh21 static int
    359  1.41      kent vidcaudio_round_blocksize(void *addr, int wantblk,
    360  1.41      kent 			  int mode, const audio_params_t *param)
    361   1.1   reinoud {
    362  1.24     bjh21 	int blk;
    363  1.18     bjh21 
    364  1.42      kent 	/*
    365  1.24     bjh21 	 * Find the smallest power of two that's larger than the
    366  1.24     bjh21 	 * requested block size, but don't allow < 32 (DMA burst is 16
    367  1.24     bjh21 	 * bytes, and single bursts are tricky) or > PAGE_SIZE (DMA is
    368  1.24     bjh21 	 * confined to a page).
    369  1.24     bjh21 	 */
    370  1.24     bjh21 
    371  1.24     bjh21 	for (blk = 32; blk < PAGE_SIZE; blk <<= 1)
    372  1.42      kent 		if (blk >= wantblk)
    373  1.42      kent 			return blk;
    374  1.24     bjh21 	return blk;
    375   1.1   reinoud }
    376   1.1   reinoud 
    377  1.25     bjh21 static int
    378  1.24     bjh21 vidcaudio_trigger_output(void *addr, void *start, void *end, int blksize,
    379  1.41      kent     void (*intr)(void *), void *arg, const audio_params_t *params)
    380   1.1   reinoud {
    381  1.42      kent 	struct vidcaudio_softc *sc;
    382  1.30     bjh21 	size_t npages, i;
    383   1.1   reinoud 
    384  1.28     bjh21 	DPRINTF(("vidcaudio_trigger_output %p-%p/0x%x\n",
    385  1.28     bjh21 	    start, end, blksize));
    386   1.1   reinoud 
    387  1.42      kent 	sc = addr;
    388  1.41      kent 	KASSERT(blksize == vidcaudio_round_blocksize(addr, blksize, 0, NULL));
    389  1.30     bjh21 	KASSERT((vaddr_t)start % blksize == 0);
    390  1.30     bjh21 
    391  1.24     bjh21 	sc->sc_pblksize = blksize;
    392  1.30     bjh21 	sc->sc_pbufsize = (char *)end - (char *)start;
    393  1.30     bjh21 	npages = sc->sc_pbufsize >> PGSHIFT;
    394  1.30     bjh21 	if (sc->sc_ppages != NULL)
    395  1.62     skrll 		kmem_free(sc->sc_ppages, sc->sc_szppages);
    396  1.62     skrll 	sc->sc_szppages = npages * sizeof(paddr_t);
    397  1.62     skrll 	sc->sc_ppages = kmem_alloc(sc->sc_szppages, KM_SLEEP);
    398  1.49     skrll 	if (sc->sc_ppages == NULL)
    399  1.49     skrll 		return ENOMEM;
    400  1.30     bjh21 	for (i = 0; i < npages; i++)
    401  1.30     bjh21 		if (!pmap_extract(pmap_kernel(),
    402  1.30     bjh21 		    (vaddr_t)start + i * PAGE_SIZE, &sc->sc_ppages[i]))
    403  1.30     bjh21 			return EIO;
    404  1.30     bjh21 	sc->sc_poffset = 0;
    405  1.24     bjh21 	sc->sc_pintr = intr;
    406  1.24     bjh21 	sc->sc_parg = arg;
    407   1.1   reinoud 
    408  1.24     bjh21 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    409  1.24     bjh21 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_ENABLE | IOMD_DMACR_QUADWORD);
    410  1.24     bjh21 
    411  1.24     bjh21 	/*
    412  1.24     bjh21 	 * Queue up the first two blocks, but don't tell audio code
    413  1.24     bjh21 	 * we're finished with them yet.
    414  1.24     bjh21 	 */
    415  1.24     bjh21 	sc->sc_pcountdown = 2;
    416  1.24     bjh21 
    417  1.24     bjh21 	enable_irq(sc->sc_dma_intr);
    418   1.1   reinoud 
    419   1.1   reinoud 	return 0;
    420   1.1   reinoud }
    421   1.1   reinoud 
    422  1.25     bjh21 static int
    423  1.17     bjh21 vidcaudio_halt_output(void *addr)
    424   1.1   reinoud {
    425  1.42      kent 	struct vidcaudio_softc *sc;
    426  1.18     bjh21 
    427  1.28     bjh21 	DPRINTF(("vidcaudio_halt_output\n"));
    428  1.42      kent 	sc = addr;
    429  1.30     bjh21 	disable_irq(sc->sc_dma_intr);
    430  1.24     bjh21 	IOMD_WRITE_WORD(IOMD_SD0CR, IOMD_DMACR_CLEAR | IOMD_DMACR_QUADWORD);
    431  1.24     bjh21 	return 0;
    432   1.1   reinoud }
    433   1.1   reinoud 
    434  1.25     bjh21 static int
    435  1.17     bjh21 vidcaudio_getdev(void *addr, struct audio_device *retp)
    436   1.1   reinoud {
    437  1.18     bjh21 
    438   1.1   reinoud 	*retp = vidcaudio_device;
    439   1.1   reinoud 	return 0;
    440   1.1   reinoud }
    441   1.1   reinoud 
    442   1.1   reinoud 
    443  1.25     bjh21 static int
    444  1.17     bjh21 vidcaudio_set_port(void *addr, mixer_ctrl_t *cp)
    445   1.1   reinoud {
    446  1.18     bjh21 
    447   1.1   reinoud 	return EINVAL;
    448   1.1   reinoud }
    449   1.1   reinoud 
    450  1.25     bjh21 static int
    451  1.17     bjh21 vidcaudio_get_port(void *addr, mixer_ctrl_t *cp)
    452   1.1   reinoud {
    453  1.18     bjh21 
    454   1.1   reinoud 	return EINVAL;
    455   1.1   reinoud }
    456   1.1   reinoud 
    457  1.25     bjh21 static int
    458  1.17     bjh21 vidcaudio_query_devinfo(void *addr, mixer_devinfo_t *dip)
    459   1.1   reinoud {
    460  1.18     bjh21 
    461   1.1   reinoud 	return ENXIO;
    462   1.1   reinoud }
    463   1.1   reinoud 
    464  1.25     bjh21 static int
    465  1.17     bjh21 vidcaudio_get_props(void *addr)
    466   1.1   reinoud {
    467  1.18     bjh21 
    468  1.59     isaki 	return AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE;
    469   1.1   reinoud }
    470  1.25     bjh21 
    471  1.25     bjh21 static void
    472  1.47  jmcneill vidcaudio_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
    473  1.47  jmcneill {
    474  1.47  jmcneill 	struct vidcaudio_softc *sc = opaque;
    475  1.47  jmcneill 
    476  1.47  jmcneill 	*intr = &sc->sc_intr_lock;
    477  1.47  jmcneill 	*thread = &sc->sc_lock;
    478  1.47  jmcneill }
    479  1.47  jmcneill 
    480  1.47  jmcneill static void
    481  1.24     bjh21 vidcaudio_rate(int rate)
    482   1.1   reinoud {
    483  1.18     bjh21 
    484  1.24     bjh21 	WriteWord(vidc_base, VIDC_SFR | rate);
    485   1.1   reinoud }
    486   1.1   reinoud 
    487  1.25     bjh21 static void
    488  1.24     bjh21 vidcaudio_ctrl(int ctrl)
    489   1.1   reinoud {
    490  1.18     bjh21 
    491  1.24     bjh21 	WriteWord(vidc_base, VIDC_SCR | ctrl);
    492   1.1   reinoud }
    493   1.1   reinoud 
    494  1.25     bjh21 static void
    495  1.17     bjh21 vidcaudio_stereo(int channel, int position)
    496   1.1   reinoud {
    497  1.18     bjh21 
    498  1.18     bjh21 	channel = channel << 24 | VIDC_SIR0;
    499   1.1   reinoud 	WriteWord(vidc_base, channel | position);
    500   1.1   reinoud }
    501   1.1   reinoud 
    502  1.25     bjh21 static int
    503  1.24     bjh21 vidcaudio_intr(void *arg)
    504   1.1   reinoud {
    505  1.42      kent 	struct vidcaudio_softc *sc;
    506  1.24     bjh21 	int status;
    507  1.24     bjh21 	paddr_t pnext, pend;
    508   1.1   reinoud 
    509  1.42      kent 	sc = arg;
    510  1.47  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    511  1.47  jmcneill 
    512  1.24     bjh21 	status = IOMD_READ_BYTE(IOMD_SD0ST);
    513  1.28     bjh21 	DPRINTF(("I[%x]", status));
    514  1.47  jmcneill 	if ((status & IOMD_DMAST_INT) == 0) {
    515  1.47  jmcneill 		mutex_spin_exit(&sc->sc_intr_lock);
    516  1.24     bjh21 		return 0;
    517  1.47  jmcneill 	}
    518   1.1   reinoud 
    519  1.30     bjh21 	pnext = sc->sc_ppages[sc->sc_poffset >> PGSHIFT] |
    520  1.30     bjh21 	    (sc->sc_poffset & PGOFSET);
    521  1.24     bjh21 	pend = (pnext + sc->sc_pblksize - 16) & IOMD_DMAEND_OFFSET;
    522   1.1   reinoud 
    523  1.24     bjh21 	switch (status &
    524  1.24     bjh21 	    (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB)) {
    525   1.1   reinoud 
    526  1.24     bjh21 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    527  1.24     bjh21 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    528  1.28     bjh21 		DPRINTF(("B<0x%08lx,0x%03lx>", pnext, pend));
    529  1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0CURB, pnext);
    530  1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pend);
    531  1.24     bjh21 		break;
    532   1.1   reinoud 
    533  1.24     bjh21 	case (IOMD_DMAST_INT | IOMD_DMAST_BANKB):
    534  1.24     bjh21 	case (IOMD_DMAST_OVERRUN | IOMD_DMAST_INT | IOMD_DMAST_BANKA):
    535  1.28     bjh21 		DPRINTF(("A<0x%08lx,0x%03lx>", pnext, pend));
    536  1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0CURA, pnext);
    537  1.24     bjh21 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pend);
    538  1.24     bjh21 		break;
    539   1.1   reinoud 	}
    540   1.1   reinoud 
    541  1.30     bjh21 	sc->sc_poffset += sc->sc_pblksize;
    542  1.30     bjh21 	if (sc->sc_poffset >= sc->sc_pbufsize)
    543  1.30     bjh21 		sc->sc_poffset = 0;
    544  1.42      kent 
    545  1.24     bjh21 	if (sc->sc_pcountdown > 0)
    546  1.24     bjh21 		sc->sc_pcountdown--;
    547  1.24     bjh21 	else
    548  1.24     bjh21 		(*sc->sc_pintr)(sc->sc_parg);
    549   1.1   reinoud 
    550  1.47  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    551  1.24     bjh21 	return 1;
    552   1.1   reinoud }
    553