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