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