Home | History | Annotate | Line # | Download | only in pci
cs428x.c revision 1.2.2.2
      1  1.2.2.2  he /*	$NetBSD: cs428x.c,v 1.2.2.2 2001/05/06 15:11:14 he Exp $	*/
      2  1.2.2.2  he 
      3  1.2.2.2  he /*
      4  1.2.2.2  he  * Copyright (c) 2000 Tatoku Ogaito.  All rights reserved.
      5  1.2.2.2  he  *
      6  1.2.2.2  he  * Redistribution and use in source and binary forms, with or without
      7  1.2.2.2  he  * modification, are permitted provided that the following conditions
      8  1.2.2.2  he  * are met:
      9  1.2.2.2  he  * 1. Redistributions of source code must retain the above copyright
     10  1.2.2.2  he  *    notice, this list of conditions and the following disclaimer.
     11  1.2.2.2  he  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.2.2.2  he  *    notice, this list of conditions and the following disclaimer in the
     13  1.2.2.2  he  *    documentation and/or other materials provided with the distribution.
     14  1.2.2.2  he  * 3. All advertising materials mentioning features or use of this software
     15  1.2.2.2  he  *    must display the following acknowledgement:
     16  1.2.2.2  he  *      This product includes software developed by Tatoku Ogaito
     17  1.2.2.2  he  *	for the NetBSD Project.
     18  1.2.2.2  he  * 4. The name of the author may not be used to endorse or promote products
     19  1.2.2.2  he  *    derived from this software without specific prior written permission
     20  1.2.2.2  he  *
     21  1.2.2.2  he  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.2.2.2  he  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.2.2.2  he  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.2.2.2  he  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.2.2.2  he  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.2.2.2  he  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.2.2.2  he  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.2.2.2  he  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.2.2.2  he  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.2.2.2  he  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.2.2.2  he  */
     32  1.2.2.2  he 
     33  1.2.2.2  he /* Common functions for CS4280 and CS4281 */
     34  1.2.2.2  he 
     35  1.2.2.2  he #include <sys/param.h>
     36  1.2.2.2  he #include <sys/systm.h>
     37  1.2.2.2  he #include <sys/types.h>
     38  1.2.2.2  he #include <sys/kernel.h>
     39  1.2.2.2  he #include <sys/malloc.h>
     40  1.2.2.2  he #include <sys/device.h>
     41  1.2.2.2  he 
     42  1.2.2.2  he #include <dev/pci/pcidevs.h>
     43  1.2.2.2  he #include <dev/pci/pcivar.h>
     44  1.2.2.2  he 
     45  1.2.2.2  he #include <sys/audioio.h>
     46  1.2.2.2  he #include <dev/audio_if.h>
     47  1.2.2.2  he #include <dev/midi_if.h>
     48  1.2.2.2  he #include <dev/mulaw.h>
     49  1.2.2.2  he #include <dev/auconv.h>
     50  1.2.2.2  he 
     51  1.2.2.2  he #include <dev/ic/ac97reg.h>
     52  1.2.2.2  he #include <dev/ic/ac97var.h>
     53  1.2.2.2  he 
     54  1.2.2.2  he #include <machine/bus.h>
     55  1.2.2.2  he 
     56  1.2.2.2  he #include <dev/pci/cs428xreg.h>
     57  1.2.2.2  he #include <dev/pci/cs428x.h>
     58  1.2.2.2  he 
     59  1.2.2.2  he #if defined(CS4280_DEBUG) || defined(CS4281_DEBUG)
     60  1.2.2.2  he int cs428x_debug = 0;
     61  1.2.2.2  he #endif
     62  1.2.2.2  he 
     63  1.2.2.2  he int
     64  1.2.2.2  he cs428x_open(void *addr, int flags)
     65  1.2.2.2  he {
     66  1.2.2.2  he 	return 0;
     67  1.2.2.2  he }
     68  1.2.2.2  he 
     69  1.2.2.2  he void
     70  1.2.2.2  he cs428x_close(void *addr)
     71  1.2.2.2  he {
     72  1.2.2.2  he 	struct cs428x_softc *sc;
     73  1.2.2.2  he 
     74  1.2.2.2  he 	sc = addr;
     75  1.2.2.2  he 
     76  1.2.2.2  he 	(*sc->halt_output)(sc);
     77  1.2.2.2  he 	(*sc->halt_input)(sc);
     78  1.2.2.2  he 
     79  1.2.2.2  he 	sc->sc_pintr = 0;
     80  1.2.2.2  he 	sc->sc_rintr = 0;
     81  1.2.2.2  he }
     82  1.2.2.2  he 
     83  1.2.2.2  he int
     84  1.2.2.2  he cs428x_round_blocksize(void *addr, int blk)
     85  1.2.2.2  he {
     86  1.2.2.2  he 	struct cs428x_softc *sc;
     87  1.2.2.2  he 	int retval;
     88  1.2.2.2  he 
     89  1.2.2.2  he 	DPRINTFN(5,("cs428x_round_blocksize blk=%d -> ", blk));
     90  1.2.2.2  he 
     91  1.2.2.2  he 	sc = addr;
     92  1.2.2.2  he 	if (blk < sc->hw_blocksize)
     93  1.2.2.2  he 		retval = sc->hw_blocksize;
     94  1.2.2.2  he 	else
     95  1.2.2.2  he 		retval = blk & -(sc->hw_blocksize);
     96  1.2.2.2  he 
     97  1.2.2.2  he 	DPRINTFN(5,("%d\n", retval));
     98  1.2.2.2  he 
     99  1.2.2.2  he 	return retval;
    100  1.2.2.2  he }
    101  1.2.2.2  he 
    102  1.2.2.2  he int
    103  1.2.2.2  he cs428x_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    104  1.2.2.2  he {
    105  1.2.2.2  he 	struct cs428x_softc *sc;
    106  1.2.2.2  he 	int val;
    107  1.2.2.2  he 
    108  1.2.2.2  he 	sc = addr;
    109  1.2.2.2  he 	val = sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    110  1.2.2.2  he 	DPRINTFN(3,("mixer_set_port: val=%d\n", val));
    111  1.2.2.2  he 	return (val);
    112  1.2.2.2  he }
    113  1.2.2.2  he 
    114  1.2.2.2  he int
    115  1.2.2.2  he cs428x_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    116  1.2.2.2  he {
    117  1.2.2.2  he 	struct cs428x_softc *sc;
    118  1.2.2.2  he 
    119  1.2.2.2  he 	sc = addr;
    120  1.2.2.2  he 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    121  1.2.2.2  he }
    122  1.2.2.2  he 
    123  1.2.2.2  he int
    124  1.2.2.2  he cs428x_query_devinfo(void *addr, mixer_devinfo_t *dip)
    125  1.2.2.2  he {
    126  1.2.2.2  he 	struct cs428x_softc *sc;
    127  1.2.2.2  he 
    128  1.2.2.2  he 	sc = addr;
    129  1.2.2.2  he 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
    130  1.2.2.2  he }
    131  1.2.2.2  he 
    132  1.2.2.2  he void *
    133  1.2.2.2  he cs428x_malloc(void *addr, int direction, size_t size, int pool, int flags)
    134  1.2.2.2  he {
    135  1.2.2.2  he 	struct cs428x_softc *sc;
    136  1.2.2.2  he 	struct cs428x_dma   *p;
    137  1.2.2.2  he 	int error;
    138  1.2.2.2  he 
    139  1.2.2.2  he 	sc = addr;
    140  1.2.2.2  he 
    141  1.2.2.2  he 	p = malloc(sizeof(*p), pool, flags);
    142  1.2.2.2  he 	if (p == NULL)
    143  1.2.2.2  he 		return 0;
    144  1.2.2.2  he 
    145  1.2.2.2  he 	error = cs428x_allocmem(sc, size, pool, flags, p);
    146  1.2.2.2  he 
    147  1.2.2.2  he 	if (error) {
    148  1.2.2.2  he 		free(p, pool);
    149  1.2.2.2  he 		return 0;
    150  1.2.2.2  he 	}
    151  1.2.2.2  he 
    152  1.2.2.2  he 	p->next = sc->sc_dmas;
    153  1.2.2.2  he 	sc->sc_dmas = p;
    154  1.2.2.2  he 	return BUFADDR(p);
    155  1.2.2.2  he }
    156  1.2.2.2  he 
    157  1.2.2.2  he void
    158  1.2.2.2  he cs428x_free(void *addr, void *ptr, int pool)
    159  1.2.2.2  he {
    160  1.2.2.2  he 	struct cs428x_softc *sc;
    161  1.2.2.2  he 	struct cs428x_dma **pp, *p;
    162  1.2.2.2  he 
    163  1.2.2.2  he 	sc = addr;
    164  1.2.2.2  he 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
    165  1.2.2.2  he 		if (BUFADDR(p) == ptr) {
    166  1.2.2.2  he 			bus_dmamap_unload(sc->sc_dmatag, p->map);
    167  1.2.2.2  he 			bus_dmamap_destroy(sc->sc_dmatag, p->map);
    168  1.2.2.2  he 			bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    169  1.2.2.2  he 			bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    170  1.2.2.2  he 			free(p->dum, pool);
    171  1.2.2.2  he 			*pp = p->next;
    172  1.2.2.2  he 			free(p, pool);
    173  1.2.2.2  he 			return;
    174  1.2.2.2  he 		}
    175  1.2.2.2  he 	}
    176  1.2.2.2  he }
    177  1.2.2.2  he 
    178  1.2.2.2  he size_t
    179  1.2.2.2  he cs428x_round_buffersize(void *addr, int direction, size_t size)
    180  1.2.2.2  he {
    181  1.2.2.2  he 	/* The real dma buffersize are 4KB for CS4280
    182  1.2.2.2  he 	 * and 64kB/MAX_CHANNELS for CS4281.
    183  1.2.2.2  he 	 * But they are too small for high quality audio,
    184  1.2.2.2  he 	 * let the upper layer(audio) use a larger buffer.
    185  1.2.2.2  he 	 * (originally suggested by Lennart Augustsson.)
    186  1.2.2.2  he 	 */
    187  1.2.2.2  he 	return size;
    188  1.2.2.2  he }
    189  1.2.2.2  he 
    190  1.2.2.2  he paddr_t
    191  1.2.2.2  he cs428x_mappage(void *addr, void *mem, off_t off, int prot)
    192  1.2.2.2  he {
    193  1.2.2.2  he 	struct cs428x_softc *sc;
    194  1.2.2.2  he 	struct cs428x_dma *p;
    195  1.2.2.2  he 
    196  1.2.2.2  he 	sc = addr;
    197  1.2.2.2  he 
    198  1.2.2.2  he 	if (off < 0)
    199  1.2.2.2  he 		return -1;
    200  1.2.2.2  he 
    201  1.2.2.2  he 	for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next)
    202  1.2.2.2  he 		;
    203  1.2.2.2  he 
    204  1.2.2.2  he 	if (p == NULL) {
    205  1.2.2.2  he 		DPRINTF(("cs428x_mappage: bad buffer address\n"));
    206  1.2.2.2  he 		return -1;
    207  1.2.2.2  he 	}
    208  1.2.2.2  he 
    209  1.2.2.2  he 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
    210  1.2.2.2  he 	    off, prot, BUS_DMA_WAITOK));
    211  1.2.2.2  he }
    212  1.2.2.2  he 
    213  1.2.2.2  he int
    214  1.2.2.2  he cs428x_get_props(void *addr)
    215  1.2.2.2  he {
    216  1.2.2.2  he 	int retval;
    217  1.2.2.2  he 
    218  1.2.2.2  he 	retval = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    219  1.2.2.2  he #ifdef MMAP_READY
    220  1.2.2.2  he 	/* How can I mmap ? */
    221  1.2.2.2  he 	retval |= AUDIO_PROP_MMAP;
    222  1.2.2.2  he #endif
    223  1.2.2.2  he 	return retval;
    224  1.2.2.2  he }
    225  1.2.2.2  he 
    226  1.2.2.2  he /* AC97 */
    227  1.2.2.2  he int
    228  1.2.2.2  he cs428x_attach_codec(void *addr, struct ac97_codec_if *codec_if)
    229  1.2.2.2  he {
    230  1.2.2.2  he 	struct cs428x_softc *sc;
    231  1.2.2.2  he 
    232  1.2.2.2  he 	DPRINTF(("cs428x_attach_codec:\n"));
    233  1.2.2.2  he 	sc = addr;
    234  1.2.2.2  he 	sc->codec_if = codec_if;
    235  1.2.2.2  he 	return 0;
    236  1.2.2.2  he }
    237  1.2.2.2  he 
    238  1.2.2.2  he int
    239  1.2.2.2  he cs428x_read_codec(void *addr, u_int8_t ac97_addr, u_int16_t *ac97_data)
    240  1.2.2.2  he {
    241  1.2.2.2  he 	struct cs428x_softc *sc;
    242  1.2.2.2  he 	u_int32_t acctl;
    243  1.2.2.2  he 	int n;
    244  1.2.2.2  he 
    245  1.2.2.2  he 	sc = addr;
    246  1.2.2.2  he 
    247  1.2.2.2  he 	DPRINTFN(5,("read_codec: add=0x%02x ", ac97_addr));
    248  1.2.2.2  he 	/*
    249  1.2.2.2  he 	 * Make sure that there is not data sitting around from a preivous
    250  1.2.2.2  he 	 * uncompleted access.
    251  1.2.2.2  he 	 */
    252  1.2.2.2  he 	BA0READ4(sc, CS428X_ACSDA);
    253  1.2.2.2  he 
    254  1.2.2.2  he 	/* Set up AC97 control registers. */
    255  1.2.2.2  he 	BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
    256  1.2.2.2  he 	BA0WRITE4(sc, CS428X_ACCDA, 0);
    257  1.2.2.2  he 
    258  1.2.2.2  he 	acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW  | ACCTL_DCV;
    259  1.2.2.2  he 	if (sc->type == TYPE_CS4280)
    260  1.2.2.2  he 		acctl |= ACCTL_RSTN;
    261  1.2.2.2  he 	BA0WRITE4(sc, CS428X_ACCTL, acctl);
    262  1.2.2.2  he 
    263  1.2.2.2  he 	if (cs428x_src_wait(sc) < 0) {
    264  1.2.2.2  he 		printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n",
    265  1.2.2.2  he 		       sc->sc_dev.dv_xname, ac97_addr);
    266  1.2.2.2  he 		return 1;
    267  1.2.2.2  he 	}
    268  1.2.2.2  he 
    269  1.2.2.2  he 	/* wait for valid status bit is active */
    270  1.2.2.2  he 	n = 0;
    271  1.2.2.2  he 	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_VSTS) == 0) {
    272  1.2.2.2  he 		delay(1);
    273  1.2.2.2  he 		while (++n > 1000) {
    274  1.2.2.2  he 			printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n",
    275  1.2.2.2  he 			       sc->sc_dev.dv_xname, ac97_addr);
    276  1.2.2.2  he 			return 1;
    277  1.2.2.2  he 		}
    278  1.2.2.2  he 	}
    279  1.2.2.2  he 	*ac97_data = BA0READ4(sc, CS428X_ACSDA);
    280  1.2.2.2  he 	DPRINTFN(5,("data=0x%04x\n", *ac97_data));
    281  1.2.2.2  he 	return 0;
    282  1.2.2.2  he }
    283  1.2.2.2  he 
    284  1.2.2.2  he int
    285  1.2.2.2  he cs428x_write_codec(void *addr, u_int8_t ac97_addr, u_int16_t ac97_data)
    286  1.2.2.2  he {
    287  1.2.2.2  he 	struct cs428x_softc *sc;
    288  1.2.2.2  he 	u_int32_t acctl;
    289  1.2.2.2  he 
    290  1.2.2.2  he 	sc = addr;
    291  1.2.2.2  he 
    292  1.2.2.2  he 	DPRINTFN(5,("write_codec: add=0x%02x  data=0x%04x\n", ac97_addr, ac97_data));
    293  1.2.2.2  he 	BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
    294  1.2.2.2  he 	BA0WRITE4(sc, CS428X_ACCDA, ac97_data);
    295  1.2.2.2  he 
    296  1.2.2.2  he 	acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV;
    297  1.2.2.2  he 	if (sc->type == TYPE_CS4280)
    298  1.2.2.2  he 		acctl |= ACCTL_RSTN;
    299  1.2.2.2  he 	BA0WRITE4(sc, CS428X_ACCTL, acctl);
    300  1.2.2.2  he 
    301  1.2.2.2  he 	if (cs428x_src_wait(sc) < 0) {
    302  1.2.2.2  he 		printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data="
    303  1.2.2.2  he 		       "0x%04x\n", sc->sc_dev.dv_xname, ac97_addr, ac97_data);
    304  1.2.2.2  he 		return 1;
    305  1.2.2.2  he 	}
    306  1.2.2.2  he 	return 0;
    307  1.2.2.2  he }
    308  1.2.2.2  he 
    309  1.2.2.2  he /* Internal functions */
    310  1.2.2.2  he int
    311  1.2.2.2  he cs428x_allocmem(struct cs428x_softc *sc,
    312  1.2.2.2  he 		size_t size, int pool, int flags,
    313  1.2.2.2  he 		struct cs428x_dma *p)
    314  1.2.2.2  he {
    315  1.2.2.2  he 	int error;
    316  1.2.2.2  he 	size_t align;
    317  1.2.2.2  he 
    318  1.2.2.2  he 	align   = sc->dma_align;
    319  1.2.2.2  he 	p->size = sc->dma_size;
    320  1.2.2.2  he 	/* allocate memory for upper audio driver */
    321  1.2.2.2  he 	p->dum  = malloc(size, pool, flags);
    322  1.2.2.2  he 	if (p->dum == NULL)
    323  1.2.2.2  he 		return 1;
    324  1.2.2.2  he 
    325  1.2.2.2  he 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    326  1.2.2.2  he 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    327  1.2.2.2  he 				 &p->nsegs, BUS_DMA_NOWAIT);
    328  1.2.2.2  he 	if (error) {
    329  1.2.2.2  he 		printf("%s: unable to allocate dma. error=%d\n",
    330  1.2.2.2  he 		       sc->sc_dev.dv_xname, error);
    331  1.2.2.2  he 		goto allfree;
    332  1.2.2.2  he 	}
    333  1.2.2.2  he 
    334  1.2.2.2  he 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    335  1.2.2.2  he 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    336  1.2.2.2  he 	if (error) {
    337  1.2.2.2  he 		printf("%s: unable to map dma, error=%d\n",
    338  1.2.2.2  he 		       sc->sc_dev.dv_xname, error);
    339  1.2.2.2  he 		goto free;
    340  1.2.2.2  he 	}
    341  1.2.2.2  he 
    342  1.2.2.2  he 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    343  1.2.2.2  he 				  0, BUS_DMA_NOWAIT, &p->map);
    344  1.2.2.2  he 	if (error) {
    345  1.2.2.2  he 		printf("%s: unable to create dma map, error=%d\n",
    346  1.2.2.2  he 		       sc->sc_dev.dv_xname, error);
    347  1.2.2.2  he 		goto unmap;
    348  1.2.2.2  he 	}
    349  1.2.2.2  he 
    350  1.2.2.2  he 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    351  1.2.2.2  he 				BUS_DMA_NOWAIT);
    352  1.2.2.2  he 	if (error) {
    353  1.2.2.2  he 		printf("%s: unable to load dma map, error=%d\n",
    354  1.2.2.2  he 		       sc->sc_dev.dv_xname, error);
    355  1.2.2.2  he 		goto destroy;
    356  1.2.2.2  he 	}
    357  1.2.2.2  he 	return 0;
    358  1.2.2.2  he 
    359  1.2.2.2  he  destroy:
    360  1.2.2.2  he 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    361  1.2.2.2  he  unmap:
    362  1.2.2.2  he 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    363  1.2.2.2  he  free:
    364  1.2.2.2  he 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    365  1.2.2.2  he  allfree:
    366  1.2.2.2  he 	free(p->dum, pool);
    367  1.2.2.2  he 
    368  1.2.2.2  he 	return error;
    369  1.2.2.2  he }
    370  1.2.2.2  he 
    371  1.2.2.2  he int
    372  1.2.2.2  he cs428x_src_wait(sc)
    373  1.2.2.2  he 	struct cs428x_softc *sc;
    374  1.2.2.2  he {
    375  1.2.2.2  he 	int n;
    376  1.2.2.2  he 
    377  1.2.2.2  he 	n = 0;
    378  1.2.2.2  he 	while ((BA0READ4(sc, CS428X_ACCTL) & ACCTL_DCV)) {
    379  1.2.2.2  he 		delay(1000);
    380  1.2.2.2  he 		while (++n > 1000) {
    381  1.2.2.2  he 			printf("cs428x_src_wait: 0x%08x\n",
    382  1.2.2.2  he 			    BA0READ4(sc, CS428X_ACCTL));
    383  1.2.2.2  he 			return -1;
    384  1.2.2.2  he 		}
    385  1.2.2.2  he 	}
    386  1.2.2.2  he 	return 0;
    387  1.2.2.2  he }
    388