Home | History | Annotate | Line # | Download | only in vsa
      1 /*	$NetBSD: vsaudio.c,v 1.7 2020/09/12 05:19:16 isaki Exp $	*/
      2 /*	$OpenBSD: vsaudio.c,v 1.4 2013/05/15 21:21:11 ratchov Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2011 Miodrag Vallat.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 /*
     20  * Copyright (c) 1995 Rolf Grossmann
     21  * All rights reserved.
     22  *
     23  * Redistribution and use in source and binary forms, with or without
     24  * modification, are permitted provided that the following conditions
     25  * are met:
     26  * 1. Redistributions of source code must retain the above copyright
     27  *    notice, this list of conditions and the following disclaimer.
     28  * 2. Redistributions in binary form must reproduce the above copyright
     29  *    notice, this list of conditions and the following disclaimer in the
     30  *    documentation and/or other materials provided with the distribution.
     31  * 3. All advertising materials mentioning features or use of this software
     32  *    must display the following acknowledgement:
     33  *      This product includes software developed by Rolf Grossmann.
     34  * 4. The name of the author may not be used to endorse or promote products
     35  *    derived from this software without specific prior written permission
     36  *
     37  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     38  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     40  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     41  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     43  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     44  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     45  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     46  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     47  */
     48 
     49 /*
     50  * Audio backend for the VAXstation 4000 AMD79C30 audio chip.
     51  * Currently working in pseudo-DMA mode; DMA operation may be possible and
     52  * needs to be investigated.
     53  */
     54 /*
     55  * Although he did not claim copyright for his work, this code owes a lot
     56  * to Blaz Antonic <blaz.antonic (at) siol.net> who figured out a working
     57  * interrupt triggering routine in vsaudio_match().
     58  */
     59 /*
     60  * Ported to NetBSD, from OpenBSD, by Bjrn Johannesson (rherdware (at) yahoo.com)
     61  * in December 2014
     62  */
     63 
     64 #include "audio.h"
     65 #if NAUDIO > 0
     66 
     67 #include <sys/errno.h>
     68 #include <sys/evcnt.h>
     69 #include <sys/intr.h>
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/device.h>
     73 
     74 #include <machine/cpu.h>
     75 #include <machine/sid.h>
     76 #include <machine/scb.h>
     77 #include <machine/vsbus.h>
     78 
     79 #include <sys/audioio.h>
     80 #include <dev/audio/audio_if.h>
     81 
     82 #include <dev/ic/am7930reg.h>
     83 #include <dev/ic/am7930var.h>
     84 
     85 /* physical addresses of the AM79C30 chip */
     86 #define VSAUDIO_CSR			0x200d0000
     87 #define VSAUDIO_CSR_KA49		0x26800000
     88 
     89 struct vsaudio_softc {
     90 	struct am7930_softc sc_am7930;	/* glue to MI code */
     91 
     92 	bus_space_tag_t sc_bt;		/* bus cookie */
     93 	bus_space_handle_t sc_bh;	/* device registers */
     94 };
     95 
     96 static int vsaudio_match(struct device *parent, struct cfdata *match, void *);
     97 static void vsaudio_attach(device_t parent, device_t self, void *);
     98 
     99 static void vsaudio_hwintr(void *);
    100 
    101 CFATTACH_DECL_NEW(vsaudio, sizeof(struct vsaudio_softc), vsaudio_match,
    102 		vsaudio_attach, NULL, NULL);
    103 
    104 /*
    105  * Hardware access routines for the MI code
    106  */
    107 uint8_t	vsaudio_codec_dread(struct am7930_softc *, int);
    108 void	vsaudio_codec_dwrite(struct am7930_softc *, int, uint8_t);
    109 
    110 struct am7930_glue vsaudio_glue = {
    111 	vsaudio_codec_dread,
    112 	vsaudio_codec_dwrite,
    113 };
    114 
    115 /*
    116  * Interface to the MI audio layer.
    117  */
    118 int	vsaudio_getdev(void *, struct audio_device *);
    119 
    120 struct audio_hw_if vsaudio_hw_if = {
    121 	.query_format		= am7930_query_format,
    122 	.set_format		= am7930_set_format,
    123 	.commit_settings	= am7930_commit_settings,
    124 	.trigger_output		= am7930_trigger_output,
    125 	.trigger_input		= am7930_trigger_input,
    126 	.halt_output		= am7930_halt_output,
    127 	.halt_input		= am7930_halt_input,
    128 	.getdev			= vsaudio_getdev,
    129 	.set_port		= am7930_set_port,
    130 	.get_port		= am7930_get_port,
    131 	.query_devinfo		= am7930_query_devinfo,
    132 	.get_props		= am7930_get_props,
    133 	.get_locks		= am7930_get_locks,
    134 };
    135 
    136 
    137 struct audio_device vsaudio_device = {
    138 	"am7930",
    139 	"x",
    140 	"vsaudio"
    141 };
    142 
    143 
    144 static int
    145 vsaudio_match(struct device *parent, struct cfdata *match, void *aux)
    146 {
    147 	struct vsbus_attach_args *va = aux;
    148 	volatile uint32_t *regs;
    149 	int i;
    150 
    151 	switch (vax_boardtype) {
    152 #if defined(VAX_BTYP_46) || defined(VAX_BTYP_48)
    153 	case VAX_BTYP_46:
    154 	case VAX_BTYP_48:
    155 		if (va->va_paddr != VSAUDIO_CSR)
    156 			return 0;
    157 		break;
    158 #endif
    159 #if defined(VAX_BTYP_49)
    160 	case VAX_BTYP_49:
    161 		if (va->va_paddr != VSAUDIO_CSR_KA49)
    162 			return 0;
    163 		break;
    164 #endif
    165 	default:
    166 		return 0;
    167 	}
    168 
    169 	regs = (volatile uint32_t *)va->va_addr;
    170 	regs[AM7930_DREG_CR] = AM7930_IREG_INIT;
    171 	regs[AM7930_DREG_DR] = AM7930_INIT_PMS_ACTIVE | AM7930_INIT_INT_ENABLE;
    172 
    173 	regs[AM7930_DREG_CR] = AM7930_IREG_MUX_MCR1;
    174 	regs[AM7930_DREG_DR] = 0;
    175 
    176 	regs[AM7930_DREG_CR] = AM7930_IREG_MUX_MCR2;
    177 	regs[AM7930_DREG_DR] = 0;
    178 
    179 	regs[AM7930_DREG_CR] = AM7930_IREG_MUX_MCR3;
    180 	regs[AM7930_DREG_DR] = (AM7930_MCRCHAN_BB << 4) | AM7930_MCRCHAN_BA;
    181 
    182 	regs[AM7930_DREG_CR] = AM7930_IREG_MUX_MCR4;
    183 	regs[AM7930_DREG_DR] = AM7930_MCR4_INT_ENABLE;
    184 
    185 	for (i = 10; i < 20; i++)
    186 		regs[AM7930_DREG_BBTB] = i;
    187 	delay(1000000); /* XXX too large */
    188 
    189 	return 1;
    190 }
    191 
    192 
    193 static void
    194 vsaudio_attach(device_t parent, device_t self, void *aux)
    195 {
    196 	struct vsbus_attach_args *va = aux;
    197 	struct vsaudio_softc *sc = device_private(self);
    198 	struct am7930_softc *amsc = &sc->sc_am7930;
    199 
    200 	if (bus_space_map(va->va_memt, va->va_paddr, AM7930_DREG_SIZE << 2, 0,
    201 	    &sc->sc_bh) != 0) {
    202 		printf(": can't map registers\n");
    203 		return;
    204 	}
    205 	sc->sc_bt = va->va_memt;
    206 	amsc->sc_dev = self;
    207 	amsc->sc_glue = &vsaudio_glue;
    208 	am7930_init(amsc, AUDIOAMD_POLL_MODE);
    209 	scb_vecalloc(va->va_cvec, vsaudio_hwintr, amsc, SCB_ISTACK,
    210 	    &amsc->sc_intrcnt);
    211 	evcnt_attach_dynamic(&amsc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    212 	    device_xname(self), "intr");
    213 
    214 	aprint_normal("\n");
    215 	audio_attach_mi(&vsaudio_hw_if, sc, self);
    216 }
    217 
    218 static void
    219 vsaudio_hwintr(void *arg)
    220 {
    221 
    222 	am7930_hwintr(arg);
    223 }
    224 
    225 /* direct read */
    226 uint8_t
    227 vsaudio_codec_dread(struct am7930_softc *amsc, int reg)
    228 {
    229 	struct vsaudio_softc *sc = (struct vsaudio_softc *)amsc;
    230 
    231 	return bus_space_read_1(sc->sc_bt, sc->sc_bh, reg << 2);
    232 }
    233 
    234 /* direct write */
    235 void
    236 vsaudio_codec_dwrite(struct am7930_softc *amsc, int reg, uint8_t val)
    237 {
    238 	struct vsaudio_softc *sc = (struct vsaudio_softc *)amsc;
    239 
    240 	bus_space_write_1(sc->sc_bt, sc->sc_bh, reg << 2, val);
    241 }
    242 
    243 int
    244 vsaudio_getdev(void *addr, struct audio_device *retp)
    245 {
    246 
    247 	*retp = vsaudio_device;
    248 	return 0;
    249 }
    250 
    251 #endif /* NAUDIO > 0 */
    252