Home | History | Annotate | Line # | Download | only in pci
cs428x.c revision 1.3
      1 /*	$NetBSD: cs428x.c,v 1.3 2001/11/13 07:48:41 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 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 /* Common functions for CS4280 and CS4281 */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: cs428x.c,v 1.3 2001/11/13 07:48:41 lukem Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/types.h>
     41 #include <sys/kernel.h>
     42 #include <sys/malloc.h>
     43 #include <sys/device.h>
     44 
     45 #include <dev/pci/pcidevs.h>
     46 #include <dev/pci/pcivar.h>
     47 
     48 #include <sys/audioio.h>
     49 #include <dev/audio_if.h>
     50 #include <dev/midi_if.h>
     51 #include <dev/mulaw.h>
     52 #include <dev/auconv.h>
     53 
     54 #include <dev/ic/ac97reg.h>
     55 #include <dev/ic/ac97var.h>
     56 
     57 #include <machine/bus.h>
     58 
     59 #include <dev/pci/cs428xreg.h>
     60 #include <dev/pci/cs428x.h>
     61 
     62 #if defined(CS4280_DEBUG) || defined(CS4281_DEBUG)
     63 int cs428x_debug = 0;
     64 #endif
     65 
     66 int
     67 cs428x_open(void *addr, int flags)
     68 {
     69 	return 0;
     70 }
     71 
     72 void
     73 cs428x_close(void *addr)
     74 {
     75 	struct cs428x_softc *sc;
     76 
     77 	sc = addr;
     78 
     79 	(*sc->halt_output)(sc);
     80 	(*sc->halt_input)(sc);
     81 
     82 	sc->sc_pintr = 0;
     83 	sc->sc_rintr = 0;
     84 }
     85 
     86 int
     87 cs428x_round_blocksize(void *addr, int blk)
     88 {
     89 	struct cs428x_softc *sc;
     90 	int retval;
     91 
     92 	DPRINTFN(5,("cs428x_round_blocksize blk=%d -> ", blk));
     93 
     94 	sc = addr;
     95 	if (blk < sc->hw_blocksize)
     96 		retval = sc->hw_blocksize;
     97 	else
     98 		retval = blk & -(sc->hw_blocksize);
     99 
    100 	DPRINTFN(5,("%d\n", retval));
    101 
    102 	return retval;
    103 }
    104 
    105 int
    106 cs428x_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    107 {
    108 	struct cs428x_softc *sc;
    109 	int val;
    110 
    111 	sc = addr;
    112 	val = sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    113 	DPRINTFN(3,("mixer_set_port: val=%d\n", val));
    114 	return (val);
    115 }
    116 
    117 int
    118 cs428x_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    119 {
    120 	struct cs428x_softc *sc;
    121 
    122 	sc = addr;
    123 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
    124 }
    125 
    126 int
    127 cs428x_query_devinfo(void *addr, mixer_devinfo_t *dip)
    128 {
    129 	struct cs428x_softc *sc;
    130 
    131 	sc = addr;
    132 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
    133 }
    134 
    135 void *
    136 cs428x_malloc(void *addr, int direction, size_t size, int pool, int flags)
    137 {
    138 	struct cs428x_softc *sc;
    139 	struct cs428x_dma   *p;
    140 	int error;
    141 
    142 	sc = addr;
    143 
    144 	p = malloc(sizeof(*p), pool, flags);
    145 	if (p == NULL)
    146 		return 0;
    147 
    148 	error = cs428x_allocmem(sc, size, pool, flags, p);
    149 
    150 	if (error) {
    151 		free(p, pool);
    152 		return 0;
    153 	}
    154 
    155 	p->next = sc->sc_dmas;
    156 	sc->sc_dmas = p;
    157 	return BUFADDR(p);
    158 }
    159 
    160 void
    161 cs428x_free(void *addr, void *ptr, int pool)
    162 {
    163 	struct cs428x_softc *sc;
    164 	struct cs428x_dma **pp, *p;
    165 
    166 	sc = addr;
    167 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
    168 		if (BUFADDR(p) == ptr) {
    169 			bus_dmamap_unload(sc->sc_dmatag, p->map);
    170 			bus_dmamap_destroy(sc->sc_dmatag, p->map);
    171 			bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    172 			bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    173 			free(p->dum, pool);
    174 			*pp = p->next;
    175 			free(p, pool);
    176 			return;
    177 		}
    178 	}
    179 }
    180 
    181 size_t
    182 cs428x_round_buffersize(void *addr, int direction, size_t size)
    183 {
    184 	/* The real dma buffersize are 4KB for CS4280
    185 	 * and 64kB/MAX_CHANNELS for CS4281.
    186 	 * But they are too small for high quality audio,
    187 	 * let the upper layer(audio) use a larger buffer.
    188 	 * (originally suggested by Lennart Augustsson.)
    189 	 */
    190 	return size;
    191 }
    192 
    193 paddr_t
    194 cs428x_mappage(void *addr, void *mem, off_t off, int prot)
    195 {
    196 	struct cs428x_softc *sc;
    197 	struct cs428x_dma *p;
    198 
    199 	sc = addr;
    200 
    201 	if (off < 0)
    202 		return -1;
    203 
    204 	for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next)
    205 		;
    206 
    207 	if (p == NULL) {
    208 		DPRINTF(("cs428x_mappage: bad buffer address\n"));
    209 		return -1;
    210 	}
    211 
    212 	return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
    213 	    off, prot, BUS_DMA_WAITOK));
    214 }
    215 
    216 int
    217 cs428x_get_props(void *addr)
    218 {
    219 	int retval;
    220 
    221 	retval = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    222 #ifdef MMAP_READY
    223 	/* How can I mmap ? */
    224 	retval |= AUDIO_PROP_MMAP;
    225 #endif
    226 	return retval;
    227 }
    228 
    229 /* AC97 */
    230 int
    231 cs428x_attach_codec(void *addr, struct ac97_codec_if *codec_if)
    232 {
    233 	struct cs428x_softc *sc;
    234 
    235 	DPRINTF(("cs428x_attach_codec:\n"));
    236 	sc = addr;
    237 	sc->codec_if = codec_if;
    238 	return 0;
    239 }
    240 
    241 int
    242 cs428x_read_codec(void *addr, u_int8_t ac97_addr, u_int16_t *ac97_data)
    243 {
    244 	struct cs428x_softc *sc;
    245 	u_int32_t acctl;
    246 	int n;
    247 
    248 	sc = addr;
    249 
    250 	DPRINTFN(5,("read_codec: add=0x%02x ", ac97_addr));
    251 	/*
    252 	 * Make sure that there is not data sitting around from a preivous
    253 	 * uncompleted access.
    254 	 */
    255 	BA0READ4(sc, CS428X_ACSDA);
    256 
    257 	/* Set up AC97 control registers. */
    258 	BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
    259 	BA0WRITE4(sc, CS428X_ACCDA, 0);
    260 
    261 	acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW  | ACCTL_DCV;
    262 	if (sc->type == TYPE_CS4280)
    263 		acctl |= ACCTL_RSTN;
    264 	BA0WRITE4(sc, CS428X_ACCTL, acctl);
    265 
    266 	if (cs428x_src_wait(sc) < 0) {
    267 		printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n",
    268 		       sc->sc_dev.dv_xname, ac97_addr);
    269 		return 1;
    270 	}
    271 
    272 	/* wait for valid status bit is active */
    273 	n = 0;
    274 	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_VSTS) == 0) {
    275 		delay(1);
    276 		while (++n > 1000) {
    277 			printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n",
    278 			       sc->sc_dev.dv_xname, ac97_addr);
    279 			return 1;
    280 		}
    281 	}
    282 	*ac97_data = BA0READ4(sc, CS428X_ACSDA);
    283 	DPRINTFN(5,("data=0x%04x\n", *ac97_data));
    284 	return 0;
    285 }
    286 
    287 int
    288 cs428x_write_codec(void *addr, u_int8_t ac97_addr, u_int16_t ac97_data)
    289 {
    290 	struct cs428x_softc *sc;
    291 	u_int32_t acctl;
    292 
    293 	sc = addr;
    294 
    295 	DPRINTFN(5,("write_codec: add=0x%02x  data=0x%04x\n", ac97_addr, ac97_data));
    296 	BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
    297 	BA0WRITE4(sc, CS428X_ACCDA, ac97_data);
    298 
    299 	acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV;
    300 	if (sc->type == TYPE_CS4280)
    301 		acctl |= ACCTL_RSTN;
    302 	BA0WRITE4(sc, CS428X_ACCTL, acctl);
    303 
    304 	if (cs428x_src_wait(sc) < 0) {
    305 		printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data="
    306 		       "0x%04x\n", sc->sc_dev.dv_xname, ac97_addr, ac97_data);
    307 		return 1;
    308 	}
    309 	return 0;
    310 }
    311 
    312 /* Internal functions */
    313 int
    314 cs428x_allocmem(struct cs428x_softc *sc,
    315 		size_t size, int pool, int flags,
    316 		struct cs428x_dma *p)
    317 {
    318 	int error;
    319 	size_t align;
    320 
    321 	align   = sc->dma_align;
    322 	p->size = sc->dma_size;
    323 	/* allocate memory for upper audio driver */
    324 	p->dum  = malloc(size, pool, flags);
    325 	if (p->dum == NULL)
    326 		return 1;
    327 
    328 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    329 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    330 				 &p->nsegs, BUS_DMA_NOWAIT);
    331 	if (error) {
    332 		printf("%s: unable to allocate dma. error=%d\n",
    333 		       sc->sc_dev.dv_xname, error);
    334 		goto allfree;
    335 	}
    336 
    337 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    338 			       &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    339 	if (error) {
    340 		printf("%s: unable to map dma, error=%d\n",
    341 		       sc->sc_dev.dv_xname, error);
    342 		goto free;
    343 	}
    344 
    345 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    346 				  0, BUS_DMA_NOWAIT, &p->map);
    347 	if (error) {
    348 		printf("%s: unable to create dma map, error=%d\n",
    349 		       sc->sc_dev.dv_xname, error);
    350 		goto unmap;
    351 	}
    352 
    353 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    354 				BUS_DMA_NOWAIT);
    355 	if (error) {
    356 		printf("%s: unable to load dma map, error=%d\n",
    357 		       sc->sc_dev.dv_xname, error);
    358 		goto destroy;
    359 	}
    360 	return 0;
    361 
    362  destroy:
    363 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    364  unmap:
    365 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    366  free:
    367 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    368  allfree:
    369 	free(p->dum, pool);
    370 
    371 	return error;
    372 }
    373 
    374 int
    375 cs428x_src_wait(sc)
    376 	struct cs428x_softc *sc;
    377 {
    378 	int n;
    379 
    380 	n = 0;
    381 	while ((BA0READ4(sc, CS428X_ACCTL) & ACCTL_DCV)) {
    382 		delay(1000);
    383 		while (++n > 1000) {
    384 			printf("cs428x_src_wait: 0x%08x\n",
    385 			    BA0READ4(sc, CS428X_ACCTL));
    386 			return -1;
    387 		}
    388 	}
    389 	return 0;
    390 }
    391