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