Home | History | Annotate | Line # | Download | only in tc
bba.c revision 1.31
      1 /* $NetBSD: bba.c,v 1.31 2007/03/04 06:02:46 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by the NetBSD
     18  *        Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /* maxine/alpha baseboard audio (bba) */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: bba.c,v 1.31 2007/03/04 06:02:46 christos Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/autoconf.h>
     49 #include <machine/cpu.h>
     50 
     51 #include <sys/audioio.h>
     52 #include <dev/audio_if.h>
     53 #include <dev/auconv.h>
     54 
     55 #include <dev/ic/am7930reg.h>
     56 #include <dev/ic/am7930var.h>
     57 
     58 #include <dev/tc/tcvar.h>
     59 #include <dev/tc/ioasicreg.h>
     60 #include <dev/tc/ioasicvar.h>
     61 
     62 #ifdef AUDIO_DEBUG
     63 #define DPRINTF(x)	if (am7930debug) printf x
     64 #else
     65 #define DPRINTF(x)
     66 #endif  /* AUDIO_DEBUG */
     67 
     68 #define BBA_MAX_DMA_SEGMENTS	16
     69 #define BBA_DMABUF_SIZE		(BBA_MAX_DMA_SEGMENTS*IOASIC_DMA_BLOCKSIZE)
     70 #define BBA_DMABUF_ALIGN	IOASIC_DMA_BLOCKSIZE
     71 #define BBA_DMABUF_BOUNDARY	0
     72 
     73 struct bba_mem {
     74 	struct bba_mem *next;
     75 	bus_addr_t addr;
     76 	bus_size_t size;
     77 	void *kva;
     78 };
     79 
     80 struct bba_dma_state {
     81 	bus_dmamap_t dmam;		/* DMA map */
     82 	int active;
     83 	int curseg;			/* current segment in DMA buffer */
     84 	void (*intr)(void *);		/* higher-level audio handler */
     85 	void *intr_arg;
     86 };
     87 
     88 struct bba_softc {
     89 	struct am7930_softc sc_am7930;		/* glue to MI code */
     90 
     91 	bus_space_tag_t sc_bst;			/* IOASIC bus tag/handle */
     92 	bus_space_handle_t sc_bsh;
     93 	bus_dma_tag_t sc_dmat;
     94 	bus_space_handle_t sc_codec_bsh;	/* codec bus space handle */
     95 
     96 	struct bba_mem *sc_mem_head;		/* list of buffers */
     97 
     98 	struct bba_dma_state sc_tx_dma_state;
     99 	struct bba_dma_state sc_rx_dma_state;
    100 };
    101 
    102 static int	bba_match(struct device *, struct cfdata *, void *);
    103 static void	bba_attach(struct device *, struct device *, void *);
    104 
    105 CFATTACH_DECL(bba, sizeof(struct bba_softc),
    106     bba_match, bba_attach, NULL, NULL);
    107 
    108 /*
    109  * Define our interface into the am7930 MI driver.
    110  */
    111 
    112 static uint8_t	bba_codec_iread(struct am7930_softc *, int);
    113 static uint16_t	bba_codec_iread16(struct am7930_softc *, int);
    114 static void	bba_codec_iwrite(struct am7930_softc *, int, uint8_t);
    115 static void	bba_codec_iwrite16(struct am7930_softc *, int, uint16_t);
    116 static void	bba_onopen(struct am7930_softc *);
    117 static void	bba_onclose(struct am7930_softc *);
    118 
    119 static stream_filter_factory_t bba_output_conv;
    120 static stream_filter_factory_t bba_input_conv;
    121 static int	bba_output_conv_fetch_to(stream_fetcher_t *, audio_stream_t *,
    122 					 int);
    123 static int	bba_input_conv_fetch_to(stream_fetcher_t *, audio_stream_t *,
    124 					int);
    125 
    126 struct am7930_glue bba_glue = {
    127 	bba_codec_iread,
    128 	bba_codec_iwrite,
    129 	bba_codec_iread16,
    130 	bba_codec_iwrite16,
    131 	bba_onopen,
    132 	bba_onclose,
    133 	4,
    134 	bba_input_conv,
    135 	bba_output_conv,
    136 };
    137 
    138 /*
    139  * Define our interface to the higher level audio driver.
    140  */
    141 
    142 static int	bba_round_blocksize(void *, int, int, const audio_params_t *);
    143 static int	bba_halt_output(void *);
    144 static int	bba_halt_input(void *);
    145 static int	bba_getdev(void *, struct audio_device *);
    146 static void	*bba_allocm(void *, int, size_t, struct malloc_type *, int);
    147 static void	bba_freem(void *, void *, struct malloc_type *);
    148 static size_t	bba_round_buffersize(void *, int, size_t);
    149 static int	bba_get_props(void *);
    150 static paddr_t	bba_mappage(void *, void *, off_t, int);
    151 static int	bba_trigger_output(void *, void *, void *, int,
    152 				   void (*)(void *), void *,
    153 				   const audio_params_t *);
    154 static int	bba_trigger_input(void *, void *, void *, int,
    155 				  void (*)(void *), void *,
    156 				  const audio_params_t *);
    157 
    158 static const struct audio_hw_if sa_hw_if = {
    159 	am7930_open,
    160 	am7930_close,
    161 	0,
    162 	am7930_query_encoding,
    163 	am7930_set_params,
    164 	bba_round_blocksize,		/* md */
    165 	am7930_commit_settings,
    166 	0,
    167 	0,
    168 	0,
    169 	0,
    170 	bba_halt_output,		/* md */
    171 	bba_halt_input,			/* md */
    172 	0,
    173 	bba_getdev,
    174 	0,
    175 	am7930_set_port,
    176 	am7930_get_port,
    177 	am7930_query_devinfo,
    178 	bba_allocm,			/* md */
    179 	bba_freem,			/* md */
    180 	bba_round_buffersize,		/* md */
    181 	bba_mappage,
    182 	bba_get_props,
    183 	bba_trigger_output,		/* md */
    184 	bba_trigger_input,		/* md */
    185 	0,
    186 };
    187 
    188 static struct audio_device bba_device = {
    189 	"am7930",
    190 	"x",
    191 	"bba"
    192 };
    193 
    194 static int	bba_intr(void *);
    195 static void	bba_reset(struct bba_softc *, int);
    196 static void	bba_codec_dwrite(struct am7930_softc *, int, u_int8_t);
    197 static uint8_t	bba_codec_dread(struct am7930_softc *, int);
    198 
    199 static int
    200 bba_match(struct device *parent, struct cfdata *cf, void *aux)
    201 {
    202 	struct ioasicdev_attach_args *ia;
    203 
    204 	ia = aux;
    205 	if (strcmp(ia->iada_modname, "isdn") != 0 &&
    206 	    strcmp(ia->iada_modname, "AMD79c30") != 0)
    207 		return 0;
    208 
    209 	return 1;
    210 }
    211 
    212 
    213 static void
    214 bba_attach(struct device *parent, struct device *self, void *aux)
    215 {
    216 	struct ioasicdev_attach_args *ia;
    217 	struct bba_softc *sc;
    218 	struct am7930_softc *asc;
    219 	struct ioasic_softc *iosc = device_private(parent);
    220 
    221 	ia = aux;
    222 	sc = device_private(self);
    223 	asc = &sc->sc_am7930;
    224 	sc->sc_bst = iosc->sc_bst;
    225 	sc->sc_bsh = iosc->sc_bsh;
    226 	sc->sc_dmat = iosc->sc_dmat;
    227 
    228 	/* get the bus space handle for codec */
    229 	if (bus_space_subregion(sc->sc_bst, sc->sc_bsh,
    230 	    ia->iada_offset, 0, &sc->sc_codec_bsh)) {
    231 		printf("%s: unable to map device\n", asc->sc_dev.dv_xname);
    232 		return;
    233 	}
    234 
    235 	printf("\n");
    236 
    237 	bba_reset(sc,1);
    238 
    239 	/*
    240 	 * Set up glue for MI code early; we use some of it here.
    241 	 */
    242 	asc->sc_glue = &bba_glue;
    243 
    244 	/*
    245 	 *  MI initialisation.  We will be doing DMA.
    246 	 */
    247 	am7930_init(asc, AUDIOAMD_DMA_MODE);
    248 
    249 	ioasic_intr_establish(parent, ia->iada_cookie, TC_IPL_NONE,
    250 	    bba_intr, sc);
    251 
    252 	audio_attach_mi(&sa_hw_if, asc, &asc->sc_dev);
    253 }
    254 
    255 
    256 static void
    257 bba_onopen(struct am7930_softc *sc)
    258 {
    259 }
    260 
    261 
    262 static void
    263 bba_onclose(struct am7930_softc *sc)
    264 {
    265 }
    266 
    267 
    268 static void
    269 bba_reset(struct bba_softc *sc, int reset)
    270 {
    271 	uint32_t ssr;
    272 
    273 	/* disable any DMA and reset the codec */
    274 	ssr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR);
    275 	ssr &= ~(IOASIC_CSR_DMAEN_ISDN_T | IOASIC_CSR_DMAEN_ISDN_R);
    276 	if (reset)
    277 		ssr &= ~IOASIC_CSR_ISDN_ENABLE;
    278 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    279 	DELAY(10);	/* 400ns required for codec to reset */
    280 
    281 	/* initialise DMA pointers */
    282 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_X_DMAPTR, -1);
    283 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_X_NEXTPTR, -1);
    284 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_R_DMAPTR, -1);
    285 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_R_NEXTPTR, -1);
    286 
    287 	/* take out of reset state */
    288 	if (reset) {
    289 		ssr |= IOASIC_CSR_ISDN_ENABLE;
    290 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    291 	}
    292 
    293 }
    294 
    295 
    296 static void *
    297 bba_allocm(void *addr, int direction, size_t size,
    298 	   struct malloc_type *pool, int flags)
    299 {
    300 	struct am7930_softc *asc;
    301 	struct bba_softc *sc;
    302 	bus_dma_segment_t seg;
    303 	int rseg;
    304 	void *kva;
    305 	struct bba_mem *m;
    306 	int w;
    307 	int state;
    308 
    309 	DPRINTF(("bba_allocm: size = %zu\n", size));
    310 	asc = addr;
    311 	sc = addr;
    312 	state = 0;
    313 	w = (flags & M_NOWAIT) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
    314 
    315 	if (bus_dmamem_alloc(sc->sc_dmat, size, BBA_DMABUF_ALIGN,
    316 	    BBA_DMABUF_BOUNDARY, &seg, 1, &rseg, w)) {
    317 		printf("%s: can't allocate DMA buffer\n",
    318 		    asc->sc_dev.dv_xname);
    319 		goto bad;
    320 	}
    321 	state |= 1;
    322 
    323 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    324 	    &kva, w | BUS_DMA_COHERENT)) {
    325 		printf("%s: can't map DMA buffer\n", asc->sc_dev.dv_xname);
    326 		goto bad;
    327 	}
    328 	state |= 2;
    329 
    330 	m = malloc(sizeof(struct bba_mem), pool, flags);
    331 	if (m == NULL)
    332 		goto bad;
    333 	m->addr = seg.ds_addr;
    334 	m->size = seg.ds_len;
    335 	m->kva = kva;
    336 	m->next = sc->sc_mem_head;
    337 	sc->sc_mem_head = m;
    338 
    339 	return (void *)kva;
    340 
    341 bad:
    342 	if (state & 2)
    343 		bus_dmamem_unmap(sc->sc_dmat, kva, size);
    344 	if (state & 1)
    345 		bus_dmamem_free(sc->sc_dmat, &seg, 1);
    346 	return NULL;
    347 }
    348 
    349 
    350 static void
    351 bba_freem(void *addr, void *ptr, struct malloc_type *pool)
    352 {
    353 	struct bba_softc *sc;
    354 	struct bba_mem **mp, *m;
    355 	bus_dma_segment_t seg;
    356 	void *kva;
    357 
    358 	sc = addr;
    359 	kva = (void *)addr;
    360 	for (mp = &sc->sc_mem_head; *mp && (*mp)->kva != kva;
    361 	    mp = &(*mp)->next)
    362 		continue;
    363 	m = *mp;
    364 	if (m == NULL) {
    365 		printf("bba_freem: freeing unallocated memory\n");
    366 		return;
    367 	}
    368 	*mp = m->next;
    369 	bus_dmamem_unmap(sc->sc_dmat, kva, m->size);
    370 
    371 	seg.ds_addr = m->addr;
    372 	seg.ds_len = m->size;
    373 	bus_dmamem_free(sc->sc_dmat, &seg, 1);
    374 	free(m, pool);
    375 }
    376 
    377 
    378 static size_t
    379 bba_round_buffersize(void *addr, int direction, size_t size)
    380 {
    381 
    382 	DPRINTF(("bba_round_buffersize: size=%zu\n", size));
    383 	return size > BBA_DMABUF_SIZE ? BBA_DMABUF_SIZE :
    384 	    roundup(size, IOASIC_DMA_BLOCKSIZE);
    385 }
    386 
    387 
    388 static int
    389 bba_halt_output(void *addr)
    390 {
    391 	struct bba_softc *sc;
    392 	struct bba_dma_state *d;
    393 	uint32_t ssr;
    394 
    395 	sc = addr;
    396 	d = &sc->sc_tx_dma_state;
    397 	/* disable any DMA */
    398 	ssr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR);
    399 	ssr &= ~IOASIC_CSR_DMAEN_ISDN_T;
    400 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    401 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_X_DMAPTR, -1);
    402 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_X_NEXTPTR, -1);
    403 
    404 	if (d->active) {
    405 		bus_dmamap_unload(sc->sc_dmat, d->dmam);
    406 		bus_dmamap_destroy(sc->sc_dmat, d->dmam);
    407 		d->active = 0;
    408 	}
    409 
    410 	return 0;
    411 }
    412 
    413 
    414 static int
    415 bba_halt_input(void *addr)
    416 {
    417 	struct bba_softc *sc;
    418 	struct bba_dma_state *d;
    419 	uint32_t ssr;
    420 
    421 	sc = addr;
    422 	d = &sc->sc_rx_dma_state;
    423 	/* disable any DMA */
    424 	ssr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR);
    425 	ssr &= ~IOASIC_CSR_DMAEN_ISDN_R;
    426 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    427 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_R_DMAPTR, -1);
    428 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_R_NEXTPTR, -1);
    429 
    430 	if (d->active) {
    431 		bus_dmamap_unload(sc->sc_dmat, d->dmam);
    432 		bus_dmamap_destroy(sc->sc_dmat, d->dmam);
    433 		d->active = 0;
    434 	}
    435 
    436 	return 0;
    437 }
    438 
    439 
    440 static int
    441 bba_getdev(void *addr, struct audio_device *retp)
    442 {
    443 
    444 	*retp = bba_device;
    445 	return 0;
    446 }
    447 
    448 
    449 static int
    450 bba_trigger_output(void *addr, void *start, void *end, int blksize,
    451 		   void (*intr)(void *), void *arg,
    452 		   const audio_params_t *param)
    453 {
    454 	struct bba_softc *sc;
    455 	struct bba_dma_state *d;
    456 	uint32_t ssr;
    457 	tc_addr_t phys, nphys;
    458 	int state;
    459 
    460 	DPRINTF(("bba_trigger_output: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
    461 	    addr, start, end, blksize, intr, arg));
    462 	sc = addr;
    463 	d = &sc->sc_tx_dma_state;
    464 	state = 0;
    465 
    466 	/* disable any DMA */
    467 	ssr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR);
    468 	ssr &= ~IOASIC_CSR_DMAEN_ISDN_T;
    469 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    470 
    471 	if (bus_dmamap_create(sc->sc_dmat, (char *)end - (char *)start,
    472 	    BBA_MAX_DMA_SEGMENTS, IOASIC_DMA_BLOCKSIZE,
    473 	    BBA_DMABUF_BOUNDARY, BUS_DMA_NOWAIT, &d->dmam)) {
    474 		printf("bba_trigger_output: can't create DMA map\n");
    475 		goto bad;
    476 	}
    477 	state |= 1;
    478 
    479 	if (bus_dmamap_load(sc->sc_dmat, d->dmam, start,
    480 	    (char *)end - (char *)start, NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT)) {
    481 	    printf("bba_trigger_output: can't load DMA map\n");
    482 		goto bad;
    483 	}
    484 	state |= 2;
    485 
    486 	d->intr = intr;
    487 	d->intr_arg = arg;
    488 	d->curseg = 1;
    489 
    490 	/* get physical address of buffer start */
    491 	phys = (tc_addr_t)d->dmam->dm_segs[0].ds_addr;
    492 	nphys = (tc_addr_t)d->dmam->dm_segs[1].ds_addr;
    493 
    494 	/* setup DMA pointer */
    495 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_X_DMAPTR,
    496 	    IOASIC_DMA_ADDR(phys));
    497 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_X_NEXTPTR,
    498 	    IOASIC_DMA_ADDR(nphys));
    499 
    500 	/* kick off DMA */
    501 	ssr |= IOASIC_CSR_DMAEN_ISDN_T;
    502 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    503 
    504 	d->active = 1;
    505 
    506 	return 0;
    507 
    508 bad:
    509 	if (state & 2)
    510 		bus_dmamap_unload(sc->sc_dmat, d->dmam);
    511 	if (state & 1)
    512 		bus_dmamap_destroy(sc->sc_dmat, d->dmam);
    513 	return 1;
    514 }
    515 
    516 
    517 static int
    518 bba_trigger_input(void *addr, void *start, void *end, int blksize,
    519 		  void (*intr)(void *), void *arg, const audio_params_t *param)
    520 {
    521 	struct bba_softc *sc;
    522 	struct bba_dma_state *d;
    523 	tc_addr_t phys, nphys;
    524 	u_int32_t ssr;
    525 	int state = 0;
    526 
    527 	DPRINTF(("bba_trigger_input: sc=%p start=%p end=%p blksize=%d intr=%p(%p)\n",
    528 	    addr, start, end, blksize, intr, arg));
    529 	sc = (struct bba_softc *)addr;
    530 	d = &sc->sc_rx_dma_state;
    531 	state = 0;
    532 
    533 	/* disable any DMA */
    534 	ssr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR);
    535 	ssr &= ~IOASIC_CSR_DMAEN_ISDN_R;
    536 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    537 
    538 	if (bus_dmamap_create(sc->sc_dmat, (char *)end - (char *)start,
    539 	    BBA_MAX_DMA_SEGMENTS, IOASIC_DMA_BLOCKSIZE,
    540 	    BBA_DMABUF_BOUNDARY, BUS_DMA_NOWAIT, &d->dmam)) {
    541 		printf("bba_trigger_input: can't create DMA map\n");
    542 		goto bad;
    543 	}
    544 	state |= 1;
    545 
    546 	if (bus_dmamap_load(sc->sc_dmat, d->dmam, start,
    547 	    (char *)end - (char *)start, NULL, BUS_DMA_READ|BUS_DMA_NOWAIT)) {
    548 		printf("bba_trigger_input: can't load DMA map\n");
    549 		goto bad;
    550 	}
    551 	state |= 2;
    552 
    553 	d->intr = intr;
    554 	d->intr_arg = arg;
    555 	d->curseg = 1;
    556 
    557 	/* get physical address of buffer start */
    558 	phys = (tc_addr_t)d->dmam->dm_segs[0].ds_addr;
    559 	nphys = (tc_addr_t)d->dmam->dm_segs[1].ds_addr;
    560 
    561 	/* setup DMA pointer */
    562 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_R_DMAPTR,
    563 	    IOASIC_DMA_ADDR(phys));
    564 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_ISDN_R_NEXTPTR,
    565 	    IOASIC_DMA_ADDR(nphys));
    566 
    567 	/* kick off DMA */
    568 	ssr |= IOASIC_CSR_DMAEN_ISDN_R;
    569 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, IOASIC_CSR, ssr);
    570 
    571 	d->active = 1;
    572 
    573 	return 0;
    574 
    575 bad:
    576 	if (state & 2)
    577 		bus_dmamap_unload(sc->sc_dmat, d->dmam);
    578 	if (state & 1)
    579 		bus_dmamap_destroy(sc->sc_dmat, d->dmam);
    580 	return 1;
    581 }
    582 
    583 static int
    584 bba_intr(void *addr)
    585 {
    586 	struct bba_softc *sc;
    587 	struct bba_dma_state *d;
    588 	tc_addr_t nphys;
    589 	int s, mask;
    590 
    591 	sc = addr;
    592 	s = splaudio();
    593 
    594 	mask = bus_space_read_4(sc->sc_bst, sc->sc_bsh, IOASIC_INTR);
    595 
    596 	if (mask & IOASIC_INTR_ISDN_TXLOAD) {
    597 		d = &sc->sc_tx_dma_state;
    598 		d->curseg = (d->curseg+1) % d->dmam->dm_nsegs;
    599 		nphys = (tc_addr_t)d->dmam->dm_segs[d->curseg].ds_addr;
    600 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    601 		    IOASIC_ISDN_X_NEXTPTR, IOASIC_DMA_ADDR(nphys));
    602 		if (d->intr != NULL)
    603 			(*d->intr)(d->intr_arg);
    604 	}
    605 	if (mask & IOASIC_INTR_ISDN_RXLOAD) {
    606 		d = &sc->sc_rx_dma_state;
    607 		d->curseg = (d->curseg+1) % d->dmam->dm_nsegs;
    608 		nphys = (tc_addr_t)d->dmam->dm_segs[d->curseg].ds_addr;
    609 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    610 		    IOASIC_ISDN_R_NEXTPTR, IOASIC_DMA_ADDR(nphys));
    611 		if (d->intr != NULL)
    612 			(*d->intr)(d->intr_arg);
    613 	}
    614 
    615 	splx(s);
    616 
    617 	return 0;
    618 }
    619 
    620 static int
    621 bba_get_props(void *addr)
    622 {
    623 
    624 	return AUDIO_PROP_MMAP | am7930_get_props(addr);
    625 }
    626 
    627 static paddr_t
    628 bba_mappage(void *addr, void *mem, off_t offset, int prot)
    629 {
    630 	struct bba_softc *sc;
    631 	struct bba_mem **mp;
    632 	bus_dma_segment_t seg;
    633 	void *kva;
    634 
    635 	sc = addr;
    636 	kva = (void *)mem;
    637 	for (mp = &sc->sc_mem_head; *mp && (*mp)->kva != kva;
    638 	    mp = &(*mp)->next)
    639 		continue;
    640 	if (*mp == NULL || offset < 0) {
    641 		return -1;
    642 	}
    643 
    644 	seg.ds_addr = (*mp)->addr;
    645 	seg.ds_len = (*mp)->size;
    646 
    647 	return bus_dmamem_mmap(sc->sc_dmat, &seg, 1, offset,
    648 	    prot, BUS_DMA_WAITOK);
    649 }
    650 
    651 static stream_filter_t *
    652 bba_input_conv(struct audio_softc *sc, const audio_params_t *from,
    653 	       const audio_params_t *to)
    654 {
    655 	return auconv_nocontext_filter_factory(bba_input_conv_fetch_to);
    656 }
    657 
    658 static int
    659 bba_input_conv_fetch_to(stream_fetcher_t *self, audio_stream_t *dst,
    660 			int max_used)
    661 {
    662 	stream_filter_t *this;
    663 	int m, err;
    664 
    665 	this = (stream_filter_t *)self;
    666 	if ((err = this->prev->fetch_to(this->prev, this->src, max_used * 4)))
    667 		return err;
    668 	m = dst->end - dst->start;
    669 	m = min(m, max_used);
    670 	FILTER_LOOP_PROLOGUE(this->src, 4, dst, 1, m) {
    671 		*d = ((*(const uint32_t *)s) >> 16) & 0xff;
    672 	} FILTER_LOOP_EPILOGUE(this->src, dst);
    673 	return 0;
    674 }
    675 
    676 static stream_filter_t *
    677 bba_output_conv(struct audio_softc *sc, const audio_params_t *from,
    678 		const audio_params_t *to)
    679 {
    680 	return auconv_nocontext_filter_factory(bba_output_conv_fetch_to);
    681 }
    682 
    683 static int
    684 bba_output_conv_fetch_to(stream_fetcher_t *self, audio_stream_t *dst,
    685 			  int max_used)
    686 {
    687 	stream_filter_t *this;
    688 	int m, err;
    689 
    690 	this = (stream_filter_t *)self;
    691 	max_used = (max_used + 3) & ~3;
    692 	if ((err = this->prev->fetch_to(this->prev, this->src, max_used / 4)))
    693 		return err;
    694 	m = (dst->end - dst->start) & ~3;
    695 	m = min(m, max_used);
    696 	FILTER_LOOP_PROLOGUE(this->src, 1, dst, 4, m) {
    697 		*(uint32_t *)d = (*s << 16);
    698 	} FILTER_LOOP_EPILOGUE(this->src, dst);
    699 	return 0;
    700 }
    701 
    702 static int
    703 bba_round_blocksize(void *addr, int blk, int mode, const audio_params_t *param)
    704 {
    705 
    706 	return IOASIC_DMA_BLOCKSIZE;
    707 }
    708 
    709 
    710 /* indirect write */
    711 static void
    712 bba_codec_iwrite(struct am7930_softc *sc, int reg, uint8_t val)
    713 {
    714 
    715 	DPRINTF(("bba_codec_iwrite(): sc=%p, reg=%d, val=%d\n", sc, reg, val));
    716 	bba_codec_dwrite(sc, AM7930_DREG_CR, reg);
    717 	bba_codec_dwrite(sc, AM7930_DREG_DR, val);
    718 }
    719 
    720 
    721 static void
    722 bba_codec_iwrite16(struct am7930_softc *sc, int reg, uint16_t val)
    723 {
    724 
    725 	DPRINTF(("bba_codec_iwrite16(): sc=%p, reg=%d, val=%d\n", sc, reg, val));
    726 	bba_codec_dwrite(sc, AM7930_DREG_CR, reg);
    727 	bba_codec_dwrite(sc, AM7930_DREG_DR, val);
    728 	bba_codec_dwrite(sc, AM7930_DREG_DR, val>>8);
    729 }
    730 
    731 
    732 static uint16_t
    733 bba_codec_iread16(struct am7930_softc *sc, int reg)
    734 {
    735 	uint16_t val;
    736 
    737 	DPRINTF(("bba_codec_iread16(): sc=%p, reg=%d\n", sc, reg));
    738 	bba_codec_dwrite(sc, AM7930_DREG_CR, reg);
    739 	val = bba_codec_dread(sc, AM7930_DREG_DR) << 8;
    740 	val |= bba_codec_dread(sc, AM7930_DREG_DR);
    741 
    742 	return val;
    743 }
    744 
    745 
    746 /* indirect read */
    747 static uint8_t
    748 bba_codec_iread(struct am7930_softc *sc, int reg)
    749 {
    750 	uint8_t val;
    751 
    752 	DPRINTF(("bba_codec_iread(): sc=%p, reg=%d\n", sc, reg));
    753 	bba_codec_dwrite(sc, AM7930_DREG_CR, reg);
    754 	val = bba_codec_dread(sc, AM7930_DREG_DR);
    755 
    756 	DPRINTF(("read 0x%x (%d)\n", val, val));
    757 
    758 	return val;
    759 }
    760 
    761 /* direct write */
    762 static void
    763 bba_codec_dwrite(struct am7930_softc *asc, int reg, uint8_t val)
    764 {
    765 	struct bba_softc *sc;
    766 
    767 	sc = (struct bba_softc *)asc;
    768 	DPRINTF(("bba_codec_dwrite(): sc=%p, reg=%d, val=%d\n", sc, reg, val));
    769 
    770 #if defined(__alpha__)
    771 	bus_space_write_4(sc->sc_bst, sc->sc_codec_bsh,
    772 	    reg << 2, val << 8);
    773 #else
    774 	bus_space_write_4(sc->sc_bst, sc->sc_codec_bsh,
    775 	    reg << 6, val);
    776 #endif
    777 }
    778 
    779 /* direct read */
    780 static uint8_t
    781 bba_codec_dread(struct am7930_softc *asc, int reg)
    782 {
    783 	struct bba_softc *sc;
    784 
    785 	sc = (struct bba_softc *)asc;
    786 	DPRINTF(("bba_codec_dread(): sc=%p, reg=%d\n", sc, reg));
    787 
    788 #if defined(__alpha__)
    789 	return ((bus_space_read_4(sc->sc_bst, sc->sc_codec_bsh,
    790 		reg << 2) >> 8) & 0xff);
    791 #else
    792 	return (bus_space_read_4(sc->sc_bst, sc->sc_codec_bsh,
    793 		reg << 6) & 0xff);
    794 #endif
    795 }
    796