vidcaudio.c revision 1.2.4.6       1  1.2.4.6  nathanw /*	$NetBSD: vidcaudio.c,v 1.2.4.6 2002/06/20 03:38:13 nathanw Exp $	*/
      2  1.2.4.2  nathanw 
      3  1.2.4.2  nathanw /*
      4  1.2.4.2  nathanw  * Copyright (c) 1995 Melvin Tang-Richardson
      5  1.2.4.2  nathanw  *
      6  1.2.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
      7  1.2.4.2  nathanw  * modification, are permitted provided that the following conditions
      8  1.2.4.2  nathanw  * are met:
      9  1.2.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     10  1.2.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     11  1.2.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.2.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     13  1.2.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     14  1.2.4.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     15  1.2.4.2  nathanw  *    must display the following acknowledgement:
     16  1.2.4.2  nathanw  *	This product includes software developed by the RiscBSD team.
     17  1.2.4.2  nathanw  * 4. The name of the author may not be used to endorse or promote products
     18  1.2.4.2  nathanw  *    derived from this software without specific prior written permission.
     19  1.2.4.2  nathanw  *
     20  1.2.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.2.4.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.2.4.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.2.4.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.2.4.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.2.4.2  nathanw  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.2.4.2  nathanw  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.2.4.2  nathanw  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.2.4.2  nathanw  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.2.4.2  nathanw  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.2.4.2  nathanw  */
     31  1.2.4.2  nathanw 
     32  1.2.4.2  nathanw /*
     33  1.2.4.2  nathanw  * audio driver for the RiscPC 16 bit sound
     34  1.2.4.2  nathanw  *
     35  1.2.4.2  nathanw  * Interfaces with the NetBSD generic audio driver to provide SUN
     36  1.2.4.2  nathanw  * /dev/audio (partial) compatibility.
     37  1.2.4.2  nathanw  */
     38  1.2.4.2  nathanw 
     39  1.2.4.2  nathanw #include <sys/param.h>	/* proc.h */
     40  1.2.4.3  nathanw 
     41  1.2.4.6  nathanw __KERNEL_RCSID(0, "$NetBSD: vidcaudio.c,v 1.2.4.6 2002/06/20 03:38:13 nathanw Exp $");
     42  1.2.4.3  nathanw 
     43  1.2.4.2  nathanw #include <sys/conf.h>   /* autoconfig functions */
     44  1.2.4.2  nathanw #include <sys/device.h> /* device calls */
     45  1.2.4.2  nathanw #include <sys/proc.h>	/* device calls */
     46  1.2.4.2  nathanw #include <sys/audioio.h>
     47  1.2.4.2  nathanw #include <sys/errno.h>
     48  1.2.4.2  nathanw #include <sys/systm.h>
     49  1.2.4.2  nathanw 
     50  1.2.4.2  nathanw #include <uvm/uvm_extern.h>
     51  1.2.4.2  nathanw 
     52  1.2.4.2  nathanw #include <dev/audio_if.h>
     53  1.2.4.2  nathanw 
     54  1.2.4.2  nathanw #include <machine/intr.h>
     55  1.2.4.2  nathanw #include <arm/arm32/katelib.h>
     56  1.2.4.2  nathanw 
     57  1.2.4.3  nathanw #include <arm/iomd/vidcaudiovar.h>
     58  1.2.4.2  nathanw #include <arm/iomd/iomdreg.h>
     59  1.2.4.2  nathanw #include <arm/iomd/iomdvar.h>
     60  1.2.4.2  nathanw #include <arm/iomd/vidc.h>
     61  1.2.4.2  nathanw #include <arm/mainbus/mainbus.h>
     62  1.2.4.2  nathanw #include <arm/iomd/waveform.h>
     63  1.2.4.2  nathanw #include "vidcaudio.h"
     64  1.2.4.2  nathanw 
     65  1.2.4.2  nathanw extern int *vidc_base;
     66  1.2.4.2  nathanw 
     67  1.2.4.2  nathanw #undef DEBUG
     68  1.2.4.2  nathanw 
     69  1.2.4.2  nathanw struct audio_general {
     70  1.2.4.5  nathanw 	vaddr_t silence;
     71  1.2.4.2  nathanw 	irqhandler_t ih;
     72  1.2.4.2  nathanw 
     73  1.2.4.3  nathanw 	void (*intr) (void *);
     74  1.2.4.2  nathanw 	void *arg;
     75  1.2.4.2  nathanw 
     76  1.2.4.5  nathanw 	paddr_t next_cur;
     77  1.2.4.5  nathanw 	paddr_t next_end;
     78  1.2.4.3  nathanw 	void (*next_intr) (void *);
     79  1.2.4.2  nathanw 	void *next_arg;
     80  1.2.4.2  nathanw 
     81  1.2.4.2  nathanw 	int buffer;
     82  1.2.4.2  nathanw 	int in_progress;
     83  1.2.4.2  nathanw 
     84  1.2.4.2  nathanw 	int open;
     85  1.2.4.2  nathanw } ag;
     86  1.2.4.2  nathanw 
     87  1.2.4.2  nathanw struct vidcaudio_softc {
     88  1.2.4.2  nathanw 	struct device device;
     89  1.2.4.2  nathanw 
     90  1.2.4.2  nathanw 	int open;
     91  1.2.4.2  nathanw };
     92  1.2.4.2  nathanw 
     93  1.2.4.2  nathanw int  vidcaudio_probe	__P((struct device *parent, struct cfdata *cf, void *aux));
     94  1.2.4.2  nathanw void vidcaudio_attach	__P((struct device *parent, struct device *self, void *aux));
     95  1.2.4.2  nathanw int  vidcaudio_open	__P((void *addr, int flags));
     96  1.2.4.2  nathanw void vidcaudio_close	__P((void *addr));
     97  1.2.4.2  nathanw 
     98  1.2.4.2  nathanw int vidcaudio_intr	__P((void *arg));
     99  1.2.4.5  nathanw int vidcaudio_dma_program	__P((vaddr_t cur, vaddr_t end, void (*intr)(void *), void *arg));
    100  1.2.4.2  nathanw void vidcaudio_dummy_routine	__P((void *arg));
    101  1.2.4.2  nathanw int vidcaudio_stereo	__P((int channel, int position));
    102  1.2.4.2  nathanw int vidcaudio_rate	__P((int rate));
    103  1.2.4.2  nathanw void vidcaudio_shutdown	__P((void));
    104  1.2.4.2  nathanw 
    105  1.2.4.2  nathanw static int sound_dma_intr;
    106  1.2.4.2  nathanw 
    107  1.2.4.2  nathanw struct cfattach vidcaudio_ca = {
    108  1.2.4.2  nathanw 	sizeof(struct vidcaudio_softc), vidcaudio_probe, vidcaudio_attach
    109  1.2.4.2  nathanw };
    110  1.2.4.2  nathanw 
    111  1.2.4.2  nathanw int    vidcaudio_query_encoding  __P((void *, struct audio_encoding *));
    112  1.2.4.2  nathanw int    vidcaudio_set_params	 __P((void *, int, int, struct audio_params *, struct audio_params *));
    113  1.2.4.2  nathanw int    vidcaudio_round_blocksize __P((void *, int));
    114  1.2.4.3  nathanw int    vidcaudio_start_output	 __P((void *, void *, int, void (*)(void *),
    115  1.2.4.3  nathanw 					 void *));
    116  1.2.4.3  nathanw int    vidcaudio_start_input	 __P((void *, void *, int, void (*)(void *),
    117  1.2.4.3  nathanw 					 void *));
    118  1.2.4.2  nathanw int    vidcaudio_halt_output	 __P((void *));
    119  1.2.4.2  nathanw int    vidcaudio_halt_input 	 __P((void *));
    120  1.2.4.2  nathanw int    vidcaudio_speaker_ctl	 __P((void *, int));
    121  1.2.4.2  nathanw int    vidcaudio_getdev		 __P((void *, struct audio_device *));
    122  1.2.4.2  nathanw int    vidcaudio_set_port	 __P((void *, mixer_ctrl_t *));
    123  1.2.4.2  nathanw int    vidcaudio_get_port	 __P((void *, mixer_ctrl_t *));
    124  1.2.4.2  nathanw int    vidcaudio_query_devinfo	 __P((void *, mixer_devinfo_t *));
    125  1.2.4.2  nathanw int    vidcaudio_get_props	 __P((void *));
    126  1.2.4.2  nathanw 
    127  1.2.4.2  nathanw struct audio_device vidcaudio_device = {
    128  1.2.4.2  nathanw 	"VidcAudio 8-bit",
    129  1.2.4.2  nathanw 	"x",
    130  1.2.4.2  nathanw 	"vidcaudio"
    131  1.2.4.2  nathanw };
    132  1.2.4.2  nathanw 
    133  1.2.4.2  nathanw struct audio_hw_if vidcaudio_hw_if = {
    134  1.2.4.2  nathanw 	vidcaudio_open,
    135  1.2.4.2  nathanw 	vidcaudio_close,
    136  1.2.4.2  nathanw 	0,
    137  1.2.4.2  nathanw 	vidcaudio_query_encoding,
    138  1.2.4.2  nathanw 	vidcaudio_set_params,
    139  1.2.4.2  nathanw 	vidcaudio_round_blocksize,
    140  1.2.4.2  nathanw 	0,
    141  1.2.4.2  nathanw 	0,
    142  1.2.4.2  nathanw 	0,
    143  1.2.4.2  nathanw 	vidcaudio_start_output,
    144  1.2.4.2  nathanw 	vidcaudio_start_input,
    145  1.2.4.2  nathanw 	vidcaudio_halt_output,
    146  1.2.4.2  nathanw 	vidcaudio_halt_input,
    147  1.2.4.2  nathanw 	vidcaudio_speaker_ctl,
    148  1.2.4.2  nathanw 	vidcaudio_getdev,
    149  1.2.4.2  nathanw 	0,
    150  1.2.4.2  nathanw 	vidcaudio_set_port,
    151  1.2.4.2  nathanw 	vidcaudio_get_port,
    152  1.2.4.2  nathanw 	vidcaudio_query_devinfo,
    153  1.2.4.2  nathanw 	0,
    154  1.2.4.2  nathanw 	0,
    155  1.2.4.2  nathanw 	0,
    156  1.2.4.2  nathanw 	0,
    157  1.2.4.2  nathanw 	vidcaudio_get_props,
    158  1.2.4.2  nathanw 	0,
    159  1.2.4.2  nathanw 	0,
    160  1.2.4.2  nathanw 	0,
    161  1.2.4.2  nathanw };
    162  1.2.4.2  nathanw 
    163  1.2.4.2  nathanw 
    164  1.2.4.2  nathanw void
    165  1.2.4.2  nathanw vidcaudio_beep_generate()
    166  1.2.4.2  nathanw {
    167  1.2.4.2  nathanw 	vidcaudio_dma_program(ag.silence, ag.silence+sizeof(beep_waveform)-16,
    168  1.2.4.2  nathanw 	    vidcaudio_dummy_routine, NULL);
    169  1.2.4.2  nathanw }
    170  1.2.4.2  nathanw 
    171  1.2.4.2  nathanw 
    172  1.2.4.2  nathanw int
    173  1.2.4.2  nathanw vidcaudio_probe(parent, cf, aux)
    174  1.2.4.2  nathanw 	struct device *parent;
    175  1.2.4.2  nathanw 	struct cfdata* cf;
    176  1.2.4.2  nathanw 	void *aux;
    177  1.2.4.2  nathanw {
    178  1.2.4.2  nathanw 	int id;
    179  1.2.4.2  nathanw 
    180  1.2.4.2  nathanw 	id = IOMD_ID;
    181  1.2.4.2  nathanw 
    182  1.2.4.2  nathanw 	/* So far I only know about this IOMD */
    183  1.2.4.2  nathanw 	switch (id) {
    184  1.2.4.2  nathanw 	case RPC600_IOMD_ID:
    185  1.2.4.2  nathanw 		return(1);
    186  1.2.4.2  nathanw 		break;
    187  1.2.4.2  nathanw 	case ARM7500_IOC_ID:
    188  1.2.4.2  nathanw 	case ARM7500FE_IOC_ID:
    189  1.2.4.2  nathanw 		return(1);
    190  1.2.4.2  nathanw 		break;
    191  1.2.4.2  nathanw 	default:
    192  1.2.4.2  nathanw 		printf("vidcaudio: Unknown IOMD id=%04x", id);
    193  1.2.4.2  nathanw 		break;
    194  1.2.4.2  nathanw 	}
    195  1.2.4.2  nathanw 
    196  1.2.4.2  nathanw 	return (0);
    197  1.2.4.2  nathanw }
    198  1.2.4.2  nathanw 
    199  1.2.4.2  nathanw 
    200  1.2.4.2  nathanw void
    201  1.2.4.2  nathanw vidcaudio_attach(parent, self, aux)
    202  1.2.4.2  nathanw 	struct device *parent;
    203  1.2.4.2  nathanw 	struct device *self;
    204  1.2.4.2  nathanw 	void *aux;
    205  1.2.4.2  nathanw {
    206  1.2.4.2  nathanw 	struct vidcaudio_softc *sc = (void *)self;
    207  1.2.4.2  nathanw 	int id;
    208  1.2.4.2  nathanw 
    209  1.2.4.2  nathanw 	sc->open = 0;
    210  1.2.4.2  nathanw 	ag.in_progress = 0;
    211  1.2.4.2  nathanw 
    212  1.2.4.2  nathanw 	ag.next_cur = 0;
    213  1.2.4.2  nathanw 	ag.next_end = 0;
    214  1.2.4.2  nathanw 	ag.next_intr = NULL;
    215  1.2.4.2  nathanw 	ag.next_arg = NULL;
    216  1.2.4.2  nathanw 
    217  1.2.4.2  nathanw 	vidcaudio_rate(32); /* 24*1024*/
    218  1.2.4.2  nathanw 
    219  1.2.4.2  nathanw 	/* Program the silence buffer and reset the DMA channel */
    220  1.2.4.2  nathanw 	ag.silence = uvm_km_alloc(kernel_map, NBPG);
    221  1.2.4.2  nathanw 	if (ag.silence == NULL)
    222  1.2.4.2  nathanw 		panic("vidcaudio: Cannot allocate memory\n");
    223  1.2.4.2  nathanw 
    224  1.2.4.2  nathanw 	memset((char *)ag.silence, 0, NBPG);
    225  1.2.4.2  nathanw 	memcpy((char *)ag.silence, (char *)beep_waveform, sizeof(beep_waveform));
    226  1.2.4.2  nathanw 
    227  1.2.4.2  nathanw 	ag.buffer = 0;
    228  1.2.4.2  nathanw 
    229  1.2.4.2  nathanw 	/* Install the irq handler for the DMA interrupt */
    230  1.2.4.2  nathanw 	ag.ih.ih_func = vidcaudio_intr;
    231  1.2.4.2  nathanw 	ag.ih.ih_arg = NULL;
    232  1.2.4.2  nathanw 	ag.ih.ih_level = IPL_AUDIO;
    233  1.2.4.2  nathanw 	ag.ih.ih_name = "vidcaudio";
    234  1.2.4.2  nathanw 
    235  1.2.4.2  nathanw 	ag.intr = NULL;
    236  1.2.4.2  nathanw /*	ag.nextintr = NULL;*/
    237  1.2.4.2  nathanw 
    238  1.2.4.2  nathanw 	id = IOMD_ID;
    239  1.2.4.2  nathanw 
    240  1.2.4.2  nathanw 	switch (id) {
    241  1.2.4.2  nathanw 	case RPC600_IOMD_ID:
    242  1.2.4.2  nathanw 		sound_dma_intr = IRQ_DMASCH0;
    243  1.2.4.2  nathanw 		break;
    244  1.2.4.2  nathanw 	case ARM7500_IOC_ID:
    245  1.2.4.2  nathanw 	case ARM7500FE_IOC_ID:
    246  1.2.4.2  nathanw 		sound_dma_intr = IRQ_SDMA;
    247  1.2.4.2  nathanw 		break;
    248  1.2.4.2  nathanw 	}
    249  1.2.4.2  nathanw 
    250  1.2.4.2  nathanw 	disable_irq(sound_dma_intr);
    251  1.2.4.2  nathanw 
    252  1.2.4.2  nathanw 	if (irq_claim(sound_dma_intr, &(ag.ih)))
    253  1.2.4.2  nathanw 		panic("vidcaudio: couldn't claim IRQ %d\n", sound_dma_intr);
    254  1.2.4.2  nathanw 
    255  1.2.4.2  nathanw 	disable_irq(sound_dma_intr);
    256  1.2.4.2  nathanw 
    257  1.2.4.2  nathanw 	printf("\n");
    258  1.2.4.2  nathanw 
    259  1.2.4.2  nathanw 	vidcaudio_dma_program(ag.silence, ag.silence+NBPG-16,
    260  1.2.4.2  nathanw 	    vidcaudio_dummy_routine, NULL);
    261  1.2.4.2  nathanw 
    262  1.2.4.2  nathanw 	audio_attach_mi(&vidcaudio_hw_if, sc, &sc->device);
    263  1.2.4.2  nathanw 
    264  1.2.4.2  nathanw #ifdef DEBUG
    265  1.2.4.2  nathanw 	printf(" UNDER DEVELOPMENT (nuts)\n");
    266  1.2.4.2  nathanw #endif
    267  1.2.4.2  nathanw }
    268  1.2.4.2  nathanw 
    269  1.2.4.2  nathanw int
    270  1.2.4.2  nathanw vidcaudio_open(addr, flags)
    271  1.2.4.2  nathanw 	void *addr;
    272  1.2.4.2  nathanw 	int flags;
    273  1.2.4.2  nathanw {
    274  1.2.4.2  nathanw 	struct vidcaudio_softc *sc = addr;
    275  1.2.4.2  nathanw 
    276  1.2.4.2  nathanw #ifdef DEBUG
    277  1.2.4.2  nathanw 	printf("DEBUG: vidcaudio_open called\n");
    278  1.2.4.2  nathanw #endif
    279  1.2.4.2  nathanw 
    280  1.2.4.2  nathanw 	if (sc->open)
    281  1.2.4.2  nathanw 		return EBUSY;
    282  1.2.4.2  nathanw 
    283  1.2.4.2  nathanw 	sc->open = 1;
    284  1.2.4.2  nathanw 	ag.open = 1;
    285  1.2.4.2  nathanw 
    286  1.2.4.2  nathanw 	return 0;
    287  1.2.4.2  nathanw }
    288  1.2.4.2  nathanw 
    289  1.2.4.2  nathanw void
    290  1.2.4.2  nathanw vidcaudio_close(addr)
    291  1.2.4.2  nathanw 	void *addr;
    292  1.2.4.2  nathanw {
    293  1.2.4.2  nathanw 	struct vidcaudio_softc *sc = addr;
    294  1.2.4.2  nathanw 
    295  1.2.4.2  nathanw 	vidcaudio_shutdown();
    296  1.2.4.2  nathanw 
    297  1.2.4.2  nathanw #ifdef DEBUG
    298  1.2.4.2  nathanw 	printf("DEBUG: vidcaudio_close called\n");
    299  1.2.4.2  nathanw #endif
    300  1.2.4.2  nathanw 
    301  1.2.4.2  nathanw 	sc->open = 0;
    302  1.2.4.2  nathanw 	ag.open = 0;
    303  1.2.4.2  nathanw }
    304  1.2.4.2  nathanw 
    305  1.2.4.2  nathanw /* ************************************************************************* *
    306  1.2.4.2  nathanw  | Interface to the generic audio driver                                     |
    307  1.2.4.2  nathanw  * ************************************************************************* */
    308  1.2.4.2  nathanw 
    309  1.2.4.2  nathanw int
    310  1.2.4.2  nathanw vidcaudio_query_encoding(addr, fp)
    311  1.2.4.2  nathanw 	void *addr;
    312  1.2.4.2  nathanw 	struct audio_encoding *fp;
    313  1.2.4.2  nathanw {
    314  1.2.4.2  nathanw 	switch (fp->index) {
    315  1.2.4.2  nathanw 	case 0:
    316  1.2.4.2  nathanw 		strcpy(fp->name, "vidc");
    317  1.2.4.2  nathanw 		fp->encoding = AUDIO_ENCODING_ULAW;
    318  1.2.4.2  nathanw 		fp->precision = 8;
    319  1.2.4.2  nathanw 		fp->flags = 0;
    320  1.2.4.2  nathanw 		break;
    321  1.2.4.2  nathanw 
    322  1.2.4.2  nathanw 	default:
    323  1.2.4.2  nathanw 		return(EINVAL);
    324  1.2.4.2  nathanw 	}
    325  1.2.4.2  nathanw 	return 0;
    326  1.2.4.2  nathanw }
    327  1.2.4.2  nathanw 
    328  1.2.4.2  nathanw int
    329  1.2.4.2  nathanw vidcaudio_set_params(addr, setmode, usemode, p, r)
    330  1.2.4.2  nathanw 	void *addr;
    331  1.2.4.2  nathanw 	int setmode, usemode;
    332  1.2.4.2  nathanw 	struct audio_params *p, *r;
    333  1.2.4.2  nathanw {
    334  1.2.4.2  nathanw 	if (p->encoding != AUDIO_ENCODING_ULAW ||
    335  1.2.4.2  nathanw 	    p->channels != 8)
    336  1.2.4.2  nathanw 		return EINVAL;
    337  1.2.4.2  nathanw 	vidcaudio_rate(4 * p->sample_rate / (3 * 1024)); /* XXX probably wrong */
    338  1.2.4.2  nathanw 
    339  1.2.4.2  nathanw 	return 0;
    340  1.2.4.2  nathanw }
    341  1.2.4.2  nathanw 
    342  1.2.4.2  nathanw int
    343  1.2.4.2  nathanw vidcaudio_round_blocksize(addr, blk)
    344  1.2.4.2  nathanw 	void *addr;
    345  1.2.4.2  nathanw 	int blk;
    346  1.2.4.2  nathanw {
    347  1.2.4.2  nathanw 	if (blk > NBPG)
    348  1.2.4.2  nathanw 		blk = NBPG;
    349  1.2.4.2  nathanw 	return (blk);
    350  1.2.4.2  nathanw }
    351  1.2.4.2  nathanw 
    352  1.2.4.2  nathanw #define ROUND(s)  ( ((int)s) & (~(NBPG-1)) )
    353  1.2.4.2  nathanw 
    354  1.2.4.2  nathanw int
    355  1.2.4.2  nathanw vidcaudio_start_output(addr, p, cc, intr, arg)
    356  1.2.4.2  nathanw 	void *addr;
    357  1.2.4.2  nathanw 	void *p;
    358  1.2.4.2  nathanw 	int cc;
    359  1.2.4.3  nathanw 	void (*intr)(void *);
    360  1.2.4.2  nathanw 	void *arg;
    361  1.2.4.2  nathanw {
    362  1.2.4.2  nathanw 	/* I can only DMA inside 1 page */
    363  1.2.4.2  nathanw 
    364  1.2.4.2  nathanw #ifdef DEBUG
    365  1.2.4.2  nathanw 	printf("vidcaudio_start_output (%d) %08x %08x\n", cc, intr, arg);
    366  1.2.4.2  nathanw #endif
    367  1.2.4.2  nathanw 
    368  1.2.4.2  nathanw 	if (ROUND(p) != ROUND(p+cc)) {
    369  1.2.4.2  nathanw 		/*
    370  1.2.4.2  nathanw 		 * If it's over a page I can fix it up by copying it into
    371  1.2.4.2  nathanw 		 * my buffer
    372  1.2.4.2  nathanw 		 */
    373  1.2.4.2  nathanw 
    374  1.2.4.2  nathanw #ifdef DEBUG
    375  1.2.4.2  nathanw 		printf("vidcaudio: DMA over page boundary requested."
    376  1.2.4.2  nathanw 		    "  Fixing up\n");
    377  1.2.4.2  nathanw #endif
    378  1.2.4.2  nathanw 		memcpy(p, (char *)ag.silence, cc > NBPG ? NBPG : cc);
    379  1.2.4.2  nathanw 		p = (void *)ag.silence;
    380  1.2.4.2  nathanw 
    381  1.2.4.2  nathanw 		/*
    382  1.2.4.2  nathanw 		 * I can't DMA any more than that, but it is possible to
    383  1.2.4.2  nathanw 		 * fix it up by handling multiple buffers and only
    384  1.2.4.2  nathanw 		 * interrupting the audio driver after sending out all the
    385  1.2.4.2  nathanw 		 * stuff it gave me.  That it more than I can be bothered
    386  1.2.4.2  nathanw 		 * to do right now and it probablly wont happen so I'll just
    387  1.2.4.2  nathanw 		 * truncate the buffer and tell the user.
    388  1.2.4.2  nathanw 		 */
    389  1.2.4.2  nathanw 
    390  1.2.4.2  nathanw 		if (cc > NBPG) {
    391  1.2.4.2  nathanw 			printf("vidcaudio: DMA buffer truncated. I could fix this up\n");
    392  1.2.4.2  nathanw 			cc = NBPG;
    393  1.2.4.2  nathanw 		}
    394  1.2.4.2  nathanw 	}
    395  1.2.4.5  nathanw 	vidcaudio_dma_program((vaddr_t)p, (vaddr_t)((char *)p+cc),
    396  1.2.4.2  nathanw 	    intr, arg);
    397  1.2.4.2  nathanw 	return 0;
    398  1.2.4.2  nathanw }
    399  1.2.4.2  nathanw 
    400  1.2.4.2  nathanw int
    401  1.2.4.2  nathanw vidcaudio_start_input(addr, p, cc, intr, arg)
    402  1.2.4.2  nathanw 	void *addr;
    403  1.2.4.2  nathanw 	void *p;
    404  1.2.4.2  nathanw 	int cc;
    405  1.2.4.3  nathanw 	void (*intr)(void *);
    406  1.2.4.2  nathanw 	void *arg;
    407  1.2.4.2  nathanw {
    408  1.2.4.2  nathanw 	return EIO;
    409  1.2.4.2  nathanw }
    410  1.2.4.2  nathanw 
    411  1.2.4.2  nathanw int
    412  1.2.4.2  nathanw vidcaudio_halt_output(addr)
    413  1.2.4.2  nathanw 	void *addr;
    414  1.2.4.2  nathanw {
    415  1.2.4.2  nathanw #ifdef DEBUG
    416  1.2.4.2  nathanw 	printf("DEBUG: vidcaudio_halt_output\n");
    417  1.2.4.2  nathanw #endif
    418  1.2.4.2  nathanw 	return EIO;
    419  1.2.4.2  nathanw }
    420  1.2.4.2  nathanw 
    421  1.2.4.2  nathanw int
    422  1.2.4.2  nathanw vidcaudio_halt_input(addr)
    423  1.2.4.2  nathanw 	void *addr;
    424  1.2.4.2  nathanw {
    425  1.2.4.2  nathanw #ifdef DEBUG
    426  1.2.4.2  nathanw 	printf("DEBUG: vidcaudio_halt_input\n");
    427  1.2.4.2  nathanw #endif
    428  1.2.4.2  nathanw 	return EIO;
    429  1.2.4.2  nathanw }
    430  1.2.4.2  nathanw 
    431  1.2.4.2  nathanw int
    432  1.2.4.2  nathanw vidcaudio_speaker_ctl(addr, newstate)
    433  1.2.4.2  nathanw 	void *addr;
    434  1.2.4.2  nathanw 	int newstate;
    435  1.2.4.2  nathanw {
    436  1.2.4.2  nathanw #ifdef DEBUG
    437  1.2.4.2  nathanw 	printf("DEBUG: vidcaudio_speaker_ctl\n");
    438  1.2.4.2  nathanw #endif
    439  1.2.4.2  nathanw 	return 0;
    440  1.2.4.2  nathanw }
    441  1.2.4.2  nathanw 
    442  1.2.4.2  nathanw int
    443  1.2.4.2  nathanw vidcaudio_getdev(addr, retp)
    444  1.2.4.2  nathanw 	void *addr;
    445  1.2.4.2  nathanw 	struct audio_device *retp;
    446  1.2.4.2  nathanw {
    447  1.2.4.2  nathanw 	*retp = vidcaudio_device;
    448  1.2.4.2  nathanw 	return 0;
    449  1.2.4.2  nathanw }
    450  1.2.4.2  nathanw 
    451  1.2.4.2  nathanw 
    452  1.2.4.2  nathanw int
    453  1.2.4.2  nathanw vidcaudio_set_port(addr, cp)
    454  1.2.4.2  nathanw 	void *addr;
    455  1.2.4.2  nathanw 	mixer_ctrl_t *cp;
    456  1.2.4.2  nathanw {
    457  1.2.4.2  nathanw 	return EINVAL;
    458  1.2.4.2  nathanw }
    459  1.2.4.2  nathanw 
    460  1.2.4.2  nathanw int
    461  1.2.4.2  nathanw vidcaudio_get_port(addr, cp)
    462  1.2.4.2  nathanw 	void *addr;
    463  1.2.4.2  nathanw 	mixer_ctrl_t *cp;
    464  1.2.4.2  nathanw {
    465  1.2.4.2  nathanw 	return EINVAL;
    466  1.2.4.2  nathanw }
    467  1.2.4.2  nathanw 
    468  1.2.4.2  nathanw int
    469  1.2.4.2  nathanw vidcaudio_query_devinfo(addr, dip)
    470  1.2.4.2  nathanw 	void *addr;
    471  1.2.4.2  nathanw 	mixer_devinfo_t *dip;
    472  1.2.4.2  nathanw {
    473  1.2.4.2  nathanw 	return ENXIO;
    474  1.2.4.2  nathanw }
    475  1.2.4.2  nathanw 
    476  1.2.4.2  nathanw int
    477  1.2.4.2  nathanw vidcaudio_get_props(addr)
    478  1.2.4.2  nathanw 	void *addr;
    479  1.2.4.2  nathanw {
    480  1.2.4.2  nathanw 	return 0;
    481  1.2.4.2  nathanw }
    482  1.2.4.2  nathanw void
    483  1.2.4.2  nathanw vidcaudio_dummy_routine(arg)
    484  1.2.4.2  nathanw 	void *arg;
    485  1.2.4.2  nathanw {
    486  1.2.4.2  nathanw #ifdef DEBUG
    487  1.2.4.2  nathanw 	printf("vidcaudio_dummy_routine\n");
    488  1.2.4.2  nathanw #endif
    489  1.2.4.2  nathanw }
    490  1.2.4.2  nathanw 
    491  1.2.4.2  nathanw int
    492  1.2.4.2  nathanw vidcaudio_rate(rate)
    493  1.2.4.2  nathanw 	int rate;
    494  1.2.4.2  nathanw {
    495  1.2.4.2  nathanw 	WriteWord(vidc_base, VIDC_SFR | rate);
    496  1.2.4.2  nathanw 	return 0;
    497  1.2.4.2  nathanw }
    498  1.2.4.2  nathanw 
    499  1.2.4.2  nathanw int
    500  1.2.4.2  nathanw vidcaudio_stereo(channel, position)
    501  1.2.4.2  nathanw 	int channel;
    502  1.2.4.2  nathanw 	int position;
    503  1.2.4.2  nathanw {
    504  1.2.4.2  nathanw 	if (channel < 0) return EINVAL;
    505  1.2.4.2  nathanw 	if (channel > 7) return EINVAL;
    506  1.2.4.2  nathanw 	channel = channel<<24 | VIDC_SIR0;
    507  1.2.4.2  nathanw 	WriteWord(vidc_base, channel | position);
    508  1.2.4.2  nathanw 	return 0;
    509  1.2.4.2  nathanw }
    510  1.2.4.2  nathanw 
    511  1.2.4.5  nathanw #define PHYS(x, y) pmap_extract(pmap_kernel(), ((x)&L2_S_FRAME), (paddr_t *)(y))
    512  1.2.4.2  nathanw 
    513  1.2.4.2  nathanw /*
    514  1.2.4.2  nathanw  * Program the next buffer to be used
    515  1.2.4.2  nathanw  * This function must be re-entrant, maximum re-entrancy of 2
    516  1.2.4.2  nathanw  */
    517  1.2.4.2  nathanw 
    518  1.2.4.2  nathanw #define FLAGS (0)
    519  1.2.4.2  nathanw 
    520  1.2.4.2  nathanw int
    521  1.2.4.2  nathanw vidcaudio_dma_program(cur, end, intr, arg)
    522  1.2.4.5  nathanw 	vaddr_t cur;
    523  1.2.4.5  nathanw 	vaddr_t end;
    524  1.2.4.3  nathanw 	void (*intr)(void *);
    525  1.2.4.2  nathanw 	void *arg;
    526  1.2.4.2  nathanw {
    527  1.2.4.2  nathanw 	paddr_t pa1, pa2;
    528  1.2.4.2  nathanw 
    529  1.2.4.2  nathanw 	/* If there isn't a transfer in progress then start a new one */
    530  1.2.4.2  nathanw 	if (ag.in_progress == 0) {
    531  1.2.4.2  nathanw 		ag.buffer = 0;
    532  1.2.4.2  nathanw 		IOMD_WRITE_WORD(IOMD_SD0CR, 0x90);	/* Reset State Machine */
    533  1.2.4.2  nathanw 		IOMD_WRITE_WORD(IOMD_SD0CR, 0x30);	/* Reset State Machine */
    534  1.2.4.2  nathanw 
    535  1.2.4.2  nathanw 		PHYS(cur, &pa1);
    536  1.2.4.2  nathanw 		PHYS(end - 16, &pa2);
    537  1.2.4.2  nathanw 
    538  1.2.4.2  nathanw 		IOMD_WRITE_WORD(IOMD_SD0CURB, pa1);
    539  1.2.4.2  nathanw 		IOMD_WRITE_WORD(IOMD_SD0ENDB, pa2|FLAGS);
    540  1.2.4.2  nathanw 		IOMD_WRITE_WORD(IOMD_SD0CURA, pa1);
    541  1.2.4.2  nathanw 		IOMD_WRITE_WORD(IOMD_SD0ENDA, pa2|FLAGS);
    542  1.2.4.2  nathanw 
    543  1.2.4.2  nathanw 		ag.in_progress = 1;
    544  1.2.4.2  nathanw 
    545  1.2.4.2  nathanw 		ag.next_cur = ag.next_end = 0;
    546  1.2.4.2  nathanw 		ag.next_intr = ag.next_arg = 0;
    547  1.2.4.2  nathanw 
    548  1.2.4.2  nathanw 		ag.intr = intr;
    549  1.2.4.2  nathanw 		ag.arg = arg;
    550  1.2.4.2  nathanw 
    551  1.2.4.2  nathanw 		/*
    552  1.2.4.2  nathanw 		 * The driver 'clicks' between buffer swaps, leading me
    553  1.2.4.2  nathanw 		 * to think  that the fifo is much small than on other
    554  1.2.4.2  nathanw 		 * sound cards so I'm going to have to do some tricks here
    555  1.2.4.2  nathanw 		 */
    556  1.2.4.2  nathanw 
    557  1.2.4.2  nathanw 		(*ag.intr)(ag.arg);			/* Schedule the next buffer */
    558  1.2.4.2  nathanw 		ag.intr = vidcaudio_dummy_routine;	/* Already done this        */
    559  1.2.4.2  nathanw 		ag.arg = NULL;
    560  1.2.4.2  nathanw 
    561  1.2.4.2  nathanw #ifdef PRINT
    562  1.2.4.2  nathanw 		printf("vidcaudio: start output\n");
    563  1.2.4.2  nathanw #endif
    564  1.2.4.2  nathanw #ifdef DEBUG
    565  1.2.4.2  nathanw 		printf("SE");
    566  1.2.4.2  nathanw #endif
    567  1.2.4.2  nathanw 		enable_irq(sound_dma_intr);
    568  1.2.4.2  nathanw 	} else {
    569  1.2.4.2  nathanw 		/* Otherwise schedule the next one */
    570  1.2.4.2  nathanw 		if (ag.next_cur != 0) {
    571  1.2.4.2  nathanw 			/* If there's one scheduled then complain */
    572  1.2.4.2  nathanw 			printf("vidcaudio: Buffer already Q'ed\n");
    573  1.2.4.2  nathanw 			return EIO;
    574  1.2.4.2  nathanw 		} else {
    575  1.2.4.2  nathanw 			/* We're OK to schedule it now */
    576  1.2.4.2  nathanw 			ag.buffer = (++ag.buffer) & 1;
    577  1.2.4.2  nathanw 			PHYS(cur, &ag.next_cur);
    578  1.2.4.2  nathanw 			PHYS(end - 16, &ag.next_end);
    579  1.2.4.2  nathanw 			ag.next_intr = intr;
    580  1.2.4.2  nathanw 			ag.next_arg = arg;
    581  1.2.4.2  nathanw #ifdef DEBUG
    582  1.2.4.2  nathanw 			printf("s");
    583  1.2.4.2  nathanw #endif
    584  1.2.4.2  nathanw 		}
    585  1.2.4.2  nathanw 	}
    586  1.2.4.2  nathanw 	return 0;
    587  1.2.4.2  nathanw }
    588  1.2.4.2  nathanw 
    589  1.2.4.2  nathanw void
    590  1.2.4.2  nathanw vidcaudio_shutdown(void)
    591  1.2.4.2  nathanw {
    592  1.2.4.2  nathanw 	/* Shut down the channel */
    593  1.2.4.2  nathanw 	ag.intr = NULL;
    594  1.2.4.2  nathanw 	ag.in_progress = 0;
    595  1.2.4.2  nathanw #ifdef PRINT
    596  1.2.4.2  nathanw 	printf("vidcaudio: stop output\n");
    597  1.2.4.2  nathanw #endif
    598  1.2.4.2  nathanw 	IOMD_WRITE_WORD(IOMD_SD0CURB, ag.silence);
    599  1.2.4.2  nathanw 	IOMD_WRITE_WORD(IOMD_SD0ENDB, (ag.silence + NBPG - 16) | (1<<30));
    600  1.2.4.2  nathanw 	disable_irq(sound_dma_intr);
    601  1.2.4.2  nathanw }
    602  1.2.4.2  nathanw 
    603  1.2.4.2  nathanw int
    604  1.2.4.2  nathanw vidcaudio_intr(arg)
    605  1.2.4.2  nathanw 	void *arg;
    606  1.2.4.2  nathanw {
    607  1.2.4.2  nathanw 	int status = IOMD_READ_BYTE(IOMD_SD0ST);
    608  1.2.4.3  nathanw 	void (*nintr)(void *);
    609  1.2.4.2  nathanw 	void *narg;
    610  1.2.4.3  nathanw 	void (*xintr)(void *);
    611  1.2.4.2  nathanw 	void *xarg;
    612  1.2.4.2  nathanw 	int xcur, xend;
    613  1.2.4.2  nathanw 	IOMD_WRITE_WORD(IOMD_DMARQ, 0x10);
    614  1.2.4.2  nathanw 
    615  1.2.4.2  nathanw #ifdef PRINT
    616  1.2.4.2  nathanw 	printf ( "I" );
    617  1.2.4.2  nathanw #endif
    618  1.2.4.2  nathanw 
    619  1.2.4.2  nathanw 	if (ag.open == 0) {
    620  1.2.4.2  nathanw 		vidcaudio_shutdown();
    621  1.2.4.2  nathanw 		return 0;
    622  1.2.4.2  nathanw 	}
    623  1.2.4.2  nathanw 
    624  1.2.4.2  nathanw 	/* Have I got the generic audio device attached */
    625  1.2.4.2  nathanw 
    626  1.2.4.2  nathanw #ifdef DEBUG
    627  1.2.4.2  nathanw 	printf ( "[B%01x]", status );
    628  1.2.4.2  nathanw #endif
    629  1.2.4.2  nathanw 
    630  1.2.4.2  nathanw 	nintr = ag.intr;
    631  1.2.4.2  nathanw 	narg = ag.arg;
    632  1.2.4.2  nathanw 	ag.intr = NULL;
    633  1.2.4.2  nathanw 
    634  1.2.4.2  nathanw 	xintr = ag.next_intr;
    635  1.2.4.2  nathanw 	xarg = ag.next_arg;
    636  1.2.4.2  nathanw 	xcur = ag.next_cur;
    637  1.2.4.2  nathanw 	xend = ag.next_end;
    638  1.2.4.2  nathanw 	ag.next_cur = 0;
    639  1.2.4.2  nathanw 	ag.intr = xintr;
    640  1.2.4.2  nathanw 	ag.arg = xarg;
    641  1.2.4.2  nathanw 
    642  1.2.4.2  nathanw 	if (nintr) {
    643  1.2.4.2  nathanw #ifdef DEBUG
    644  1.2.4.2  nathanw 		printf("i");
    645  1.2.4.2  nathanw #endif
    646  1.2.4.2  nathanw 		(*nintr)(narg);
    647  1.2.4.2  nathanw 	}
    648  1.2.4.2  nathanw 
    649  1.2.4.2  nathanw 	if (xcur == 0) {
    650  1.2.4.2  nathanw 		vidcaudio_shutdown ();
    651  1.2.4.2  nathanw 	} else {
    652  1.2.4.2  nathanw #define OVERRUN 	(0x04)
    653  1.2.4.2  nathanw #define INTERRUPT	(0x02)
    654  1.2.4.2  nathanw #define BANK_A		(0x00)
    655  1.2.4.2  nathanw #define BANK_B		(0x01)
    656  1.2.4.2  nathanw 		switch (status & 0x7) {
    657  1.2.4.2  nathanw 		case (INTERRUPT|BANK_A):
    658  1.2.4.2  nathanw #ifdef PRINT
    659  1.2.4.2  nathanw 			printf("B");
    660  1.2.4.2  nathanw #endif
    661  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
    662  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
    663  1.2.4.2  nathanw 			break;
    664  1.2.4.2  nathanw 
    665  1.2.4.2  nathanw 		case (INTERRUPT|BANK_B):
    666  1.2.4.2  nathanw #ifdef PRINT
    667  1.2.4.2  nathanw 			printf("A");
    668  1.2.4.2  nathanw #endif
    669  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
    670  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
    671  1.2.4.2  nathanw 			break;
    672  1.2.4.2  nathanw 
    673  1.2.4.2  nathanw 		case (OVERRUN|INTERRUPT|BANK_A):
    674  1.2.4.2  nathanw #ifdef PRINT
    675  1.2.4.2  nathanw 			printf("A");
    676  1.2.4.2  nathanw #endif
    677  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0CURA, xcur);
    678  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0ENDA, xend|FLAGS);
    679  1.2.4.2  nathanw 			break;
    680  1.2.4.2  nathanw 
    681  1.2.4.2  nathanw 		case (OVERRUN|INTERRUPT|BANK_B):
    682  1.2.4.2  nathanw #ifdef PRINT
    683  1.2.4.2  nathanw 			printf("B");
    684  1.2.4.2  nathanw #endif
    685  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0CURB, xcur);
    686  1.2.4.2  nathanw 			IOMD_WRITE_WORD(IOMD_SD0ENDB, xend|FLAGS);
    687  1.2.4.2  nathanw 			break;
    688  1.2.4.2  nathanw 		}
    689  1.2.4.2  nathanw /*
    690  1.2.4.2  nathanw 	ag.next_cur = 0;
    691  1.2.4.2  nathanw 	ag.intr = xintr;
    692  1.2.4.2  nathanw 	ag.arg = xarg;
    693  1.2.4.2  nathanw */
    694  1.2.4.2  nathanw 	}
    695  1.2.4.2  nathanw #ifdef PRINT
    696  1.2.4.2  nathanw 	printf ( "i" );
    697  1.2.4.2  nathanw #endif
    698  1.2.4.2  nathanw 
    699  1.2.4.2  nathanw 	if (ag.next_cur == 0) {
    700  1.2.4.2  nathanw 		(*ag.intr)(ag.arg);			/* Schedule the next buffer */
    701  1.2.4.2  nathanw 		ag.intr = vidcaudio_dummy_routine;	/* Already done this        */
    702  1.2.4.2  nathanw 		ag.arg = NULL;
    703  1.2.4.2  nathanw 	}
    704  1.2.4.2  nathanw 	return(0);	/* Pass interrupt on down the chain */
    705  1.2.4.2  nathanw }
    706