Home | History | Annotate | Line # | Download | only in sbus
dbri.c revision 1.26
      1 /*	$NetBSD: dbri.c,v 1.26 2010/01/02 01:43:42 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1997 Rudolf Koenig (rfkoenig (at) immd4.informatik.uni-erlangen.de)
      5  * Copyright (c) 1998, 1999 Brent Baccala (baccala (at) freesoft.org)
      6  * Copyright (c) 2001, 2002 Jared D. McNeill <jmcneill (at) netbsd.org>
      7  * Copyright (c) 2005 Michael Lorenz <macallan (at) netbsd.org>
      8  * All rights reserved.
      9  *
     10  * This driver is losely based on a Linux driver written by Rudolf Koenig and
     11  * Brent Baccala who kindly gave their permission to use their code in a
     12  * BSD-licensed driver.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     29  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: dbri.c,v 1.26 2010/01/02 01:43:42 christos Exp $");
     38 
     39 #include "audio.h"
     40 #if NAUDIO > 0
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/errno.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 #include <sys/proc.h>
     48 #include <sys/kernel.h>
     49 #include <sys/bus.h>
     50 #include <sys/intr.h>
     51 
     52 #include <dev/sbus/sbusvar.h>
     53 #include <sparc/sparc/auxreg.h>
     54 #include <machine/autoconf.h>
     55 
     56 #include <sys/audioio.h>
     57 #include <dev/audio_if.h>
     58 #include <dev/auconv.h>
     59 
     60 #include <dev/ic/cs4215reg.h>
     61 #include <dev/ic/cs4215var.h>
     62 #include <dev/sbus/dbrireg.h>
     63 #include <dev/sbus/dbrivar.h>
     64 
     65 #include "opt_sbus_dbri.h"
     66 
     67 #define DBRI_ROM_NAME_PREFIX		"SUNW,DBRI"
     68 
     69 #ifdef DBRI_DEBUG
     70 # define DPRINTF aprint_normal
     71 #else
     72 # define DPRINTF while (0) printf
     73 #endif
     74 
     75 static const char *dbri_supported[] = {
     76 	"e",
     77 	"s3",
     78 	""
     79 };
     80 
     81 enum ms {
     82 	CHImaster,
     83 	CHIslave
     84 };
     85 
     86 enum io {
     87 	PIPEinput,
     88 	PIPEoutput
     89 };
     90 
     91 /*
     92  * Function prototypes
     93  */
     94 
     95 /* softc stuff */
     96 static void	dbri_attach_sbus(device_t, device_t, void *);
     97 static int	dbri_match_sbus(device_t, cfdata_t, void *);
     98 
     99 static void	dbri_config_interrupts(device_t);
    100 
    101 /* interrupt handler */
    102 static int	dbri_intr(void *);
    103 static void	dbri_softint(void *);
    104 
    105 /* supporting subroutines */
    106 static int	dbri_init(struct dbri_softc *);
    107 static int	dbri_reset(struct dbri_softc *);
    108 static volatile uint32_t *dbri_command_lock(struct dbri_softc *);
    109 static void	dbri_command_send(struct dbri_softc *, volatile uint32_t *);
    110 static void	dbri_process_interrupt_buffer(struct dbri_softc *);
    111 static void	dbri_process_interrupt(struct dbri_softc *, int32_t);
    112 
    113 /* mmcodec subroutines */
    114 static int	mmcodec_init(struct dbri_softc *);
    115 static void	mmcodec_init_data(struct dbri_softc *);
    116 static void	mmcodec_pipe_init(struct dbri_softc *);
    117 static void	mmcodec_default(struct dbri_softc *);
    118 static void	mmcodec_setgain(struct dbri_softc *, int);
    119 static int	mmcodec_setcontrol(struct dbri_softc *);
    120 
    121 /* chi subroutines */
    122 static void	chi_reset(struct dbri_softc *, enum ms, int);
    123 
    124 /* pipe subroutines */
    125 static void	pipe_setup(struct dbri_softc *, int, int);
    126 static void	pipe_reset(struct dbri_softc *, int);
    127 static void	pipe_receive_fixed(struct dbri_softc *, int,
    128     volatile uint32_t *);
    129 static void	pipe_transmit_fixed(struct dbri_softc *, int, uint32_t);
    130 
    131 static void	pipe_ts_link(struct dbri_softc *, int, enum io, int, int, int);
    132 static int	pipe_active(struct dbri_softc *, int);
    133 
    134 /* audio(9) stuff */
    135 static int	dbri_query_encoding(void *, struct audio_encoding *);
    136 static int	dbri_set_params(void *, int, int, struct audio_params *,
    137     struct audio_params *,stream_filter_list_t *, stream_filter_list_t *);
    138 static int	dbri_round_blocksize(void *, int, int, const audio_params_t *);
    139 static int	dbri_halt_output(void *);
    140 static int	dbri_halt_input(void *);
    141 static int	dbri_getdev(void *, struct audio_device *);
    142 static int	dbri_set_port(void *, mixer_ctrl_t *);
    143 static int	dbri_get_port(void *, mixer_ctrl_t *);
    144 static int	dbri_query_devinfo(void *, mixer_devinfo_t *);
    145 static size_t	dbri_round_buffersize(void *, int, size_t);
    146 static int	dbri_get_props(void *);
    147 static int	dbri_open(void *, int);
    148 static void	dbri_close(void *);
    149 
    150 static void	setup_ring_xmit(struct dbri_softc *, int, int, int, int,
    151     void (*)(void *), void *);
    152 static void	setup_ring_recv(struct dbri_softc *, int, int, int, int,
    153     void (*)(void *), void *);
    154 
    155 static int	dbri_trigger_output(void *, void *, void *, int,
    156     void (*)(void *), void *, const struct audio_params *);
    157 static int	dbri_trigger_input(void *, void *, void *, int,
    158     void (*)(void *), void *, const struct audio_params *);
    159 
    160 static void	*dbri_malloc(void *, int, size_t, struct malloc_type *, int);
    161 static void	dbri_free(void *, void *, struct malloc_type *);
    162 static paddr_t	dbri_mappage(void *, void *, off_t, int);
    163 static void	dbri_set_power(struct dbri_softc *, int);
    164 static void	dbri_bring_up(struct dbri_softc *);
    165 static void	dbri_sus(int, void *);
    166 static bool	dbri_suspend(device_t PMF_FN_PROTO);
    167 static bool	dbri_resume(device_t PMF_FN_PROTO);
    168 
    169 /* stupid support routines */
    170 static uint32_t	reverse_bytes(uint32_t, int);
    171 
    172 struct audio_device dbri_device = {
    173 	"CS4215",
    174 	"",
    175 	"dbri"
    176 };
    177 
    178 struct audio_hw_if dbri_hw_if = {
    179 	dbri_open,
    180 	dbri_close,
    181 	NULL,	/* drain */
    182 	dbri_query_encoding,
    183 	dbri_set_params,
    184 	dbri_round_blocksize,
    185 	NULL,	/* commit_settings */
    186 	NULL,	/* init_output */
    187 	NULL,	/* init_input */
    188 	NULL,	/* start_output */
    189 	NULL,	/* start_input */
    190 	dbri_halt_output,
    191 	dbri_halt_input,
    192 	NULL,	/* speaker_ctl */
    193 	dbri_getdev,
    194 	NULL,	/* setfd */
    195 	dbri_set_port,
    196 	dbri_get_port,
    197 	dbri_query_devinfo,
    198 	dbri_malloc,
    199 	dbri_free,
    200 	dbri_round_buffersize,
    201 	dbri_mappage,
    202 	dbri_get_props,
    203 	dbri_trigger_output,
    204 	dbri_trigger_input
    205 };
    206 
    207 CFATTACH_DECL_NEW(dbri, sizeof(struct dbri_softc),
    208     dbri_match_sbus, dbri_attach_sbus, NULL, NULL);
    209 
    210 #define DBRI_NFORMATS		4
    211 static const struct audio_format dbri_formats[DBRI_NFORMATS] = {
    212 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
    213 	 2, AUFMT_STEREO, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    214 	 48000}},
    215 /*	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULAW, 8, 8,
    216 	 2, AUFMT_STEREO, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    217 	 48000}},
    218 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ALAW, 8, 8,
    219 	 2, AUFMT_STEREO, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    220 	 48000}},
    221 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR, 8, 8,
    222 	 2, AUFMT_STEREO, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    223 	 48000}},*/
    224 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULAW, 8, 8,
    225 	 1, AUFMT_MONAURAL, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    226 	 48000}},
    227 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ALAW, 8, 8,
    228 	 1, AUFMT_MONAURAL, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    229 	 48000}},
    230 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR, 8, 8,
    231 	 1, AUFMT_MONAURAL, 8, {8000, 9600, 11025, 16000, 22050, 32000, 44100,
    232 	 48000}},
    233 };
    234 
    235 enum {
    236 	DBRI_OUTPUT_CLASS,
    237 	DBRI_VOL_OUTPUT,
    238 	DBRI_ENABLE_MONO,
    239 	DBRI_ENABLE_HEADPHONE,
    240 	DBRI_ENABLE_LINE,
    241 	DBRI_MONITOR_CLASS,
    242 	DBRI_VOL_MONITOR,
    243 	DBRI_INPUT_CLASS,
    244 	DBRI_INPUT_GAIN,
    245 	DBRI_INPUT_SELECT,
    246 	DBRI_RECORD_CLASS,
    247 	DBRI_ENUM_LAST
    248 };
    249 
    250 /*
    251  * Autoconfig routines
    252  */
    253 static int
    254 dbri_match_sbus(device_t parent, cfdata_t match, void *aux)
    255 {
    256 	struct sbus_attach_args *sa = aux;
    257 	char *ver;
    258 	int i;
    259 
    260 	if (strncmp(DBRI_ROM_NAME_PREFIX, sa->sa_name, 9))
    261 		return (0);
    262 
    263 	ver = &sa->sa_name[9];
    264 
    265 	for (i = 0; dbri_supported[i][0] != '\0'; i++)
    266 		if (strcmp(dbri_supported[i], ver) == 0)
    267 			return (1);
    268 
    269 	return (0);
    270 }
    271 
    272 static void
    273 dbri_attach_sbus(device_t parent, device_t self, void *aux)
    274 {
    275 	struct dbri_softc *sc = device_private(self);
    276 	struct sbus_attach_args *sa = aux;
    277 	bus_space_handle_t ioh;
    278 	bus_size_t size;
    279 	int error, rseg, pwr, i;
    280 	char *ver = &sa->sa_name[9];
    281 
    282 	sc->sc_dev = self;
    283 	sc->sc_iot = sa->sa_bustag;
    284 	sc->sc_dmat = sa->sa_dmatag;
    285 	sc->sc_powerstate = 1;
    286 
    287 	pwr = prom_getpropint(sa->sa_node,"pwr-on-auxio",0);
    288 	aprint_normal(": rev %s\n", ver);
    289 
    290 	if (pwr) {
    291 		/*
    292 		 * we can control DBRI power via auxio and we're initially
    293 		 * powered down
    294 		 */
    295 
    296 		sc->sc_have_powerctl = 1;
    297 		sc->sc_powerstate = 0;
    298 		dbri_set_power(sc, 1);
    299 		if (!pmf_device_register(self, dbri_suspend, dbri_resume)) {
    300 			aprint_error_dev(self,
    301 			    "cannot set power mgmt handler\n");
    302 		}
    303 	} else {
    304 		/* we can't control power so we're always up */
    305 		sc->sc_have_powerctl = 0;
    306 		sc->sc_powerstate = 1;
    307 	}
    308 
    309 	for (i = 0; i < DBRI_NUM_DESCRIPTORS; i++) {
    310 		sc->sc_desc[i].softint = softint_establish(SOFTINT_SERIAL,
    311 		    dbri_softint, &sc->sc_desc[i]);
    312 	}
    313 
    314 	if (sa->sa_npromvaddrs)
    315 		ioh = (bus_space_handle_t)sa->sa_promvaddrs[0];
    316 	else {
    317 		if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    318 				 sa->sa_offset, sa->sa_size,
    319 				 BUS_SPACE_MAP_LINEAR, /*0,*/ &ioh) != 0) {
    320 			aprint_error("%s @ sbus: cannot map registers\n",
    321 				device_xname(self));
    322 			return;
    323 		}
    324 	}
    325 
    326 	sc->sc_ioh = ioh;
    327 
    328 	size = sizeof(struct dbri_dma);
    329 
    330 	/* get a DMA handle */
    331 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    332 				       BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    333 		aprint_error_dev(self, "DMA map create error %d\n",
    334 		    error);
    335 		return;
    336 	}
    337 
    338 	/* allocate DMA buffer */
    339 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, 0, 0, &sc->sc_dmaseg,
    340 				      1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    341 		aprint_error_dev(self, "DMA buffer alloc error %d\n",
    342 		    error);
    343 		return;
    344 	}
    345 
    346 	/* map DMA buffer into CPU addressable space */
    347 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, rseg, size,
    348 				    &sc->sc_membase,
    349 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    350 		aprint_error_dev(self, "DMA buffer map error %d\n",
    351 		    error);
    352 		return;
    353 	}
    354 
    355 	/* load the buffer */
    356 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    357 				     sc->sc_membase, size, NULL,
    358 				     BUS_DMA_NOWAIT)) != 0) {
    359 		aprint_error_dev(self, "DMA buffer map load error %d\n",
    360 		    error);
    361 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_membase, size);
    362 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, rseg);
    363 		return;
    364 	}
    365 
    366 	/* map the registers into memory */
    367 
    368 	/* kernel virtual address of DMA buffer */
    369 	sc->sc_dma = (struct dbri_dma *)sc->sc_membase;
    370 	/* physical address of DMA buffer */
    371 	sc->sc_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
    372 	sc->sc_bufsiz = size;
    373 
    374 	bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_SCHED, dbri_intr,
    375 	    sc);
    376 
    377 	sc->sc_locked = 0;
    378 	sc->sc_desc_used = 0;
    379 	sc->sc_refcount = 0;
    380 	sc->sc_playing = 0;
    381 	sc->sc_recording = 0;
    382 	config_interrupts(self, &dbri_config_interrupts);
    383 
    384 	return;
    385 }
    386 
    387 /*
    388  * lowlevel routine to switch power for the DBRI chip
    389  */
    390 static void
    391 dbri_set_power(struct dbri_softc *sc, int state)
    392 {
    393 	int s;
    394 
    395 	if (sc->sc_have_powerctl == 0)
    396 		return;
    397 	if (sc->sc_powerstate == state)
    398 		return;
    399 
    400 	if (state) {
    401 		DPRINTF("%s: waiting to power up... ",
    402 		    device_xname(sc->sc_dev));
    403 		s = splhigh();
    404 		*AUXIO4M_REG |= (AUXIO4M_MMX);
    405 		splx(s);
    406 		delay(10000);
    407 		DPRINTF("done (%02x)\n", *AUXIO4M_REG);
    408 	} else {
    409 		DPRINTF("%s: powering down\n", device_xname(sc->sc_dev));
    410 		s = splhigh();
    411 		*AUXIO4M_REG &= ~AUXIO4M_MMX;
    412 		splx(s);
    413 		DPRINTF("done (%02x})\n", *AUXIO4M_REG);
    414 	}
    415 	sc->sc_powerstate = state;
    416 }
    417 
    418 /*
    419  * power up and re-initialize the chip
    420  */
    421 static void
    422 dbri_bring_up(struct dbri_softc *sc)
    423 {
    424 
    425 	if (sc->sc_have_powerctl == 0)
    426 		return;
    427 
    428 	if (sc->sc_powerstate == 1)
    429 		return;
    430 
    431 	/* ok, we really need to do something */
    432 	dbri_set_power(sc, 1);
    433 
    434 	/*
    435 	 * re-initialize the chip but skip all the probing, don't overwrite
    436 	 * any other settings either
    437 	 */
    438 	dbri_init(sc);
    439 	mmcodec_setgain(sc, 1);
    440 	mmcodec_pipe_init(sc);
    441 	mmcodec_init_data(sc);
    442 	mmcodec_setgain(sc, 0);
    443 }
    444 
    445 static void
    446 dbri_config_interrupts(device_t dev)
    447 {
    448 	struct dbri_softc *sc = device_private(dev);
    449 
    450 	dbri_init(sc);
    451 	mmcodec_init(sc);
    452 
    453 	/* Attach ourselves to the high level audio interface */
    454 	audio_attach_mi(&dbri_hw_if, sc, sc->sc_dev);
    455 
    456 	/* power down until open() */
    457 	dbri_set_power(sc, 0);
    458 	return;
    459 }
    460 
    461 static int
    462 dbri_intr(void *hdl)
    463 {
    464 	struct dbri_softc *sc = hdl;
    465 	bus_space_tag_t iot = sc->sc_iot;
    466 	bus_space_handle_t ioh = sc->sc_ioh;
    467 	int x;
    468 
    469 	/* clear interrupt */
    470 	x = bus_space_read_4(iot, ioh, DBRI_REG1);
    471 	if (x & (DBRI_MRR | DBRI_MLE | DBRI_LBG | DBRI_MBE)) {
    472 		uint32_t tmp;
    473 
    474 		if (x & DBRI_MRR)
    475 			aprint_debug_dev(sc->sc_dev,
    476 			     "multiple ack error on sbus\n");
    477 		if (x & DBRI_MLE)
    478 			aprint_debug_dev(sc->sc_dev,
    479 			    "multiple late error on sbus\n");
    480 		if (x & DBRI_LBG)
    481 			aprint_debug_dev(sc->sc_dev,
    482 			    "lost bus grant on sbus\n");
    483 		if (x & DBRI_MBE)
    484 			aprint_debug_dev(sc->sc_dev, "burst error on sbus\n");
    485 
    486 		/*
    487 		 * Some of these errors disable the chip's circuitry.
    488 		 * Re-enable the circuitry and keep on going.
    489 		 */
    490 
    491 		tmp = bus_space_read_4(iot, ioh, DBRI_REG0);
    492 		tmp &= ~(DBRI_DISABLE_MASTER);
    493 		bus_space_write_4(iot, ioh, DBRI_REG0, tmp);
    494 	}
    495 
    496 #if 0
    497 	if (!x & 1)	/* XXX: DBRI_INTR_REQ */
    498 		return (1);
    499 #endif
    500 
    501 	dbri_process_interrupt_buffer(sc);
    502 
    503 	return (1);
    504 }
    505 
    506 static void
    507 dbri_softint(void *cookie)
    508 {
    509 	struct dbri_desc *dd = cookie;
    510 
    511 	if (dd->callback != NULL)
    512 		dd->callback(dd->callback_args);
    513 }
    514 
    515 static int
    516 dbri_init(struct dbri_softc *sc)
    517 {
    518 	bus_space_tag_t iot = sc->sc_iot;
    519 	bus_space_handle_t ioh = sc->sc_ioh;
    520 	uint32_t reg;
    521 	volatile uint32_t *cmd;
    522 	bus_addr_t dmaaddr;
    523 	int n;
    524 
    525 	dbri_reset(sc);
    526 
    527 	cmd = dbri_command_lock(sc);
    528 
    529 	/* XXX: Initialize interrupt ring buffer */
    530 	sc->sc_dma->intr[0] = (uint32_t)sc->sc_dmabase + dbri_dma_off(intr, 0);
    531 	sc->sc_irqp = 1;
    532 
    533 	/* Initialize pipes */
    534 	for (n = 0; n < DBRI_PIPE_MAX; n++)
    535 		sc->sc_pipe[n].desc = sc->sc_pipe[n].next = -1;
    536 
    537 	for (n = 1; n < DBRI_INT_BLOCKS; n++) {
    538 		sc->sc_dma->intr[n] = 0;
    539 	}
    540 
    541 	/* Disable all SBus bursts */
    542 	/* XXX 16 byte bursts cause errors, the rest works */
    543 	reg = bus_space_read_4(iot, ioh, DBRI_REG0);
    544 
    545 	/*reg &= ~(DBRI_BURST_4 | DBRI_BURST_8 | DBRI_BURST_16);*/
    546 	reg |= (DBRI_BURST_4 | DBRI_BURST_8);
    547 	bus_space_write_4(iot, ioh, DBRI_REG0, reg);
    548 
    549 	/* setup interrupt queue */
    550 	dmaaddr = (uint32_t)sc->sc_dmabase + dbri_dma_off(intr, 0);
    551 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_IIQ, 0, 0);
    552 	*(cmd++) = dmaaddr;
    553 
    554 	dbri_command_send(sc, cmd);
    555 	return (0);
    556 }
    557 
    558 static int
    559 dbri_reset(struct dbri_softc *sc)
    560 {
    561 	int bail = 0;
    562 
    563 	bus_space_tag_t iot = sc->sc_iot;
    564 	bus_space_handle_t ioh = sc->sc_ioh;
    565 
    566 	bus_space_write_4(iot, ioh, DBRI_REG0, DBRI_SOFT_RESET);
    567 	while ((bus_space_read_4(iot, ioh, DBRI_REG0) & DBRI_SOFT_RESET) &&
    568 	    (bail < 100000)) {
    569 		bail++;
    570 		delay(10);
    571 	}
    572 	if (bail == 100000)
    573 		aprint_error_dev(sc->sc_dev, "reset timed out\n");
    574 	return (0);
    575 }
    576 
    577 static volatile uint32_t *
    578 dbri_command_lock(struct dbri_softc *sc)
    579 {
    580 
    581 	if (sc->sc_locked)
    582 		aprint_debug_dev(sc->sc_dev, "command buffer locked\n");
    583 
    584 	sc->sc_locked++;
    585 
    586 	return (&sc->sc_dma->command[0]);
    587 }
    588 
    589 static void
    590 dbri_command_send(struct dbri_softc *sc, volatile uint32_t *cmd)
    591 {
    592 	bus_space_handle_t ioh = sc->sc_ioh;
    593 	bus_space_tag_t iot = sc->sc_iot;
    594 	int maxloops = 1000000;
    595 	int x;
    596 
    597 	x = splsched();
    598 
    599 	sc->sc_locked--;
    600 
    601 	if (sc->sc_locked != 0) {
    602 		aprint_error_dev(sc->sc_dev,
    603 		    "command buffer improperly locked\n");
    604 	} else if ((cmd - &sc->sc_dma->command[0]) >= DBRI_NUM_COMMANDS - 1) {
    605 		aprint_error_dev(sc->sc_dev, "command buffer overflow\n");
    606 	} else {
    607 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_PAUSE, 0, 0);
    608 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_WAIT, 1, 0);
    609 		sc->sc_waitseen = 0;
    610 		bus_space_write_4(iot, ioh, DBRI_REG8, sc->sc_dmabase);
    611 		while ((--maxloops) > 0 &&
    612 		    (bus_space_read_4(iot, ioh, DBRI_REG0)
    613 		     & DBRI_COMMAND_VALID)) {
    614 			bus_space_barrier(iot, ioh, DBRI_REG0, 4,
    615 					  BUS_SPACE_BARRIER_READ);
    616 			delay(1000);
    617 		}
    618 
    619 		if (maxloops == 0) {
    620 			aprint_error_dev(sc->sc_dev,
    621 			    "chip never completed command buffer\n");
    622 		} else {
    623 
    624 			DPRINTF("%s: command completed\n",
    625 			    device_xname(sc->sc_dev));
    626 
    627 			while ((--maxloops) > 0 && (!sc->sc_waitseen))
    628 				dbri_process_interrupt_buffer(sc);
    629 			if (maxloops == 0) {
    630 				aprint_error_dev(sc->sc_dev, "chip never acked WAIT\n");
    631 			}
    632 		}
    633 	}
    634 
    635 	splx(x);
    636 
    637 	return;
    638 }
    639 
    640 static void
    641 dbri_process_interrupt_buffer(struct dbri_softc *sc)
    642 {
    643 	int32_t i;
    644 
    645 	while ((i = sc->sc_dma->intr[sc->sc_irqp]) != 0) {
    646 		sc->sc_dma->intr[sc->sc_irqp] = 0;
    647 		sc->sc_irqp++;
    648 
    649 		if (sc->sc_irqp == DBRI_INT_BLOCKS)
    650 			sc->sc_irqp = 1;
    651 		else if ((sc->sc_irqp & (DBRI_INT_BLOCKS - 1)) == 0)
    652 			sc->sc_irqp++;
    653 
    654 		dbri_process_interrupt(sc, i);
    655 	}
    656 
    657 	return;
    658 }
    659 
    660 static void
    661 dbri_process_interrupt(struct dbri_softc *sc, int32_t i)
    662 {
    663 #if 0
    664 	const int liu_states[] = { 1, 0, 8, 3, 4, 5, 6, 7 };
    665 #endif
    666 	int val = DBRI_INTR_GETVAL(i);
    667 	int channel = DBRI_INTR_GETCHAN(i);
    668 	int command = DBRI_INTR_GETCMD(i);
    669 	int code = DBRI_INTR_GETCODE(i);
    670 #if 0
    671 	int rval = DBRI_INTR_GETRVAL(i);
    672 #endif
    673 	if (channel == DBRI_INTR_CMD && command == DBRI_COMMAND_WAIT)
    674 		sc->sc_waitseen++;
    675 
    676 	switch (code) {
    677 	case DBRI_INTR_XCMP:	/* transmission complete */
    678 	{
    679 		int td;
    680 		struct dbri_desc *dd;
    681 
    682 		td = sc->sc_pipe[channel].desc;
    683 		dd = &sc->sc_desc[td];
    684 
    685 		if (dd->callback != NULL)
    686 			softint_schedule(dd->softint);
    687 		break;
    688 	}
    689 	case DBRI_INTR_FXDT:		/* fixed data change */
    690 		DPRINTF("dbri_intr: Fixed data change (%d: %x)\n", channel,
    691 		    val);
    692 #if 0
    693 		printf("reg: %08x\n", sc->sc_mm.status);
    694 #endif
    695 		if (sc->sc_pipe[channel].sdp & DBRI_SDP_MSB)
    696 			val = reverse_bytes(val, sc->sc_pipe[channel].length);
    697 		if (sc->sc_pipe[channel].prec)
    698 			*(sc->sc_pipe[channel].prec) = val;
    699 #ifndef DBRI_SPIN
    700 		DPRINTF("%s: wakeup %p\n", device_xname(sc->sc_dev), sc);
    701 		wakeup(sc);
    702 #endif
    703 		break;
    704 	case DBRI_INTR_SBRI:
    705 		DPRINTF("dbri_intr: SBRI\n");
    706 		break;
    707 	case DBRI_INTR_BRDY:
    708 	{
    709 		int td;
    710 		struct dbri_desc *dd;
    711 
    712 		td = sc->sc_pipe[channel].desc;
    713 		dd = &sc->sc_desc[td];
    714 
    715 		if (dd->callback != NULL)
    716 			softint_schedule(dd->softint);
    717 		break;
    718 	}
    719 	case DBRI_INTR_UNDR:
    720 	{
    721 		volatile uint32_t *cmd;
    722 		int td = sc->sc_pipe[channel].desc;
    723 
    724 		DPRINTF("%s: DBRI_INTR_UNDR\n", device_xname(sc->sc_dev));
    725 
    726 		sc->sc_dma->xmit[td].status = 0;
    727 
    728 		cmd = dbri_command_lock(sc);
    729 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_SDP, 0,
    730 				    sc->sc_pipe[channel].sdp |
    731 				    DBRI_SDP_VALID_POINTER |
    732 				    DBRI_SDP_CLEAR |
    733 				    DBRI_SDP_2SAME);
    734 		*(cmd++) = sc->sc_dmabase + dbri_dma_off(xmit, td);
    735 		dbri_command_send(sc, cmd);
    736 		break;
    737 	}
    738 	case DBRI_INTR_CMDI:
    739 		DPRINTF("ok");
    740 		break;
    741 	default:
    742 
    743 		aprint_error_dev(sc->sc_dev, "unknown interrupt code %d\n",
    744 		    code);
    745 		break;
    746 	}
    747 
    748 	return;
    749 }
    750 
    751 /*
    752  * mmcodec stuff
    753  */
    754 
    755 static int
    756 mmcodec_init(struct dbri_softc *sc)
    757 {
    758 	bus_space_handle_t ioh = sc->sc_ioh;
    759 	bus_space_tag_t iot = sc->sc_iot;
    760 	uint32_t reg2;
    761 	int bail;
    762 
    763 	reg2 = bus_space_read_4(iot, ioh, DBRI_REG2);
    764 	DPRINTF("mmcodec_init: PIO reads %x\n", reg2);
    765 
    766 	if (reg2 & DBRI_PIO2) {
    767 		aprint_normal_dev(sc->sc_dev, " onboard CS4215 detected\n");
    768 		sc->sc_mm.onboard = 1;
    769 	}
    770 
    771 	if (reg2 & DBRI_PIO0) {
    772 		aprint_normal_dev(sc->sc_dev, "speakerbox detected\n");
    773 		bus_space_write_4(iot, ioh, DBRI_REG2, DBRI_PIO2_ENABLE);
    774 		sc->sc_mm.onboard = 0;
    775 	}
    776 
    777 	if ((reg2 & DBRI_PIO2) && (reg2 & DBRI_PIO0)) {
    778 		aprint_normal_dev(sc->sc_dev, "using speakerbox\n");
    779 		bus_space_write_4(iot, ioh, DBRI_REG2, DBRI_PIO2_ENABLE);
    780 		sc->sc_mm.onboard = 0;
    781 	}
    782 
    783 	if (!(reg2 & (DBRI_PIO0|DBRI_PIO2))) {
    784 		aprint_normal_dev(sc->sc_dev, "no mmcodec found\n");
    785 		return -1;
    786 	}
    787 
    788 	sc->sc_version = 0xff;
    789 
    790 	mmcodec_pipe_init(sc);
    791 	mmcodec_default(sc);
    792 
    793 	sc->sc_mm.offset = sc->sc_mm.onboard ? 0 : 8;
    794 
    795 	/*
    796 	 * mmcodec_setcontrol() sometimes fails right after powerup
    797 	 * so we just try again until we either get a useful response or run
    798 	 * out of time
    799 	 */
    800 	bail = 0;
    801 	while (mmcodec_setcontrol(sc) == -1 || sc->sc_version == 0xff) {
    802 
    803 		bail++;
    804 		if (bail > 100) {
    805 			DPRINTF("%s: cs4215 probe failed at offset %d\n",
    806 		    	    device_xname(sc->sc_dev), sc->sc_mm.offset);
    807 			return (-1);
    808 		}
    809 		delay(10000);
    810 	}
    811 
    812 	aprint_normal_dev(sc->sc_dev, "cs4215 rev %c found at offset %d\n",
    813 	    0x43 + (sc->sc_version & 0xf), sc->sc_mm.offset);
    814 
    815 	/* set some sane defaults for mmcodec_init_data */
    816 	sc->sc_params.channels = 2;
    817 	sc->sc_params.precision = 16;
    818 
    819 	mmcodec_init_data(sc);
    820 
    821 	return (0);
    822 }
    823 
    824 static void
    825 mmcodec_init_data(struct dbri_softc *sc)
    826 {
    827 	bus_space_tag_t iot = sc->sc_iot;
    828 	bus_space_handle_t ioh = sc->sc_ioh;
    829 	uint32_t tmp;
    830 	int data_width;
    831 
    832 	tmp = bus_space_read_4(iot, ioh, DBRI_REG0);
    833 	tmp &= ~(DBRI_CHI_ACTIVATE);	/* disable CHI */
    834 	bus_space_write_4(iot, ioh, DBRI_REG0, tmp);
    835 
    836 	/* switch CS4215 to data mode - set PIO3 to 1 */
    837 	tmp = DBRI_PIO_ENABLE_ALL | DBRI_PIO1 | DBRI_PIO3;
    838 
    839 	/* XXX */
    840 	tmp |= (sc->sc_mm.onboard ? DBRI_PIO0 : DBRI_PIO2);
    841 
    842 	bus_space_write_4(iot, ioh, DBRI_REG2, tmp);
    843 	chi_reset(sc, CHIslave, 128);
    844 
    845 	data_width = sc->sc_params.channels * sc->sc_params.precision;
    846 
    847 	if ((data_width != 32) && (data_width != 8))
    848 		aprint_error("%s: data_width is %d\n", __func__, data_width);
    849 
    850 	pipe_ts_link(sc, 20, PIPEoutput, 16, 32, sc->sc_mm.offset + 32);
    851 	pipe_ts_link(sc, 4, PIPEoutput, 16, data_width, sc->sc_mm.offset);
    852 	pipe_ts_link(sc, 6, PIPEinput, 16, data_width, sc->sc_mm.offset);
    853 	pipe_ts_link(sc, 21, PIPEinput, 16, 32, sc->sc_mm.offset + 32);
    854 
    855 	pipe_receive_fixed(sc, 21, &sc->sc_mm.status);
    856 
    857 	mmcodec_setgain(sc, 0);
    858 
    859 	tmp = bus_space_read_4(iot, ioh, DBRI_REG0);
    860 	tmp |= DBRI_CHI_ACTIVATE;
    861 	bus_space_write_4(iot, ioh, DBRI_REG0, tmp);
    862 
    863 	return;
    864 }
    865 
    866 static void
    867 mmcodec_pipe_init(struct dbri_softc *sc)
    868 {
    869 
    870 	pipe_setup(sc, 4, DBRI_SDP_MEM | DBRI_SDP_TO_SER | DBRI_SDP_MSB);
    871 	pipe_setup(sc, 20, DBRI_SDP_FIXED | DBRI_SDP_TO_SER | DBRI_SDP_MSB);
    872 	pipe_setup(sc, 6, DBRI_SDP_MEM | DBRI_SDP_FROM_SER | DBRI_SDP_MSB);
    873 	pipe_setup(sc, 21, DBRI_SDP_FIXED | DBRI_SDP_FROM_SER | DBRI_SDP_MSB);
    874 
    875 	pipe_setup(sc, 17, DBRI_SDP_FIXED | DBRI_SDP_TO_SER | DBRI_SDP_MSB);
    876 	pipe_setup(sc, 18, DBRI_SDP_FIXED | DBRI_SDP_FROM_SER | DBRI_SDP_MSB);
    877 	pipe_setup(sc, 19, DBRI_SDP_FIXED | DBRI_SDP_FROM_SER | DBRI_SDP_MSB);
    878 
    879 	sc->sc_mm.status = 0;
    880 
    881 	pipe_receive_fixed(sc, 18, &sc->sc_mm.status);
    882 	pipe_receive_fixed(sc, 19, &sc->sc_mm.version);
    883 
    884 	return;
    885 }
    886 
    887 static void
    888 mmcodec_default(struct dbri_softc *sc)
    889 {
    890 	struct cs4215_state *mm = &sc->sc_mm;
    891 
    892 	/*
    893 	 * no action, memory resetting only
    894 	 *
    895 	 * data time slots 5-8
    896 	 * speaker, line and headphone enable. set gain to half.
    897 	 * input is line
    898 	 */
    899 	mm->d.bdata[0] = sc->sc_latt = 0x20 | CS4215_HE | CS4215_LE;
    900 	mm->d.bdata[1] = sc->sc_ratt = 0x20 | CS4215_SE;
    901 	sc->sc_linp = 128;
    902 	sc->sc_rinp = 128;
    903 	sc->sc_monitor = 0;
    904 	sc->sc_input = 1;	/* line */
    905 	mm->d.bdata[2] = (CS4215_LG((sc->sc_linp >> 4)) & 0x0f) |
    906 	    ((sc->sc_input == 2) ? CS4215_IS : 0) | CS4215_PIO0 | CS4215_PIO1;
    907 	mm->d.bdata[3] = (CS4215_RG((sc->sc_rinp >> 4) & 0x0f)) |
    908 	    CS4215_MA(15 - ((sc->sc_monitor >> 4) & 0x0f));
    909 
    910 
    911 	/*
    912 	 * control time slots 1-4
    913 	 *
    914 	 * 0: default I/O voltage scale
    915 	 * 1: 8 bit ulaw, 8kHz, mono, high pass filter disabled
    916 	 * 2: serial enable, CHI master, 128 bits per frame, clock 1
    917 	 * 3: tests disabled
    918 	 */
    919 	mm->c.bcontrol[0] = CS4215_RSRVD_1 | CS4215_MLB;
    920 	mm->c.bcontrol[1] = CS4215_DFR_ULAW | CS4215_FREQ[0].csval;
    921 	mm->c.bcontrol[2] = CS4215_XCLK | CS4215_BSEL_128 | CS4215_FREQ[0].xtal;
    922 	mm->c.bcontrol[3] = 0;
    923 
    924 	return;
    925 }
    926 
    927 static void
    928 mmcodec_setgain(struct dbri_softc *sc, int mute)
    929 {
    930 	if (mute) {
    931 		/* disable all outputs, max. attenuation */
    932 		sc->sc_mm.d.bdata[0] = sc->sc_latt | 63;
    933 		sc->sc_mm.d.bdata[1] = sc->sc_ratt | 63;
    934 	} else {
    935 
    936 		sc->sc_mm.d.bdata[0] = sc->sc_latt;
    937 		sc->sc_mm.d.bdata[1] = sc->sc_ratt;
    938 	}
    939 
    940 	/* input stuff */
    941 	sc->sc_mm.d.bdata[2] = CS4215_LG((sc->sc_linp >> 4) & 0x0f) |
    942 	    ((sc->sc_input == 2) ? CS4215_IS : 0) | CS4215_PIO0 | CS4215_PIO1;
    943 	sc->sc_mm.d.bdata[3] = (CS4215_RG((sc->sc_rinp >> 4)) & 0x0f) |
    944 	    (CS4215_MA(15 - ((sc->sc_monitor >> 4) & 0x0f)));
    945 
    946 	if (sc->sc_powerstate == 0)
    947 		return;
    948 	pipe_transmit_fixed(sc, 20, sc->sc_mm.d.ldata);
    949 
    950 	DPRINTF("mmcodec_setgain: %08x\n", sc->sc_mm.d.ldata);
    951 	/* give the chip some time to execute the command */
    952 	delay(250);
    953 
    954 	return;
    955 }
    956 
    957 static int
    958 mmcodec_setcontrol(struct dbri_softc *sc)
    959 {
    960 	bus_space_tag_t iot = sc->sc_iot;
    961 	bus_space_handle_t ioh = sc->sc_ioh;
    962 	uint32_t val;
    963 	uint32_t tmp;
    964 	int bail = 0;
    965 #if DBRI_SPIN
    966 	int i;
    967 #endif
    968 
    969 	/*
    970 	 * Temporarily mute outputs and wait 125 us to make sure that it
    971 	 * happens. This avoids clicking noises.
    972 	 */
    973 	mmcodec_setgain(sc, 1);
    974 	delay(125);
    975 
    976 	bus_space_write_4(iot, ioh, DBRI_REG2, 0);
    977 	delay(125);
    978 
    979 	/* enable control mode */
    980 	val = DBRI_PIO_ENABLE_ALL | DBRI_PIO1;	/* was PIO1 */
    981 
    982 	/* XXX */
    983 	val |= (sc->sc_mm.onboard ? DBRI_PIO0 : DBRI_PIO2);
    984 
    985 	bus_space_write_4(iot, ioh, DBRI_REG2, val);
    986 
    987 	delay(34);
    988 
    989 	/*
    990 	 * in control mode, the cs4215 is the slave device, so the
    991 	 * DBRI must act as the CHI master.
    992 	 *
    993 	 * in data mode, the cs4215 must be the CHI master to insure
    994 	 * that the data stream is in sync with its codec
    995 	 */
    996 	tmp = bus_space_read_4(iot, ioh, DBRI_REG0);
    997 	tmp &= ~DBRI_COMMAND_CHI;
    998 	bus_space_write_4(iot, ioh, DBRI_REG0, tmp);
    999 
   1000 	chi_reset(sc, CHImaster, 128);
   1001 
   1002 	/* control mode */
   1003 	pipe_ts_link(sc, 17, PIPEoutput, 16, 32, sc->sc_mm.offset);
   1004 	pipe_ts_link(sc, 18, PIPEinput, 16, 8, sc->sc_mm.offset);
   1005 	pipe_ts_link(sc, 19, PIPEinput, 16, 8, sc->sc_mm.offset + 48);
   1006 
   1007 	/* wait for the chip to echo back CLB as zero */
   1008 	sc->sc_mm.c.bcontrol[0] &= ~CS4215_CLB;
   1009 	pipe_transmit_fixed(sc, 17, sc->sc_mm.c.lcontrol);
   1010 
   1011 	tmp = bus_space_read_4(iot, ioh, DBRI_REG0);
   1012 	tmp |= DBRI_CHI_ACTIVATE;
   1013 	bus_space_write_4(iot, ioh, DBRI_REG0, tmp);
   1014 
   1015 #if DBRI_SPIN
   1016 	i = 1024;
   1017 	while (((sc->sc_mm.status & 0xe4) != 0x20) && --i) {
   1018 		delay(125);
   1019 	}
   1020 
   1021 	if (i == 0) {
   1022 		DPRINTF("%s: cs4215 didn't respond to CLB (0x%02x)\n",
   1023 		    device_xname(sc->sc_dev), sc->sc_mm.status);
   1024 		return (-1);
   1025 	}
   1026 #else
   1027 	while (((sc->sc_mm.status & 0xe4) != 0x20) && (bail < 10)) {
   1028 		DPRINTF("%s: tsleep %p\n", device_xname(sc->sc_dev), sc);
   1029 		tsleep(sc, PCATCH | PZERO, "dbrifxdt", hz);
   1030 		bail++;
   1031 	}
   1032 #endif
   1033 	if (bail >= 10) {
   1034 		DPRINTF("%s: switching to control mode timed out (%x %x)\n",
   1035 		    device_xname(sc->sc_dev), sc->sc_mm.status,
   1036 		    bus_space_read_4(iot, ioh, DBRI_REG2));
   1037 		return -1;
   1038 	}
   1039 
   1040 	/* copy the version information before it becomes unreadable again */
   1041 	sc->sc_version = sc->sc_mm.version;
   1042 
   1043 	/* terminate cs4215 control mode */
   1044 	sc->sc_mm.c.bcontrol[0] |= CS4215_CLB;
   1045 	pipe_transmit_fixed(sc, 17, sc->sc_mm.c.lcontrol);
   1046 
   1047 	/* two frames of control info @ 8kHz frame rate = 250us delay */
   1048 	delay(250);
   1049 
   1050 	mmcodec_setgain(sc, 0);
   1051 
   1052 	return (0);
   1053 
   1054 }
   1055 
   1056 /*
   1057  * CHI combo
   1058  */
   1059 static void
   1060 chi_reset(struct dbri_softc *sc, enum ms ms, int bpf)
   1061 {
   1062 	volatile uint32_t *cmd;
   1063 	int val;
   1064 	int clockrate, divisor;
   1065 
   1066 	cmd = dbri_command_lock(sc);
   1067 
   1068 	/* set CHI anchor: pipe 16 */
   1069 	val = DBRI_DTS_VI | DBRI_DTS_INS | DBRI_DTS_PRVIN(16) | DBRI_PIPE(16);
   1070 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_DTS, 0, val);
   1071 	*(cmd++) = DBRI_TS_ANCHOR | DBRI_TS_NEXT(16);
   1072 	*(cmd++) = 0;
   1073 
   1074 	val = DBRI_DTS_VO | DBRI_DTS_INS | DBRI_DTS_PRVOUT(16) | DBRI_PIPE(16);
   1075 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_DTS, 0, val);
   1076 	*(cmd++) = 0;
   1077 	*(cmd++) = DBRI_TS_ANCHOR | DBRI_TS_NEXT(16);
   1078 
   1079 	sc->sc_pipe[16].sdp = 1;
   1080 	sc->sc_pipe[16].next = 16;
   1081 	sc->sc_chi_pipe_in = 16;
   1082 	sc->sc_chi_pipe_out = 16;
   1083 
   1084 	switch (ms) {
   1085 	case CHIslave:
   1086 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_CHI, 0, DBRI_CHI_CHICM(0));
   1087 		break;
   1088 	case CHImaster:
   1089 		clockrate = bpf * 8;
   1090 		divisor = 12288 / clockrate;
   1091 
   1092 		if (divisor > 255 || divisor * clockrate != 12288)
   1093 			aprint_error_dev(sc->sc_dev,
   1094 			    "illegal bits-per-frame %d\n", bpf);
   1095 
   1096 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_CHI, 0,
   1097 		    DBRI_CHI_CHICM(divisor) | DBRI_CHI_FD | DBRI_CHI_BPF(bpf));
   1098 		break;
   1099 	default:
   1100 		aprint_error_dev(sc->sc_dev, "unknown value for ms!\n");
   1101 		break;
   1102 	}
   1103 
   1104 	sc->sc_chi_bpf = bpf;
   1105 
   1106 	/* CHI data mode */
   1107 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_PAUSE, 0, 0);
   1108 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_CDM, 0,
   1109 	    DBRI_CDM_XCE | DBRI_CDM_XEN | DBRI_CDM_REN);
   1110 
   1111 	dbri_command_send(sc, cmd);
   1112 
   1113 	return;
   1114 }
   1115 
   1116 /*
   1117  * pipe stuff
   1118  */
   1119 static void
   1120 pipe_setup(struct dbri_softc *sc, int pipe, int sdp)
   1121 {
   1122 	DPRINTF("pipe setup: %d\n", pipe);
   1123 	if (pipe < 0 || pipe >= DBRI_PIPE_MAX) {
   1124 		aprint_error_dev(sc->sc_dev, "illegal pipe number %d\n",
   1125 		    pipe);
   1126 		return;
   1127 	}
   1128 
   1129 	if ((sdp & 0xf800) != sdp)
   1130 		aprint_error_dev(sc->sc_dev, "strange SDP value %d\n",
   1131 		    sdp);
   1132 
   1133 	if (DBRI_SDP_MODE(sdp) == DBRI_SDP_FIXED &&
   1134 	    !(sdp & DBRI_SDP_TO_SER))
   1135 		sdp |= DBRI_SDP_CHANGE;
   1136 
   1137 	sdp |= DBRI_PIPE(pipe);
   1138 
   1139 	sc->sc_pipe[pipe].sdp = sdp;
   1140 	sc->sc_pipe[pipe].desc = -1;
   1141 
   1142 	pipe_reset(sc, pipe);
   1143 
   1144 	return;
   1145 }
   1146 
   1147 static void
   1148 pipe_reset(struct dbri_softc *sc, int pipe)
   1149 {
   1150 	struct dbri_desc *dd;
   1151 	int sdp;
   1152 	int desc;
   1153 	volatile uint32_t *cmd;
   1154 
   1155 	if (pipe < 0 || pipe >= DBRI_PIPE_MAX) {
   1156 		aprint_error_dev(sc->sc_dev, "illegal pipe number %d\n",
   1157 		    pipe);
   1158 		return;
   1159 	}
   1160 
   1161 	sdp = sc->sc_pipe[pipe].sdp;
   1162 	if (sdp == 0) {
   1163 		aprint_error_dev(sc->sc_dev, "can not reset uninitialized pipe %d\n",
   1164 		    pipe);
   1165 		return;
   1166 	}
   1167 
   1168 	cmd = dbri_command_lock(sc);
   1169 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_SDP, 0,
   1170 	    sdp | DBRI_SDP_CLEAR | DBRI_SDP_VALID_POINTER);
   1171 	*(cmd++) = 0;
   1172 	dbri_command_send(sc, cmd);
   1173 
   1174 	desc = sc->sc_pipe[pipe].desc;
   1175 
   1176 	dd = &sc->sc_desc[desc];
   1177 
   1178 	dd->busy = 0;
   1179 
   1180 #if 0
   1181 	if (dd->callback)
   1182 		softint_schedule(dd->softint);
   1183 #endif
   1184 
   1185 	sc->sc_pipe[pipe].desc = -1;
   1186 
   1187 	return;
   1188 }
   1189 
   1190 static void
   1191 pipe_receive_fixed(struct dbri_softc *sc, int pipe, volatile uint32_t *prec)
   1192 {
   1193 
   1194 	if (pipe < DBRI_PIPE_MAX / 2 || pipe >= DBRI_PIPE_MAX) {
   1195 		aprint_error_dev(sc->sc_dev, "illegal pipe number %d\n",
   1196 		    pipe);
   1197 		return;
   1198 	}
   1199 
   1200 	if (DBRI_SDP_MODE(sc->sc_pipe[pipe].sdp) != DBRI_SDP_FIXED) {
   1201 		aprint_error_dev(sc->sc_dev, "non-fixed pipe %d\n",
   1202 		    pipe);
   1203 		return;
   1204 	}
   1205 
   1206 	if (sc->sc_pipe[pipe].sdp & DBRI_SDP_TO_SER) {
   1207 		aprint_error_dev(sc->sc_dev, "can not receive on transmit pipe %d\b",
   1208 		    pipe);
   1209 		return;
   1210 	}
   1211 
   1212 	sc->sc_pipe[pipe].prec = prec;
   1213 
   1214 	return;
   1215 }
   1216 
   1217 static void
   1218 pipe_transmit_fixed(struct dbri_softc *sc, int pipe, uint32_t data)
   1219 {
   1220 	volatile uint32_t *cmd;
   1221 
   1222 	if (pipe < DBRI_PIPE_MAX / 2 || pipe >= DBRI_PIPE_MAX) {
   1223 		aprint_error_dev(sc->sc_dev, "illegal pipe number %d\n",
   1224 		    pipe);
   1225 		return;
   1226 	}
   1227 
   1228 	if (DBRI_SDP_MODE(sc->sc_pipe[pipe].sdp) == 0) {
   1229 		aprint_error_dev(sc->sc_dev, "uninitialized pipe %d\n",
   1230 		    pipe);
   1231 		return;
   1232 	}
   1233 
   1234 	if (DBRI_SDP_MODE(sc->sc_pipe[pipe].sdp) != DBRI_SDP_FIXED) {
   1235 		aprint_error_dev(sc->sc_dev, "non-fixed pipe %d\n",
   1236 		    pipe);
   1237 		return;
   1238 	}
   1239 
   1240 	if (!(sc->sc_pipe[pipe].sdp & DBRI_SDP_TO_SER)) {
   1241 		aprint_error_dev(sc->sc_dev, "called on receive pipe %d\n",
   1242 		    pipe);
   1243 		return;
   1244 	}
   1245 
   1246 	if (sc->sc_pipe[pipe].sdp & DBRI_SDP_MSB)
   1247 		data = reverse_bytes(data, sc->sc_pipe[pipe].length);
   1248 
   1249 	cmd = dbri_command_lock(sc);
   1250 	*(cmd++) = DBRI_CMD(DBRI_COMMAND_SSP, 0, pipe);
   1251 	*(cmd++) = data;
   1252 
   1253 	dbri_command_send(sc, cmd);
   1254 
   1255 	return;
   1256 }
   1257 
   1258 static void
   1259 setup_ring_xmit(struct dbri_softc *sc, int pipe, int which, int num, int blksz,
   1260 		void (*callback)(void *), void *callback_args)
   1261 {
   1262 	volatile uint32_t *cmd;
   1263 	int x, i;
   1264 	int td;
   1265 	int td_first, td_last;
   1266 	bus_addr_t dmabuf, dmabase;
   1267 	struct dbri_desc *dd = &sc->sc_desc[which];
   1268 
   1269 	switch (pipe) {
   1270 		case 4:
   1271 			/* output, offset 0 */
   1272 			break;
   1273 		default:
   1274 			aprint_error("%s: illegal pipe number (%d)\n",
   1275 			    __func__, pipe);
   1276 			return;
   1277 	}
   1278 
   1279 	td = 0;
   1280 	td_first = td_last = -1;
   1281 
   1282 	if (sc->sc_pipe[pipe].sdp == 0) {
   1283 		aprint_error_dev(sc->sc_dev, "uninitialized pipe %d\n",
   1284 		    pipe);
   1285 		return;
   1286 	}
   1287 
   1288 	dmabuf = dd->dmabase;
   1289 	dmabase = sc->sc_dmabase;
   1290 	td = 0;
   1291 
   1292 	for (i = 0; i < (num - 1); i++) {
   1293 
   1294 		sc->sc_dma->xmit[i].flags = TX_BCNT(blksz)
   1295 		    | TX_EOF | TX_BINT;
   1296 		sc->sc_dma->xmit[i].ba = dmabuf;
   1297 		sc->sc_dma->xmit[i].nda = dmabase + dbri_dma_off(xmit, i + 1);
   1298 		sc->sc_dma->xmit[i].status = 0;
   1299 
   1300 		td_last = td;
   1301 		dmabuf += blksz;
   1302 	}
   1303 
   1304 	sc->sc_dma->xmit[i].flags = TX_BCNT(blksz) | TX_EOF | TX_BINT;
   1305 
   1306 	sc->sc_dma->xmit[i].ba = dmabuf;
   1307 	sc->sc_dma->xmit[i].nda = dmabase + dbri_dma_off(xmit, 0);
   1308 	sc->sc_dma->xmit[i].status = 0;
   1309 
   1310 	dd->callback = callback;
   1311 	dd->callback_args = callback_args;
   1312 
   1313 	x = splsched();
   1314 
   1315 	/* the pipe shouldn't be active */
   1316 	if (pipe_active(sc, pipe)) {
   1317 		aprint_error("pipe active (CDP)\n");
   1318 		/* pipe is already active */
   1319 #if 0
   1320 		td_last = sc->sc_pipe[pipe].desc;
   1321 		while (sc->sc_desc[td_last].next != -1)
   1322 			td_last = sc->sc_desc[td_last].next;
   1323 
   1324 		sc->sc_desc[td_last].next = td_first;
   1325 		sc->sc_dma->desc[td_last].nda =
   1326 		    sc->sc_dmabase + dbri_dma_off(desc, td_first);
   1327 
   1328 		cmd = dbri_command_lock(sc);
   1329 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_CDP, 0, pipe);
   1330 		dbri_command_send(sc, cmd);
   1331 #endif
   1332 	} else {
   1333 		/*
   1334 		 * pipe isn't active - issue an SDP command to start our
   1335 		 * chain of TDs running
   1336 		 */
   1337 		sc->sc_pipe[pipe].desc = which;
   1338 		cmd = dbri_command_lock(sc);
   1339 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_SDP, 0,
   1340 					sc->sc_pipe[pipe].sdp |
   1341 					DBRI_SDP_VALID_POINTER |
   1342 					DBRI_SDP_EVERY |
   1343 					DBRI_SDP_CLEAR);
   1344 		*(cmd++) = sc->sc_dmabase + dbri_dma_off(xmit, 0);
   1345 		dbri_command_send(sc, cmd);
   1346 		DPRINTF("%s: starting DMA\n", __func__);
   1347 	}
   1348 
   1349 	splx(x);
   1350 
   1351 	return;
   1352 }
   1353 
   1354 static void
   1355 setup_ring_recv(struct dbri_softc *sc, int pipe, int which, int num, int blksz,
   1356 		void (*callback)(void *), void *callback_args)
   1357 {
   1358 	volatile uint32_t *cmd;
   1359 	int x, i;
   1360 	int td_first, td_last;
   1361 	bus_addr_t dmabuf, dmabase;
   1362 	struct dbri_desc *dd = &sc->sc_desc[which];
   1363 
   1364 	switch (pipe) {
   1365 		case 6:
   1366 			break;
   1367 		default:
   1368 			aprint_error("%s: illegal pipe number (%d)\n",
   1369 			    __func__, pipe);
   1370 			return;
   1371 	}
   1372 
   1373 	td_first = td_last = -1;
   1374 
   1375 	if (sc->sc_pipe[pipe].sdp == 0) {
   1376 		aprint_error_dev(sc->sc_dev, "uninitialized pipe %d\n",
   1377 		    pipe);
   1378 		return;
   1379 	}
   1380 
   1381 	dmabuf = dd->dmabase;
   1382 	dmabase = sc->sc_dmabase;
   1383 
   1384 	for (i = 0; i < (num - 1); i++) {
   1385 
   1386 		sc->sc_dma->recv[i].flags = RX_BSIZE(blksz) | RX_FINAL;
   1387 		sc->sc_dma->recv[i].ba = dmabuf;
   1388 		sc->sc_dma->recv[i].nda = dmabase + dbri_dma_off(recv, i + 1);
   1389 		sc->sc_dma->recv[i].status = RX_EOF;
   1390 
   1391 		td_last = i;
   1392 		dmabuf += blksz;
   1393 	}
   1394 
   1395 	sc->sc_dma->recv[i].flags = RX_BSIZE(blksz) | RX_FINAL;
   1396 
   1397 	sc->sc_dma->recv[i].ba = dmabuf;
   1398 	sc->sc_dma->recv[i].nda = dmabase + dbri_dma_off(recv, 0);
   1399 	sc->sc_dma->recv[i].status = RX_EOF;
   1400 
   1401 	dd->callback = callback;
   1402 	dd->callback_args = callback_args;
   1403 
   1404 	x = splsched();
   1405 
   1406 	/* the pipe shouldn't be active */
   1407 	if (pipe_active(sc, pipe)) {
   1408 		aprint_error("pipe active (CDP)\n");
   1409 		/* pipe is already active */
   1410 #if 0
   1411 		td_last = sc->sc_pipe[pipe].desc;
   1412 		while (sc->sc_desc[td_last].next != -1)
   1413 			td_last = sc->sc_desc[td_last].next;
   1414 
   1415 		sc->sc_desc[td_last].next = td_first;
   1416 		sc->sc_dma->desc[td_last].nda =
   1417 		    sc->sc_dmabase + dbri_dma_off(desc, td_first);
   1418 
   1419 		cmd = dbri_command_lock(sc);
   1420 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_CDP, 0, pipe);
   1421 		dbri_command_send(sc, cmd);
   1422 #endif
   1423 	} else {
   1424 		/*
   1425 		 * pipe isn't active - issue an SDP command to start our
   1426 		 * chain of TDs running
   1427 		 */
   1428 		sc->sc_pipe[pipe].desc = which;
   1429 		cmd = dbri_command_lock(sc);
   1430 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_SDP, 0,
   1431 					sc->sc_pipe[pipe].sdp |
   1432 					DBRI_SDP_VALID_POINTER |
   1433 					DBRI_SDP_EVERY |
   1434 					DBRI_SDP_CLEAR);
   1435 		*(cmd++) = sc->sc_dmabase + dbri_dma_off(recv, 0);
   1436 		dbri_command_send(sc, cmd);
   1437 		DPRINTF("%s: starting DMA\n", __func__);
   1438 	}
   1439 
   1440 	splx(x);
   1441 
   1442 	return;
   1443 }
   1444 
   1445 static void
   1446 pipe_ts_link(struct dbri_softc *sc, int pipe, enum io dir, int basepipe,
   1447 		int len, int cycle)
   1448 {
   1449 	volatile uint32_t *cmd;
   1450 	int prevpipe, nextpipe;
   1451 	int val;
   1452 
   1453 	DPRINTF("%s: %d\n", __func__, pipe);
   1454 	if (pipe < 0 || pipe >= DBRI_PIPE_MAX ||
   1455 	    basepipe < 0 || basepipe >= DBRI_PIPE_MAX) {
   1456 		aprint_error_dev(sc->sc_dev, "illegal pipe numbers (%d, %d)\n",
   1457 		    pipe, basepipe);
   1458 		return;
   1459 	}
   1460 
   1461 	if (sc->sc_pipe[pipe].sdp == 0 || sc->sc_pipe[basepipe].sdp == 0) {
   1462 		aprint_error_dev(sc->sc_dev, "uninitialized pipe (%d, %d)\n",
   1463 		    pipe, basepipe);
   1464 		return;
   1465 	}
   1466 
   1467 	if (basepipe == 16 && dir == PIPEoutput && cycle == 0)
   1468 		cycle = sc->sc_chi_bpf;
   1469 
   1470 	if (basepipe == pipe)
   1471 		prevpipe = nextpipe = pipe;
   1472 	else {
   1473 		if (basepipe == 16) {
   1474 			if (dir == PIPEinput) {
   1475 				prevpipe = sc->sc_chi_pipe_in;
   1476 			} else {
   1477 				prevpipe = sc->sc_chi_pipe_out;
   1478 			}
   1479 		} else
   1480 			prevpipe = basepipe;
   1481 
   1482 		nextpipe = sc->sc_pipe[prevpipe].next;
   1483 
   1484 		while (sc->sc_pipe[nextpipe].cycle < cycle &&
   1485 		    sc->sc_pipe[nextpipe].next != basepipe) {
   1486 			prevpipe = nextpipe;
   1487 			nextpipe = sc->sc_pipe[nextpipe].next;
   1488 		}
   1489 	}
   1490 
   1491 	if (prevpipe == 16) {
   1492 		if (dir == PIPEinput) {
   1493 			sc->sc_chi_pipe_in = pipe;
   1494 		} else {
   1495 			sc->sc_chi_pipe_out = pipe;
   1496 		}
   1497 	} else
   1498 		sc->sc_pipe[prevpipe].next = pipe;
   1499 
   1500 	sc->sc_pipe[pipe].next = nextpipe;
   1501 	sc->sc_pipe[pipe].cycle = cycle;
   1502 	sc->sc_pipe[pipe].length = len;
   1503 
   1504 	cmd = dbri_command_lock(sc);
   1505 
   1506 	switch (dir) {
   1507 	case PIPEinput:
   1508 		val = DBRI_DTS_VI | DBRI_DTS_INS | DBRI_DTS_PRVIN(prevpipe);
   1509 		val |= pipe;
   1510 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_DTS, 0, val);
   1511 		*(cmd++) = DBRI_TS_LEN(len) | DBRI_TS_CYCLE(cycle) |
   1512 		    DBRI_TS_NEXT(nextpipe);
   1513 		*(cmd++) = 0;
   1514 		break;
   1515 	case PIPEoutput:
   1516 		val = DBRI_DTS_VO | DBRI_DTS_INS | DBRI_DTS_PRVOUT(prevpipe);
   1517 		val |= pipe;
   1518 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_DTS, 0, val);
   1519 		*(cmd++) = 0;
   1520 		*(cmd++) = DBRI_TS_LEN(len) | DBRI_TS_CYCLE(cycle) |
   1521 		    DBRI_TS_NEXT(nextpipe);
   1522 		break;
   1523 	default:
   1524 		DPRINTF("%s: should not have happened!\n",
   1525 		    device_xname(sc->sc_dev));
   1526 		break;
   1527 	}
   1528 
   1529 	dbri_command_send(sc, cmd);
   1530 
   1531 	return;
   1532 }
   1533 
   1534 static int
   1535 pipe_active(struct dbri_softc *sc, int pipe)
   1536 {
   1537 
   1538 	return (sc->sc_pipe[pipe].desc != -1);
   1539 }
   1540 
   1541 /*
   1542  * subroutines required to interface with audio(9)
   1543  */
   1544 
   1545 static int
   1546 dbri_query_encoding(void *hdl, struct audio_encoding *ae)
   1547 {
   1548 
   1549 	switch (ae->index) {
   1550 	case 0:
   1551 		strcpy(ae->name, AudioEulinear);
   1552 		ae->encoding = AUDIO_ENCODING_ULINEAR;
   1553 		ae->precision = 8;
   1554 		ae->flags = 0;
   1555 		break;
   1556 	case 1:
   1557 		strcpy(ae->name, AudioEmulaw);
   1558 		ae->encoding = AUDIO_ENCODING_ULAW;
   1559 		ae->precision = 8;
   1560 		ae->flags = 0;
   1561 		break;
   1562 	case 2:
   1563 		strcpy(ae->name, AudioEalaw);
   1564 		ae->encoding = AUDIO_ENCODING_ALAW;
   1565 		ae->precision = 8;
   1566 		ae->flags = 0;
   1567 		break;
   1568 	case 3:
   1569 		strcpy(ae->name, AudioEslinear);
   1570 		ae->encoding = AUDIO_ENCODING_SLINEAR;
   1571 		ae->precision = 8;
   1572 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1573 		break;
   1574 	case 4:
   1575 		strcpy(ae->name, AudioEslinear_le);
   1576 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
   1577 		ae->precision = 16;
   1578 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1579 		break;
   1580 	case 5:
   1581 		strcpy(ae->name, AudioEulinear_le);
   1582 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
   1583 		ae->precision = 16;
   1584 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1585 		break;
   1586 	case 6:
   1587 		strcpy(ae->name, AudioEslinear_be);
   1588 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
   1589 		ae->precision = 16;
   1590 		ae->flags = 0;
   1591 		break;
   1592 	case 7:
   1593 		strcpy(ae->name, AudioEulinear_be);
   1594 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
   1595 		ae->precision = 16;
   1596 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
   1597 		break;
   1598 	case 8:
   1599 		strcpy(ae->name, AudioEslinear);
   1600 		ae->encoding = AUDIO_ENCODING_SLINEAR;
   1601 		ae->precision = 16;
   1602 		ae->flags = 0;
   1603 		break;
   1604 	default:
   1605 		return (EINVAL);
   1606 	}
   1607 
   1608 	return (0);
   1609 }
   1610 
   1611 static int
   1612 dbri_set_params(void *hdl, int setmode, int usemode,
   1613 		struct audio_params *play, struct audio_params *rec,
   1614 		stream_filter_list_t *pfil, stream_filter_list_t *rfil)
   1615 {
   1616 	struct dbri_softc *sc = hdl;
   1617 	int rate;
   1618 	audio_params_t *p = NULL;
   1619 	stream_filter_list_t *fil;
   1620 	int mode;
   1621 
   1622 	/*
   1623 	 * This device only has one clock, so make the sample rates match.
   1624 	 */
   1625 	if (play->sample_rate != rec->sample_rate &&
   1626 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
   1627 		if (setmode == AUMODE_PLAY) {
   1628 			rec->sample_rate = play->sample_rate;
   1629 			setmode |= AUMODE_RECORD;
   1630 		} else if (setmode == AUMODE_RECORD) {
   1631 			play->sample_rate = rec->sample_rate;
   1632 			setmode |= AUMODE_PLAY;
   1633 		} else
   1634 			return EINVAL;
   1635 	}
   1636 
   1637 	for (mode = AUMODE_RECORD; mode != -1;
   1638 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
   1639 		if ((setmode & mode) == 0)
   1640 			continue;
   1641 
   1642 		p = mode == AUMODE_PLAY ? play : rec;
   1643 		if (p->sample_rate < 4000 || p->sample_rate > 50000) {
   1644 			DPRINTF("dbri_set_params: invalid rate %d\n",
   1645 			    p->sample_rate);
   1646 			return EINVAL;
   1647 		}
   1648 
   1649 		fil = mode == AUMODE_PLAY ? pfil : rfil;
   1650 	DPRINTF("requested enc: %d rate: %d prec: %d chan: %d\n", p->encoding,
   1651 	    p->sample_rate, p->precision, p->channels);
   1652 		if (auconv_set_converter(dbri_formats, DBRI_NFORMATS,
   1653 					 mode, p, true, fil) < 0) {
   1654 			aprint_debug("dbri_set_params: auconv_set_converter failed\n");
   1655 			return EINVAL;
   1656 		}
   1657 		if (fil->req_size > 0)
   1658 			p = &fil->filters[0].param;
   1659 	}
   1660 
   1661 	if (p == NULL) {
   1662 		DPRINTF("dbri_set_params: no parameters to set\n");
   1663 		return 0;
   1664 	}
   1665 
   1666 	DPRINTF("native enc: %d rate: %d prec: %d chan: %d\n", p->encoding,
   1667 	    p->sample_rate, p->precision, p->channels);
   1668 
   1669 	for (rate = 0; CS4215_FREQ[rate].freq; rate++)
   1670 		if (CS4215_FREQ[rate].freq == p->sample_rate)
   1671 			break;
   1672 
   1673 	if (CS4215_FREQ[rate].freq == 0)
   1674 		return (EINVAL);
   1675 
   1676 	/* set frequency */
   1677 	sc->sc_mm.c.bcontrol[1] &= ~0x38;
   1678 	sc->sc_mm.c.bcontrol[1] |= CS4215_FREQ[rate].csval;
   1679 	sc->sc_mm.c.bcontrol[2] &= ~0x70;
   1680 	sc->sc_mm.c.bcontrol[2] |= CS4215_FREQ[rate].xtal;
   1681 
   1682 	switch (p->encoding) {
   1683 	case AUDIO_ENCODING_ULAW:
   1684 		sc->sc_mm.c.bcontrol[1] &= ~3;
   1685 		sc->sc_mm.c.bcontrol[1] |= CS4215_DFR_ULAW;
   1686 		break;
   1687 	case AUDIO_ENCODING_ALAW:
   1688 		sc->sc_mm.c.bcontrol[1] &= ~3;
   1689 		sc->sc_mm.c.bcontrol[1] |= CS4215_DFR_ALAW;
   1690 		break;
   1691 	case AUDIO_ENCODING_ULINEAR:
   1692 		sc->sc_mm.c.bcontrol[1] &= ~3;
   1693 		if (p->precision == 8) {
   1694 			sc->sc_mm.c.bcontrol[1] |= CS4215_DFR_LINEAR8;
   1695 		} else {
   1696 			sc->sc_mm.c.bcontrol[1] |= CS4215_DFR_LINEAR16;
   1697 		}
   1698 		break;
   1699 	case AUDIO_ENCODING_SLINEAR_BE:
   1700 	case AUDIO_ENCODING_SLINEAR:
   1701 		sc->sc_mm.c.bcontrol[1] &= ~3;
   1702 		sc->sc_mm.c.bcontrol[1] |= CS4215_DFR_LINEAR16;
   1703 		break;
   1704 	}
   1705 
   1706 	switch (p->channels) {
   1707 	case 1:
   1708 		sc->sc_mm.c.bcontrol[1] &= ~CS4215_DFR_STEREO;
   1709 		break;
   1710 	case 2:
   1711 		sc->sc_mm.c.bcontrol[1] |= CS4215_DFR_STEREO;
   1712 		break;
   1713 	}
   1714 
   1715 	return (0);
   1716 }
   1717 
   1718 static int
   1719 dbri_round_blocksize(void *hdl, int bs, int mode,
   1720 			const audio_params_t *param)
   1721 {
   1722 
   1723 	/* DBRI DMA segment size, rounded down to 32bit alignment */
   1724 	return 0x1ffc;
   1725 }
   1726 
   1727 static int
   1728 dbri_halt_output(void *hdl)
   1729 {
   1730 	struct dbri_softc *sc = hdl;
   1731 
   1732 	if (!sc->sc_playing)
   1733 		return 0;
   1734 
   1735 	sc->sc_playing = 0;
   1736 	pipe_reset(sc, 4);
   1737 	return (0);
   1738 }
   1739 
   1740 static int
   1741 dbri_getdev(void *hdl, struct audio_device *ret)
   1742 {
   1743 
   1744 	*ret = dbri_device;
   1745 	return (0);
   1746 }
   1747 
   1748 static int
   1749 dbri_set_port(void *hdl, mixer_ctrl_t *mc)
   1750 {
   1751 	struct dbri_softc *sc = hdl;
   1752 	int latt = sc->sc_latt, ratt = sc->sc_ratt;
   1753 
   1754 	switch (mc->dev) {
   1755 	    case DBRI_VOL_OUTPUT:	/* master volume */
   1756 		latt = (latt & 0xc0) | (63 -
   1757 		    min(mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] >> 2, 63));
   1758 		ratt = (ratt & 0xc0) | (63 -
   1759 		    min(mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] >> 2, 63));
   1760 		break;
   1761 	    case DBRI_ENABLE_MONO:	/* built-in speaker */
   1762 	    	if (mc->un.ord == 1) {
   1763 			ratt |= CS4215_SE;
   1764 		} else
   1765 			ratt &= ~CS4215_SE;
   1766 		break;
   1767 	    case DBRI_ENABLE_HEADPHONE:	/* headphones output */
   1768 	    	if (mc->un.ord == 1) {
   1769 			latt |= CS4215_HE;
   1770 		} else
   1771 			latt &= ~CS4215_HE;
   1772 		break;
   1773 	    case DBRI_ENABLE_LINE:	/* line out */
   1774 	    	if (mc->un.ord == 1) {
   1775 			latt |= CS4215_LE;
   1776 		} else
   1777 			latt &= ~CS4215_LE;
   1778 		break;
   1779 	    case DBRI_VOL_MONITOR:
   1780 		if (mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] ==
   1781 		    sc->sc_monitor)
   1782 			return 0;
   1783 		sc->sc_monitor = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1784 		break;
   1785 	    case DBRI_INPUT_GAIN:
   1786 		sc->sc_linp = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1787 		sc->sc_rinp = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1788 		break;
   1789 	    case DBRI_INPUT_SELECT:
   1790 	    	if (mc->un.mask == sc->sc_input)
   1791 	    		return 0;
   1792 	    	sc->sc_input =  mc->un.mask;
   1793 	    	break;
   1794 	}
   1795 
   1796 	sc->sc_latt = latt;
   1797 	sc->sc_ratt = ratt;
   1798 
   1799 	mmcodec_setgain(sc, 0);
   1800 
   1801 	return (0);
   1802 }
   1803 
   1804 static int
   1805 dbri_get_port(void *hdl, mixer_ctrl_t *mc)
   1806 {
   1807 	struct dbri_softc *sc = hdl;
   1808 
   1809 	switch (mc->dev) {
   1810 	    case DBRI_VOL_OUTPUT:	/* master volume */
   1811 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
   1812 		    (63 - (sc->sc_latt & 0x3f)) << 2;
   1813 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
   1814 		    (63 - (sc->sc_ratt & 0x3f)) << 2;
   1815 		return (0);
   1816 	    case DBRI_ENABLE_MONO:	/* built-in speaker */
   1817 	    	mc->un.ord = (sc->sc_ratt & CS4215_SE) ? 1 : 0;
   1818 		return 0;
   1819 	    case DBRI_ENABLE_HEADPHONE:	/* headphones output */
   1820 	    	mc->un.ord = (sc->sc_latt & CS4215_HE) ? 1 : 0;
   1821 		return 0;
   1822 	    case DBRI_ENABLE_LINE:	/* line out */
   1823 	    	mc->un.ord = (sc->sc_latt & CS4215_LE) ? 1 : 0;
   1824 		return 0;
   1825 	    case DBRI_VOL_MONITOR:
   1826 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_monitor;
   1827 		return 0;
   1828 	    case DBRI_INPUT_GAIN:
   1829 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_linp;
   1830 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_rinp;
   1831 		return 0;
   1832 	    case DBRI_INPUT_SELECT:
   1833 	    	mc->un.mask = sc->sc_input;
   1834 	    	return 0;
   1835 	}
   1836 	return (EINVAL);
   1837 }
   1838 
   1839 static int
   1840 dbri_query_devinfo(void *hdl, mixer_devinfo_t *di)
   1841 {
   1842 
   1843 	switch (di->index) {
   1844 	case DBRI_MONITOR_CLASS:
   1845 		di->mixer_class = DBRI_MONITOR_CLASS;
   1846 		strcpy(di->label.name, AudioCmonitor);
   1847 		di->type = AUDIO_MIXER_CLASS;
   1848 		di->next = di->prev = AUDIO_MIXER_LAST;
   1849 		return 0;
   1850 	case DBRI_OUTPUT_CLASS:
   1851 		di->mixer_class = DBRI_OUTPUT_CLASS;
   1852 		strcpy(di->label.name, AudioCoutputs);
   1853 		di->type = AUDIO_MIXER_CLASS;
   1854 		di->next = di->prev = AUDIO_MIXER_LAST;
   1855 		return 0;
   1856 	case DBRI_INPUT_CLASS:
   1857 		di->mixer_class = DBRI_INPUT_CLASS;
   1858 		strcpy(di->label.name, AudioCinputs);
   1859 		di->type = AUDIO_MIXER_CLASS;
   1860 		di->next = di->prev = AUDIO_MIXER_LAST;
   1861 		return 0;
   1862 	case DBRI_VOL_OUTPUT:	/* master volume */
   1863 		di->mixer_class = DBRI_OUTPUT_CLASS;
   1864 		di->next = di->prev = AUDIO_MIXER_LAST;
   1865 		strcpy(di->label.name, AudioNmaster);
   1866 		di->type = AUDIO_MIXER_VALUE;
   1867 		di->un.v.num_channels = 2;
   1868 		strcpy(di->un.v.units.name, AudioNvolume);
   1869 		return (0);
   1870 	case DBRI_INPUT_GAIN:	/* input gain */
   1871 		di->mixer_class = DBRI_INPUT_CLASS;
   1872 		di->next = di->prev = AUDIO_MIXER_LAST;
   1873 		strcpy(di->label.name, AudioNrecord);
   1874 		di->type = AUDIO_MIXER_VALUE;
   1875 		di->un.v.num_channels = 2;
   1876 		strcpy(di->un.v.units.name, AudioNvolume);
   1877 		return (0);
   1878 	case DBRI_VOL_MONITOR:	/* monitor volume */
   1879 		di->mixer_class = DBRI_MONITOR_CLASS;
   1880 		di->next = di->prev = AUDIO_MIXER_LAST;
   1881 		strcpy(di->label.name, AudioNmonitor);
   1882 		di->type = AUDIO_MIXER_VALUE;
   1883 		di->un.v.num_channels = 1;
   1884 		strcpy(di->un.v.units.name, AudioNvolume);
   1885 		return (0);
   1886 	case DBRI_ENABLE_MONO:	/* built-in speaker */
   1887 		di->mixer_class = DBRI_OUTPUT_CLASS;
   1888 		di->next = di->prev = AUDIO_MIXER_LAST;
   1889 		strcpy(di->label.name, AudioNmono);
   1890 		di->type = AUDIO_MIXER_ENUM;
   1891 		di->un.e.num_mem = 2;
   1892 		strcpy(di->un.e.member[0].label.name, AudioNoff);
   1893 		di->un.e.member[0].ord = 0;
   1894 		strcpy(di->un.e.member[1].label.name, AudioNon);
   1895 		di->un.e.member[1].ord = 1;
   1896 		return (0);
   1897 	case DBRI_ENABLE_HEADPHONE:	/* headphones output */
   1898 		di->mixer_class = DBRI_OUTPUT_CLASS;
   1899 		di->next = di->prev = AUDIO_MIXER_LAST;
   1900 		strcpy(di->label.name, AudioNheadphone);
   1901 		di->type = AUDIO_MIXER_ENUM;
   1902 		di->un.e.num_mem = 2;
   1903 		strcpy(di->un.e.member[0].label.name, AudioNoff);
   1904 		di->un.e.member[0].ord = 0;
   1905 		strcpy(di->un.e.member[1].label.name, AudioNon);
   1906 		di->un.e.member[1].ord = 1;
   1907 		return (0);
   1908 	case DBRI_ENABLE_LINE:	/* line out */
   1909 		di->mixer_class = DBRI_OUTPUT_CLASS;
   1910 		di->next = di->prev = AUDIO_MIXER_LAST;
   1911 		strcpy(di->label.name, AudioNline);
   1912 		di->type = AUDIO_MIXER_ENUM;
   1913 		di->un.e.num_mem = 2;
   1914 		strcpy(di->un.e.member[0].label.name, AudioNoff);
   1915 		di->un.e.member[0].ord = 0;
   1916 		strcpy(di->un.e.member[1].label.name, AudioNon);
   1917 		di->un.e.member[1].ord = 1;
   1918 		return (0);
   1919 	case DBRI_INPUT_SELECT:
   1920 		di->mixer_class = DBRI_INPUT_CLASS;
   1921 		strcpy(di->label.name, AudioNsource);
   1922 		di->type = AUDIO_MIXER_SET;
   1923 		di->prev = di->next = AUDIO_MIXER_LAST;
   1924 		di->un.s.num_mem = 2;
   1925 		strcpy(di->un.s.member[0].label.name, AudioNline);
   1926 		di->un.s.member[0].mask = 1 << 0;
   1927 		strcpy(di->un.s.member[1].label.name, AudioNmicrophone);
   1928 		di->un.s.member[1].mask = 1 << 1;
   1929 		return 0;
   1930 	}
   1931 
   1932 	return (ENXIO);
   1933 }
   1934 
   1935 static size_t
   1936 dbri_round_buffersize(void *hdl, int dir, size_t bufsize)
   1937 {
   1938 #ifdef DBRI_BIG_BUFFER
   1939 	return 16*0x1ffc;	/* use ~128KB buffer */
   1940 #else
   1941 	return bufsize;
   1942 #endif
   1943 }
   1944 
   1945 static int
   1946 dbri_get_props(void *hdl)
   1947 {
   1948 
   1949 	return AUDIO_PROP_MMAP | AUDIO_PROP_FULLDUPLEX;
   1950 }
   1951 
   1952 static int
   1953 dbri_trigger_output(void *hdl, void *start, void *end, int blksize,
   1954 		    void (*intr)(void *), void *intrarg,
   1955 		    const struct audio_params *param)
   1956 {
   1957 	struct dbri_softc *sc = hdl;
   1958 	unsigned long count, num;
   1959 
   1960 	if (sc->sc_playing)
   1961 		return 0;
   1962 
   1963 	count = (unsigned long)(((char *)end - (char *)start));
   1964 	num = count / blksize;
   1965 
   1966 	DPRINTF("trigger_output(%lx %lx) : %d %ld %ld\n",
   1967 	    (unsigned long)intr,
   1968 	    (unsigned long)intrarg, blksize, count, num);
   1969 
   1970 	sc->sc_params = *param;
   1971 
   1972 	if (sc->sc_recording == 0) {
   1973 		/* do not muck with the codec when it's already in use */
   1974 		if (mmcodec_setcontrol(sc) != 0)
   1975 			return -1;
   1976 		mmcodec_init_data(sc);
   1977 	}
   1978 
   1979 	/*
   1980 	 * always use DMA descriptor 0 for output
   1981 	 * no need to allocate them dynamically since we only ever have
   1982 	 * exactly one input stream and exactly one output stream
   1983 	 */
   1984 	setup_ring_xmit(sc, 4, 0, num, blksize, intr, intrarg);
   1985 	sc->sc_playing = 1;
   1986 	return 0;
   1987 }
   1988 
   1989 static int
   1990 dbri_halt_input(void *cookie)
   1991 {
   1992 	struct dbri_softc *sc = cookie;
   1993 
   1994 	if (!sc->sc_recording)
   1995 		return 0;
   1996 
   1997 	sc->sc_recording = 0;
   1998 	pipe_reset(sc, 6);
   1999 	return 0;
   2000 }
   2001 
   2002 static int
   2003 dbri_trigger_input(void *hdl, void *start, void *end, int blksize,
   2004 		    void (*intr)(void *), void *intrarg,
   2005 		    const struct audio_params *param)
   2006 {
   2007 	struct dbri_softc *sc = hdl;
   2008 	unsigned long count, num;
   2009 
   2010 	if (sc->sc_recording)
   2011 		return 0;
   2012 
   2013 	count = (unsigned long)(((char *)end - (char *)start));
   2014 	num = count / blksize;
   2015 
   2016 	DPRINTF("trigger_input(%lx %lx) : %d %ld %ld\n",
   2017 	    (unsigned long)intr,
   2018 	    (unsigned long)intrarg, blksize, count, num);
   2019 
   2020 	sc->sc_params = *param;
   2021 
   2022 	if (sc->sc_playing == 0) {
   2023 
   2024 		/*
   2025 		 * we don't support different parameters for playing and
   2026 		 * recording anyway so don't bother whacking the codec if
   2027 		 * it's already set up
   2028 		 */
   2029 		mmcodec_setcontrol(sc);
   2030 		mmcodec_init_data(sc);
   2031 	}
   2032 
   2033 	sc->sc_recording = 1;
   2034 	setup_ring_recv(sc, 6, 1, num, blksize, intr, intrarg);
   2035 	return 0;
   2036 }
   2037 
   2038 
   2039 static uint32_t
   2040 reverse_bytes(uint32_t b, int len)
   2041 {
   2042 	switch (len) {
   2043 	case 32:
   2044 		b = ((b & 0xffff0000) >> 16) | ((b & 0x0000ffff) << 16);
   2045 	case 16:
   2046 		b = ((b & 0xff00ff00) >>  8) | ((b & 0x00ff00ff) <<  8);
   2047 	case 8:
   2048 		b = ((b & 0xf0f0f0f0) >>  4) | ((b & 0x0f0f0f0f) <<  4);
   2049 	case 4:
   2050 		b = ((b & 0xcccccccc) >>  2) | ((b & 0x33333333) <<  2);
   2051 	case 2:
   2052 		b = ((b & 0xaaaaaaaa) >>  1) | ((b & 0x55555555) <<  1);
   2053 	case 1:
   2054 	case 0:
   2055 		break;
   2056 	default:
   2057 		DPRINTF("reverse_bytes: unsupported length\n");
   2058 	};
   2059 
   2060 	return (b);
   2061 }
   2062 
   2063 static void *
   2064 dbri_malloc(void *v, int dir, size_t s, struct malloc_type *mt, int flags)
   2065 {
   2066 	struct dbri_softc *sc = v;
   2067 	struct dbri_desc *dd = &sc->sc_desc[sc->sc_desc_used];
   2068 	int rseg;
   2069 
   2070 	if (bus_dmamap_create(sc->sc_dmat, s, 1, s, 0, BUS_DMA_NOWAIT,
   2071 	    &dd->dmamap) == 0) {
   2072 		if (bus_dmamem_alloc(sc->sc_dmat, s, 0, 0, &dd->dmaseg,
   2073 		    1, &rseg, BUS_DMA_NOWAIT) == 0) {
   2074 			if (bus_dmamem_map(sc->sc_dmat, &dd->dmaseg, rseg, s,
   2075 			    &dd->buf, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) == 0) {
   2076 				if (dd->buf != NULL) {
   2077 					if (bus_dmamap_load(sc->sc_dmat,
   2078 					    dd->dmamap, dd->buf, s, NULL,
   2079 					    BUS_DMA_NOWAIT) == 0) {
   2080 						dd->len = s;
   2081 						dd->busy = 0;
   2082 						dd->callback = NULL;
   2083 						dd->dmabase =
   2084 						 dd->dmamap->dm_segs[0].ds_addr;
   2085 						DPRINTF("dbri_malloc: using buffer %d %08x\n",
   2086 						    sc->sc_desc_used, (uint32_t)dd->buf);
   2087 						sc->sc_desc_used++;
   2088 						return dd->buf;
   2089 					} else
   2090 						aprint_error("dbri_malloc: load failed\n");
   2091 				} else
   2092 					aprint_error("dbri_malloc: map returned NULL\n");
   2093 			} else
   2094 				aprint_error("dbri_malloc: map failed\n");
   2095 			bus_dmamem_free(sc->sc_dmat, &dd->dmaseg, rseg);
   2096 		} else
   2097 			aprint_error("dbri_malloc: malloc() failed\n");
   2098 		bus_dmamap_destroy(sc->sc_dmat, dd->dmamap);
   2099 	} else
   2100 		aprint_error("dbri_malloc: bus_dmamap_create() failed\n");
   2101 	return NULL;
   2102 }
   2103 
   2104 static void
   2105 dbri_free(void *v, void *p, struct malloc_type *mt)
   2106 {
   2107 	struct dbri_softc *sc = v;
   2108 	struct dbri_desc *dd;
   2109 	int i;
   2110 
   2111 	for (i = 0; i < sc->sc_desc_used; i++) {
   2112 		dd = &sc->sc_desc[i];
   2113 		if (dd->buf == p)
   2114 			break;
   2115 	}
   2116 	if (i >= sc->sc_desc_used)
   2117 		return;
   2118 	bus_dmamap_unload(sc->sc_dmat, dd->dmamap);
   2119 	bus_dmamap_destroy(sc->sc_dmat, dd->dmamap);
   2120 }
   2121 
   2122 static paddr_t
   2123 dbri_mappage(void *v, void *mem, off_t off, int prot)
   2124 {
   2125 	struct dbri_softc *sc = v;
   2126 	int current;
   2127 
   2128 	if (off < 0)
   2129 		return -1;
   2130 
   2131 	current = 0;
   2132 	while ((current < sc->sc_desc_used) &&
   2133 	    (sc->sc_desc[current].buf != mem))
   2134 	    	current++;
   2135 
   2136 	if (current < sc->sc_desc_used) {
   2137 		return bus_dmamem_mmap(sc->sc_dmat,
   2138 		    &sc->sc_desc[current].dmaseg, 1, off, prot, BUS_DMA_WAITOK);
   2139 	}
   2140 
   2141 	return -1;
   2142 }
   2143 
   2144 static int
   2145 dbri_open(void *cookie, int flags)
   2146 {
   2147 	struct dbri_softc *sc = cookie;
   2148 
   2149 	DPRINTF("%s: %d\n", __func__, sc->sc_refcount);
   2150 
   2151 	if (sc->sc_refcount == 0)
   2152 		dbri_bring_up(sc);
   2153 
   2154 	sc->sc_refcount++;
   2155 
   2156 	return 0;
   2157 }
   2158 
   2159 static void
   2160 dbri_close(void *cookie)
   2161 {
   2162 	struct dbri_softc *sc = cookie;
   2163 
   2164 	DPRINTF("%s: %d\n", __func__, sc->sc_refcount);
   2165 
   2166 	sc->sc_refcount--;
   2167 	KASSERT(sc->sc_refcount >= 0);
   2168 	if (sc->sc_refcount > 0)
   2169 		return;
   2170 
   2171 	dbri_set_power(sc, 0);
   2172 	sc->sc_playing = 0;
   2173 	sc->sc_recording = 0;
   2174 }
   2175 
   2176 static bool
   2177 dbri_suspend(device_t self PMF_FN_ARGS)
   2178 {
   2179 	struct dbri_softc *sc = device_private(self);
   2180 
   2181 	dbri_set_power(sc, 0);
   2182 	return true;
   2183 }
   2184 
   2185 static bool
   2186 dbri_resume(device_t self PMF_FN_ARGS)
   2187 {
   2188 	if (sc->sc_powerstate != 0)
   2189 		break;
   2190 	aprint_verbose("resume: %d\n", sc->sc_refcount);
   2191 	if (sc->sc_playing) {
   2192 		volatile uint32_t *cmd;
   2193 		int s;
   2194 
   2195 		dbri_bring_up(sc);
   2196 		s = splsched();
   2197 		cmd = dbri_command_lock(sc);
   2198 		*(cmd++) = DBRI_CMD(DBRI_COMMAND_SDP,
   2199 		    0, sc->sc_pipe[4].sdp |
   2200 		    DBRI_SDP_VALID_POINTER |
   2201 		    DBRI_SDP_EVERY | DBRI_SDP_CLEAR);
   2202 		*(cmd++) = sc->sc_dmabase +
   2203 		    dbri_dma_off(xmit, 0);
   2204 		dbri_command_send(sc, cmd);
   2205 		splx(s);
   2206 	}
   2207 	return true;
   2208 }
   2209 
   2210 #endif /* NAUDIO > 0 */
   2211