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