1 /* $NetBSD: cs428x.c,v 1.15 2008/04/10 19:13:36 cegger 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.15 2008/04/10 19:13:36 cegger 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 <sys/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, 164 size_t size) 165 { 166 /* The real DMA buffersize are 4KB for CS4280 167 * and 64kB/MAX_CHANNELS for CS4281. 168 * But they are too small for high quality audio, 169 * let the upper layer(audio) use a larger buffer. 170 * (originally suggested by Lennart Augustsson.) 171 */ 172 return size; 173 } 174 175 paddr_t 176 cs428x_mappage(void *addr, void *mem, off_t off, int prot) 177 { 178 struct cs428x_softc *sc; 179 struct cs428x_dma *p; 180 181 sc = addr; 182 183 if (off < 0) 184 return -1; 185 186 for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next) 187 continue; 188 189 if (p == NULL) { 190 DPRINTF(("cs428x_mappage: bad buffer address\n")); 191 return -1; 192 } 193 194 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs, 195 off, prot, BUS_DMA_WAITOK)); 196 } 197 198 int 199 cs428x_get_props(void *addr) 200 { 201 int retval; 202 203 retval = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX; 204 #ifdef MMAP_READY 205 /* How can I mmap ? */ 206 retval |= AUDIO_PROP_MMAP; 207 #endif 208 return retval; 209 } 210 211 /* AC97 */ 212 int 213 cs428x_attach_codec(void *addr, struct ac97_codec_if *codec_if) 214 { 215 struct cs428x_softc *sc; 216 217 DPRINTF(("cs428x_attach_codec:\n")); 218 sc = addr; 219 sc->codec_if = codec_if; 220 return 0; 221 } 222 223 int 224 cs428x_read_codec(void *addr, uint8_t ac97_addr, uint16_t *ac97_data) 225 { 226 struct cs428x_softc *sc; 227 uint32_t acctl; 228 int n; 229 230 sc = addr; 231 232 DPRINTFN(5,("read_codec: add=0x%02x ", ac97_addr)); 233 /* 234 * Make sure that there is not data sitting around from a previous 235 * uncompleted access. 236 */ 237 BA0READ4(sc, CS428X_ACSDA); 238 239 /* Set up AC97 control registers. */ 240 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr); 241 BA0WRITE4(sc, CS428X_ACCDA, 0); 242 243 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW | ACCTL_DCV; 244 if (sc->type == TYPE_CS4280) 245 acctl |= ACCTL_RSTN; 246 BA0WRITE4(sc, CS428X_ACCTL, acctl); 247 248 if (cs428x_src_wait(sc) < 0) { 249 printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n", 250 device_xname(&sc->sc_dev), ac97_addr); 251 return 1; 252 } 253 254 /* wait for valid status bit is active */ 255 n = 0; 256 while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_VSTS) == 0) { 257 delay(1); 258 while (++n > 1000) { 259 printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n", 260 device_xname(&sc->sc_dev), ac97_addr); 261 return 1; 262 } 263 } 264 *ac97_data = BA0READ4(sc, CS428X_ACSDA); 265 DPRINTFN(5,("data=0x%04x\n", *ac97_data)); 266 return 0; 267 } 268 269 int 270 cs428x_write_codec(void *addr, uint8_t ac97_addr, uint16_t ac97_data) 271 { 272 struct cs428x_softc *sc; 273 uint32_t acctl; 274 275 sc = addr; 276 277 DPRINTFN(5,("write_codec: add=0x%02x data=0x%04x\n", ac97_addr, ac97_data)); 278 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr); 279 BA0WRITE4(sc, CS428X_ACCDA, ac97_data); 280 281 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV; 282 if (sc->type == TYPE_CS4280) 283 acctl |= ACCTL_RSTN; 284 BA0WRITE4(sc, CS428X_ACCTL, acctl); 285 286 if (cs428x_src_wait(sc) < 0) { 287 printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data=" 288 "0x%04x\n", device_xname(&sc->sc_dev), ac97_addr, ac97_data); 289 return 1; 290 } 291 return 0; 292 } 293 294 /* Internal functions */ 295 int 296 cs428x_allocmem(struct cs428x_softc *sc, 297 size_t size, struct malloc_type *pool, int flags, 298 struct cs428x_dma *p) 299 { 300 int error; 301 size_t align; 302 303 align = sc->dma_align; 304 p->size = sc->dma_size; 305 /* allocate memory for upper audio driver */ 306 p->dum = malloc(size, pool, flags); 307 if (p->dum == NULL) 308 return 1; 309 310 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0, 311 p->segs, sizeof(p->segs)/sizeof(p->segs[0]), 312 &p->nsegs, BUS_DMA_NOWAIT); 313 if (error) { 314 aprint_error_dev(&sc->sc_dev, "unable to allocate DMA. error=%d\n", 315 error); 316 goto allfree; 317 } 318 319 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size, 320 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 321 if (error) { 322 aprint_error_dev(&sc->sc_dev, "unable to map DMA, error=%d\n", 323 error); 324 goto free; 325 } 326 327 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size, 328 0, BUS_DMA_NOWAIT, &p->map); 329 if (error) { 330 aprint_error_dev(&sc->sc_dev, "unable to create DMA map, error=%d\n", 331 error); 332 goto unmap; 333 } 334 335 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL, 336 BUS_DMA_NOWAIT); 337 if (error) { 338 aprint_error_dev(&sc->sc_dev, "unable to load DMA map, error=%d\n", 339 error); 340 goto destroy; 341 } 342 return 0; 343 344 destroy: 345 bus_dmamap_destroy(sc->sc_dmatag, p->map); 346 unmap: 347 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size); 348 free: 349 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs); 350 allfree: 351 free(p->dum, pool); 352 353 return error; 354 } 355 356 int 357 cs428x_src_wait(struct cs428x_softc *sc) 358 { 359 int n; 360 361 n = 0; 362 while ((BA0READ4(sc, CS428X_ACCTL) & ACCTL_DCV)) { 363 delay(1000); 364 while (++n > 1000) { 365 printf("cs428x_src_wait: 0x%08x\n", 366 BA0READ4(sc, CS428X_ACCTL)); 367 return -1; 368 } 369 } 370 return 0; 371 } 372