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