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