Home | History | Annotate | Line # | Download | only in pci
cs4280.c revision 1.4.4.8
      1  1.4.4.8        he /*	$NetBSD: cs4280.c,v 1.4.4.8 2001/05/06 15:10:26 he Exp $	*/
      2      1.1  augustss 
      3      1.1  augustss /*
      4      1.2  augustss  * Copyright (c) 1999, 2000 Tatoku Ogaito.  All rights reserved.
      5      1.1  augustss  *
      6      1.1  augustss  * Redistribution and use in source and binary forms, with or without
      7      1.1  augustss  * modification, are permitted provided that the following conditions
      8      1.1  augustss  * are met:
      9      1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     10      1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     11      1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  augustss  *    documentation and/or other materials provided with the distribution.
     14      1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     15      1.1  augustss  *    must display the following acknowledgement:
     16      1.1  augustss  *	This product includes software developed by Tatoku Ogaito
     17      1.1  augustss  *	for the NetBSD Project.
     18      1.1  augustss  * 4. The name of the author may not be used to endorse or promote products
     19      1.1  augustss  *    derived from this software without specific prior written permission
     20      1.1  augustss  *
     21      1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  augustss  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1  augustss  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1  augustss  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  augustss  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1  augustss  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1  augustss  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1  augustss  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1  augustss  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1  augustss  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1  augustss  */
     32      1.1  augustss 
     33      1.1  augustss /*
     34      1.1  augustss  * Cirrus Logic CS4280 (and maybe CS461x) driver.
     35      1.1  augustss  * Data sheets can be found
     36      1.1  augustss  * http://www.cirrus.com/ftp/pubs/4280.pdf
     37      1.1  augustss  * http://www.cirrus.com/ftp/pubs/4297.pdf
     38      1.1  augustss  * ftp://ftp.alsa-project.org/pub/manuals/cirrus/embedded_audio_spec.pdf
     39      1.1  augustss  * ftp://ftp.alsa-project.org/pub/manuals/cirrus/embedded_audio_spec.doc
     40  1.4.4.2  augustss  *
     41  1.4.4.8        he  * Note:  CS4610/CS4611 + CS423x ISA codec should be worked with
     42  1.4.4.2  augustss  *	 wss* at pnpbios?
     43  1.4.4.8        he  * or
     44  1.4.4.8        he  *       sb* at pnpbios?
     45  1.4.4.8        he  * Since I could not find any documents on handling ISA codec,
     46  1.4.4.8        he  * clcs does not support those chips.
     47      1.1  augustss  */
     48      1.1  augustss 
     49      1.1  augustss /*
     50      1.1  augustss  * TODO
     51      1.1  augustss  * Joystick support
     52      1.1  augustss  */
     53      1.1  augustss 
     54  1.4.4.2  augustss #include "midi.h"
     55  1.4.4.2  augustss 
     56      1.1  augustss #include <sys/param.h>
     57      1.1  augustss #include <sys/systm.h>
     58      1.1  augustss #include <sys/kernel.h>
     59      1.1  augustss #include <sys/fcntl.h>
     60      1.1  augustss #include <sys/malloc.h>
     61      1.1  augustss #include <sys/device.h>
     62  1.4.4.8        he #include <sys/proc.h>
     63      1.1  augustss #include <sys/types.h>
     64      1.1  augustss #include <sys/systm.h>
     65      1.1  augustss 
     66      1.1  augustss #include <dev/pci/pcidevs.h>
     67      1.1  augustss #include <dev/pci/pcivar.h>
     68      1.1  augustss #include <dev/pci/cs4280reg.h>
     69      1.1  augustss #include <dev/pci/cs4280_image.h>
     70  1.4.4.8        he #include <dev/pci/cs428xreg.h>
     71      1.1  augustss 
     72      1.1  augustss #include <sys/audioio.h>
     73      1.1  augustss #include <dev/audio_if.h>
     74      1.1  augustss #include <dev/midi_if.h>
     75      1.1  augustss #include <dev/mulaw.h>
     76      1.1  augustss #include <dev/auconv.h>
     77      1.4   thorpej 
     78      1.4   thorpej #include <dev/ic/ac97reg.h>
     79      1.3   thorpej #include <dev/ic/ac97var.h>
     80      1.1  augustss 
     81  1.4.4.8        he #include <dev/pci/cs428x.h>
     82  1.4.4.8        he 
     83      1.1  augustss #include <machine/bus.h>
     84      1.1  augustss #include <machine/bswap.h>
     85      1.1  augustss 
     86      1.1  augustss #define BA1READ4(sc, r) bus_space_read_4((sc)->ba1t, (sc)->ba1h, (r))
     87      1.1  augustss #define BA1WRITE4(sc, r, x) bus_space_write_4((sc)->ba1t, (sc)->ba1h, (r), (x))
     88      1.1  augustss 
     89  1.4.4.8        he /* IF functions for audio driver */
     90  1.4.4.8        he int  cs4280_match(struct device *, struct cfdata *, void *);
     91  1.4.4.8        he void cs4280_attach(struct device *, struct device *, void *);
     92  1.4.4.8        he int  cs4280_intr(void *);
     93  1.4.4.8        he int  cs4280_query_encoding(void *, struct audio_encoding *);
     94  1.4.4.8        he int  cs4280_set_params(void *, int, int, struct audio_params *, struct audio_params *);
     95  1.4.4.8        he int  cs4280_halt_output(void *);
     96  1.4.4.8        he int  cs4280_halt_input(void *);
     97  1.4.4.8        he int  cs4280_getdev(void *, struct audio_device *);
     98  1.4.4.8        he int  cs4280_trigger_output(void *, void *, void *, int, void (*)(void *),
     99  1.4.4.8        he                            void *, struct audio_params *);
    100  1.4.4.8        he int  cs4280_trigger_input(void *, void *, void *, int, void (*)(void *),
    101  1.4.4.8        he                           void *, struct audio_params *);
    102  1.4.4.8        he 
    103  1.4.4.8        he void cs4280_reset_codec(void *);
    104  1.4.4.8        he 
    105  1.4.4.8        he /* For PowerHook */
    106  1.4.4.8        he void cs4280_power(int, void *);
    107  1.4.4.8        he 
    108  1.4.4.8        he /* Internal functions */
    109  1.4.4.8        he void cs4280_set_adc_rate(struct cs428x_softc *, int );
    110  1.4.4.8        he void cs4280_set_dac_rate(struct cs428x_softc *, int );
    111  1.4.4.8        he int  cs4280_download(struct cs428x_softc *, const u_int32_t *, u_int32_t, u_int32_t);
    112  1.4.4.8        he int  cs4280_download_image(struct cs428x_softc *);
    113  1.4.4.8        he void cs4280_reset(void *);
    114  1.4.4.8        he int  cs4280_get_portnum_by_name(struct cs428x_softc *, char *, char *, char *);
    115  1.4.4.8        he int  cs4280_init(struct cs428x_softc *, int);
    116  1.4.4.8        he void cs4280_clear_fifos(struct cs428x_softc *);
    117  1.4.4.8        he 
    118  1.4.4.8        he #if CS4280_DEBUG > 10
    119  1.4.4.8        he /* Thease two function is only for checking image loading is succeeded or not. */
    120  1.4.4.8        he int  cs4280_check_images(struct cs428x_softc *);
    121  1.4.4.8        he int  cs4280_checkimage(struct cs428x_softc *, u_int32_t *, u_int32_t, u_int32_t);
    122      1.1  augustss #endif
    123      1.1  augustss 
    124      1.1  augustss struct audio_hw_if cs4280_hw_if = {
    125  1.4.4.8        he 	cs428x_open,
    126  1.4.4.8        he 	cs428x_close,
    127      1.1  augustss 	NULL,
    128      1.1  augustss 	cs4280_query_encoding,
    129      1.1  augustss 	cs4280_set_params,
    130  1.4.4.8        he 	cs428x_round_blocksize,
    131      1.1  augustss 	NULL,
    132      1.1  augustss 	NULL,
    133      1.1  augustss 	NULL,
    134      1.1  augustss 	NULL,
    135      1.1  augustss 	NULL,
    136      1.1  augustss 	cs4280_halt_output,
    137      1.1  augustss 	cs4280_halt_input,
    138      1.1  augustss 	NULL,
    139      1.1  augustss 	cs4280_getdev,
    140      1.1  augustss 	NULL,
    141  1.4.4.8        he 	cs428x_mixer_set_port,
    142  1.4.4.8        he 	cs428x_mixer_get_port,
    143  1.4.4.8        he 	cs428x_query_devinfo,
    144  1.4.4.8        he 	cs428x_malloc,
    145  1.4.4.8        he 	cs428x_free,
    146  1.4.4.8        he 	cs428x_round_buffersize,
    147  1.4.4.8        he 	cs428x_mappage,
    148  1.4.4.8        he 	cs428x_get_props,
    149      1.1  augustss 	cs4280_trigger_output,
    150      1.1  augustss 	cs4280_trigger_input,
    151      1.1  augustss };
    152      1.1  augustss 
    153      1.1  augustss #if NMIDI > 0
    154  1.4.4.8        he /* Midi Interface */
    155  1.4.4.8        he int  cs4280_midi_open(void *, int, void (*)(void *, int),
    156  1.4.4.8        he                       void (*)(void *), void *);
    157  1.4.4.8        he void cs4280_midi_close(void*);
    158  1.4.4.8        he int  cs4280_midi_output(void *, int);
    159  1.4.4.8        he void cs4280_midi_getinfo(void *, struct midi_info *);
    160  1.4.4.8        he 
    161      1.1  augustss struct midi_hw_if cs4280_midi_hw_if = {
    162      1.1  augustss 	cs4280_midi_open,
    163      1.1  augustss 	cs4280_midi_close,
    164      1.1  augustss 	cs4280_midi_output,
    165      1.1  augustss 	cs4280_midi_getinfo,
    166      1.1  augustss 	0,
    167      1.1  augustss };
    168      1.1  augustss #endif
    169      1.1  augustss 
    170  1.4.4.8        he struct cfattach clcs_ca = {
    171  1.4.4.8        he 	sizeof(struct cs428x_softc), cs4280_match, cs4280_attach
    172  1.4.4.8        he };
    173      1.1  augustss 
    174      1.1  augustss struct audio_device cs4280_device = {
    175      1.1  augustss 	"CS4280",
    176      1.1  augustss 	"",
    177      1.1  augustss 	"cs4280"
    178      1.1  augustss };
    179      1.1  augustss 
    180      1.1  augustss 
    181      1.1  augustss int
    182      1.1  augustss cs4280_match(parent, match, aux)
    183      1.1  augustss 	struct device *parent;
    184      1.1  augustss 	struct cfdata *match;
    185      1.1  augustss 	void *aux;
    186      1.1  augustss {
    187      1.1  augustss 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    188      1.1  augustss 
    189      1.1  augustss 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
    190  1.4.4.8        he 		return 0;
    191      1.1  augustss 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4280
    192      1.1  augustss #if 0  /* I can't confirm */
    193      1.1  augustss 	    || PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4610
    194      1.1  augustss #endif
    195  1.4.4.2  augustss 	    )
    196  1.4.4.8        he 		return 1;
    197  1.4.4.8        he 	return 0;
    198      1.1  augustss }
    199      1.1  augustss 
    200      1.1  augustss void
    201      1.1  augustss cs4280_attach(parent, self, aux)
    202      1.1  augustss 	struct device *parent;
    203      1.1  augustss 	struct device *self;
    204      1.1  augustss 	void *aux;
    205      1.1  augustss {
    206  1.4.4.8        he 	struct cs428x_softc *sc = (struct cs428x_softc *)self;
    207      1.1  augustss 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    208      1.1  augustss 	pci_chipset_tag_t pc = pa->pa_pc;
    209      1.1  augustss 	char const *intrstr;
    210      1.1  augustss 	pci_intr_handle_t ih;
    211  1.4.4.6        he 	pcireg_t reg;
    212      1.1  augustss 	char devinfo[256];
    213      1.1  augustss 	mixer_ctrl_t ctl;
    214      1.1  augustss 	u_int32_t mem;
    215  1.4.4.6        he 	int pci_pwrmgmt_cap_reg, pci_pwrmgmt_csr_reg;
    216  1.4.4.8        he 
    217      1.1  augustss 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    218      1.1  augustss 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    219      1.1  augustss 
    220      1.1  augustss 	/* Map I/O register */
    221  1.4.4.8        he 	if (pci_mapreg_map(pa, PCI_BA0,
    222  1.4.4.8        he 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    223  1.4.4.8        he 	    &sc->ba0t, &sc->ba0h, NULL, NULL)) {
    224      1.1  augustss 		printf("%s: can't map BA0 space\n", sc->sc_dev.dv_xname);
    225      1.1  augustss 		return;
    226      1.1  augustss 	}
    227  1.4.4.8        he 	if (pci_mapreg_map(pa, PCI_BA1,
    228  1.4.4.8        he 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    229  1.4.4.8        he 	    &sc->ba1t, &sc->ba1h, NULL, NULL)) {
    230      1.1  augustss 		printf("%s: can't map BA1 space\n", sc->sc_dev.dv_xname);
    231      1.1  augustss 		return;
    232      1.1  augustss 	}
    233      1.1  augustss 
    234      1.1  augustss 	sc->sc_dmatag = pa->pa_dmat;
    235      1.1  augustss 
    236  1.4.4.6        he 	/* Check and set Power State */
    237  1.4.4.6        he 	if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PWRMGMT,
    238  1.4.4.6        he 	    &pci_pwrmgmt_cap_reg, 0)) {
    239  1.4.4.6        he 		pci_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + 4;
    240  1.4.4.6        he 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag,
    241  1.4.4.6        he 		    pci_pwrmgmt_csr_reg);
    242  1.4.4.6        he 		DPRINTF(("%s: Power State is %d\n",
    243  1.4.4.6        he 		    sc->sc_dev.dv_xname, reg & PCI_PMCSR_STATE_MASK));
    244  1.4.4.6        he 		if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0) {
    245  1.4.4.6        he 			pci_conf_write(pc, pa->pa_tag, pci_pwrmgmt_csr_reg,
    246  1.4.4.6        he 			    (reg & ~PCI_PMCSR_STATE_MASK) |
    247  1.4.4.6        he 			    PCI_PMCSR_STATE_D0);
    248  1.4.4.6        he 		}
    249  1.4.4.6        he 	}
    250  1.4.4.6        he 
    251      1.1  augustss 	/* Enable the device (set bus master flag) */
    252  1.4.4.6        he 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    253      1.1  augustss 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    254  1.4.4.6        he 		       reg | PCI_COMMAND_MASTER_ENABLE);
    255      1.1  augustss 
    256      1.1  augustss 	/* LATENCY_TIMER setting */
    257      1.1  augustss 	mem = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    258      1.1  augustss 	if ( PCI_LATTIMER(mem) < 32 ) {
    259      1.1  augustss 		mem &= 0xffff00ff;
    260      1.1  augustss 		mem |= 0x00002000;
    261      1.1  augustss 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, mem);
    262      1.1  augustss 	}
    263      1.1  augustss 
    264      1.1  augustss 	/* Map and establish the interrupt. */
    265      1.1  augustss 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    266      1.1  augustss 			 pa->pa_intrline, &ih)) {
    267      1.1  augustss 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    268      1.1  augustss 		return;
    269      1.1  augustss 	}
    270      1.1  augustss 	intrstr = pci_intr_string(pc, ih);
    271      1.1  augustss 
    272      1.1  augustss 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, cs4280_intr, sc);
    273      1.1  augustss 	if (sc->sc_ih == NULL) {
    274      1.1  augustss 		printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
    275      1.1  augustss 		if (intrstr != NULL)
    276      1.1  augustss 			printf(" at %s", intrstr);
    277      1.1  augustss 		printf("\n");
    278      1.1  augustss 		return;
    279      1.1  augustss 	}
    280      1.1  augustss 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    281      1.1  augustss 
    282      1.1  augustss 	/* Initialization */
    283      1.2  augustss 	if(cs4280_init(sc, 1) != 0)
    284      1.2  augustss 		return;
    285      1.1  augustss 
    286  1.4.4.8        he 	sc->type = TYPE_CS4280;
    287  1.4.4.8        he 	sc->halt_input  = cs4280_halt_input;
    288  1.4.4.8        he 	sc->halt_output = cs4280_halt_output;
    289  1.4.4.8        he 
    290  1.4.4.8        he 	/* setup buffer related parameters */
    291  1.4.4.8        he 	sc->dma_size     = CS4280_DCHUNK;
    292  1.4.4.8        he 	sc->dma_align    = CS4280_DALIGN;
    293  1.4.4.8        he 	sc->hw_blocksize = CS4280_ICHUNK;
    294  1.4.4.8        he 
    295  1.4.4.8        he 	/* AC 97 attachment */
    296      1.1  augustss 	sc->host_if.arg = sc;
    297  1.4.4.8        he 	sc->host_if.attach = cs428x_attach_codec;
    298  1.4.4.8        he 	sc->host_if.read   = cs428x_read_codec;
    299  1.4.4.8        he 	sc->host_if.write  = cs428x_write_codec;
    300      1.1  augustss 	sc->host_if.reset  = cs4280_reset_codec;
    301      1.1  augustss 	if (ac97_attach(&sc->host_if) != 0) {
    302      1.1  augustss 		printf("%s: ac97_attach failed\n", sc->sc_dev.dv_xname);
    303      1.1  augustss 		return;
    304      1.1  augustss 	}
    305      1.1  augustss 
    306      1.1  augustss 	/* Turn mute off of DAC, CD and master volumes by default */
    307      1.1  augustss 	ctl.type = AUDIO_MIXER_ENUM;
    308      1.1  augustss 	ctl.un.ord = 0;	 /* off */
    309      1.1  augustss 
    310      1.1  augustss 	ctl.dev = cs4280_get_portnum_by_name(sc, AudioCoutputs,
    311      1.1  augustss 					     AudioNmaster, AudioNmute);
    312  1.4.4.8        he 	cs428x_mixer_set_port(sc, &ctl);
    313      1.1  augustss 
    314      1.1  augustss 	ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
    315      1.1  augustss 					     AudioNdac, AudioNmute);
    316  1.4.4.8        he 	cs428x_mixer_set_port(sc, &ctl);
    317      1.1  augustss 
    318      1.1  augustss 	ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
    319      1.1  augustss 					     AudioNcd, AudioNmute);
    320  1.4.4.8        he 	cs428x_mixer_set_port(sc, &ctl);
    321      1.1  augustss 
    322      1.1  augustss 	audio_attach_mi(&cs4280_hw_if, sc, &sc->sc_dev);
    323      1.2  augustss 
    324      1.1  augustss #if NMIDI > 0
    325      1.1  augustss 	midi_attach_mi(&cs4280_midi_hw_if, sc, &sc->sc_dev);
    326      1.1  augustss #endif
    327  1.4.4.8        he 
    328      1.1  augustss 	sc->sc_suspend = PWR_RESUME;
    329      1.1  augustss 	sc->sc_powerhook = powerhook_establish(cs4280_power, sc);
    330      1.1  augustss }
    331      1.1  augustss 
    332  1.4.4.8        he /* Interrupt handling function */
    333      1.1  augustss int
    334      1.1  augustss cs4280_intr(p)
    335      1.1  augustss 	void *p;
    336      1.1  augustss {
    337      1.1  augustss 	/*
    338      1.1  augustss 	 * XXX
    339      1.1  augustss 	 *
    340      1.1  augustss 	 * Since CS4280 has only 4kB dma buffer and
    341      1.1  augustss 	 * interrupt occurs every 2kB block, I create dummy buffer
    342      1.1  augustss 	 * which returns to audio driver and actual dma buffer
    343      1.1  augustss 	 * using in DMA transfer.
    344      1.1  augustss 	 *
    345      1.1  augustss 	 *
    346      1.1  augustss 	 *  ring buffer in audio.c is pointed by BUFADDR
    347      1.1  augustss 	 *	 <------ ring buffer size == 64kB ------>
    348      1.1  augustss 	 *	 <-----> blksize == 2048*(sc->sc_[pr]count) kB
    349      1.1  augustss 	 *	|= = = =|= = = =|= = = =|= = = =|= = = =|
    350      1.1  augustss 	 *	|	|	|	|	|	| <- call audio_intp every
    351      1.1  augustss 	 *						     sc->sc_[pr]_count time.
    352      1.1  augustss 	 *
    353      1.1  augustss 	 *  actual dma buffer is pointed by KERNADDR
    354      1.1  augustss 	 *	 <-> dma buffer size = 4kB
    355      1.1  augustss 	 *	|= =|
    356      1.1  augustss 	 *
    357      1.1  augustss 	 *
    358      1.1  augustss 	 */
    359  1.4.4.8        he 	struct cs428x_softc *sc = p;
    360      1.1  augustss 	u_int32_t intr, mem;
    361      1.1  augustss 	char * empty_dma;
    362  1.4.4.8        he 	int handled = 0;
    363      1.1  augustss 
    364  1.4.4.8        he 	/* grab interrupt register then clear it */
    365      1.1  augustss 	intr = BA0READ4(sc, CS4280_HISR);
    366  1.4.4.8        he 	BA0WRITE4(sc, CS4280_HICR, HICR_CHGM | HICR_IEV);
    367      1.1  augustss 
    368      1.1  augustss 	/* Playback Interrupt */
    369      1.1  augustss 	if (intr & HISR_PINT) {
    370  1.4.4.8        he 		handled = 1;
    371      1.1  augustss 		mem = BA1READ4(sc, CS4280_PFIE);
    372      1.1  augustss 		BA1WRITE4(sc, CS4280_PFIE, (mem & ~PFIE_PI_MASK) | PFIE_PI_DISABLE);
    373      1.1  augustss 		if (sc->sc_pintr) {
    374      1.1  augustss 			if ((sc->sc_pi%sc->sc_pcount) == 0)
    375      1.1  augustss 				sc->sc_pintr(sc->sc_parg);
    376      1.1  augustss 		} else {
    377      1.1  augustss 			printf("unexpected play intr\n");
    378      1.1  augustss 		}
    379      1.1  augustss 		/* copy buffer */
    380      1.1  augustss 		++sc->sc_pi;
    381      1.1  augustss 		empty_dma = sc->sc_pdma->addr;
    382      1.1  augustss 		if (sc->sc_pi&1)
    383  1.4.4.8        he 			empty_dma += sc->hw_blocksize;
    384  1.4.4.8        he 		memcpy(empty_dma, sc->sc_pn, sc->hw_blocksize);
    385  1.4.4.8        he 		sc->sc_pn += sc->hw_blocksize;
    386      1.1  augustss 		if (sc->sc_pn >= sc->sc_pe)
    387      1.1  augustss 			sc->sc_pn = sc->sc_ps;
    388      1.1  augustss 		BA1WRITE4(sc, CS4280_PFIE, mem);
    389      1.1  augustss 	}
    390      1.1  augustss 	/* Capture Interrupt */
    391      1.1  augustss 	if (intr & HISR_CINT) {
    392      1.1  augustss 		int  i;
    393      1.1  augustss 		int16_t rdata;
    394      1.1  augustss 
    395  1.4.4.8        he 		handled = 1;
    396      1.1  augustss 		mem = BA1READ4(sc, CS4280_CIE);
    397      1.1  augustss 		BA1WRITE4(sc, CS4280_CIE, (mem & ~CIE_CI_MASK) | CIE_CI_DISABLE);
    398      1.1  augustss 		++sc->sc_ri;
    399      1.1  augustss 		empty_dma = sc->sc_rdma->addr;
    400      1.1  augustss 		if ((sc->sc_ri&1) == 0)
    401  1.4.4.8        he 			empty_dma += sc->hw_blocksize;
    402      1.1  augustss 
    403      1.1  augustss 		/*
    404      1.1  augustss 		 * XXX
    405      1.1  augustss 		 * I think this audio data conversion should be
    406      1.1  augustss 		 * happend in upper layer, but I put this here
    407      1.1  augustss 		 * since there is no conversion function available.
    408      1.1  augustss 		 */
    409      1.1  augustss 		switch(sc->sc_rparam) {
    410      1.1  augustss 		case CF_16BIT_STEREO:
    411      1.1  augustss 			/* just copy it */
    412  1.4.4.8        he 			memcpy(sc->sc_rn, empty_dma, sc->hw_blocksize);
    413  1.4.4.8        he 			sc->sc_rn += sc->hw_blocksize;
    414      1.1  augustss 			break;
    415      1.1  augustss 		case CF_16BIT_MONO:
    416      1.1  augustss 			for (i = 0; i < 512; i++) {
    417      1.1  augustss 				rdata  = *((int16_t *)empty_dma)++>>1;
    418      1.1  augustss 				rdata += *((int16_t *)empty_dma)++>>1;
    419      1.1  augustss 				*((int16_t *)sc->sc_rn)++ = rdata;
    420      1.1  augustss 			}
    421      1.1  augustss 			break;
    422      1.1  augustss 		case CF_8BIT_STEREO:
    423      1.1  augustss 			for (i = 0; i < 512; i++) {
    424      1.1  augustss 				rdata = *((int16_t*)empty_dma)++;
    425      1.1  augustss 				*sc->sc_rn++ = rdata >> 8;
    426      1.1  augustss 				rdata = *((int16_t*)empty_dma)++;
    427      1.1  augustss 				*sc->sc_rn++ = rdata >> 8;
    428      1.1  augustss 			}
    429      1.1  augustss 			break;
    430      1.1  augustss 		case CF_8BIT_MONO:
    431      1.1  augustss 			for (i = 0; i < 512; i++) {
    432      1.1  augustss 				rdata =	 *((int16_t*)empty_dma)++ >>1;
    433      1.1  augustss 				rdata += *((int16_t*)empty_dma)++ >>1;
    434      1.1  augustss 				*sc->sc_rn++ = rdata >>8;
    435      1.1  augustss 			}
    436      1.1  augustss 			break;
    437      1.1  augustss 		default:
    438      1.1  augustss 			/* Should not reach here */
    439      1.1  augustss 			printf("unknown sc->sc_rparam: %d\n", sc->sc_rparam);
    440      1.1  augustss 		}
    441      1.1  augustss 		if (sc->sc_rn >= sc->sc_re)
    442      1.1  augustss 			sc->sc_rn = sc->sc_rs;
    443      1.1  augustss 		BA1WRITE4(sc, CS4280_CIE, mem);
    444      1.1  augustss 		if (sc->sc_rintr) {
    445      1.1  augustss 			if ((sc->sc_ri%(sc->sc_rcount)) == 0)
    446      1.1  augustss 				sc->sc_rintr(sc->sc_rarg);
    447      1.1  augustss 		} else {
    448      1.1  augustss 			printf("unexpected record intr\n");
    449      1.1  augustss 		}
    450      1.1  augustss 	}
    451      1.1  augustss 
    452      1.1  augustss #if NMIDI > 0
    453      1.1  augustss 	/* Midi port Interrupt */
    454      1.1  augustss 	if (intr & HISR_MIDI) {
    455      1.2  augustss 		int data;
    456      1.2  augustss 
    457  1.4.4.8        he 		handled = 1;
    458      1.2  augustss 		DPRINTF(("i: %d: ",
    459      1.2  augustss 			 BA0READ4(sc, CS4280_MIDSR)));
    460      1.2  augustss 		/* Read the received data */
    461      1.2  augustss 		while ((sc->sc_iintr != NULL) &&
    462      1.2  augustss 		       ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_RBE) == 0)) {
    463      1.2  augustss 			data = BA0READ4(sc, CS4280_MIDRP) & MIDRP_MASK;
    464      1.2  augustss 			DPRINTF(("r:%x\n",data));
    465      1.2  augustss 			sc->sc_iintr(sc->sc_arg, data);
    466      1.2  augustss 		}
    467      1.2  augustss 
    468      1.2  augustss 		/* Write the data */
    469      1.2  augustss #if 1
    470      1.2  augustss 		/* XXX:
    471      1.2  augustss 		 * It seems "Transmit Buffer Full" never activate until EOI
    472      1.2  augustss 		 * is deliverd.  Shall I throw EOI top of this routine ?
    473      1.2  augustss 		 */
    474      1.2  augustss 		if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
    475      1.2  augustss 			DPRINTF(("w: "));
    476      1.2  augustss 			if (sc->sc_ointr != NULL)
    477      1.2  augustss 				sc->sc_ointr(sc->sc_arg);
    478      1.2  augustss 		}
    479      1.2  augustss #else
    480      1.2  augustss 		while ((sc->sc_ointr != NULL) &&
    481      1.2  augustss 		       ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0)) {
    482      1.2  augustss 			DPRINTF(("w: "));
    483      1.2  augustss 			sc->sc_ointr(sc->sc_arg);
    484      1.2  augustss 		}
    485      1.2  augustss #endif
    486      1.2  augustss 		DPRINTF(("\n"));
    487      1.1  augustss 	}
    488      1.1  augustss #endif
    489      1.1  augustss 
    490  1.4.4.8        he 	return handled;
    491  1.4.4.8        he }
    492      1.1  augustss 
    493      1.1  augustss int
    494  1.4.4.8        he cs4280_query_encoding(addr, fp)
    495  1.4.4.8        he 	void *addr;
    496  1.4.4.8        he 	struct audio_encoding *fp;
    497      1.1  augustss {
    498      1.1  augustss 	switch (fp->index) {
    499      1.1  augustss 	case 0:
    500      1.1  augustss 		strcpy(fp->name, AudioEulinear);
    501      1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    502      1.1  augustss 		fp->precision = 8;
    503      1.1  augustss 		fp->flags = 0;
    504      1.1  augustss 		break;
    505      1.1  augustss 	case 1:
    506      1.1  augustss 		strcpy(fp->name, AudioEmulaw);
    507      1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULAW;
    508      1.1  augustss 		fp->precision = 8;
    509      1.1  augustss 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    510      1.1  augustss 		break;
    511      1.1  augustss 	case 2:
    512      1.1  augustss 		strcpy(fp->name, AudioEalaw);
    513      1.1  augustss 		fp->encoding = AUDIO_ENCODING_ALAW;
    514      1.1  augustss 		fp->precision = 8;
    515      1.1  augustss 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    516      1.1  augustss 		break;
    517      1.1  augustss 	case 3:
    518      1.1  augustss 		strcpy(fp->name, AudioEslinear);
    519      1.1  augustss 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    520      1.1  augustss 		fp->precision = 8;
    521      1.1  augustss 		fp->flags = 0;
    522      1.1  augustss 		break;
    523      1.1  augustss 	case 4:
    524      1.1  augustss 		strcpy(fp->name, AudioEslinear_le);
    525      1.1  augustss 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    526      1.1  augustss 		fp->precision = 16;
    527      1.1  augustss 		fp->flags = 0;
    528      1.1  augustss 		break;
    529      1.1  augustss 	case 5:
    530      1.1  augustss 		strcpy(fp->name, AudioEulinear_le);
    531      1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    532      1.1  augustss 		fp->precision = 16;
    533      1.1  augustss 		fp->flags = 0;
    534      1.1  augustss 		break;
    535      1.1  augustss 	case 6:
    536      1.1  augustss 		strcpy(fp->name, AudioEslinear_be);
    537      1.1  augustss 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    538      1.1  augustss 		fp->precision = 16;
    539      1.1  augustss 		fp->flags = 0;
    540      1.1  augustss 		break;
    541      1.1  augustss 	case 7:
    542      1.1  augustss 		strcpy(fp->name, AudioEulinear_be);
    543      1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    544      1.1  augustss 		fp->precision = 16;
    545      1.1  augustss 		fp->flags = 0;
    546      1.1  augustss 		break;
    547      1.1  augustss 	default:
    548  1.4.4.8        he 		return EINVAL;
    549      1.1  augustss 	}
    550  1.4.4.8        he 	return 0;
    551      1.1  augustss }
    552      1.1  augustss 
    553      1.1  augustss int
    554      1.1  augustss cs4280_set_params(addr, setmode, usemode, play, rec)
    555      1.1  augustss 	void *addr;
    556      1.1  augustss 	int setmode, usemode;
    557      1.1  augustss 	struct audio_params *play, *rec;
    558      1.1  augustss {
    559  1.4.4.8        he 	struct cs428x_softc *sc = addr;
    560      1.1  augustss 	struct audio_params *p;
    561      1.1  augustss 	int mode;
    562      1.1  augustss 
    563      1.1  augustss 	for (mode = AUMODE_RECORD; mode != -1;
    564      1.1  augustss 	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1 ) {
    565      1.1  augustss 		if ((setmode & mode) == 0)
    566      1.1  augustss 			continue;
    567      1.1  augustss 
    568      1.1  augustss 		p = mode == AUMODE_PLAY ? play : rec;
    569      1.1  augustss 
    570      1.1  augustss 		if (p == play) {
    571      1.1  augustss 			DPRINTFN(5,("play: sample=%ld precision=%d channels=%d\n",
    572      1.1  augustss 				p->sample_rate, p->precision, p->channels));
    573      1.1  augustss 			/* play back data format may be 8- or 16-bit and
    574      1.1  augustss 			 * either stereo or mono.
    575      1.1  augustss 			 * playback rate may range from 8000Hz to 48000Hz
    576      1.1  augustss 			 */
    577      1.1  augustss 			if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
    578      1.1  augustss 			    (p->precision != 8 && p->precision != 16) ||
    579      1.1  augustss 			    (p->channels != 1  && p->channels != 2) ) {
    580  1.4.4.8        he 				return EINVAL;
    581      1.1  augustss 			}
    582      1.1  augustss 		} else {
    583      1.1  augustss 			DPRINTFN(5,("rec: sample=%ld precision=%d channels=%d\n",
    584      1.1  augustss 				p->sample_rate, p->precision, p->channels));
    585      1.1  augustss 			/* capture data format must be 16bit stereo
    586      1.1  augustss 			 * and sample rate range from 11025Hz to 48000Hz.
    587      1.1  augustss 			 *
    588      1.1  augustss 			 * XXX: it looks like to work with 8000Hz,
    589      1.1  augustss 			 *	although data sheets say lower limit is
    590      1.1  augustss 			 *	11025 Hz.
    591      1.1  augustss 			 */
    592      1.1  augustss 
    593      1.1  augustss 			if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
    594      1.1  augustss 			    (p->precision != 8 && p->precision != 16) ||
    595      1.1  augustss 			    (p->channels  != 1 && p->channels  != 2) ) {
    596  1.4.4.8        he 				return EINVAL;
    597      1.1  augustss 			}
    598      1.1  augustss 		}
    599      1.1  augustss 		p->factor  = 1;
    600      1.1  augustss 		p->sw_code = 0;
    601      1.1  augustss 
    602      1.1  augustss 		/* capturing data is slinear */
    603      1.1  augustss 		switch (p->encoding) {
    604      1.1  augustss 		case AUDIO_ENCODING_SLINEAR_BE:
    605      1.1  augustss 			if (mode == AUMODE_RECORD) {
    606      1.1  augustss 				if (p->precision == 16)
    607      1.1  augustss 					p->sw_code = swap_bytes;
    608      1.1  augustss 			}
    609      1.1  augustss 			break;
    610      1.1  augustss 		case AUDIO_ENCODING_SLINEAR_LE:
    611      1.1  augustss 			break;
    612      1.1  augustss 		case AUDIO_ENCODING_ULINEAR_BE:
    613      1.1  augustss 			if (mode == AUMODE_RECORD) {
    614      1.1  augustss 				if (p->precision == 16)
    615      1.1  augustss 					p->sw_code = change_sign16_swap_bytes_le;
    616      1.1  augustss 				else
    617      1.1  augustss 					p->sw_code = change_sign8;
    618      1.1  augustss 			}
    619      1.1  augustss 			break;
    620      1.1  augustss 		case AUDIO_ENCODING_ULINEAR_LE:
    621      1.1  augustss 			if (mode == AUMODE_RECORD) {
    622      1.1  augustss 				if (p->precision == 16)
    623      1.1  augustss 					p->sw_code = change_sign16_le;
    624      1.1  augustss 				else
    625      1.1  augustss 					p->sw_code = change_sign8;
    626      1.1  augustss 			}
    627      1.1  augustss 			break;
    628      1.1  augustss 		case AUDIO_ENCODING_ULAW:
    629      1.1  augustss 			if (mode == AUMODE_PLAY) {
    630      1.1  augustss 				p->factor = 2;
    631      1.1  augustss 				p->sw_code = mulaw_to_slinear16_le;
    632      1.1  augustss 			} else {
    633      1.1  augustss 				p->sw_code = slinear8_to_mulaw;
    634      1.1  augustss 			}
    635      1.1  augustss 			break;
    636      1.1  augustss 		case AUDIO_ENCODING_ALAW:
    637      1.1  augustss 			if (mode == AUMODE_PLAY) {
    638      1.1  augustss 				p->factor = 2;
    639      1.1  augustss 				p->sw_code = alaw_to_slinear16_le;
    640      1.1  augustss 			} else {
    641      1.1  augustss 				p->sw_code = slinear8_to_alaw;
    642      1.1  augustss 			}
    643      1.1  augustss 			break;
    644      1.1  augustss 		default:
    645  1.4.4.8        he 			return EINVAL;
    646      1.1  augustss 		}
    647      1.1  augustss 	}
    648      1.1  augustss 
    649      1.1  augustss 	/* set sample rate */
    650      1.1  augustss 	cs4280_set_dac_rate(sc, play->sample_rate);
    651      1.1  augustss 	cs4280_set_adc_rate(sc, rec->sample_rate);
    652  1.4.4.8        he 	return 0;
    653      1.1  augustss }
    654      1.1  augustss 
    655      1.1  augustss int
    656      1.1  augustss cs4280_halt_output(addr)
    657      1.1  augustss 	void *addr;
    658      1.1  augustss {
    659  1.4.4.8        he 	struct cs428x_softc *sc = addr;
    660      1.1  augustss 	u_int32_t mem;
    661      1.1  augustss 
    662      1.1  augustss 	mem = BA1READ4(sc, CS4280_PCTL);
    663      1.1  augustss 	BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
    664      1.1  augustss 	sc->sc_prun = 0;
    665  1.4.4.8        he 	return 0;
    666      1.1  augustss }
    667      1.1  augustss 
    668      1.1  augustss int
    669      1.1  augustss cs4280_halt_input(addr)
    670      1.1  augustss 	void *addr;
    671      1.1  augustss {
    672  1.4.4.8        he 	struct cs428x_softc *sc = addr;
    673      1.1  augustss 	u_int32_t mem;
    674      1.1  augustss 
    675      1.1  augustss 	mem = BA1READ4(sc, CS4280_CCTL);
    676      1.1  augustss 	BA1WRITE4(sc, CS4280_CCTL, mem & ~CCTL_MASK);
    677      1.1  augustss 	sc->sc_rrun = 0;
    678  1.4.4.8        he 	return 0;
    679      1.1  augustss }
    680      1.1  augustss 
    681      1.1  augustss int
    682      1.1  augustss cs4280_getdev(addr, retp)
    683      1.1  augustss 	void *addr;
    684      1.1  augustss 	struct audio_device *retp;
    685      1.1  augustss {
    686      1.1  augustss 	*retp = cs4280_device;
    687  1.4.4.8        he 	return 0;
    688      1.1  augustss }
    689      1.1  augustss 
    690      1.1  augustss int
    691      1.1  augustss cs4280_trigger_output(addr, start, end, blksize, intr, arg, param)
    692      1.1  augustss 	void *addr;
    693      1.1  augustss 	void *start, *end;
    694      1.1  augustss 	int blksize;
    695      1.1  augustss 	void (*intr) __P((void *));
    696      1.1  augustss 	void *arg;
    697      1.1  augustss 	struct audio_params *param;
    698      1.1  augustss {
    699  1.4.4.8        he 	struct cs428x_softc *sc = addr;
    700  1.4.4.8        he 	u_int32_t pfie, pctl, pdtc;
    701  1.4.4.8        he 	struct cs428x_dma *p;
    702      1.1  augustss 
    703      1.1  augustss #ifdef DIAGNOSTIC
    704      1.1  augustss 	if (sc->sc_prun)
    705      1.1  augustss 		printf("cs4280_trigger_output: already running\n");
    706      1.1  augustss #endif
    707  1.4.4.8        he 	sc->sc_prun = 1;
    708      1.1  augustss 
    709      1.1  augustss 	DPRINTF(("cs4280_trigger_output: sc=%p start=%p end=%p "
    710      1.1  augustss 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
    711      1.1  augustss 	sc->sc_pintr = intr;
    712      1.1  augustss 	sc->sc_parg  = arg;
    713      1.1  augustss 
    714      1.1  augustss 	/* stop playback DMA */
    715  1.4.4.8        he 	BA1WRITE4(sc, CS4280_PCTL, BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK);
    716      1.1  augustss 
    717      1.1  augustss 	/* setup PDTC */
    718      1.1  augustss 	pdtc = BA1READ4(sc, CS4280_PDTC);
    719      1.1  augustss 	pdtc &= ~PDTC_MASK;
    720      1.1  augustss 	pdtc |= CS4280_MK_PDTC(param->precision * param->channels);
    721      1.1  augustss 	BA1WRITE4(sc, CS4280_PDTC, pdtc);
    722      1.1  augustss 
    723      1.1  augustss 	DPRINTF(("param: precision=%d  factor=%d channels=%d encoding=%d\n",
    724      1.1  augustss 	       param->precision, param->factor, param->channels,
    725      1.1  augustss 	       param->encoding));
    726      1.1  augustss 	for (p = sc->sc_dmas; p != NULL && BUFADDR(p) != start; p = p->next)
    727      1.1  augustss 		;
    728      1.1  augustss 	if (p == NULL) {
    729      1.1  augustss 		printf("cs4280_trigger_output: bad addr %p\n", start);
    730  1.4.4.8        he 		return EINVAL;
    731      1.1  augustss 	}
    732  1.4.4.8        he 	if (DMAADDR(p) % sc->dma_align != 0 ) {
    733      1.1  augustss 		printf("cs4280_trigger_output: DMAADDR(p)=0x%lx does not start"
    734      1.1  augustss 		       "4kB align\n", DMAADDR(p));
    735  1.4.4.8        he 		return EINVAL;
    736      1.1  augustss 	}
    737      1.1  augustss 
    738  1.4.4.8        he 	sc->sc_pcount = blksize / sc->hw_blocksize; /* sc->hw_blocksize is fixed hardware blksize*/
    739      1.1  augustss 	sc->sc_ps = (char *)start;
    740      1.1  augustss 	sc->sc_pe = (char *)end;
    741      1.1  augustss 	sc->sc_pdma = p;
    742      1.1  augustss 	sc->sc_pbuf = KERNADDR(p);
    743      1.1  augustss 	sc->sc_pi = 0;
    744      1.1  augustss 	sc->sc_pn = sc->sc_ps;
    745  1.4.4.8        he 	if (blksize >= sc->dma_size) {
    746  1.4.4.8        he 		sc->sc_pn = sc->sc_ps + sc->dma_size;
    747  1.4.4.8        he 		memcpy(sc->sc_pbuf, start, sc->dma_size);
    748      1.1  augustss 		++sc->sc_pi;
    749      1.1  augustss 	} else {
    750  1.4.4.8        he 		sc->sc_pn = sc->sc_ps + sc->hw_blocksize;
    751  1.4.4.8        he 		memcpy(sc->sc_pbuf, start, sc->hw_blocksize);
    752      1.1  augustss 	}
    753      1.1  augustss 
    754      1.1  augustss 	/* initiate playback dma */
    755      1.1  augustss 	BA1WRITE4(sc, CS4280_PBA, DMAADDR(p));
    756      1.1  augustss 
    757      1.1  augustss 	/* set PFIE */
    758      1.2  augustss 	pfie = BA1READ4(sc, CS4280_PFIE) & ~PFIE_MASK;
    759      1.1  augustss 
    760      1.1  augustss 	if (param->precision * param->factor == 8)
    761      1.1  augustss 		pfie |= PFIE_8BIT;
    762      1.1  augustss 	if (param->channels == 1)
    763      1.1  augustss 		pfie |= PFIE_MONO;
    764      1.1  augustss 
    765      1.1  augustss 	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
    766      1.1  augustss 	    param->encoding == AUDIO_ENCODING_SLINEAR_BE)
    767      1.1  augustss 		pfie |= PFIE_SWAPPED;
    768      1.1  augustss 	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
    769      1.1  augustss 	    param->encoding == AUDIO_ENCODING_ULINEAR_LE)
    770      1.1  augustss 		pfie |= PFIE_UNSIGNED;
    771      1.1  augustss 
    772      1.1  augustss 	BA1WRITE4(sc, CS4280_PFIE, pfie | PFIE_PI_ENABLE);
    773      1.1  augustss 
    774  1.4.4.8        he 	sc->sc_prate = param->sample_rate;
    775  1.4.4.8        he 	cs4280_set_dac_rate(sc, param->sample_rate);
    776  1.4.4.8        he 
    777  1.4.4.8        he 	pctl = BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK;
    778  1.4.4.8        he 	pctl |= sc->pctl;
    779  1.4.4.8        he 	BA1WRITE4(sc, CS4280_PCTL, pctl);
    780  1.4.4.8        he 	return 0;
    781  1.4.4.8        he }
    782  1.4.4.8        he 
    783  1.4.4.8        he int
    784  1.4.4.8        he cs4280_trigger_input(addr, start, end, blksize, intr, arg, param)
    785  1.4.4.8        he 	void *addr;
    786  1.4.4.8        he 	void *start, *end;
    787  1.4.4.8        he 	int blksize;
    788  1.4.4.8        he 	void (*intr) __P((void *));
    789  1.4.4.8        he 	void *arg;
    790  1.4.4.8        he 	struct audio_params *param;
    791  1.4.4.8        he {
    792  1.4.4.8        he 	struct cs428x_softc *sc = addr;
    793  1.4.4.8        he 	u_int32_t cctl, cie;
    794  1.4.4.8        he 	struct cs428x_dma *p;
    795  1.4.4.8        he 
    796  1.4.4.8        he #ifdef DIAGNOSTIC
    797  1.4.4.8        he 	if (sc->sc_rrun)
    798  1.4.4.8        he 		printf("cs4280_trigger_input: already running\n");
    799  1.4.4.8        he #endif
    800  1.4.4.8        he 	sc->sc_rrun = 1;
    801  1.4.4.8        he 
    802  1.4.4.8        he 	DPRINTF(("cs4280_trigger_input: sc=%p start=%p end=%p "
    803  1.4.4.8        he 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
    804  1.4.4.8        he 	sc->sc_rintr = intr;
    805  1.4.4.8        he 	sc->sc_rarg  = arg;
    806  1.4.4.8        he 
    807  1.4.4.8        he 	/* stop capture DMA */
    808  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CCTL, BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK);
    809  1.4.4.8        he 
    810  1.4.4.8        he 	for (p = sc->sc_dmas; p && BUFADDR(p) != start; p = p->next)
    811  1.4.4.8        he 		;
    812  1.4.4.8        he 	if (p == NULL) {
    813  1.4.4.8        he 		printf("cs4280_trigger_input: bad addr %p\n", start);
    814  1.4.4.8        he 		return EINVAL;
    815  1.4.4.8        he 	}
    816  1.4.4.8        he 	if (DMAADDR(p) % sc->dma_align != 0) {
    817  1.4.4.8        he 		printf("cs4280_trigger_input: DMAADDR(p)=0x%lx does not start"
    818  1.4.4.8        he 		       "4kB align\n", DMAADDR(p));
    819  1.4.4.8        he 		return EINVAL;
    820  1.4.4.8        he 	}
    821  1.4.4.8        he 
    822  1.4.4.8        he 	sc->sc_rcount = blksize / sc->hw_blocksize; /* sc->hw_blocksize is fixed hardware blksize*/
    823  1.4.4.8        he 	sc->sc_rs = (char *)start;
    824  1.4.4.8        he 	sc->sc_re = (char *)end;
    825  1.4.4.8        he 	sc->sc_rdma = p;
    826  1.4.4.8        he 	sc->sc_rbuf = KERNADDR(p);
    827  1.4.4.8        he 	sc->sc_ri = 0;
    828  1.4.4.8        he 	sc->sc_rn = sc->sc_rs;
    829  1.4.4.8        he 
    830  1.4.4.8        he 	/* initiate capture dma */
    831  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CBA, DMAADDR(p));
    832  1.4.4.8        he 
    833  1.4.4.8        he 	/* setup format information for internal converter */
    834  1.4.4.8        he 	sc->sc_rparam = 0;
    835  1.4.4.8        he 	if (param->precision == 8) {
    836  1.4.4.8        he 		sc->sc_rparam += CF_8BIT;
    837  1.4.4.8        he 		sc->sc_rcount <<= 1;
    838  1.4.4.8        he 	}
    839  1.4.4.8        he 	if (param->channels  == 1) {
    840  1.4.4.8        he 		sc->sc_rparam += CF_MONO;
    841  1.4.4.8        he 		sc->sc_rcount <<= 1;
    842  1.4.4.8        he 	}
    843  1.4.4.8        he 
    844  1.4.4.8        he 	/* set CIE */
    845  1.4.4.8        he 	cie = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
    846  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CIE, cie | CIE_CI_ENABLE);
    847  1.4.4.8        he 
    848  1.4.4.8        he 	sc->sc_rrate = param->sample_rate;
    849  1.4.4.8        he 	cs4280_set_adc_rate(sc, param->sample_rate);
    850  1.4.4.8        he 
    851  1.4.4.8        he 	cctl = BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK;
    852  1.4.4.8        he 	cctl |= sc->cctl;
    853  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CCTL, cctl);
    854  1.4.4.8        he 	return 0;
    855  1.4.4.8        he }
    856  1.4.4.8        he 
    857  1.4.4.8        he /* Power Hook */
    858  1.4.4.8        he void
    859  1.4.4.8        he cs4280_power(why, v)
    860  1.4.4.8        he 	int why;
    861  1.4.4.8        he 	void *v;
    862  1.4.4.8        he {
    863  1.4.4.8        he 	struct cs428x_softc *sc = (struct cs428x_softc *)v;
    864  1.4.4.8        he 	static u_int32_t pctl = 0, pba = 0, pfie = 0, pdtc = 0;
    865  1.4.4.8        he 	static u_int32_t cctl = 0, cba = 0, cie = 0;
    866  1.4.4.8        he 
    867  1.4.4.8        he 	DPRINTF(("%s: cs4280_power why=%d\n",
    868  1.4.4.8        he 	       sc->sc_dev.dv_xname, why));
    869  1.4.4.8        he 	switch (why) {
    870  1.4.4.8        he 	case PWR_SUSPEND:
    871  1.4.4.8        he 	case PWR_STANDBY:
    872  1.4.4.8        he 		sc->sc_suspend = why;
    873  1.4.4.8        he 
    874  1.4.4.8        he 		/* save current playback status */
    875  1.4.4.8        he 		if ( sc->sc_prun ) {
    876  1.4.4.8        he 			pctl = BA1READ4(sc, CS4280_PCTL);
    877  1.4.4.8        he 			pfie = BA1READ4(sc, CS4280_PFIE);
    878  1.4.4.8        he 			pba  = BA1READ4(sc, CS4280_PBA);
    879  1.4.4.8        he 			pdtc = BA1READ4(sc, CS4280_PDTC);
    880  1.4.4.8        he 			DPRINTF(("pctl=0x%08x pfie=0x%08x pba=0x%08x pdtc=0x%08x\n",
    881  1.4.4.8        he 			    pctl, pfie, pba, pdtc));
    882  1.4.4.8        he 		}
    883  1.4.4.8        he 
    884  1.4.4.8        he 		/* save current capture status */
    885  1.4.4.8        he 		if ( sc->sc_rrun ) {
    886  1.4.4.8        he 			cctl = BA1READ4(sc, CS4280_CCTL);
    887  1.4.4.8        he 			cie  = BA1READ4(sc, CS4280_CIE);
    888  1.4.4.8        he 			cba  = BA1READ4(sc, CS4280_CBA);
    889  1.4.4.8        he 			DPRINTF(("cctl=0x%08x cie=0x%08x cba=0x%08x\n",
    890  1.4.4.8        he 			    cctl, cie, cba));
    891  1.4.4.8        he 		}
    892  1.4.4.8        he 
    893  1.4.4.8        he 		/* Stop DMA */
    894  1.4.4.8        he 		BA1WRITE4(sc, CS4280_PCTL, pctl & ~PCTL_MASK);
    895  1.4.4.8        he 		BA1WRITE4(sc, CS4280_CCTL, BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK);
    896  1.4.4.8        he 		break;
    897  1.4.4.8        he 	case PWR_RESUME:
    898  1.4.4.8        he 		if (sc->sc_suspend == PWR_RESUME) {
    899  1.4.4.8        he 			printf("cs4280_power: odd, resume without suspend.\n");
    900  1.4.4.8        he 			sc->sc_suspend = why;
    901  1.4.4.8        he 			return;
    902  1.4.4.8        he 		}
    903  1.4.4.8        he 		sc->sc_suspend = why;
    904  1.4.4.8        he 		cs4280_init(sc, 0);
    905  1.4.4.8        he 		cs4280_reset_codec(sc);
    906  1.4.4.8        he 
    907  1.4.4.8        he 		/* restore ac97 registers */
    908  1.4.4.8        he 		(*sc->codec_if->vtbl->restore_ports)(sc->codec_if);
    909  1.4.4.8        he 
    910  1.4.4.8        he 		/* restore DMA related status */
    911  1.4.4.8        he 		if(sc->sc_prun) {
    912  1.4.4.8        he 			DPRINTF(("pctl=0x%08x pfie=0x%08x pba=0x%08x pdtc=0x%08x\n",
    913  1.4.4.8        he 			    pctl, pfie, pba, pdtc));
    914  1.4.4.8        he 			cs4280_set_dac_rate(sc, sc->sc_prate);
    915  1.4.4.8        he 			BA1WRITE4(sc, CS4280_PDTC, pdtc);
    916  1.4.4.8        he 			BA1WRITE4(sc, CS4280_PBA,  pba);
    917  1.4.4.8        he 			BA1WRITE4(sc, CS4280_PFIE, pfie);
    918  1.4.4.8        he 			BA1WRITE4(sc, CS4280_PCTL, pctl);
    919  1.4.4.8        he 		}
    920  1.4.4.8        he 
    921  1.4.4.8        he 		if (sc->sc_rrun) {
    922  1.4.4.8        he 			DPRINTF(("cctl=0x%08x cie=0x%08x cba=0x%08x\n",
    923  1.4.4.8        he 			    cctl, cie, cba));
    924  1.4.4.8        he 			cs4280_set_adc_rate(sc, sc->sc_rrate);
    925  1.4.4.8        he 			BA1WRITE4(sc, CS4280_CBA,  cba);
    926  1.4.4.8        he 			BA1WRITE4(sc, CS4280_CIE,  cie);
    927  1.4.4.8        he 			BA1WRITE4(sc, CS4280_CCTL, cctl);
    928  1.4.4.8        he 		}
    929  1.4.4.8        he 		break;
    930  1.4.4.8        he 	case PWR_SOFTSUSPEND:
    931  1.4.4.8        he 	case PWR_SOFTSTANDBY:
    932  1.4.4.8        he 	case PWR_SOFTRESUME:
    933  1.4.4.8        he 		break;
    934  1.4.4.8        he 	}
    935  1.4.4.8        he }
    936  1.4.4.8        he 
    937  1.4.4.8        he /* control AC97 codec */
    938  1.4.4.8        he void
    939  1.4.4.8        he cs4280_reset_codec(void *addr)
    940  1.4.4.8        he {
    941  1.4.4.8        he 	struct cs428x_softc *sc;
    942  1.4.4.8        he 	int n;
    943  1.4.4.8        he 
    944  1.4.4.8        he 	sc = addr;
    945  1.4.4.8        he 
    946  1.4.4.8        he 	/* Reset codec */
    947  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, 0);
    948  1.4.4.8        he 	delay(100);    /* delay 100us */
    949  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_RSTN);
    950  1.4.4.8        he 
    951  1.4.4.8        he 	/*
    952  1.4.4.8        he 	 * It looks like we do the following procedure, too
    953  1.4.4.8        he 	 */
    954  1.4.4.8        he 
    955  1.4.4.8        he 	/* Enable AC-link sync generation */
    956  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
    957  1.4.4.8        he 	delay(50*1000); /* XXX delay 50ms */
    958  1.4.4.8        he 
    959  1.4.4.8        he 	/* Assert valid frame signal */
    960  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
    961  1.4.4.8        he 
    962  1.4.4.8        he 	/* Wait for valid AC97 input slot */
    963  1.4.4.8        he 	n = 0;
    964  1.4.4.8        he 	while ((BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
    965  1.4.4.8        he 	       (ACISV_ISV3 | ACISV_ISV4)) {
    966  1.4.4.8        he 		delay(1000);
    967  1.4.4.8        he 		if (++n > 1000) {
    968  1.4.4.8        he 			printf("reset_codec: AC97 inputs slot ready timeout\n");
    969  1.4.4.8        he 			return;
    970  1.4.4.8        he 		}
    971  1.4.4.8        he 	}
    972  1.4.4.8        he }
    973  1.4.4.8        he 
    974  1.4.4.8        he 
    975  1.4.4.8        he /* Internal functions */
    976  1.4.4.8        he 
    977  1.4.4.8        he void
    978  1.4.4.8        he cs4280_set_adc_rate(sc, rate)
    979  1.4.4.8        he 	struct cs428x_softc *sc;
    980  1.4.4.8        he 	int rate;
    981  1.4.4.8        he {
    982  1.4.4.8        he 	/* calculate capture rate:
    983  1.4.4.8        he 	 *
    984  1.4.4.8        he 	 * capture_coefficient_increment = -round(rate*128*65536/48000;
    985  1.4.4.8        he 	 * capture_phase_increment	 = floor(48000*65536*1024/rate);
    986  1.4.4.8        he 	 * cx = round(48000*65536*1024 - capture_phase_increment*rate);
    987  1.4.4.8        he 	 * cy = floor(cx/200);
    988  1.4.4.8        he 	 * capture_sample_rate_correction = cx - 200*cy;
    989  1.4.4.8        he 	 * capture_delay = ceil(24*48000/rate);
    990  1.4.4.8        he 	 * capture_num_triplets = floor(65536*rate/24000);
    991  1.4.4.8        he 	 * capture_group_length = 24000/GCD(rate, 24000);
    992  1.4.4.8        he 	 * where GCD means "Greatest Common Divisor".
    993  1.4.4.8        he 	 *
    994  1.4.4.8        he 	 * capture_coefficient_increment, capture_phase_increment and
    995  1.4.4.8        he 	 * capture_num_triplets are 32-bit signed quantities.
    996  1.4.4.8        he 	 * capture_sample_rate_correction and capture_group_length are
    997  1.4.4.8        he 	 * 16-bit signed quantities.
    998  1.4.4.8        he 	 * capture_delay is a 14-bit unsigned quantity.
    999  1.4.4.8        he 	 */
   1000  1.4.4.8        he 	u_int32_t cci,cpi,cnt,cx,cy,  tmp1;
   1001  1.4.4.8        he 	u_int16_t csrc, cgl, cdlay;
   1002  1.4.4.8        he 
   1003  1.4.4.8        he 	/* XXX
   1004  1.4.4.8        he 	 * Even though, embedded_audio_spec says capture rate range 11025 to
   1005  1.4.4.8        he 	 * 48000, dhwiface.cpp says,
   1006  1.4.4.8        he 	 *
   1007  1.4.4.8        he 	 * "We can only decimate by up to a factor of 1/9th the hardware rate.
   1008  1.4.4.8        he 	 *  Return an error if an attempt is made to stray outside that limit."
   1009  1.4.4.8        he 	 *
   1010  1.4.4.8        he 	 * so assume range as 48000/9 to 48000
   1011  1.4.4.8        he 	 */
   1012  1.4.4.8        he 
   1013  1.4.4.8        he 	if (rate < 8000)
   1014  1.4.4.8        he 		rate = 8000;
   1015  1.4.4.8        he 	if (rate > 48000)
   1016  1.4.4.8        he 		rate = 48000;
   1017  1.4.4.8        he 
   1018  1.4.4.8        he 	cx = rate << 16;
   1019  1.4.4.8        he 	cci = cx / 48000;
   1020  1.4.4.8        he 	cx -= cci * 48000;
   1021  1.4.4.8        he 	cx <<= 7;
   1022  1.4.4.8        he 	cci <<= 7;
   1023  1.4.4.8        he 	cci += cx / 48000;
   1024  1.4.4.8        he 	cci = - cci;
   1025  1.4.4.8        he 
   1026  1.4.4.8        he 	cx = 48000 << 16;
   1027  1.4.4.8        he 	cpi = cx / rate;
   1028  1.4.4.8        he 	cx -= cpi * rate;
   1029  1.4.4.8        he 	cx <<= 10;
   1030  1.4.4.8        he 	cpi <<= 10;
   1031  1.4.4.8        he 	cy = cx / rate;
   1032  1.4.4.8        he 	cpi += cy;
   1033  1.4.4.8        he 	cx -= cy * rate;
   1034  1.4.4.8        he 
   1035  1.4.4.8        he 	cy   = cx / 200;
   1036  1.4.4.8        he 	csrc = cx - 200*cy;
   1037  1.4.4.8        he 
   1038  1.4.4.8        he 	cdlay = ((48000 * 24) + rate - 1) / rate;
   1039  1.4.4.8        he #if 0
   1040  1.4.4.8        he 	cdlay &= 0x3fff; /* make sure cdlay is 14-bit */
   1041  1.4.4.8        he #endif
   1042  1.4.4.8        he 
   1043  1.4.4.8        he 	cnt  = rate << 16;
   1044  1.4.4.8        he 	cnt  /= 24000;
   1045  1.4.4.8        he 
   1046  1.4.4.8        he 	cgl = 1;
   1047  1.4.4.8        he 	for (tmp1 = 2; tmp1 <= 64; tmp1 *= 2) {
   1048  1.4.4.8        he 		if (((rate / tmp1) * tmp1) != rate)
   1049  1.4.4.8        he 			cgl *= 2;
   1050  1.4.4.8        he 	}
   1051  1.4.4.8        he 	if (((rate / 3) * 3) != rate)
   1052  1.4.4.8        he 		cgl *= 3;
   1053  1.4.4.8        he 	for (tmp1 = 5; tmp1 <= 125; tmp1 *= 5) {
   1054  1.4.4.8        he 		if (((rate / tmp1) * tmp1) != rate)
   1055  1.4.4.8        he 			cgl *= 5;
   1056  1.4.4.8        he 	}
   1057  1.4.4.8        he #if 0
   1058  1.4.4.8        he 	/* XXX what manual says */
   1059  1.4.4.8        he 	tmp1 = BA1READ4(sc, CS4280_CSRC) & ~CSRC_MASK;
   1060  1.4.4.8        he 	tmp1 |= csrc<<16;
   1061  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CSRC, tmp1);
   1062  1.4.4.8        he #else
   1063  1.4.4.8        he 	/* suggested by cs461x.c (ALSA driver) */
   1064  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CSRC, CS4280_MK_CSRC(csrc, cy));
   1065  1.4.4.8        he #endif
   1066  1.4.4.8        he 
   1067  1.4.4.8        he #if 0
   1068  1.4.4.8        he 	/* I am confused.  The sample rate calculation section says
   1069  1.4.4.8        he 	 * cci *is* 32-bit signed quantity but in the parameter description
   1070  1.4.4.8        he 	 * section, CCI only assigned 16bit.
   1071  1.4.4.8        he 	 * I believe size of the variable.
   1072  1.4.4.8        he 	 */
   1073  1.4.4.8        he 	tmp1 = BA1READ4(sc, CS4280_CCI) & ~CCI_MASK;
   1074  1.4.4.8        he 	tmp1 |= cci<<16;
   1075  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CCI, tmp1);
   1076  1.4.4.8        he #else
   1077  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CCI, cci);
   1078  1.4.4.8        he #endif
   1079  1.4.4.8        he 
   1080  1.4.4.8        he 	tmp1 = BA1READ4(sc, CS4280_CD) & ~CD_MASK;
   1081  1.4.4.8        he 	tmp1 |= cdlay <<18;
   1082  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CD, tmp1);
   1083  1.4.4.8        he 
   1084  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CPI, cpi);
   1085  1.4.4.8        he 
   1086  1.4.4.8        he 	tmp1 = BA1READ4(sc, CS4280_CGL) & ~CGL_MASK;
   1087  1.4.4.8        he 	tmp1 |= cgl;
   1088  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CGL, tmp1);
   1089      1.1  augustss 
   1090  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CNT, cnt);
   1091  1.4.4.8        he 
   1092  1.4.4.8        he 	tmp1 = BA1READ4(sc, CS4280_CGC) & ~CGC_MASK;
   1093  1.4.4.8        he 	tmp1 |= cgl;
   1094  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CGC, tmp1);
   1095      1.1  augustss }
   1096      1.1  augustss 
   1097  1.4.4.8        he void
   1098  1.4.4.8        he cs4280_set_dac_rate(sc, rate)
   1099  1.4.4.8        he 	struct cs428x_softc *sc;
   1100  1.4.4.8        he 	int rate;
   1101      1.1  augustss {
   1102  1.4.4.8        he 	/*
   1103  1.4.4.8        he 	 * playback rate may range from 8000Hz to 48000Hz
   1104  1.4.4.8        he 	 *
   1105  1.4.4.8        he 	 * play_phase_increment = floor(rate*65536*1024/48000)
   1106  1.4.4.8        he 	 * px = round(rate*65536*1024 - play_phase_incremnt*48000)
   1107  1.4.4.8        he 	 * py=floor(px/200)
   1108  1.4.4.8        he 	 * play_sample_rate_correction = px - 200*py
   1109  1.4.4.8        he 	 *
   1110  1.4.4.8        he 	 * play_phase_increment is a 32bit signed quantity.
   1111  1.4.4.8        he 	 * play_sample_rate_correction is a 16bit signed quantity.
   1112  1.4.4.8        he 	 */
   1113  1.4.4.8        he 	int32_t ppi;
   1114  1.4.4.8        he 	int16_t psrc;
   1115  1.4.4.8        he 	u_int32_t px, py;
   1116      1.1  augustss 
   1117  1.4.4.8        he 	if (rate < 8000)
   1118  1.4.4.8        he 		rate = 8000;
   1119  1.4.4.8        he 	if (rate > 48000)
   1120  1.4.4.8        he 		rate = 48000;
   1121  1.4.4.8        he 	px = rate << 16;
   1122  1.4.4.8        he 	ppi = px/48000;
   1123  1.4.4.8        he 	px -= ppi*48000;
   1124  1.4.4.8        he 	ppi <<= 10;
   1125  1.4.4.8        he 	px  <<= 10;
   1126  1.4.4.8        he 	py  = px / 48000;
   1127  1.4.4.8        he 	ppi += py;
   1128  1.4.4.8        he 	px -= py*48000;
   1129  1.4.4.8        he 	py  = px/200;
   1130  1.4.4.8        he 	px -= py*200;
   1131  1.4.4.8        he 	psrc = px;
   1132  1.4.4.8        he #if 0
   1133  1.4.4.8        he 	/* what manual says */
   1134  1.4.4.8        he 	px = BA1READ4(sc, CS4280_PSRC) & ~PSRC_MASK;
   1135  1.4.4.8        he 	BA1WRITE4(sc, CS4280_PSRC,
   1136  1.4.4.8        he 			  ( ((psrc<<16) & PSRC_MASK) | px ));
   1137  1.4.4.8        he #else
   1138  1.4.4.8        he 	/* suggested by cs461x.c (ALSA driver) */
   1139  1.4.4.8        he 	BA1WRITE4(sc, CS4280_PSRC, CS4280_MK_PSRC(psrc,py));
   1140      1.1  augustss #endif
   1141  1.4.4.8        he 	BA1WRITE4(sc, CS4280_PPI, ppi);
   1142  1.4.4.8        he }
   1143      1.1  augustss 
   1144  1.4.4.8        he /* Download Proceessor Code and Data image */
   1145  1.4.4.8        he int
   1146  1.4.4.8        he cs4280_download(sc, src, offset, len)
   1147  1.4.4.8        he 	struct cs428x_softc *sc;
   1148  1.4.4.8        he 	const u_int32_t *src;
   1149  1.4.4.8        he 	u_int32_t offset, len;
   1150  1.4.4.8        he {
   1151  1.4.4.8        he 	u_int32_t ctr;
   1152      1.1  augustss 
   1153  1.4.4.8        he #if CS4280_DEBUG > 10
   1154  1.4.4.8        he 	u_int32_t con, data;
   1155  1.4.4.8        he 	u_int8_t c0,c1,c2,c3;
   1156  1.4.4.8        he #endif
   1157  1.4.4.8        he 	if ((offset&3) || (len&3))
   1158  1.4.4.8        he 		return -1;
   1159      1.1  augustss 
   1160  1.4.4.8        he 	len /= sizeof(u_int32_t);
   1161  1.4.4.8        he 	for (ctr = 0; ctr < len; ctr++) {
   1162  1.4.4.8        he 		/* XXX:
   1163  1.4.4.8        he 		 * I cannot confirm this is the right thing or not
   1164  1.4.4.8        he 		 * on BIG-ENDIAN machines.
   1165  1.4.4.8        he 		 */
   1166  1.4.4.8        he 		BA1WRITE4(sc, offset+ctr*4, htole32(*(src+ctr)));
   1167  1.4.4.8        he #if CS4280_DEBUG > 10
   1168  1.4.4.8        he 		data = htole32(*(src+ctr));
   1169  1.4.4.8        he 		c0 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+0);
   1170  1.4.4.8        he 		c1 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+1);
   1171  1.4.4.8        he 		c2 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+2);
   1172  1.4.4.8        he 		c3 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+3);
   1173  1.4.4.8        he 		con = ( (c3<<24) | (c2<<16) | (c1<<8) | c0 );
   1174  1.4.4.8        he 		if (data != con ) {
   1175  1.4.4.8        he 			printf("0x%06x: write=0x%08x read=0x%08x\n",
   1176  1.4.4.8        he 			       offset+ctr*4, data, con);
   1177  1.4.4.8        he 			return -1;
   1178  1.4.4.8        he 		}
   1179  1.4.4.8        he #endif
   1180      1.1  augustss 	}
   1181  1.4.4.8        he 	return 0;
   1182  1.4.4.8        he }
   1183  1.4.4.8        he 
   1184  1.4.4.8        he int
   1185  1.4.4.8        he cs4280_download_image(sc)
   1186  1.4.4.8        he 	struct cs428x_softc *sc;
   1187  1.4.4.8        he {
   1188  1.4.4.8        he 	int idx, err;
   1189  1.4.4.8        he 	u_int32_t offset = 0;
   1190  1.4.4.8        he 
   1191  1.4.4.8        he 	err = 0;
   1192  1.4.4.8        he 	for (idx = 0; idx < BA1_MEMORY_COUNT; ++idx) {
   1193  1.4.4.8        he 		err = cs4280_download(sc, &BA1Struct.map[offset],
   1194  1.4.4.8        he 				  BA1Struct.memory[idx].offset,
   1195  1.4.4.8        he 				  BA1Struct.memory[idx].size);
   1196  1.4.4.8        he 		if (err != 0) {
   1197  1.4.4.8        he 			printf("%s: load_image failed at %d\n",
   1198  1.4.4.8        he 			       sc->sc_dev.dv_xname, idx);
   1199  1.4.4.8        he 			return -1;
   1200  1.4.4.8        he 		}
   1201  1.4.4.8        he 		offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
   1202      1.1  augustss 	}
   1203  1.4.4.8        he 	return err;
   1204  1.4.4.8        he }
   1205      1.1  augustss 
   1206  1.4.4.8        he /* Processor Soft Reset */
   1207  1.4.4.8        he void
   1208  1.4.4.8        he cs4280_reset(sc_)
   1209  1.4.4.8        he 	void *sc_;
   1210  1.4.4.8        he {
   1211  1.4.4.8        he 	struct cs428x_softc *sc = sc_;
   1212      1.1  augustss 
   1213  1.4.4.8        he 	/* Set RSTSP bit in SPCR (also clear RUN, RUNFR, and DRQEN) */
   1214  1.4.4.8        he 	BA1WRITE4(sc, CS4280_SPCR, SPCR_RSTSP);
   1215  1.4.4.8        he 	delay(100);
   1216  1.4.4.8        he 	/* Clear RSTSP bit in SPCR */
   1217  1.4.4.8        he 	BA1WRITE4(sc, CS4280_SPCR, 0);
   1218  1.4.4.8        he 	/* enable DMA reqest */
   1219  1.4.4.8        he 	BA1WRITE4(sc, CS4280_SPCR, SPCR_DRQEN);
   1220  1.4.4.8        he }
   1221      1.1  augustss 
   1222  1.4.4.8        he int
   1223  1.4.4.8        he cs4280_get_portnum_by_name(sc, class, device, qualifier)
   1224  1.4.4.8        he 	struct cs428x_softc *sc;
   1225  1.4.4.8        he 	char *class, *device, *qualifier;
   1226  1.4.4.8        he {
   1227  1.4.4.8        he 	return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
   1228  1.4.4.8        he 	     device, qualifier));
   1229      1.1  augustss }
   1230      1.1  augustss 
   1231      1.2  augustss int
   1232      1.1  augustss cs4280_init(sc, init)
   1233  1.4.4.8        he 	struct cs428x_softc *sc;
   1234      1.1  augustss 	int init;
   1235      1.1  augustss {
   1236      1.1  augustss 	int n;
   1237      1.1  augustss 	u_int32_t mem;
   1238      1.1  augustss 
   1239      1.1  augustss 	/* Start PLL out in known state */
   1240      1.1  augustss 	BA0WRITE4(sc, CS4280_CLKCR1, 0);
   1241      1.1  augustss 	/* Start serial ports out in known state */
   1242      1.1  augustss 	BA0WRITE4(sc, CS4280_SERMC1, 0);
   1243      1.1  augustss 
   1244      1.1  augustss 	/* Specify type of CODEC */
   1245  1.4.4.2  augustss /* XXX should not be here */
   1246      1.1  augustss #define SERACC_CODEC_TYPE_1_03
   1247      1.1  augustss #ifdef	SERACC_CODEC_TYPE_1_03
   1248      1.1  augustss 	BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_1_03); /* AC 97 1.03 */
   1249      1.1  augustss #else
   1250      1.1  augustss 	BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_2_0);  /* AC 97 2.0 */
   1251      1.1  augustss #endif
   1252      1.1  augustss 
   1253      1.1  augustss 	/* Reset codec */
   1254  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, 0);
   1255      1.1  augustss 	delay(100);    /* delay 100us */
   1256  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_RSTN);
   1257      1.1  augustss 
   1258      1.1  augustss 	/* Enable AC-link sync generation */
   1259  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
   1260      1.1  augustss 	delay(50*1000); /* delay 50ms */
   1261      1.1  augustss 
   1262      1.1  augustss 	/* Set the serial port timing configuration */
   1263      1.1  augustss 	BA0WRITE4(sc, CS4280_SERMC1, SERMC1_PTC_AC97);
   1264      1.1  augustss 
   1265      1.1  augustss 	/* Setup clock control */
   1266      1.1  augustss 	BA0WRITE4(sc, CS4280_PLLCC, PLLCC_CDR_STATE|PLLCC_LPF_STATE);
   1267      1.1  augustss 	BA0WRITE4(sc, CS4280_PLLM, PLLM_STATE);
   1268      1.1  augustss 	BA0WRITE4(sc, CS4280_CLKCR2, CLKCR2_PDIVS_8);
   1269      1.1  augustss 
   1270      1.1  augustss 	/* Power up the PLL */
   1271      1.1  augustss 	BA0WRITE4(sc, CS4280_CLKCR1, CLKCR1_PLLP);
   1272      1.1  augustss 	delay(50*1000); /* delay 50ms */
   1273      1.1  augustss 
   1274      1.1  augustss 	/* Turn on clock */
   1275  1.4.4.8        he 	mem = BA0READ4(sc, CS4280_CLKCR1) | CLKCR1_SWCE;
   1276  1.4.4.8        he 	BA0WRITE4(sc, CS4280_CLKCR1, mem);
   1277      1.1  augustss 
   1278      1.2  augustss 	/* Set the serial port FIFO pointer to the
   1279      1.2  augustss 	 * first sample in FIFO. (not documented) */
   1280      1.1  augustss 	cs4280_clear_fifos(sc);
   1281      1.2  augustss 
   1282      1.2  augustss #if 0
   1283      1.2  augustss 	/* Set the serial port FIFO pointer to the first sample in the FIFO */
   1284      1.2  augustss 	BA0WRITE4(sc, CS4280_SERBSP, 0);
   1285      1.1  augustss #endif
   1286      1.1  augustss 
   1287      1.1  augustss 	/* Configure the serial port */
   1288      1.1  augustss 	BA0WRITE4(sc, CS4280_SERC1,  SERC1_SO1EN | SERC1_SO1F_AC97);
   1289      1.1  augustss 	BA0WRITE4(sc, CS4280_SERC2,  SERC2_SI1EN | SERC2_SI1F_AC97);
   1290      1.1  augustss 	BA0WRITE4(sc, CS4280_SERMC1, SERMC1_MSPE | SERMC1_PTC_AC97);
   1291      1.1  augustss 
   1292      1.1  augustss 	/* Wait for CODEC ready */
   1293      1.1  augustss 	n = 0;
   1294  1.4.4.8        he 	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
   1295      1.2  augustss 		delay(125);
   1296      1.2  augustss 		if (++n > 1000) {
   1297      1.1  augustss 			printf("%s: codec ready timeout\n",
   1298      1.1  augustss 			       sc->sc_dev.dv_xname);
   1299      1.2  augustss 			return(1);
   1300      1.1  augustss 		}
   1301      1.1  augustss 	}
   1302      1.1  augustss 
   1303      1.1  augustss 	/* Assert valid frame signal */
   1304  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
   1305      1.1  augustss 
   1306      1.1  augustss 	/* Wait for valid AC97 input slot */
   1307      1.1  augustss 	n = 0;
   1308  1.4.4.8        he 	while ((BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
   1309  1.4.4.8        he 	       (ACISV_ISV3 | ACISV_ISV4)) {
   1310      1.1  augustss 		delay(1000);
   1311      1.1  augustss 		if (++n > 1000) {
   1312      1.1  augustss 			printf("AC97 inputs slot ready timeout\n");
   1313      1.2  augustss 			return(1);
   1314      1.1  augustss 		}
   1315      1.1  augustss 	}
   1316      1.1  augustss 
   1317      1.1  augustss 	/* Set AC97 output slot valid signals */
   1318  1.4.4.8        he 	BA0WRITE4(sc, CS428X_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
   1319      1.1  augustss 
   1320      1.1  augustss 	/* reset the processor */
   1321      1.1  augustss 	cs4280_reset(sc);
   1322      1.1  augustss 
   1323      1.1  augustss 	/* Download the image to the processor */
   1324      1.1  augustss 	if (cs4280_download_image(sc) != 0) {
   1325      1.1  augustss 		printf("%s: image download error\n", sc->sc_dev.dv_xname);
   1326      1.2  augustss 		return(1);
   1327      1.1  augustss 	}
   1328      1.1  augustss 
   1329      1.1  augustss 	/* Save playback parameter and then write zero.
   1330      1.1  augustss 	 * this ensures that DMA doesn't immediately occur upon
   1331      1.1  augustss 	 * starting the processor core
   1332      1.1  augustss 	 */
   1333      1.1  augustss 	mem = BA1READ4(sc, CS4280_PCTL);
   1334      1.1  augustss 	sc->pctl = mem & PCTL_MASK; /* save startup value */
   1335  1.4.4.8        he 	BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
   1336  1.4.4.8        he 	if (init != 0)
   1337  1.4.4.8        he 		sc->sc_prun = 0;
   1338      1.1  augustss 
   1339      1.1  augustss 	/* Save capture parameter and then write zero.
   1340      1.1  augustss 	 * this ensures that DMA doesn't immediately occur upon
   1341      1.1  augustss 	 * starting the processor core
   1342      1.1  augustss 	 */
   1343      1.1  augustss 	mem = BA1READ4(sc, CS4280_CCTL);
   1344      1.1  augustss 	sc->cctl = mem & CCTL_MASK; /* save startup value */
   1345  1.4.4.8        he 	BA1WRITE4(sc, CS4280_CCTL, mem & ~CCTL_MASK);
   1346  1.4.4.8        he 	if (init != 0)
   1347  1.4.4.8        he 		sc->sc_rrun = 0;
   1348      1.1  augustss 
   1349      1.1  augustss 	/* Processor Startup Procedure */
   1350      1.1  augustss 	BA1WRITE4(sc, CS4280_FRMT, FRMT_FTV);
   1351      1.1  augustss 	BA1WRITE4(sc, CS4280_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
   1352      1.1  augustss 
   1353      1.1  augustss 	/* Monitor RUNFR bit in SPCR for 1 to 0 transition */
   1354      1.1  augustss 	n = 0;
   1355      1.1  augustss 	while (BA1READ4(sc, CS4280_SPCR) & SPCR_RUNFR) {
   1356      1.1  augustss 		delay(10);
   1357      1.1  augustss 		if (++n > 1000) {
   1358      1.1  augustss 			printf("SPCR 1->0 transition timeout\n");
   1359      1.2  augustss 			return(1);
   1360      1.1  augustss 		}
   1361      1.1  augustss 	}
   1362      1.1  augustss 
   1363      1.1  augustss 	n = 0;
   1364      1.1  augustss 	while (!(BA1READ4(sc, CS4280_SPCS) & SPCS_SPRUN)) {
   1365      1.1  augustss 		delay(10);
   1366      1.1  augustss 		if (++n > 1000) {
   1367      1.1  augustss 			printf("SPCS 0->1 transition timeout\n");
   1368      1.2  augustss 			return(1);
   1369      1.1  augustss 		}
   1370      1.1  augustss 	}
   1371      1.1  augustss 	/* Processor is now running !!! */
   1372      1.1  augustss 
   1373      1.1  augustss 	/* Setup  volume */
   1374      1.1  augustss 	BA1WRITE4(sc, CS4280_PVOL, 0x80008000);
   1375      1.1  augustss 	BA1WRITE4(sc, CS4280_CVOL, 0x80008000);
   1376      1.1  augustss 
   1377      1.1  augustss 	/* Interrupt enable */
   1378      1.1  augustss 	BA0WRITE4(sc, CS4280_HICR, HICR_IEV|HICR_CHGM);
   1379      1.1  augustss 
   1380      1.1  augustss 	/* playback interrupt enable */
   1381      1.1  augustss 	mem = BA1READ4(sc, CS4280_PFIE) & ~PFIE_PI_MASK;
   1382      1.1  augustss 	mem |= PFIE_PI_ENABLE;
   1383      1.1  augustss 	BA1WRITE4(sc, CS4280_PFIE, mem);
   1384      1.1  augustss 	/* capture interrupt enable */
   1385      1.1  augustss 	mem = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
   1386      1.1  augustss 	mem |= CIE_CI_ENABLE;
   1387      1.1  augustss 	BA1WRITE4(sc, CS4280_CIE, mem);
   1388      1.2  augustss 
   1389      1.2  augustss #if NMIDI > 0
   1390      1.2  augustss 	/* Reset midi port */
   1391      1.2  augustss 	mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
   1392      1.2  augustss 	BA0WRITE4(sc, CS4280_MIDCR, mem | MIDCR_MRST);
   1393      1.2  augustss 	DPRINTF(("midi reset: 0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
   1394      1.2  augustss 	/* midi interrupt enable */
   1395      1.2  augustss 	mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE;
   1396      1.2  augustss 	BA0WRITE4(sc, CS4280_MIDCR, mem);
   1397      1.2  augustss #endif
   1398      1.2  augustss 	return(0);
   1399      1.1  augustss }
   1400      1.1  augustss 
   1401      1.1  augustss void
   1402      1.1  augustss cs4280_clear_fifos(sc)
   1403  1.4.4.8        he 	struct cs428x_softc *sc;
   1404      1.1  augustss {
   1405      1.1  augustss 	int pd = 0, cnt, n;
   1406      1.1  augustss 	u_int32_t mem;
   1407      1.1  augustss 
   1408      1.1  augustss 	/*
   1409      1.1  augustss 	 * If device power down, power up the device and keep power down
   1410      1.1  augustss 	 * state.
   1411      1.1  augustss 	 */
   1412      1.1  augustss 	mem = BA0READ4(sc, CS4280_CLKCR1);
   1413      1.1  augustss 	if (!(mem & CLKCR1_SWCE)) {
   1414      1.1  augustss 		printf("cs4280_clear_fifo: power down found.\n");
   1415      1.1  augustss 		BA0WRITE4(sc, CS4280_CLKCR1, mem | CLKCR1_SWCE);
   1416      1.1  augustss 		pd = 1;
   1417      1.1  augustss 	}
   1418      1.1  augustss 	BA0WRITE4(sc, CS4280_SERBWP, 0);
   1419      1.1  augustss 	for (cnt = 0; cnt < 256; cnt++) {
   1420      1.1  augustss 		n = 0;
   1421      1.1  augustss 		while (BA0READ4(sc, CS4280_SERBST) & SERBST_WBSY) {
   1422      1.1  augustss 			delay(1000);
   1423      1.1  augustss 			if (++n > 1000) {
   1424      1.1  augustss 				printf("clear_fifo: fist timeout cnt=%d\n", cnt);
   1425      1.1  augustss 				break;
   1426      1.1  augustss 			}
   1427      1.1  augustss 		}
   1428      1.1  augustss 		BA0WRITE4(sc, CS4280_SERBAD, cnt);
   1429      1.1  augustss 		BA0WRITE4(sc, CS4280_SERBCM, SERBCM_WRC);
   1430      1.1  augustss 	}
   1431      1.1  augustss 	if (pd)
   1432      1.1  augustss 		BA0WRITE4(sc, CS4280_CLKCR1, mem);
   1433      1.1  augustss }
   1434      1.1  augustss 
   1435      1.1  augustss #if NMIDI > 0
   1436      1.1  augustss int
   1437      1.1  augustss cs4280_midi_open(addr, flags, iintr, ointr, arg)
   1438      1.1  augustss 	void *addr;
   1439      1.1  augustss 	int flags;
   1440      1.1  augustss 	void (*iintr)__P((void *, int));
   1441      1.1  augustss 	void (*ointr)__P((void *));
   1442      1.1  augustss 	void *arg;
   1443      1.1  augustss {
   1444  1.4.4.8        he 	struct cs428x_softc *sc = addr;
   1445      1.1  augustss 	u_int32_t mem;
   1446      1.1  augustss 
   1447      1.1  augustss 	DPRINTF(("midi_open\n"));
   1448      1.1  augustss 	sc->sc_iintr = iintr;
   1449      1.1  augustss 	sc->sc_ointr = ointr;
   1450      1.1  augustss 	sc->sc_arg = arg;
   1451      1.1  augustss 
   1452      1.2  augustss 	/* midi interrupt enable */
   1453      1.2  augustss 	mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
   1454      1.1  augustss 	mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE | MIDCR_MLB;
   1455      1.1  augustss 	BA0WRITE4(sc, CS4280_MIDCR, mem);
   1456      1.2  augustss #ifdef CS4280_DEBUG
   1457      1.2  augustss 	if (mem != BA0READ4(sc, CS4280_MIDCR)) {
   1458      1.2  augustss 		DPRINTF(("midi_open: MIDCR=%d\n", BA0READ4(sc, CS4280_MIDCR)));
   1459      1.2  augustss 		return(EINVAL);
   1460      1.2  augustss 	}
   1461      1.2  augustss 	DPRINTF(("MIDCR=0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
   1462      1.2  augustss #endif
   1463  1.4.4.8        he 	return 0;
   1464      1.1  augustss }
   1465      1.1  augustss 
   1466      1.1  augustss void
   1467      1.1  augustss cs4280_midi_close(addr)
   1468      1.1  augustss 	void *addr;
   1469      1.1  augustss {
   1470  1.4.4.8        he 	struct cs428x_softc *sc = addr;
   1471      1.1  augustss 	u_int32_t mem;
   1472      1.1  augustss 
   1473      1.1  augustss 	DPRINTF(("midi_close\n"));
   1474  1.4.4.8        he 	tsleep(sc, PWAIT, "cs0clm", hz/10); /* give uart a chance to drain */
   1475      1.1  augustss 	mem = BA0READ4(sc, CS4280_MIDCR);
   1476      1.2  augustss 	mem &= ~MIDCR_MASK;
   1477      1.1  augustss 	BA0WRITE4(sc, CS4280_MIDCR, mem);
   1478      1.1  augustss 
   1479      1.1  augustss 	sc->sc_iintr = 0;
   1480      1.1  augustss 	sc->sc_ointr = 0;
   1481      1.1  augustss }
   1482      1.1  augustss 
   1483      1.1  augustss int
   1484      1.1  augustss cs4280_midi_output(addr, d)
   1485      1.1  augustss 	void *addr;
   1486      1.1  augustss 	int d;
   1487      1.1  augustss {
   1488  1.4.4.8        he 	struct cs428x_softc *sc = addr;
   1489      1.1  augustss 	u_int32_t mem;
   1490      1.1  augustss 	int x;
   1491      1.1  augustss 
   1492      1.1  augustss 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1493      1.2  augustss 		if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
   1494      1.2  augustss 			mem = BA0READ4(sc, CS4280_MIDWP) & ~MIDWP_MASK;
   1495      1.2  augustss 			mem |= d & MIDWP_MASK;
   1496      1.2  augustss 			DPRINTFN(5,("midi_output d=0x%08x",d));
   1497      1.1  augustss 			BA0WRITE4(sc, CS4280_MIDWP, mem);
   1498  1.4.4.2  augustss #ifdef DIAGNOSTIC
   1499      1.2  augustss 			if (mem != BA0READ4(sc, CS4280_MIDWP)) {
   1500      1.2  augustss 				DPRINTF(("Bad write data: %d %d",
   1501      1.2  augustss 					 mem, BA0READ4(sc, CS4280_MIDWP)));
   1502      1.2  augustss 				return(EIO);
   1503      1.2  augustss 			}
   1504  1.4.4.2  augustss #endif
   1505  1.4.4.8        he 			return 0;
   1506      1.1  augustss 		}
   1507      1.1  augustss 		delay(MIDI_BUSY_DELAY);
   1508      1.1  augustss 	}
   1509      1.1  augustss 	return (EIO);
   1510      1.1  augustss }
   1511      1.1  augustss 
   1512      1.1  augustss void
   1513      1.1  augustss cs4280_midi_getinfo(addr, mi)
   1514      1.1  augustss 	void *addr;
   1515      1.1  augustss 	struct midi_info *mi;
   1516      1.1  augustss {
   1517      1.1  augustss 	mi->name = "CS4280 MIDI UART";
   1518      1.1  augustss 	mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
   1519  1.4.4.8        he }
   1520  1.4.4.8        he 
   1521  1.4.4.8        he #endif
   1522  1.4.4.8        he 
   1523  1.4.4.8        he /* DEBUG functions */
   1524  1.4.4.8        he #if CS4280_DEBUG > 10
   1525  1.4.4.8        he int
   1526  1.4.4.8        he cs4280_checkimage(sc, src, offset, len)
   1527  1.4.4.8        he 	struct cs428x_softc *sc;
   1528  1.4.4.8        he 	u_int32_t *src;
   1529  1.4.4.8        he 	u_int32_t offset, len;
   1530  1.4.4.8        he {
   1531  1.4.4.8        he 	u_int32_t ctr, data;
   1532  1.4.4.8        he 	int err = 0;
   1533  1.4.4.8        he 
   1534  1.4.4.8        he 	if ((offset&3) || (len&3))
   1535  1.4.4.8        he 		return -1;
   1536  1.4.4.8        he 
   1537  1.4.4.8        he 	len /= sizeof(u_int32_t);
   1538  1.4.4.8        he 	for (ctr = 0; ctr < len; ctr++) {
   1539  1.4.4.8        he 		/* I cannot confirm this is the right thing
   1540  1.4.4.8        he 		 * on BIG-ENDIAN machines
   1541  1.4.4.8        he 		 */
   1542  1.4.4.8        he 		data = BA1READ4(sc, offset+ctr*4);
   1543  1.4.4.8        he 		if (data != htole32(*(src+ctr))) {
   1544  1.4.4.8        he 			printf("0x%06x: 0x%08x(0x%08x)\n",
   1545  1.4.4.8        he 			       offset+ctr*4, data, *(src+ctr));
   1546  1.4.4.8        he 			*(src+ctr) = data;
   1547  1.4.4.8        he 			++err;
   1548  1.4.4.8        he 		}
   1549  1.4.4.8        he 	}
   1550  1.4.4.8        he 	return err;
   1551  1.4.4.8        he }
   1552  1.4.4.8        he 
   1553  1.4.4.8        he int
   1554  1.4.4.8        he cs4280_check_images(sc)
   1555  1.4.4.8        he 	struct cs428x_softc *sc;
   1556  1.4.4.8        he {
   1557  1.4.4.8        he 	int idx, err;
   1558  1.4.4.8        he 	u_int32_t offset = 0;
   1559  1.4.4.8        he 
   1560  1.4.4.8        he 	err = 0;
   1561  1.4.4.8        he 	/*for (idx=0; idx < BA1_MEMORY_COUNT; ++idx) { */
   1562  1.4.4.8        he 	for (idx = 0; idx < 1; ++idx) {
   1563  1.4.4.8        he 		err = cs4280_checkimage(sc, &BA1Struct.map[offset],
   1564  1.4.4.8        he 				      BA1Struct.memory[idx].offset,
   1565  1.4.4.8        he 				      BA1Struct.memory[idx].size);
   1566  1.4.4.8        he 		if (err != 0) {
   1567  1.4.4.8        he 			printf("%s: check_image failed at %d\n",
   1568  1.4.4.8        he 			       sc->sc_dev.dv_xname, idx);
   1569  1.4.4.8        he 		}
   1570  1.4.4.8        he 		offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
   1571  1.4.4.8        he 	}
   1572  1.4.4.8        he 	return err;
   1573      1.1  augustss }
   1574      1.1  augustss 
   1575      1.1  augustss #endif
   1576