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