Home | History | Annotate | Line # | Download | only in dev
audioamd.c revision 1.12
      1 /*	$NetBSD: audioamd.c,v 1.12 2002/10/02 16:02:12 thorpej Exp $	*/
      2 /*	NetBSD: am7930_sparc.c,v 1.44 1999/03/14 22:29:00 jonathan Exp 	*/
      3 
      4 /*
      5  * Copyright (c) 1995 Rolf Grossmann
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Rolf Grossmann.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include "audio.h"
     35 #if NAUDIO > 0
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/errno.h>
     40 #include <sys/device.h>
     41 
     42 #include <machine/bus.h>
     43 #include <machine/intr.h>
     44 #include <machine/autoconf.h>
     45 
     46 #include <sys/audioio.h>
     47 #include <dev/audio_if.h>
     48 
     49 #include <dev/ic/am7930reg.h>
     50 #include <dev/ic/am7930var.h>
     51 #include <sparc/dev/audioamdvar.h>
     52 
     53 #define AUDIO_ROM_NAME "audio"
     54 
     55 #ifdef AUDIO_DEBUG
     56 #define DPRINTF(x)      if (am7930debug) printf x
     57 #define DPRINTFN(n,x)   if (am7930debug>(n)) printf x
     58 #else
     59 #define DPRINTF(x)
     60 #define DPRINTFN(n,x)
     61 #endif	/* AUDIO_DEBUG */
     62 
     63 
     64 /* interrupt interfaces */
     65 #ifdef AUDIO_C_HANDLER
     66 int	am7930hwintr __P((void *));
     67 #if defined(SUN4M)
     68 #define AUDIO_SET_SWINTR do {		\
     69 	if (CPU_ISSUN4M)		\
     70 		raise(0, 4);		\
     71 	else				\
     72 		ienab_bis(IE_L4);	\
     73 } while(0);
     74 #else
     75 #define AUDIO_SET_SWINTR ienab_bis(IE_L4)
     76 #endif /* defined(SUN4M) */
     77 #else
     78 struct auio *auiop;
     79 #endif /* AUDIO_C_HANDLER */
     80 int	am7930swintr __P((void *));
     81 
     82 /*
     83  * interrupt-handler status
     84  */
     85 struct am7930_intrhand {
     86 	int	(*ih_fun) __P((void *));
     87 	void	*ih_arg;
     88 };
     89 
     90 struct audioamd_softc {
     91 	struct am7930_softc sc_am7930;	/* glue to MI code */
     92 
     93 	bus_space_tag_t sc_bt;		/* bus cookie */
     94 	bus_space_handle_t sc_bh;	/* device registers */
     95 
     96 	struct am7930_intrhand	sc_ih;	/* interrupt vector (hw or sw)  */
     97 	void	(*sc_rintr)(void*);	/* input completion intr handler */
     98 	void	*sc_rarg;		/* arg for sc_rintr() */
     99 	void	(*sc_pintr)(void*);	/* output completion intr handler */
    100 	void	*sc_parg;		/* arg for sc_pintr() */
    101 
    102 	/* sc_au is special in that the hardware interrupt handler uses it */
    103 	struct  auio sc_au;		/* recv and xmit buffers, etc */
    104 #define sc_intrcnt	sc_au.au_intrcnt	/* statistics */
    105 };
    106 
    107 void	audioamd_mainbus_attach __P((struct device *,
    108 		struct device *, void *));
    109 int	audioamd_mainbus_match __P((struct device *, struct cfdata *, void *));
    110 void	audioamd_sbus_attach __P((struct device *, struct device *, void *));
    111 int	audioamd_sbus_match __P((struct device *, struct cfdata *, void *));
    112 void	audioamd_attach(struct audioamd_softc *sc, int);
    113 
    114 CFATTACH_DECL(audioamd_mainbus, sizeof(struct audioamd_softc),
    115     audioamd_mainbus_match, audioamd_mainbus_attach, NULL, NULL);
    116 
    117 CFATTACH_DECL(audioamd_sbus, sizeof(struct audioamd_softc),
    118     audioamd_sbus_match, audioamd_sbus_attach, NULL, NULL);
    119 
    120 /*
    121  * Define our interface into the am7930 MI driver.
    122  */
    123 
    124 u_int8_t	audioamd_codec_iread __P((struct am7930_softc *, int));
    125 u_int16_t	audioamd_codec_iread16 __P((struct am7930_softc *, int));
    126 u_int8_t	audioamd_codec_dread __P((struct audioamd_softc *, int));
    127 void	audioamd_codec_iwrite __P((struct am7930_softc *, int, u_int8_t));
    128 void	audioamd_codec_iwrite16 __P((struct am7930_softc *, int, u_int16_t));
    129 void	audioamd_codec_dwrite __P((struct audioamd_softc *, int, u_int8_t));
    130 void	audioamd_onopen __P((struct am7930_softc *sc));
    131 void	audioamd_onclose __P((struct am7930_softc *sc));
    132 
    133 struct am7930_glue audioamd_glue = {
    134 	audioamd_codec_iread,
    135 	audioamd_codec_iwrite,
    136 	audioamd_codec_iread16,
    137 	audioamd_codec_iwrite16,
    138 	audioamd_onopen,
    139 	audioamd_onclose,
    140 	0,
    141 	0,
    142 	0,
    143 };
    144 
    145 /*
    146  * Define our interface to the higher level audio driver.
    147  */
    148 int	audioamd_start_output __P((void *, void *, int, void (*)(void *),
    149 				  void *));
    150 int	audioamd_start_input __P((void *, void *, int, void (*)(void *),
    151 				 void *));
    152 int	audioamd_getdev __P((void *, struct audio_device *));
    153 
    154 struct audio_hw_if sa_hw_if = {
    155 	am7930_open,
    156 	am7930_close,
    157 	0,
    158 	am7930_query_encoding,
    159 	am7930_set_params,
    160 	am7930_round_blocksize,
    161 	am7930_commit_settings,
    162 	0,
    163 	0,
    164 	audioamd_start_output,		/* md */
    165 	audioamd_start_input,		/* md */
    166 	am7930_halt_output,
    167 	am7930_halt_input,
    168 	0,
    169 	audioamd_getdev,
    170 	0,
    171 	am7930_set_port,
    172 	am7930_get_port,
    173 	am7930_query_devinfo,
    174 	0,
    175 	0,
    176 	0,
    177         0,
    178 	am7930_get_props,
    179 	0,
    180 	0,
    181         0,
    182 };
    183 
    184 struct audio_device audioamd_device = {
    185 	"am7930",
    186 	"x",
    187 	"audioamd"
    188 };
    189 
    190 
    191 int
    192 audioamd_mainbus_match(parent, cf, aux)
    193 	struct device *parent;
    194 	struct cfdata *cf;
    195 	void *aux;
    196 {
    197 	struct mainbus_attach_args *ma = aux;
    198 
    199 	if (CPU_ISSUN4)
    200 		return (0);
    201 	return (strcmp(AUDIO_ROM_NAME, ma->ma_name) == 0);
    202 }
    203 
    204 int
    205 audioamd_sbus_match(parent, cf, aux)
    206 	struct device *parent;
    207 	struct cfdata *cf;
    208 	void *aux;
    209 {
    210 	struct sbus_attach_args *sa = aux;
    211 
    212 	return (strcmp(AUDIO_ROM_NAME, sa->sa_name) == 0);
    213 }
    214 
    215 void
    216 audioamd_mainbus_attach(parent, self, aux)
    217 	struct device *parent, *self;
    218 	void *aux;
    219 {
    220 	struct mainbus_attach_args *ma = aux;
    221 	struct audioamd_softc *sc = (struct audioamd_softc *)self;
    222 	bus_space_handle_t bh;
    223 
    224 	sc->sc_bt = ma->ma_bustag;
    225 
    226 	if (bus_space_map(
    227 			ma->ma_bustag,
    228 			ma->ma_paddr,
    229 			AM7930_DREG_SIZE,
    230 			BUS_SPACE_MAP_LINEAR,
    231 			&bh) != 0) {
    232 		printf("%s: cannot map registers\n", self->dv_xname);
    233 		return;
    234 	}
    235 	sc->sc_bh = bh;
    236 	audioamd_attach(sc, ma->ma_pri);
    237 }
    238 
    239 
    240 void
    241 audioamd_sbus_attach(parent, self, aux)
    242 	struct device *parent, *self;
    243 	void *aux;
    244 {
    245 	struct sbus_attach_args *sa = aux;
    246 	struct audioamd_softc *sc = (struct audioamd_softc *)self;
    247 	bus_space_handle_t bh;
    248 
    249 	sc->sc_bt = sa->sa_bustag;
    250 
    251 	if (sbus_bus_map(sa->sa_bustag,
    252 			 sa->sa_slot, sa->sa_offset,
    253 			 AM7930_DREG_SIZE,
    254 			 0, &bh) != 0) {
    255 		printf("%s: cannot map registers\n", self->dv_xname);
    256 		return;
    257 	}
    258 	sc->sc_bh = bh;
    259 	audioamd_attach(sc, sa->sa_pri);
    260 }
    261 
    262 void
    263 audioamd_attach(sc, pri)
    264 	struct audioamd_softc *sc;
    265 	int pri;
    266 {
    267 
    268 	printf(" softpri %d\n", PIL_AUSOFT);
    269 
    270 	/*
    271 	 * Set up glue for MI code early; we use some of it here.
    272 	 */
    273 	sc->sc_am7930.sc_glue = &audioamd_glue;
    274 
    275 	am7930_init(&sc->sc_am7930, AUDIOAMD_POLL_MODE);
    276 
    277 #ifndef AUDIO_C_HANDLER
    278 	auiop = &sc->sc_au;
    279 	(void)bus_intr_establish(sc->sc_bt, pri, IPL_AUDIO,
    280 				 BUS_INTR_ESTABLISH_FASTTRAP,
    281 				 (int (*) __P((void *)))amd7930_trap, NULL);
    282 #else
    283 	(void)bus_intr_establish(sc->sc_bt, pri, IPL_AUDIO, 0,
    284 				 am7930hwintr, sc);
    285 #endif
    286 	(void)bus_intr_establish(sc->sc_bt, PIL_AUSOFT, IPL_AUDIO,
    287 				 BUS_INTR_ESTABLISH_SOFTINTR,
    288 				 am7930swintr, sc);
    289 
    290 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    291 	    sc->sc_am7930.sc_dev.dv_xname, "intr");
    292 
    293 	audio_attach_mi(&sa_hw_if, sc, &sc->sc_am7930.sc_dev);
    294 }
    295 
    296 
    297 void
    298 audioamd_onopen(sc)
    299 	struct am7930_softc *sc;
    300 {
    301 	struct audioamd_softc *mdsc = (struct audioamd_softc *)sc;
    302 
    303 	/* reset pdma state */
    304 	mdsc->sc_rintr = 0;
    305 	mdsc->sc_rarg = 0;
    306 	mdsc->sc_pintr = 0;
    307 	mdsc->sc_parg = 0;
    308 
    309 	mdsc->sc_au.au_rdata = 0;
    310 	mdsc->sc_au.au_pdata = 0;
    311 }
    312 
    313 
    314 void
    315 audioamd_onclose(sc)
    316 	struct am7930_softc *sc;
    317 {
    318 	/* On sparc, just do the chipset-level halt. */
    319 	am7930_halt_input(sc);
    320 	am7930_halt_output(sc);
    321 }
    322 
    323 int
    324 audioamd_start_output(addr, p, cc, intr, arg)
    325 	void *addr;
    326 	void *p;
    327 	int cc;
    328 	void (*intr) __P((void *));
    329 	void *arg;
    330 {
    331 	struct audioamd_softc *sc = addr;
    332 
    333 	DPRINTFN(1, ("sa_start_output: cc=%d %p (%p)\n", cc, intr, arg));
    334 
    335 	if (!sc->sc_am7930.sc_locked) {
    336 		audioamd_codec_iwrite(&sc->sc_am7930,
    337 			AM7930_IREG_INIT, AM7930_INIT_PMS_ACTIVE);
    338 		sc->sc_am7930.sc_locked = 1;
    339 		DPRINTF(("sa_start_output: started intrs.\n"));
    340 	}
    341 	sc->sc_pintr = intr;
    342 	sc->sc_parg = arg;
    343 #ifndef AUDIO_C_HANDLER
    344 	sc->sc_au.au_bt = sc->sc_bt;
    345 	sc->sc_au.au_bh = sc->sc_bh;
    346 #endif
    347 	sc->sc_au.au_pdata = p;
    348 	sc->sc_au.au_pend = (char *)p + cc - 1;
    349 	return(0);
    350 }
    351 
    352 int
    353 audioamd_start_input(addr, p, cc, intr, arg)
    354 	void *addr;
    355 	void *p;
    356 	int cc;
    357 	void (*intr) __P((void *));
    358 	void *arg;
    359 {
    360 	struct audioamd_softc *sc = addr;
    361 
    362 	DPRINTFN(1, ("sa_start_input: cc=%d %p (%p)\n", cc, intr, arg));
    363 
    364 	if (!sc->sc_am7930.sc_locked) {
    365 		audioamd_codec_iwrite(&sc->sc_am7930,
    366 			AM7930_IREG_INIT, AM7930_INIT_PMS_ACTIVE);
    367 		sc->sc_am7930.sc_locked = 1;
    368 		DPRINTF(("sa_start_input: started intrs.\n"));
    369 	}
    370 	sc->sc_rintr = intr;
    371 	sc->sc_rarg = arg;
    372 #ifndef AUDIO_C_HANDLER
    373 	sc->sc_au.au_bt = sc->sc_bt;
    374 	sc->sc_au.au_bh = sc->sc_bh;
    375 #endif
    376 	sc->sc_au.au_rdata = p;
    377 	sc->sc_au.au_rend = (char *)p + cc -1;
    378 	return(0);
    379 }
    380 
    381 
    382 /*
    383  * Pseudo-DMA support: either C or locore assember.
    384  */
    385 
    386 #ifdef AUDIO_C_HANDLER
    387 int
    388 am7930hwintr(v)
    389 	void *v;
    390 {
    391 	struct audioamd_softc *sc = v;
    392 	struct auio *au = &sc->sc_au;
    393 	u_int8_t *d, *e;
    394 	int k;
    395 
    396 	/* clear interrupt */
    397 	k = audioamd_codec_dread(sc, AM7930_DREG_IR);
    398 
    399 	/* receive incoming data */
    400 	d = au->au_rdata;
    401 	e = au->au_rend;
    402 	if (d && d <= e) {
    403 		*d = audioamd_codec_dread(sc, AM7930_DREG_BBRB);
    404 		au->au_rdata++;
    405 		if (d == e) {
    406 			DPRINTFN(1, ("am7930hwintr: swintr(r) requested"));
    407 			AUDIO_SET_SWINTR;
    408 		}
    409 	}
    410 
    411 	/* send outgoing data */
    412 	d = au->au_pdata;
    413 	e = au->au_pend;
    414 	if (d && d <= e) {
    415 		audioamd_codec_dwrite(sc, AM7930_DREG_BBTB, *d);
    416 		au->au_pdata++;
    417 		if (d == e) {
    418 			DPRINTFN(1, ("am7930hwintr: swintr(p) requested"));
    419 			AUDIO_SET_SWINTR;
    420 		}
    421 	}
    422 
    423 	au->au_intrcnt.ev_count++;
    424 	return (1);
    425 }
    426 #endif /* AUDIO_C_HANDLER */
    427 
    428 int
    429 am7930swintr(sc0)
    430 	void *sc0;
    431 {
    432 	struct audioamd_softc *sc = sc0;
    433 	struct auio *au;
    434 	int s, ret = 0;
    435 
    436 	DPRINTFN(1, ("audiointr: sc=%p\n", sc););
    437 
    438 	au = &sc->sc_au;
    439 	s = splaudio();
    440 	if (au->au_rdata > au->au_rend && sc->sc_rintr != NULL) {
    441 		splx(s);
    442 		ret = 1;
    443 		(*sc->sc_rintr)(sc->sc_rarg);
    444 		s = splaudio();
    445 	}
    446 	if (au->au_pdata > au->au_pend && sc->sc_pintr != NULL) {
    447 		splx(s);
    448 		ret = 1;
    449 		(*sc->sc_pintr)(sc->sc_parg);
    450 	} else
    451 		splx(s);
    452 	return (ret);
    453 }
    454 
    455 
    456 /* indirect write */
    457 void
    458 audioamd_codec_iwrite(sc, reg, val)
    459 	struct am7930_softc *sc;
    460 	int reg;
    461 	u_int8_t val;
    462 {
    463 	struct audioamd_softc *mdsc = (struct audioamd_softc *)sc;
    464 
    465 	audioamd_codec_dwrite(mdsc, AM7930_DREG_CR, reg);
    466 	audioamd_codec_dwrite(mdsc, AM7930_DREG_DR, val);
    467 }
    468 
    469 void
    470 audioamd_codec_iwrite16(sc, reg, val)
    471 	struct am7930_softc *sc;
    472 	int reg;
    473 	u_int16_t val;
    474 {
    475 	struct audioamd_softc *mdsc = (struct audioamd_softc *)sc;
    476 
    477 	audioamd_codec_dwrite(mdsc, AM7930_DREG_CR, reg);
    478 	audioamd_codec_dwrite(mdsc, AM7930_DREG_DR, val);
    479 	audioamd_codec_dwrite(mdsc, AM7930_DREG_DR, val>>8);
    480 }
    481 
    482 
    483 /* indirect read */
    484 u_int8_t
    485 audioamd_codec_iread(sc, reg)
    486 	struct am7930_softc *sc;
    487 	int reg;
    488 {
    489 	struct audioamd_softc *mdsc = (struct audioamd_softc *)sc;
    490 
    491 	audioamd_codec_dwrite(mdsc, AM7930_DREG_CR, reg);
    492 	return (audioamd_codec_dread(mdsc, AM7930_DREG_DR));
    493 }
    494 
    495 u_int16_t
    496 audioamd_codec_iread16(sc, reg)
    497 	struct am7930_softc *sc;
    498 	int reg;
    499 {
    500 	struct audioamd_softc *mdsc = (struct audioamd_softc *)sc;
    501 	u_int8_t lo, hi;
    502 
    503 	audioamd_codec_dwrite(mdsc, AM7930_DREG_CR, reg);
    504 	lo = audioamd_codec_dread(mdsc, AM7930_DREG_DR);
    505 	hi = audioamd_codec_dread(mdsc, AM7930_DREG_DR);
    506 	return ((hi << 8) | lo);
    507 }
    508 
    509 /* direct read */
    510 u_int8_t
    511 audioamd_codec_dread(sc, reg)
    512 	struct audioamd_softc *sc;
    513 	int reg;
    514 {
    515 	return (bus_space_read_1(sc->sc_bt, sc->sc_bh, reg));
    516 }
    517 
    518 /* direct write */
    519 void
    520 audioamd_codec_dwrite(sc, reg, val)
    521 	struct audioamd_softc *sc;
    522 	int reg;
    523 	u_int8_t val;
    524 {
    525 	bus_space_write_1(sc->sc_bt, sc->sc_bh, reg, val);
    526 }
    527 
    528 int
    529 audioamd_getdev(addr, retp)
    530 	void *addr;
    531 	struct audio_device *retp;
    532 {
    533 
    534 	*retp = audioamd_device;
    535 	return (0);
    536 }
    537 
    538 #endif /* NAUDIO > 0 */
    539