Home | History | Annotate | Line # | Download | only in sbus
cs4231_sbus.c revision 1.37
      1  1.37   cegger /*	$NetBSD: cs4231_sbus.c,v 1.37 2008/04/05 18:35:31 cegger Exp $	*/
      2   1.2       pk 
      3   1.2       pk /*-
      4  1.36       ad  * Copyright (c) 1998, 1999, 2002, 2007 The NetBSD Foundation, Inc.
      5   1.2       pk  * All rights reserved.
      6   1.2       pk  *
      7   1.2       pk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2       pk  * by Paul Kranenburg.
      9   1.2       pk  *
     10   1.2       pk  * Redistribution and use in source and binary forms, with or without
     11   1.2       pk  * modification, are permitted provided that the following conditions
     12   1.2       pk  * are met:
     13   1.2       pk  * 1. Redistributions of source code must retain the above copyright
     14   1.2       pk  *    notice, this list of conditions and the following disclaimer.
     15   1.2       pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2       pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.2       pk  *    documentation and/or other materials provided with the distribution.
     18   1.2       pk  * 3. All advertising materials mentioning features or use of this software
     19   1.2       pk  *    must display the following acknowledgement:
     20   1.2       pk  *        This product includes software developed by the NetBSD
     21   1.2       pk  *        Foundation, Inc. and its contributors.
     22   1.2       pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.2       pk  *    contributors may be used to endorse or promote products derived
     24   1.2       pk  *    from this software without specific prior written permission.
     25   1.2       pk  *
     26   1.2       pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.2       pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.2       pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.2       pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.2       pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.2       pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.2       pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.2       pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.2       pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.2       pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.2       pk  * POSSIBILITY OF SUCH DAMAGE.
     37   1.2       pk  */
     38  1.16    lukem 
     39  1.16    lukem #include <sys/cdefs.h>
     40  1.37   cegger __KERNEL_RCSID(0, "$NetBSD: cs4231_sbus.c,v 1.37 2008/04/05 18:35:31 cegger Exp $");
     41   1.1       pk 
     42   1.1       pk #include "audio.h"
     43   1.1       pk #if NAUDIO > 0
     44   1.1       pk 
     45   1.1       pk #include <sys/param.h>
     46   1.1       pk #include <sys/systm.h>
     47   1.1       pk #include <sys/errno.h>
     48   1.1       pk #include <sys/device.h>
     49   1.1       pk #include <sys/malloc.h>
     50  1.35       ad #include <sys/bus.h>
     51  1.35       ad #include <sys/intr.h>
     52  1.15       pk 
     53  1.15       pk #include <dev/sbus/sbusvar.h>
     54   1.1       pk 
     55   1.1       pk #include <sys/audioio.h>
     56   1.1       pk #include <dev/audio_if.h>
     57   1.1       pk 
     58   1.1       pk #include <dev/ic/ad1848reg.h>
     59   1.1       pk #include <dev/ic/cs4231reg.h>
     60   1.1       pk #include <dev/ic/ad1848var.h>
     61  1.11      mrg #include <dev/ic/cs4231var.h>
     62   1.1       pk 
     63  1.18      uwe #include <dev/ic/apcdmareg.h>
     64   1.1       pk 
     65  1.18      uwe #ifdef AUDIO_DEBUG
     66  1.18      uwe int cs4231_sbus_debug = 0;
     67  1.18      uwe #define DPRINTF(x)      if (cs4231_sbus_debug) printf x
     68  1.18      uwe #else
     69  1.18      uwe #define DPRINTF(x)
     70  1.18      uwe #endif
     71  1.18      uwe 
     72  1.18      uwe /* where APC DMA registers are located */
     73  1.18      uwe #define CS4231_APCDMA_OFFSET	16
     74  1.18      uwe 
     75  1.18      uwe /* interrupt enable bits except those specific for playback/capture */
     76  1.18      uwe #define APC_ENABLE		(APC_EI | APC_IE | APC_EIE)
     77  1.18      uwe 
     78  1.18      uwe struct cs4231_sbus_softc {
     79  1.18      uwe 	struct cs4231_softc sc_cs4231;
     80  1.18      uwe 
     81  1.36       ad 	void *sc_pint;
     82  1.36       ad 	void *sc_rint;
     83  1.18      uwe 	struct sbusdev sc_sd;			/* sbus device */
     84  1.19      eeh 	bus_space_tag_t sc_bt;			/* DMA controller tag */
     85  1.19      eeh 	bus_space_handle_t sc_bh;		/* DMA controller registers */
     86  1.18      uwe };
     87  1.18      uwe 
     88  1.18      uwe 
     89  1.18      uwe static int	cs4231_sbus_match(struct device *, struct cfdata *, void *);
     90  1.18      uwe static void	cs4231_sbus_attach(struct device *, struct device *, void *);
     91  1.36       ad static void	cs4231_sbus_pint(void *);
     92  1.36       ad static void	cs4231_sbus_rint(void *);
     93   1.1       pk 
     94  1.23  thorpej CFATTACH_DECL(audiocs_sbus, sizeof(struct cs4231_sbus_softc),
     95  1.24  thorpej     cs4231_sbus_match, cs4231_sbus_attach, NULL, NULL);
     96  1.18      uwe 
     97  1.28      wiz /* audio_hw_if methods specific to apc DMA */
     98  1.18      uwe static int	cs4231_sbus_trigger_output(void *, void *, void *, int,
     99  1.18      uwe 					   void (*)(void *), void *,
    100  1.31     kent 					   const audio_params_t *);
    101  1.18      uwe static int	cs4231_sbus_trigger_input(void *, void *, void *, int,
    102  1.18      uwe 					  void (*)(void *), void *,
    103  1.31     kent 					  const audio_params_t *);
    104  1.18      uwe static int	cs4231_sbus_halt_output(void *);
    105  1.18      uwe static int	cs4231_sbus_halt_input(void *);
    106  1.18      uwe 
    107  1.30     yamt const struct audio_hw_if audiocs_sbus_hw_if = {
    108  1.18      uwe 	cs4231_open,
    109  1.18      uwe 	cs4231_close,
    110  1.18      uwe 	NULL,			/* drain */
    111  1.18      uwe 	ad1848_query_encoding,
    112  1.18      uwe 	ad1848_set_params,
    113  1.29      uwe 	NULL,			/* round_blocksize */
    114  1.18      uwe 	ad1848_commit_settings,
    115  1.18      uwe 	NULL,			/* init_output */
    116  1.18      uwe 	NULL,			/* init_input */
    117  1.18      uwe 	NULL,			/* start_output */
    118  1.18      uwe 	NULL,			/* start_input */
    119  1.18      uwe 	cs4231_sbus_halt_output,
    120  1.18      uwe 	cs4231_sbus_halt_input,
    121  1.18      uwe 	NULL,			/* speaker_ctl */
    122  1.18      uwe 	cs4231_getdev,
    123  1.18      uwe 	NULL,			/* setfd */
    124  1.18      uwe 	cs4231_set_port,
    125  1.18      uwe 	cs4231_get_port,
    126  1.18      uwe 	cs4231_query_devinfo,
    127  1.18      uwe 	cs4231_malloc,
    128  1.18      uwe 	cs4231_free,
    129  1.29      uwe 	NULL,			/* round_buffersize */
    130  1.32     kent 	NULL,			/* mappage */
    131  1.18      uwe 	cs4231_get_props,
    132  1.18      uwe 	cs4231_sbus_trigger_output,
    133  1.18      uwe 	cs4231_sbus_trigger_input,
    134  1.18      uwe 	NULL,			/* dev_ioctl */
    135  1.34   martin 	NULL,			/* powerstate */
    136   1.1       pk };
    137   1.1       pk 
    138   1.1       pk 
    139  1.18      uwe #ifdef AUDIO_DEBUG
    140  1.18      uwe static void	cs4231_sbus_regdump(char *, struct cs4231_sbus_softc *);
    141  1.18      uwe #endif
    142  1.18      uwe 
    143  1.18      uwe static int	cs4231_sbus_intr(void *);
    144  1.18      uwe 
    145  1.18      uwe 
    146  1.18      uwe 
    147  1.18      uwe static int
    148  1.32     kent cs4231_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
    149   1.1       pk {
    150  1.32     kent 	struct sbus_attach_args *sa;
    151   1.1       pk 
    152  1.32     kent 	sa = aux;
    153  1.32     kent 	return strcmp(sa->sa_name, AUDIOCS_PROM_NAME) == 0;
    154   1.1       pk }
    155   1.1       pk 
    156  1.18      uwe 
    157  1.18      uwe static void
    158  1.32     kent cs4231_sbus_attach(struct device *parent, struct device *self, void *aux)
    159   1.1       pk {
    160  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    161  1.32     kent 	struct cs4231_softc *sc;
    162  1.32     kent 	struct sbus_attach_args *sa;
    163   1.1       pk 	bus_space_handle_t bh;
    164   1.1       pk 
    165  1.32     kent 	sbsc = (struct cs4231_sbus_softc *)self;
    166  1.32     kent 	sc = &sbsc->sc_cs4231;
    167  1.32     kent 	sa = aux;
    168  1.19      eeh 	sbsc->sc_bt = sc->sc_bustag = sa->sa_bustag;
    169   1.1       pk 	sc->sc_dmatag = sa->sa_dmatag;
    170   1.1       pk 
    171  1.36       ad 	sbsc->sc_pint = softint_establish(SOFTINT_SERIAL,
    172  1.36       ad 	    cs4231_sbus_pint, sbsc);
    173  1.36       ad 	sbsc->sc_rint = softint_establish(SOFTINT_SERIAL,
    174  1.36       ad 	    cs4231_sbus_rint, sbsc);
    175  1.36       ad 
    176   1.1       pk 	/*
    177   1.1       pk 	 * Map my registers in, if they aren't already in virtual
    178   1.1       pk 	 * address space.
    179   1.1       pk 	 */
    180   1.1       pk 	if (sa->sa_npromvaddrs) {
    181  1.19      eeh 		sbus_promaddr_to_handle(sa->sa_bustag,
    182  1.19      eeh 			sa->sa_promvaddrs[0], &bh);
    183   1.1       pk 	} else {
    184  1.19      eeh 		if (sbus_bus_map(sa->sa_bustag,	sa->sa_slot,
    185  1.19      eeh 			sa->sa_offset, sa->sa_size, 0, &bh) != 0) {
    186  1.37   cegger 			aprint_error("%s @ sbus: cannot map registers\n",
    187  1.37   cegger 				device_xname(self));
    188   1.1       pk 			return;
    189   1.1       pk 		}
    190   1.1       pk 	}
    191   1.1       pk 
    192  1.19      eeh 	bus_space_subregion(sa->sa_bustag, bh, CS4231_APCDMA_OFFSET,
    193  1.29      uwe 		APC_DMA_SIZE, &sbsc->sc_bh);
    194   1.1       pk 
    195  1.18      uwe 	cs4231_common_attach(sc, bh);
    196   1.1       pk 	printf("\n");
    197   1.1       pk 
    198  1.18      uwe 	sbus_establish(&sbsc->sc_sd, &sc->sc_ad1848.sc_dev);
    199   1.1       pk 
    200   1.1       pk 	/* Establish interrupt channel */
    201  1.12       pk 	if (sa->sa_nintr)
    202  1.12       pk 		bus_intr_establish(sa->sa_bustag,
    203  1.36       ad 				   sa->sa_pri, IPL_SCHED,
    204  1.18      uwe 				   cs4231_sbus_intr, sbsc);
    205  1.18      uwe 
    206  1.18      uwe 	audio_attach_mi(&audiocs_sbus_hw_if, sbsc, &sc->sc_ad1848.sc_dev);
    207  1.18      uwe }
    208  1.18      uwe 
    209  1.18      uwe 
    210  1.18      uwe #ifdef AUDIO_DEBUG
    211  1.18      uwe static void
    212  1.32     kent cs4231_sbus_regdump(char *label, struct cs4231_sbus_softc *sc)
    213  1.18      uwe {
    214  1.18      uwe 	char bits[128];
    215  1.18      uwe 
    216  1.18      uwe 	printf("cs4231regdump(%s): regs:", label);
    217  1.19      eeh 	printf("dmapva: 0x%x; ",
    218  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_PVA));
    219  1.19      eeh 	printf("dmapc: 0x%x; ",
    220  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_PC));
    221  1.19      eeh 	printf("dmapnva: 0x%x; ",
    222  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_PNVA));
    223  1.19      eeh 	printf("dmapnc: 0x%x\n",
    224  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_PNC));
    225  1.32     kent 	printf("dmacva: 0x%x; ",
    226  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_CVA));
    227  1.32     kent 	printf("dmacc: 0x%x; ",
    228  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_CC));
    229  1.32     kent 	printf("dmacnva: 0x%x; ",
    230  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_CNVA));
    231  1.32     kent 	printf("dmacnc: 0x%x\n",
    232  1.29      uwe 		bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_CNC));
    233  1.18      uwe 
    234  1.18      uwe 	printf("apc_dmacsr=%s\n",
    235  1.19      eeh 		bitmask_snprintf(
    236  1.29      uwe 			bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_CSR),
    237  1.19      eeh 				APC_BITS, bits, sizeof(bits)));
    238  1.18      uwe 
    239  1.18      uwe 	ad1848_dump_regs(&sc->sc_cs4231.sc_ad1848);
    240  1.18      uwe }
    241  1.18      uwe #endif /* AUDIO_DEBUG */
    242  1.18      uwe 
    243  1.18      uwe 
    244  1.18      uwe static int
    245  1.32     kent cs4231_sbus_trigger_output(void *addr, void *start, void *end, int blksize,
    246  1.32     kent 			   void (*intr)(void *), void *arg,
    247  1.32     kent 			   const audio_params_t *param)
    248  1.18      uwe {
    249  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    250  1.32     kent 	struct cs4231_softc *sc;
    251  1.32     kent 	struct cs_transfer *t;
    252  1.32     kent 	uint32_t csr;
    253  1.18      uwe 	bus_addr_t dmaaddr;
    254  1.18      uwe 	bus_size_t dmasize;
    255  1.18      uwe 	int ret;
    256  1.18      uwe #ifdef AUDIO_DEBUG
    257  1.18      uwe 	char bits[128];
    258  1.18      uwe #endif
    259  1.18      uwe 
    260  1.32     kent 	sbsc = addr;
    261  1.32     kent 	sc = &sbsc->sc_cs4231;
    262  1.32     kent 	t = &sc->sc_playback;
    263  1.18      uwe 	ret = cs4231_transfer_init(sc, t, &dmaaddr, &dmasize,
    264  1.18      uwe 				   start, end, blksize, intr, arg);
    265  1.18      uwe 	if (ret != 0)
    266  1.32     kent 		return ret;
    267  1.18      uwe 
    268  1.18      uwe 	DPRINTF(("trigger_output: was: %x %d, %x %d\n",
    269  1.32     kent 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PVA),
    270  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PC),
    271  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA),
    272  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC)));
    273  1.18      uwe 
    274  1.18      uwe 	/* load first block */
    275  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA, dmaaddr);
    276  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC, dmasize);
    277  1.18      uwe 
    278  1.18      uwe 	DPRINTF(("trigger_output: 1st: %x %d, %x %d\n",
    279  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PVA),
    280  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PC),
    281  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA),
    282  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC)));
    283  1.18      uwe 
    284  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    285  1.19      eeh 	DPRINTF(("trigger_output: csr=%s\n",
    286  1.19      eeh 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    287  1.18      uwe 	if ((csr & PDMA_GO) == 0 || (csr & APC_PPAUSE) != 0) {
    288  1.18      uwe 		int cfg;
    289  1.18      uwe 
    290  1.19      eeh 		csr &= ~(APC_PPAUSE | APC_PMIE | APC_INTR_MASK);
    291  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    292  1.18      uwe 
    293  1.29      uwe 		csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    294  1.19      eeh 		csr &= ~APC_INTR_MASK;
    295  1.18      uwe 		csr |= APC_ENABLE | APC_PIE | APC_PMIE | PDMA_GO;
    296  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    297  1.18      uwe 
    298  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_LOWER_BASE_COUNT, 0xff);
    299  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_UPPER_BASE_COUNT, 0xff);
    300  1.18      uwe 
    301  1.18      uwe 		cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    302  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG,
    303  1.18      uwe 			 (cfg | PLAYBACK_ENABLE));
    304  1.18      uwe 	} else {
    305  1.18      uwe 		DPRINTF(("trigger_output: already: csr=%s\n",
    306  1.18      uwe 			 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    307  1.18      uwe 	}
    308  1.18      uwe 
    309  1.18      uwe 	/* load next block if we can */
    310  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    311  1.19      eeh 	if (csr & APC_PD) {
    312  1.21      mrg 		cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    313  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA, dmaaddr);
    314  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC, dmasize);
    315  1.19      eeh 
    316  1.21      mrg 		DPRINTF(("trigger_output: 2nd: %x %d, %x %d\n",
    317  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PVA),
    318  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PC),
    319  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA),
    320  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC)));
    321  1.18      uwe 	}
    322  1.18      uwe 
    323  1.32     kent 	return 0;
    324  1.18      uwe }
    325  1.18      uwe 
    326  1.18      uwe 
    327  1.18      uwe static int
    328  1.32     kent cs4231_sbus_halt_output(void *addr)
    329  1.18      uwe {
    330  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    331  1.32     kent 	struct cs4231_softc *sc;
    332  1.32     kent 	uint32_t csr;
    333  1.18      uwe 	int cfg;
    334  1.18      uwe #ifdef AUDIO_DEBUG
    335  1.18      uwe 	char bits[128];
    336  1.18      uwe #endif
    337  1.18      uwe 
    338  1.32     kent 	sbsc = addr;
    339  1.32     kent 	sc = &sbsc->sc_cs4231;
    340  1.18      uwe 	sc->sc_playback.t_active = 0;
    341  1.18      uwe 
    342  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    343  1.18      uwe 	DPRINTF(("halt_output: csr=%s\n",
    344  1.19      eeh 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    345  1.18      uwe 
    346  1.18      uwe 	csr &= ~APC_INTR_MASK;	/* do not clear interrupts accidentally */
    347  1.18      uwe 	csr |= APC_PPAUSE;	/* pause playback (let current complete) */
    348  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    349  1.32     kent 
    350  1.18      uwe 	/* let the curernt transfer complete */
    351  1.18      uwe 	if (csr & PDMA_GO)
    352  1.18      uwe 		do {
    353  1.32     kent 			csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh,
    354  1.29      uwe 				APC_DMA_CSR);
    355  1.18      uwe 			DPRINTF(("halt_output: csr=%s\n",
    356  1.19      eeh 				 bitmask_snprintf(csr, APC_BITS,
    357  1.18      uwe 						  bits, sizeof(bits))));
    358  1.18      uwe 		} while ((csr & APC_PM) == 0);
    359   1.1       pk 
    360  1.18      uwe 	cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    361  1.18      uwe 	ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG,(cfg & ~PLAYBACK_ENABLE));
    362  1.18      uwe 
    363  1.32     kent 	return 0;
    364   1.1       pk }
    365  1.18      uwe 
    366  1.18      uwe 
    367  1.18      uwe /* NB: we don't enable APC_CMIE and won't use APC_CM */
    368  1.18      uwe static int
    369  1.32     kent cs4231_sbus_trigger_input(void *addr, void *start, void *end, int blksize,
    370  1.32     kent 			  void (*intr)(void *), void *arg,
    371  1.32     kent 			  const audio_params_t *param)
    372  1.18      uwe {
    373  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    374  1.32     kent 	struct cs4231_softc *sc;
    375  1.32     kent 	struct cs_transfer *t;
    376  1.32     kent 	uint32_t csr;
    377  1.18      uwe 	bus_addr_t dmaaddr;
    378  1.18      uwe 	bus_size_t dmasize;
    379  1.18      uwe 	int ret;
    380  1.18      uwe #ifdef AUDIO_DEBUG
    381  1.18      uwe 	char bits[128];
    382   1.1       pk #endif
    383  1.18      uwe 
    384  1.32     kent 	sbsc = addr;
    385  1.32     kent 	sc = &sbsc->sc_cs4231;
    386  1.32     kent 	t = &sc->sc_capture;
    387  1.18      uwe 	ret = cs4231_transfer_init(sc, t, &dmaaddr, &dmasize,
    388  1.18      uwe 				   start, end, blksize, intr, arg);
    389  1.18      uwe 	if (ret != 0)
    390  1.32     kent 		return ret;
    391  1.18      uwe 
    392  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    393  1.18      uwe 	DPRINTF(("trigger_input: csr=%s\n",
    394  1.18      uwe 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    395  1.18      uwe 	DPRINTF(("trigger_input: was: %x %d, %x %d\n",
    396  1.32     kent 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CVA),
    397  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CC),
    398  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA),
    399  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC)));
    400  1.18      uwe 
    401  1.18      uwe 	/* supply first block */
    402  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA, dmaaddr);
    403  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC, dmasize);
    404  1.18      uwe 
    405  1.18      uwe 	DPRINTF(("trigger_input: 1st: %x %d, %x %d\n",
    406  1.32     kent 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CVA),
    407  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CC),
    408  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA),
    409  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC)));
    410  1.18      uwe 
    411  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    412  1.18      uwe 	if ((csr & CDMA_GO) == 0 || (csr & APC_CPAUSE) != 0) {
    413  1.18      uwe 		int cfg;
    414  1.18      uwe 
    415  1.19      eeh 		csr &= ~(APC_CPAUSE | APC_CMIE | APC_INTR_MASK);
    416  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    417  1.18      uwe 
    418  1.29      uwe 		csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    419  1.19      eeh 		csr &= ~APC_INTR_MASK;
    420  1.18      uwe 		csr |= APC_ENABLE | APC_CIE | CDMA_GO;
    421  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    422  1.18      uwe 
    423  1.18      uwe 		ad_write(&sc->sc_ad1848, CS_LOWER_REC_CNT, 0xff);
    424  1.18      uwe 		ad_write(&sc->sc_ad1848, CS_UPPER_REC_CNT, 0xff);
    425  1.18      uwe 
    426  1.18      uwe 		cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    427  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG,
    428  1.18      uwe 			 (cfg | CAPTURE_ENABLE));
    429  1.18      uwe 	} else {
    430  1.18      uwe 		DPRINTF(("trigger_input: already: csr=%s\n",
    431  1.18      uwe 			 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    432  1.18      uwe 	}
    433  1.18      uwe 
    434  1.18      uwe 	/* supply next block if we can */
    435  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    436  1.21      mrg 	if (csr & APC_CD) {
    437  1.21      mrg 		cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    438  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA, dmaaddr);
    439  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC, dmasize);
    440  1.21      mrg 		DPRINTF(("trigger_input: 2nd: %x %d, %x %d\n",
    441  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CVA),
    442  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CC),
    443  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA),
    444  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC)));
    445  1.18      uwe 	}
    446  1.18      uwe 
    447  1.32     kent 	return 0;
    448  1.18      uwe }
    449  1.18      uwe 
    450  1.18      uwe 
    451  1.18      uwe static int
    452  1.32     kent cs4231_sbus_halt_input(void *addr)
    453  1.18      uwe {
    454  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    455  1.32     kent 	struct cs4231_softc *sc;
    456  1.32     kent 	uint32_t csr;
    457  1.18      uwe 	int cfg;
    458  1.18      uwe #ifdef AUDIO_DEBUG
    459  1.18      uwe 	char bits[128];
    460  1.18      uwe #endif
    461  1.18      uwe 
    462  1.32     kent 	sbsc = addr;
    463  1.32     kent 	sc = &sbsc->sc_cs4231;
    464  1.18      uwe 	sc->sc_capture.t_active = 0;
    465  1.18      uwe 
    466  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    467  1.18      uwe 	DPRINTF(("halt_input: csr=%s\n",
    468  1.19      eeh 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    469  1.18      uwe 
    470  1.18      uwe 	csr &= ~APC_INTR_MASK;	/* do not clear interrupts accidentally */
    471  1.18      uwe 	csr |= APC_CPAUSE;
    472  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    473  1.18      uwe 
    474  1.18      uwe 	/* let the curernt transfer complete */
    475  1.18      uwe 	if (csr & CDMA_GO)
    476  1.18      uwe 		do {
    477  1.19      eeh 			csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh,
    478  1.29      uwe 				APC_DMA_CSR);
    479  1.18      uwe 			DPRINTF(("halt_input: csr=%s\n",
    480  1.19      eeh 				 bitmask_snprintf(csr, APC_BITS,
    481  1.18      uwe 						  bits, sizeof(bits))));
    482  1.18      uwe 		} while ((csr & APC_CM) == 0);
    483  1.18      uwe 
    484  1.18      uwe 	cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    485  1.18      uwe 	ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG, (cfg & ~CAPTURE_ENABLE));
    486  1.18      uwe 
    487  1.32     kent 	return 0;
    488  1.18      uwe }
    489  1.18      uwe 
    490  1.18      uwe 
    491  1.18      uwe static int
    492  1.32     kent cs4231_sbus_intr(void *arg)
    493  1.18      uwe {
    494  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    495  1.32     kent 	struct cs4231_softc *sc;
    496  1.32     kent 	uint32_t csr;
    497  1.18      uwe 	int status;
    498  1.18      uwe 	bus_addr_t dmaaddr;
    499  1.18      uwe 	bus_size_t dmasize;
    500  1.18      uwe 	int served;
    501  1.18      uwe #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
    502  1.18      uwe 	char bits[128];
    503  1.18      uwe #endif
    504  1.18      uwe 
    505  1.32     kent 	sbsc = arg;
    506  1.32     kent 	sc = &sbsc->sc_cs4231;
    507  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    508  1.18      uwe 	if ((csr & APC_INTR_MASK) == 0)	/* any interrupt pedning? */
    509  1.32     kent 		return 0;
    510  1.18      uwe 
    511  1.19      eeh 	/* write back DMA status to clear interrupt */
    512  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    513  1.18      uwe 	++sc->sc_intrcnt.ev_count;
    514  1.18      uwe 	served = 0;
    515  1.18      uwe 
    516  1.18      uwe #ifdef AUDIO_DEBUG
    517  1.18      uwe 	if (cs4231_sbus_debug > 1)
    518  1.18      uwe 		cs4231_sbus_regdump("audiointr", sbsc);
    519  1.18      uwe #endif
    520  1.18      uwe 
    521  1.18      uwe 	status = ADREAD(&sc->sc_ad1848, AD1848_STATUS);
    522  1.37   cegger 	DPRINTF(("%s: status: %s\n", device_xname(&sc->sc_ad1848.sc_dev),
    523  1.18      uwe 		bitmask_snprintf(status, AD_R2_BITS, bits, sizeof(bits))));
    524  1.18      uwe 	if (status & INTERRUPT_STATUS) {
    525  1.18      uwe #ifdef AUDIO_DEBUG
    526  1.18      uwe 		int reason;
    527  1.18      uwe 
    528  1.18      uwe 		reason = ad_read(&sc->sc_ad1848, CS_IRQ_STATUS);
    529  1.37   cegger 		DPRINTF(("%s: i24: %s\n", device_xname(&sc->sc_ad1848.sc_dev),
    530  1.18      uwe 		  bitmask_snprintf(reason, CS_I24_BITS, bits, sizeof(bits))));
    531  1.18      uwe #endif
    532  1.18      uwe 		/* clear ad1848 interrupt */
    533  1.18      uwe 		ADWRITE(&sc->sc_ad1848, AD1848_STATUS, 0);
    534  1.18      uwe 	}
    535  1.32     kent 
    536  1.18      uwe 	if (csr & APC_CI) {
    537  1.18      uwe 		if (csr & APC_CD) { /* can supply new block */
    538  1.18      uwe 			struct cs_transfer *t = &sc->sc_capture;
    539  1.18      uwe 
    540  1.18      uwe 			cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    541  1.19      eeh 			bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    542  1.29      uwe 				APC_DMA_CNVA, dmaaddr);
    543  1.19      eeh 			bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    544  1.29      uwe 				APC_DMA_CNC, dmasize);
    545  1.18      uwe 
    546  1.18      uwe 			if (t->t_intr != NULL)
    547  1.36       ad 				softint_schedule(sbsc->sc_rint);
    548  1.18      uwe 			++t->t_intrcnt.ev_count;
    549  1.18      uwe 			served = 1;
    550  1.18      uwe 		}
    551  1.18      uwe 	}
    552  1.18      uwe 
    553  1.18      uwe 	if (csr & APC_PMI) {
    554  1.18      uwe 		if (!sc->sc_playback.t_active)
    555  1.18      uwe 			served = 1; /* draining in halt_output() */
    556  1.18      uwe 	}
    557  1.18      uwe 
    558  1.18      uwe 	if (csr & APC_PI) {
    559  1.18      uwe 		if (csr & APC_PD) { /* can load new block */
    560  1.18      uwe 			struct cs_transfer *t = &sc->sc_playback;
    561  1.18      uwe 
    562  1.18      uwe 			if (t->t_active) {
    563  1.18      uwe 				cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    564  1.19      eeh 				bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    565  1.29      uwe 					APC_DMA_PNVA, dmaaddr);
    566  1.19      eeh 				bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    567  1.29      uwe 					APC_DMA_PNC, dmasize);
    568  1.18      uwe 			}
    569  1.18      uwe 
    570  1.18      uwe 			if (t->t_intr != NULL)
    571  1.36       ad 				softint_schedule(sbsc->sc_pint);
    572  1.18      uwe 			++t->t_intrcnt.ev_count;
    573  1.18      uwe 			served = 1;
    574  1.18      uwe 		}
    575  1.18      uwe 	}
    576  1.18      uwe 
    577  1.18      uwe 	/* got an interrupt we don't know how to handle */
    578  1.18      uwe 	if (!served) {
    579  1.18      uwe #ifdef DIAGNOSTIC
    580  1.37   cegger 		printf("%s: unhandled csr=%s\n", device_xname(&sc->sc_ad1848.sc_dev),
    581  1.18      uwe 		       bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits)));
    582  1.18      uwe #endif
    583  1.18      uwe 		/* evcnt? */
    584  1.18      uwe 	}
    585  1.18      uwe 
    586  1.32     kent 	return 1;
    587  1.18      uwe }
    588  1.18      uwe 
    589  1.36       ad static void
    590  1.36       ad cs4231_sbus_pint(void *cookie)
    591  1.36       ad {
    592  1.36       ad 	struct cs4231_softc *sc = cookie;
    593  1.36       ad 	struct cs_transfer *t = &sc->sc_playback;
    594  1.36       ad 
    595  1.36       ad 	if (t->t_intr != NULL)
    596  1.36       ad 		(*t->t_intr)(t->t_arg);
    597  1.36       ad }
    598  1.36       ad 
    599  1.36       ad static void
    600  1.36       ad cs4231_sbus_rint(void *cookie)
    601  1.36       ad {
    602  1.36       ad 	struct cs4231_softc *sc = cookie;
    603  1.36       ad 	struct cs_transfer *t = &sc->sc_capture;
    604  1.36       ad 
    605  1.36       ad 	if (t->t_intr != NULL)
    606  1.36       ad 		(*t->t_intr)(t->t_arg);
    607  1.36       ad }
    608  1.36       ad 
    609  1.18      uwe #endif /* NAUDIO > 0 */
    610