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