Home | History | Annotate | Line # | Download | only in sbus
cs4231_sbus.c revision 1.40
      1  1.40   martin /*	$NetBSD: cs4231_sbus.c,v 1.40 2008/04/29 14:46:57 martin 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.40   martin __KERNEL_RCSID(0, "$NetBSD: cs4231_sbus.c,v 1.40 2008/04/29 14:46:57 martin 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.36       ad 	    cs4231_sbus_pint, sbsc);
    166  1.39       ad 	sbsc->sc_rint = sparc_softintr_establish(IPL_VM,
    167  1.36       ad 	    cs4231_sbus_rint, sbsc);
    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.18      uwe 	printf("apc_dmacsr=%s\n",
    228  1.19      eeh 		bitmask_snprintf(
    229  1.29      uwe 			bus_space_read_4(sc->sc_bh, sc->sc_bh, APC_DMA_CSR),
    230  1.19      eeh 				APC_BITS, bits, sizeof(bits)));
    231  1.18      uwe 
    232  1.18      uwe 	ad1848_dump_regs(&sc->sc_cs4231.sc_ad1848);
    233  1.18      uwe }
    234  1.18      uwe #endif /* AUDIO_DEBUG */
    235  1.18      uwe 
    236  1.18      uwe 
    237  1.18      uwe static int
    238  1.32     kent cs4231_sbus_trigger_output(void *addr, void *start, void *end, int blksize,
    239  1.32     kent 			   void (*intr)(void *), void *arg,
    240  1.32     kent 			   const audio_params_t *param)
    241  1.18      uwe {
    242  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    243  1.32     kent 	struct cs4231_softc *sc;
    244  1.32     kent 	struct cs_transfer *t;
    245  1.32     kent 	uint32_t csr;
    246  1.18      uwe 	bus_addr_t dmaaddr;
    247  1.18      uwe 	bus_size_t dmasize;
    248  1.18      uwe 	int ret;
    249  1.18      uwe #ifdef AUDIO_DEBUG
    250  1.18      uwe 	char bits[128];
    251  1.18      uwe #endif
    252  1.18      uwe 
    253  1.32     kent 	sbsc = addr;
    254  1.32     kent 	sc = &sbsc->sc_cs4231;
    255  1.32     kent 	t = &sc->sc_playback;
    256  1.18      uwe 	ret = cs4231_transfer_init(sc, t, &dmaaddr, &dmasize,
    257  1.18      uwe 				   start, end, blksize, intr, arg);
    258  1.18      uwe 	if (ret != 0)
    259  1.32     kent 		return ret;
    260  1.18      uwe 
    261  1.18      uwe 	DPRINTF(("trigger_output: was: %x %d, %x %d\n",
    262  1.32     kent 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PVA),
    263  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PC),
    264  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA),
    265  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC)));
    266  1.18      uwe 
    267  1.18      uwe 	/* load first block */
    268  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA, dmaaddr);
    269  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC, dmasize);
    270  1.18      uwe 
    271  1.18      uwe 	DPRINTF(("trigger_output: 1st: %x %d, %x %d\n",
    272  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PVA),
    273  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PC),
    274  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA),
    275  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC)));
    276  1.18      uwe 
    277  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    278  1.19      eeh 	DPRINTF(("trigger_output: csr=%s\n",
    279  1.19      eeh 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    280  1.18      uwe 	if ((csr & PDMA_GO) == 0 || (csr & APC_PPAUSE) != 0) {
    281  1.18      uwe 		int cfg;
    282  1.18      uwe 
    283  1.19      eeh 		csr &= ~(APC_PPAUSE | APC_PMIE | APC_INTR_MASK);
    284  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    285  1.18      uwe 
    286  1.29      uwe 		csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    287  1.19      eeh 		csr &= ~APC_INTR_MASK;
    288  1.18      uwe 		csr |= APC_ENABLE | APC_PIE | APC_PMIE | PDMA_GO;
    289  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    290  1.18      uwe 
    291  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_LOWER_BASE_COUNT, 0xff);
    292  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_UPPER_BASE_COUNT, 0xff);
    293  1.18      uwe 
    294  1.18      uwe 		cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    295  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG,
    296  1.18      uwe 			 (cfg | PLAYBACK_ENABLE));
    297  1.18      uwe 	} else {
    298  1.18      uwe 		DPRINTF(("trigger_output: already: csr=%s\n",
    299  1.18      uwe 			 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    300  1.18      uwe 	}
    301  1.18      uwe 
    302  1.18      uwe 	/* load next block if we can */
    303  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    304  1.19      eeh 	if (csr & APC_PD) {
    305  1.21      mrg 		cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    306  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA, dmaaddr);
    307  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC, dmasize);
    308  1.19      eeh 
    309  1.21      mrg 		DPRINTF(("trigger_output: 2nd: %x %d, %x %d\n",
    310  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PVA),
    311  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PC),
    312  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNVA),
    313  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_PNC)));
    314  1.18      uwe 	}
    315  1.18      uwe 
    316  1.32     kent 	return 0;
    317  1.18      uwe }
    318  1.18      uwe 
    319  1.18      uwe 
    320  1.18      uwe static int
    321  1.32     kent cs4231_sbus_halt_output(void *addr)
    322  1.18      uwe {
    323  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    324  1.32     kent 	struct cs4231_softc *sc;
    325  1.32     kent 	uint32_t csr;
    326  1.18      uwe 	int cfg;
    327  1.18      uwe #ifdef AUDIO_DEBUG
    328  1.18      uwe 	char bits[128];
    329  1.18      uwe #endif
    330  1.18      uwe 
    331  1.32     kent 	sbsc = addr;
    332  1.32     kent 	sc = &sbsc->sc_cs4231;
    333  1.18      uwe 	sc->sc_playback.t_active = 0;
    334  1.18      uwe 
    335  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    336  1.18      uwe 	DPRINTF(("halt_output: csr=%s\n",
    337  1.19      eeh 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    338  1.18      uwe 
    339  1.18      uwe 	csr &= ~APC_INTR_MASK;	/* do not clear interrupts accidentally */
    340  1.18      uwe 	csr |= APC_PPAUSE;	/* pause playback (let current complete) */
    341  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    342  1.32     kent 
    343  1.18      uwe 	/* let the curernt transfer complete */
    344  1.18      uwe 	if (csr & PDMA_GO)
    345  1.18      uwe 		do {
    346  1.32     kent 			csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh,
    347  1.29      uwe 				APC_DMA_CSR);
    348  1.18      uwe 			DPRINTF(("halt_output: csr=%s\n",
    349  1.19      eeh 				 bitmask_snprintf(csr, APC_BITS,
    350  1.18      uwe 						  bits, sizeof(bits))));
    351  1.18      uwe 		} while ((csr & APC_PM) == 0);
    352   1.1       pk 
    353  1.18      uwe 	cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    354  1.18      uwe 	ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG,(cfg & ~PLAYBACK_ENABLE));
    355  1.18      uwe 
    356  1.32     kent 	return 0;
    357   1.1       pk }
    358  1.18      uwe 
    359  1.18      uwe 
    360  1.18      uwe /* NB: we don't enable APC_CMIE and won't use APC_CM */
    361  1.18      uwe static int
    362  1.32     kent cs4231_sbus_trigger_input(void *addr, void *start, void *end, int blksize,
    363  1.32     kent 			  void (*intr)(void *), void *arg,
    364  1.32     kent 			  const audio_params_t *param)
    365  1.18      uwe {
    366  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    367  1.32     kent 	struct cs4231_softc *sc;
    368  1.32     kent 	struct cs_transfer *t;
    369  1.32     kent 	uint32_t csr;
    370  1.18      uwe 	bus_addr_t dmaaddr;
    371  1.18      uwe 	bus_size_t dmasize;
    372  1.18      uwe 	int ret;
    373  1.18      uwe #ifdef AUDIO_DEBUG
    374  1.18      uwe 	char bits[128];
    375   1.1       pk #endif
    376  1.18      uwe 
    377  1.32     kent 	sbsc = addr;
    378  1.32     kent 	sc = &sbsc->sc_cs4231;
    379  1.32     kent 	t = &sc->sc_capture;
    380  1.18      uwe 	ret = cs4231_transfer_init(sc, t, &dmaaddr, &dmasize,
    381  1.18      uwe 				   start, end, blksize, intr, arg);
    382  1.18      uwe 	if (ret != 0)
    383  1.32     kent 		return ret;
    384  1.18      uwe 
    385  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    386  1.18      uwe 	DPRINTF(("trigger_input: csr=%s\n",
    387  1.18      uwe 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    388  1.18      uwe 	DPRINTF(("trigger_input: was: %x %d, %x %d\n",
    389  1.32     kent 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CVA),
    390  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CC),
    391  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA),
    392  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC)));
    393  1.18      uwe 
    394  1.18      uwe 	/* supply first block */
    395  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA, dmaaddr);
    396  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC, dmasize);
    397  1.18      uwe 
    398  1.18      uwe 	DPRINTF(("trigger_input: 1st: %x %d, %x %d\n",
    399  1.32     kent 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CVA),
    400  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CC),
    401  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA),
    402  1.29      uwe 		bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC)));
    403  1.18      uwe 
    404  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    405  1.18      uwe 	if ((csr & CDMA_GO) == 0 || (csr & APC_CPAUSE) != 0) {
    406  1.18      uwe 		int cfg;
    407  1.18      uwe 
    408  1.19      eeh 		csr &= ~(APC_CPAUSE | APC_CMIE | APC_INTR_MASK);
    409  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    410  1.18      uwe 
    411  1.29      uwe 		csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    412  1.19      eeh 		csr &= ~APC_INTR_MASK;
    413  1.18      uwe 		csr |= APC_ENABLE | APC_CIE | CDMA_GO;
    414  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    415  1.18      uwe 
    416  1.18      uwe 		ad_write(&sc->sc_ad1848, CS_LOWER_REC_CNT, 0xff);
    417  1.18      uwe 		ad_write(&sc->sc_ad1848, CS_UPPER_REC_CNT, 0xff);
    418  1.18      uwe 
    419  1.18      uwe 		cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    420  1.18      uwe 		ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG,
    421  1.18      uwe 			 (cfg | CAPTURE_ENABLE));
    422  1.18      uwe 	} else {
    423  1.18      uwe 		DPRINTF(("trigger_input: already: csr=%s\n",
    424  1.18      uwe 			 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    425  1.18      uwe 	}
    426  1.18      uwe 
    427  1.18      uwe 	/* supply next block if we can */
    428  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    429  1.21      mrg 	if (csr & APC_CD) {
    430  1.21      mrg 		cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    431  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA, dmaaddr);
    432  1.29      uwe 		bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC, dmasize);
    433  1.21      mrg 		DPRINTF(("trigger_input: 2nd: %x %d, %x %d\n",
    434  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CVA),
    435  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CC),
    436  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNVA),
    437  1.29      uwe 		    bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CNC)));
    438  1.18      uwe 	}
    439  1.18      uwe 
    440  1.32     kent 	return 0;
    441  1.18      uwe }
    442  1.18      uwe 
    443  1.18      uwe 
    444  1.18      uwe static int
    445  1.32     kent cs4231_sbus_halt_input(void *addr)
    446  1.18      uwe {
    447  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    448  1.32     kent 	struct cs4231_softc *sc;
    449  1.32     kent 	uint32_t csr;
    450  1.18      uwe 	int cfg;
    451  1.18      uwe #ifdef AUDIO_DEBUG
    452  1.18      uwe 	char bits[128];
    453  1.18      uwe #endif
    454  1.18      uwe 
    455  1.32     kent 	sbsc = addr;
    456  1.32     kent 	sc = &sbsc->sc_cs4231;
    457  1.18      uwe 	sc->sc_capture.t_active = 0;
    458  1.18      uwe 
    459  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    460  1.18      uwe 	DPRINTF(("halt_input: csr=%s\n",
    461  1.19      eeh 		 bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits))));
    462  1.18      uwe 
    463  1.18      uwe 	csr &= ~APC_INTR_MASK;	/* do not clear interrupts accidentally */
    464  1.18      uwe 	csr |= APC_CPAUSE;
    465  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    466  1.18      uwe 
    467  1.18      uwe 	/* let the curernt transfer complete */
    468  1.18      uwe 	if (csr & CDMA_GO)
    469  1.18      uwe 		do {
    470  1.19      eeh 			csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh,
    471  1.29      uwe 				APC_DMA_CSR);
    472  1.18      uwe 			DPRINTF(("halt_input: csr=%s\n",
    473  1.19      eeh 				 bitmask_snprintf(csr, APC_BITS,
    474  1.18      uwe 						  bits, sizeof(bits))));
    475  1.18      uwe 		} while ((csr & APC_CM) == 0);
    476  1.18      uwe 
    477  1.18      uwe 	cfg = ad_read(&sc->sc_ad1848, SP_INTERFACE_CONFIG);
    478  1.18      uwe 	ad_write(&sc->sc_ad1848, SP_INTERFACE_CONFIG, (cfg & ~CAPTURE_ENABLE));
    479  1.18      uwe 
    480  1.32     kent 	return 0;
    481  1.18      uwe }
    482  1.18      uwe 
    483  1.18      uwe 
    484  1.18      uwe static int
    485  1.32     kent cs4231_sbus_intr(void *arg)
    486  1.18      uwe {
    487  1.32     kent 	struct cs4231_sbus_softc *sbsc;
    488  1.32     kent 	struct cs4231_softc *sc;
    489  1.32     kent 	uint32_t csr;
    490  1.18      uwe 	int status;
    491  1.18      uwe 	bus_addr_t dmaaddr;
    492  1.18      uwe 	bus_size_t dmasize;
    493  1.18      uwe 	int served;
    494  1.18      uwe #if defined(AUDIO_DEBUG) || defined(DIAGNOSTIC)
    495  1.18      uwe 	char bits[128];
    496  1.18      uwe #endif
    497  1.18      uwe 
    498  1.32     kent 	sbsc = arg;
    499  1.32     kent 	sc = &sbsc->sc_cs4231;
    500  1.29      uwe 	csr = bus_space_read_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR);
    501  1.18      uwe 	if ((csr & APC_INTR_MASK) == 0)	/* any interrupt pedning? */
    502  1.32     kent 		return 0;
    503  1.18      uwe 
    504  1.19      eeh 	/* write back DMA status to clear interrupt */
    505  1.29      uwe 	bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh, APC_DMA_CSR, csr);
    506  1.18      uwe 	++sc->sc_intrcnt.ev_count;
    507  1.18      uwe 	served = 0;
    508  1.18      uwe 
    509  1.18      uwe #ifdef AUDIO_DEBUG
    510  1.18      uwe 	if (cs4231_sbus_debug > 1)
    511  1.18      uwe 		cs4231_sbus_regdump("audiointr", sbsc);
    512  1.18      uwe #endif
    513  1.18      uwe 
    514  1.18      uwe 	status = ADREAD(&sc->sc_ad1848, AD1848_STATUS);
    515  1.37   cegger 	DPRINTF(("%s: status: %s\n", device_xname(&sc->sc_ad1848.sc_dev),
    516  1.18      uwe 		bitmask_snprintf(status, AD_R2_BITS, bits, sizeof(bits))));
    517  1.18      uwe 	if (status & INTERRUPT_STATUS) {
    518  1.18      uwe #ifdef AUDIO_DEBUG
    519  1.18      uwe 		int reason;
    520  1.18      uwe 
    521  1.18      uwe 		reason = ad_read(&sc->sc_ad1848, CS_IRQ_STATUS);
    522  1.37   cegger 		DPRINTF(("%s: i24: %s\n", device_xname(&sc->sc_ad1848.sc_dev),
    523  1.18      uwe 		  bitmask_snprintf(reason, CS_I24_BITS, bits, sizeof(bits))));
    524  1.18      uwe #endif
    525  1.18      uwe 		/* clear ad1848 interrupt */
    526  1.18      uwe 		ADWRITE(&sc->sc_ad1848, AD1848_STATUS, 0);
    527  1.18      uwe 	}
    528  1.32     kent 
    529  1.18      uwe 	if (csr & APC_CI) {
    530  1.18      uwe 		if (csr & APC_CD) { /* can supply new block */
    531  1.18      uwe 			struct cs_transfer *t = &sc->sc_capture;
    532  1.18      uwe 
    533  1.18      uwe 			cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    534  1.19      eeh 			bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    535  1.29      uwe 				APC_DMA_CNVA, dmaaddr);
    536  1.19      eeh 			bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    537  1.29      uwe 				APC_DMA_CNC, dmasize);
    538  1.18      uwe 
    539  1.18      uwe 			if (t->t_intr != NULL)
    540  1.39       ad 				sparc_softintr_schedule(sbsc->sc_rint);
    541  1.18      uwe 			++t->t_intrcnt.ev_count;
    542  1.18      uwe 			served = 1;
    543  1.18      uwe 		}
    544  1.18      uwe 	}
    545  1.18      uwe 
    546  1.18      uwe 	if (csr & APC_PMI) {
    547  1.18      uwe 		if (!sc->sc_playback.t_active)
    548  1.18      uwe 			served = 1; /* draining in halt_output() */
    549  1.18      uwe 	}
    550  1.18      uwe 
    551  1.18      uwe 	if (csr & APC_PI) {
    552  1.18      uwe 		if (csr & APC_PD) { /* can load new block */
    553  1.18      uwe 			struct cs_transfer *t = &sc->sc_playback;
    554  1.18      uwe 
    555  1.18      uwe 			if (t->t_active) {
    556  1.18      uwe 				cs4231_transfer_advance(t, &dmaaddr, &dmasize);
    557  1.19      eeh 				bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    558  1.29      uwe 					APC_DMA_PNVA, dmaaddr);
    559  1.19      eeh 				bus_space_write_4(sbsc->sc_bt, sbsc->sc_bh,
    560  1.29      uwe 					APC_DMA_PNC, dmasize);
    561  1.18      uwe 			}
    562  1.18      uwe 
    563  1.18      uwe 			if (t->t_intr != NULL)
    564  1.39       ad 				sparc_softintr_schedule(sbsc->sc_pint);
    565  1.18      uwe 			++t->t_intrcnt.ev_count;
    566  1.18      uwe 			served = 1;
    567  1.18      uwe 		}
    568  1.18      uwe 	}
    569  1.18      uwe 
    570  1.18      uwe 	/* got an interrupt we don't know how to handle */
    571  1.18      uwe 	if (!served) {
    572  1.18      uwe #ifdef DIAGNOSTIC
    573  1.37   cegger 		printf("%s: unhandled csr=%s\n", device_xname(&sc->sc_ad1848.sc_dev),
    574  1.18      uwe 		       bitmask_snprintf(csr, APC_BITS, bits, sizeof(bits)));
    575  1.18      uwe #endif
    576  1.18      uwe 		/* evcnt? */
    577  1.18      uwe 	}
    578  1.18      uwe 
    579  1.32     kent 	return 1;
    580  1.18      uwe }
    581  1.18      uwe 
    582  1.40   martin static int
    583  1.36       ad cs4231_sbus_pint(void *cookie)
    584  1.36       ad {
    585  1.36       ad 	struct cs4231_softc *sc = cookie;
    586  1.39       ad 	struct cs_transfer *t;
    587  1.36       ad 
    588  1.39       ad 	KERNEL_LOCK(1, NULL);
    589  1.39       ad 	t = &sc->sc_playback;
    590  1.36       ad 	if (t->t_intr != NULL)
    591  1.36       ad 		(*t->t_intr)(t->t_arg);
    592  1.39       ad 	KERNEL_UNLOCK_ONE(NULL);
    593  1.40   martin 	return 0;
    594  1.36       ad }
    595  1.36       ad 
    596  1.40   martin static int
    597  1.36       ad cs4231_sbus_rint(void *cookie)
    598  1.36       ad {
    599  1.36       ad 	struct cs4231_softc *sc = cookie;
    600  1.39       ad 	struct cs_transfer *t;
    601  1.36       ad 
    602  1.39       ad 	KERNEL_LOCK(1, NULL);
    603  1.39       ad 	t = &sc->sc_capture;
    604  1.36       ad 	if (t->t_intr != NULL)
    605  1.36       ad 		(*t->t_intr)(t->t_arg);
    606  1.39       ad 	KERNEL_UNLOCK_ONE(NULL);
    607  1.40   martin 	return 0;
    608  1.36       ad }
    609  1.36       ad 
    610  1.18      uwe #endif /* NAUDIO > 0 */
    611