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