| 1 | /* $NetBSD: neo.c,v 1.50 2014/03/29 19:28:25 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Derived from the public domain Linux driver |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | * |
| 30 | * FreeBSD: src/sys/dev/sound/pci/neomagic.c,v 1.8 2000/03/20 15:30:50 cg Exp |
| 31 | * OpenBSD: neo.c,v 1.4 2000/07/19 09:04:37 csapuntz Exp |
| 32 | */ |
| 33 | |
| 34 | #include <sys/cdefs.h> |
| 35 | __KERNEL_RCSID(0, "$NetBSD: neo.c,v 1.50 2014/03/29 19:28:25 christos Exp $" ); |
| 36 | |
| 37 | #include <sys/param.h> |
| 38 | #include <sys/systm.h> |
| 39 | #include <sys/kernel.h> |
| 40 | #include <sys/kmem.h> |
| 41 | #include <sys/device.h> |
| 42 | #include <sys/bus.h> |
| 43 | #include <sys/audioio.h> |
| 44 | |
| 45 | #include <dev/audio_if.h> |
| 46 | #include <dev/mulaw.h> |
| 47 | #include <dev/auconv.h> |
| 48 | |
| 49 | #include <dev/ic/ac97var.h> |
| 50 | |
| 51 | #include <dev/pci/pcidevs.h> |
| 52 | #include <dev/pci/pcivar.h> |
| 53 | #include <dev/pci/neoreg.h> |
| 54 | #include <dev/pci/neo-coeff.h> |
| 55 | |
| 56 | /* -------------------------------------------------------------------- */ |
| 57 | /* |
| 58 | * As of 04/13/00, public documentation on the Neomagic 256 is not available. |
| 59 | * These comments were gleaned by looking at the driver carefully. |
| 60 | * |
| 61 | * The Neomagic 256 AV/ZX chips provide both video and audio capabilities |
| 62 | * on one chip. About 2-6 megabytes of memory are associated with |
| 63 | * the chip. Most of this goes to video frame buffers, but some is used for |
| 64 | * audio buffering |
| 65 | * |
| 66 | * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA. |
| 67 | * Instead, the chip allows you to carve out two ring buffers out of its |
| 68 | * memory. However you carve this and how much you can carve seems to be |
| 69 | * voodoo. The algorithm is in nm_init. |
| 70 | * |
| 71 | * Most Neomagic audio chips use the AC-97 codec interface. However, there |
| 72 | * seem to be a select few chips 256AV chips that do not support AC-97. |
| 73 | * This driver does not support them but there are rumors that it |
| 74 | * might work with wss isa drivers. This might require some playing around |
| 75 | * with your BIOS. |
| 76 | * |
| 77 | * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of |
| 78 | * them describe a memory region. The frame buffer is the first region |
| 79 | * and the register set is the second region. |
| 80 | * |
| 81 | * The register manipulation logic is taken from the Linux driver, |
| 82 | * which is in the public domain. |
| 83 | * |
| 84 | * The Neomagic is even nice enough to map the AC-97 codec registers into |
| 85 | * the register space to allow direct manipulation. Watch out, accessing |
| 86 | * AC-97 registers on the Neomagic requires great delicateness, otherwise |
| 87 | * the thing will hang the PCI bus, rendering your system frozen. |
| 88 | * |
| 89 | * For one, it seems the Neomagic status register that reports AC-97 |
| 90 | * readiness should NOT be polled more often than once each 1ms. |
| 91 | * |
| 92 | * Also, writes to the AC-97 register space may take order 40us to |
| 93 | * complete. |
| 94 | * |
| 95 | * Unlike many sound engines, the Neomagic does not support (as far as |
| 96 | * we know :) the notion of interrupting every n bytes transferred, |
| 97 | * unlike many DMA engines. Instead, it allows you to specify one |
| 98 | * location in each ring buffer (called the watermark). When the chip |
| 99 | * passes that location while playing, it signals an interrupt. |
| 100 | * |
| 101 | * The ring buffer size is currently 16k. That is about 100ms of audio |
| 102 | * at 44.1kHz/stero/16 bit. However, to keep the buffer full, interrupts |
| 103 | * are generated more often than that, so 20-40 interrupts per second |
| 104 | * should not be unexpected. Increasing BUFFSIZE should help minimize |
| 105 | * of glitches due to drivers that spend to much time looping at high |
| 106 | * privelege levels as well as the impact of badly written audio |
| 107 | * interface clients. |
| 108 | * |
| 109 | * TO-DO list: |
| 110 | * Figure out interaction with video stuff (look at Xfree86 driver?) |
| 111 | * |
| 112 | * Figure out how to shrink that huge table neo-coeff.h |
| 113 | */ |
| 114 | |
| 115 | #define NM_BUFFSIZE 16384 |
| 116 | |
| 117 | /* device private data */ |
| 118 | struct neo_softc { |
| 119 | device_t dev; |
| 120 | kmutex_t lock; |
| 121 | kmutex_t intr_lock; |
| 122 | |
| 123 | bus_space_tag_t bufiot; |
| 124 | bus_space_handle_t bufioh; |
| 125 | |
| 126 | bus_space_tag_t regiot; |
| 127 | bus_space_handle_t regioh; |
| 128 | |
| 129 | uint32_t type; |
| 130 | void *ih; |
| 131 | |
| 132 | void (*pintr)(void *); /* DMA completion intr handler */ |
| 133 | void *parg; /* arg for intr() */ |
| 134 | |
| 135 | void (*rintr)(void *); /* DMA completion intr handler */ |
| 136 | void *rarg; /* arg for intr() */ |
| 137 | |
| 138 | vaddr_t buf_vaddr; |
| 139 | vaddr_t rbuf_vaddr; |
| 140 | vaddr_t pbuf_vaddr; |
| 141 | int pbuf_allocated; |
| 142 | int rbuf_allocated; |
| 143 | |
| 144 | bus_addr_t buf_pciaddr; |
| 145 | bus_addr_t rbuf_pciaddr; |
| 146 | bus_addr_t pbuf_pciaddr; |
| 147 | |
| 148 | uint32_t ac97_base, ac97_status, ac97_busy; |
| 149 | uint32_t buftop, pbuf, rbuf, cbuf, acbuf; |
| 150 | uint32_t playint, recint, misc1int, misc2int; |
| 151 | uint32_t irsz, badintr; |
| 152 | |
| 153 | uint32_t pbufsize; |
| 154 | uint32_t rbufsize; |
| 155 | |
| 156 | uint32_t pblksize; |
| 157 | uint32_t rblksize; |
| 158 | |
| 159 | uint32_t pwmark; |
| 160 | uint32_t rwmark; |
| 161 | |
| 162 | struct ac97_codec_if *codec_if; |
| 163 | struct ac97_host_if host_if; |
| 164 | }; |
| 165 | |
| 166 | /* -------------------------------------------------------------------- */ |
| 167 | |
| 168 | /* |
| 169 | * prototypes |
| 170 | */ |
| 171 | |
| 172 | static int nm_waitcd(struct neo_softc *); |
| 173 | static int nm_loadcoeff(struct neo_softc *, int, int); |
| 174 | static int nm_init(struct neo_softc *); |
| 175 | |
| 176 | static int neo_match(device_t, cfdata_t, void *); |
| 177 | static void neo_attach(device_t, device_t, void *); |
| 178 | static int neo_intr(void *); |
| 179 | |
| 180 | static int neo_query_encoding(void *, struct audio_encoding *); |
| 181 | static int neo_set_params(void *, int, int, audio_params_t *, |
| 182 | audio_params_t *, stream_filter_list_t *, |
| 183 | stream_filter_list_t *); |
| 184 | static int neo_round_blocksize(void *, int, int, const audio_params_t *); |
| 185 | static int neo_trigger_output(void *, void *, void *, int, |
| 186 | void (*)(void *), void *, |
| 187 | const audio_params_t *); |
| 188 | static int neo_trigger_input(void *, void *, void *, int, |
| 189 | void (*)(void *), void *, |
| 190 | const audio_params_t *); |
| 191 | static int neo_halt_output(void *); |
| 192 | static int neo_halt_input(void *); |
| 193 | static int neo_getdev(void *, struct audio_device *); |
| 194 | static int neo_mixer_set_port(void *, mixer_ctrl_t *); |
| 195 | static int neo_mixer_get_port(void *, mixer_ctrl_t *); |
| 196 | static int neo_attach_codec(void *, struct ac97_codec_if *); |
| 197 | static int neo_read_codec(void *, uint8_t, uint16_t *); |
| 198 | static int neo_write_codec(void *, uint8_t, uint16_t); |
| 199 | static int neo_reset_codec(void *); |
| 200 | static enum ac97_host_flags neo_flags_codec(void *); |
| 201 | static int neo_query_devinfo(void *, mixer_devinfo_t *); |
| 202 | static void * neo_malloc(void *, int, size_t); |
| 203 | static void neo_free(void *, void *, size_t); |
| 204 | static size_t neo_round_buffersize(void *, int, size_t); |
| 205 | static paddr_t neo_mappage(void *, void *, off_t, int); |
| 206 | static int neo_get_props(void *); |
| 207 | static void neo_get_locks(void *, kmutex_t **, kmutex_t **); |
| 208 | |
| 209 | CFATTACH_DECL_NEW(neo, sizeof(struct neo_softc), |
| 210 | neo_match, neo_attach, NULL, NULL); |
| 211 | |
| 212 | static struct audio_device neo_device = { |
| 213 | "NeoMagic 256" , |
| 214 | "" , |
| 215 | "neo" |
| 216 | }; |
| 217 | |
| 218 | /* The actual rates supported by the card. */ |
| 219 | static const int samplerates[9] = { |
| 220 | 8000, |
| 221 | 11025, |
| 222 | 16000, |
| 223 | 22050, |
| 224 | 24000, |
| 225 | 32000, |
| 226 | 44100, |
| 227 | 48000, |
| 228 | 99999999 |
| 229 | }; |
| 230 | |
| 231 | #define NEO_NFORMATS 4 |
| 232 | static const struct audio_format neo_formats[NEO_NFORMATS] = { |
| 233 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, |
| 234 | 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}}, |
| 235 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, |
| 236 | 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}}, |
| 237 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8, |
| 238 | 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}}, |
| 239 | {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8, |
| 240 | 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}}, |
| 241 | }; |
| 242 | |
| 243 | /* -------------------------------------------------------------------- */ |
| 244 | |
| 245 | static const struct audio_hw_if neo_hw_if = { |
| 246 | NULL, /* open */ |
| 247 | NULL, /* close */ |
| 248 | NULL, /* drain */ |
| 249 | neo_query_encoding, |
| 250 | neo_set_params, |
| 251 | neo_round_blocksize, |
| 252 | NULL, /* commit_setting */ |
| 253 | NULL, /* init_output */ |
| 254 | NULL, /* init_input */ |
| 255 | NULL, /* start_output */ |
| 256 | NULL, /* start_input */ |
| 257 | neo_halt_output, |
| 258 | neo_halt_input, |
| 259 | NULL, /* speaker_ctl */ |
| 260 | neo_getdev, |
| 261 | NULL, /* getfd */ |
| 262 | neo_mixer_set_port, |
| 263 | neo_mixer_get_port, |
| 264 | neo_query_devinfo, |
| 265 | neo_malloc, |
| 266 | neo_free, |
| 267 | neo_round_buffersize, |
| 268 | neo_mappage, |
| 269 | neo_get_props, |
| 270 | neo_trigger_output, |
| 271 | neo_trigger_input, |
| 272 | NULL, |
| 273 | neo_get_locks, |
| 274 | }; |
| 275 | |
| 276 | /* -------------------------------------------------------------------- */ |
| 277 | |
| 278 | #define nm_rd_1(sc, regno) \ |
| 279 | bus_space_read_1((sc)->regiot, (sc)->regioh, (regno)) |
| 280 | |
| 281 | #define nm_rd_2(sc, regno) \ |
| 282 | bus_space_read_2((sc)->regiot, (sc)->regioh, (regno)) |
| 283 | |
| 284 | #define nm_rd_4(sc, regno) \ |
| 285 | bus_space_read_4((sc)->regiot, (sc)->regioh, (regno)) |
| 286 | |
| 287 | #define nm_wr_1(sc, regno, val) \ |
| 288 | bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val)) |
| 289 | |
| 290 | #define nm_wr_2(sc, regno, val) \ |
| 291 | bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val)) |
| 292 | |
| 293 | #define nm_wr_4(sc, regno, val) \ |
| 294 | bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val)) |
| 295 | |
| 296 | #define nm_rdbuf_4(sc, regno) \ |
| 297 | bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno)) |
| 298 | |
| 299 | #define nm_wrbuf_1(sc, regno, val) \ |
| 300 | bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val)) |
| 301 | |
| 302 | /* ac97 codec */ |
| 303 | static int |
| 304 | nm_waitcd(struct neo_softc *sc) |
| 305 | { |
| 306 | int cnt; |
| 307 | int fail; |
| 308 | |
| 309 | cnt = 10; |
| 310 | fail = 1; |
| 311 | while (cnt-- > 0) { |
| 312 | if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy) |
| 313 | DELAY(100); |
| 314 | else { |
| 315 | fail = 0; |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | return fail; |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | nm_ackint(struct neo_softc *sc, uint32_t num) |
| 324 | { |
| 325 | |
| 326 | switch (sc->type) { |
| 327 | case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: |
| 328 | nm_wr_2(sc, NM_INT_REG, num << 1); |
| 329 | break; |
| 330 | |
| 331 | case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: |
| 332 | nm_wr_4(sc, NM_INT_REG, num); |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static int |
| 338 | nm_loadcoeff(struct neo_softc *sc, int dir, int num) |
| 339 | { |
| 340 | int ofs, sz, i; |
| 341 | uint32_t addr; |
| 342 | |
| 343 | addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c; |
| 344 | if (dir == AUMODE_RECORD) |
| 345 | num += 8; |
| 346 | sz = coefficientSizes[num]; |
| 347 | ofs = 0; |
| 348 | while (num-- > 0) |
| 349 | ofs+= coefficientSizes[num]; |
| 350 | for (i = 0; i < sz; i++) |
| 351 | nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]); |
| 352 | nm_wr_4(sc, addr, sc->cbuf); |
| 353 | if (dir == AUMODE_PLAY) |
| 354 | sz--; |
| 355 | nm_wr_4(sc, addr + 4, sc->cbuf + sz); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* The interrupt handler */ |
| 360 | static int |
| 361 | neo_intr(void *p) |
| 362 | { |
| 363 | struct neo_softc *sc; |
| 364 | int status, x; |
| 365 | int rv; |
| 366 | |
| 367 | sc = (struct neo_softc *)p; |
| 368 | mutex_spin_enter(&sc->intr_lock); |
| 369 | |
| 370 | rv = 0; |
| 371 | status = (sc->irsz == 2) ? |
| 372 | nm_rd_2(sc, NM_INT_REG) : |
| 373 | nm_rd_4(sc, NM_INT_REG); |
| 374 | |
| 375 | if (status & sc->playint) { |
| 376 | status &= ~sc->playint; |
| 377 | |
| 378 | sc->pwmark += sc->pblksize; |
| 379 | sc->pwmark %= sc->pbufsize; |
| 380 | |
| 381 | nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark); |
| 382 | |
| 383 | nm_ackint(sc, sc->playint); |
| 384 | |
| 385 | if (sc->pintr) |
| 386 | (*sc->pintr)(sc->parg); |
| 387 | |
| 388 | rv = 1; |
| 389 | } |
| 390 | if (status & sc->recint) { |
| 391 | status &= ~sc->recint; |
| 392 | |
| 393 | sc->rwmark += sc->rblksize; |
| 394 | sc->rwmark %= sc->rbufsize; |
| 395 | nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark); |
| 396 | nm_ackint(sc, sc->recint); |
| 397 | if (sc->rintr) |
| 398 | (*sc->rintr)(sc->rarg); |
| 399 | |
| 400 | rv = 1; |
| 401 | } |
| 402 | if (status & sc->misc1int) { |
| 403 | status &= ~sc->misc1int; |
| 404 | nm_ackint(sc, sc->misc1int); |
| 405 | x = nm_rd_1(sc, 0x400); |
| 406 | nm_wr_1(sc, 0x400, x | 2); |
| 407 | printf("%s: misc int 1\n" , device_xname(sc->dev)); |
| 408 | rv = 1; |
| 409 | } |
| 410 | if (status & sc->misc2int) { |
| 411 | status &= ~sc->misc2int; |
| 412 | nm_ackint(sc, sc->misc2int); |
| 413 | x = nm_rd_1(sc, 0x400); |
| 414 | nm_wr_1(sc, 0x400, x & ~2); |
| 415 | printf("%s: misc int 2\n" , device_xname(sc->dev)); |
| 416 | rv = 1; |
| 417 | } |
| 418 | if (status) { |
| 419 | status &= ~sc->misc2int; |
| 420 | nm_ackint(sc, sc->misc2int); |
| 421 | printf("%s: unknown int\n" , device_xname(sc->dev)); |
| 422 | rv = 1; |
| 423 | } |
| 424 | |
| 425 | mutex_spin_exit(&sc->intr_lock); |
| 426 | return rv; |
| 427 | } |
| 428 | |
| 429 | /* -------------------------------------------------------------------- */ |
| 430 | |
| 431 | /* |
| 432 | * Probe and attach the card |
| 433 | */ |
| 434 | |
| 435 | static int |
| 436 | nm_init(struct neo_softc *sc) |
| 437 | { |
| 438 | uint32_t ofs, i; |
| 439 | |
| 440 | switch (sc->type) { |
| 441 | case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: |
| 442 | sc->ac97_base = NM_MIXER_OFFSET; |
| 443 | sc->ac97_status = NM_MIXER_STATUS_OFFSET; |
| 444 | sc->ac97_busy = NM_MIXER_READY_MASK; |
| 445 | |
| 446 | sc->buftop = 2560 * 1024; |
| 447 | |
| 448 | sc->irsz = 2; |
| 449 | sc->playint = NM_PLAYBACK_INT; |
| 450 | sc->recint = NM_RECORD_INT; |
| 451 | sc->misc1int = NM_MISC_INT_1; |
| 452 | sc->misc2int = NM_MISC_INT_2; |
| 453 | break; |
| 454 | |
| 455 | case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: |
| 456 | sc->ac97_base = NM_MIXER_OFFSET; |
| 457 | sc->ac97_status = NM2_MIXER_STATUS_OFFSET; |
| 458 | sc->ac97_busy = NM2_MIXER_READY_MASK; |
| 459 | |
| 460 | sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024; |
| 461 | |
| 462 | sc->irsz = 4; |
| 463 | sc->playint = NM2_PLAYBACK_INT; |
| 464 | sc->recint = NM2_RECORD_INT; |
| 465 | sc->misc1int = NM2_MISC_INT_1; |
| 466 | sc->misc2int = NM2_MISC_INT_2; |
| 467 | break; |
| 468 | #ifdef DIAGNOSTIC |
| 469 | default: |
| 470 | panic("nm_init: impossible" ); |
| 471 | #endif |
| 472 | } |
| 473 | |
| 474 | sc->badintr = 0; |
| 475 | ofs = sc->buftop - 0x0400; |
| 476 | sc->buftop -= 0x1400; |
| 477 | |
| 478 | if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) { |
| 479 | i = nm_rdbuf_4(sc, ofs + 4); |
| 480 | if (i != 0 && i != 0xffffffff) |
| 481 | sc->buftop = i; |
| 482 | } |
| 483 | |
| 484 | sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT; |
| 485 | sc->rbuf = sc->cbuf - NM_BUFFSIZE; |
| 486 | sc->pbuf = sc->rbuf - NM_BUFFSIZE; |
| 487 | sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4); |
| 488 | |
| 489 | sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh); |
| 490 | sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf; |
| 491 | sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf; |
| 492 | |
| 493 | sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf; |
| 494 | sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf; |
| 495 | |
| 496 | nm_wr_1(sc, 0, 0x11); |
| 497 | nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0); |
| 498 | nm_wr_2(sc, 0x214, 0); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int |
| 504 | neo_match(device_t parent, cfdata_t match, void *aux) |
| 505 | { |
| 506 | struct pci_attach_args *pa; |
| 507 | pcireg_t subdev; |
| 508 | |
| 509 | pa = aux; |
| 510 | if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC) |
| 511 | return 0; |
| 512 | |
| 513 | subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); |
| 514 | |
| 515 | switch (PCI_PRODUCT(pa->pa_id)) { |
| 516 | case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU: |
| 517 | /* |
| 518 | * We have to weed-out the non-AC'97 versions of |
| 519 | * the chip (i.e. the ones that are known to work |
| 520 | * in WSS emulation mode), as they won't work with |
| 521 | * this driver. |
| 522 | */ |
| 523 | switch (PCI_VENDOR(subdev)) { |
| 524 | case PCI_VENDOR_DELL: |
| 525 | switch (PCI_PRODUCT(subdev)) { |
| 526 | case 0x008f: |
| 527 | return 0; |
| 528 | } |
| 529 | break; |
| 530 | |
| 531 | case PCI_VENDOR_HP: |
| 532 | switch (PCI_PRODUCT(subdev)) { |
| 533 | case 0x0007: |
| 534 | return 0; |
| 535 | } |
| 536 | break; |
| 537 | |
| 538 | case PCI_VENDOR_IBM: |
| 539 | switch (PCI_PRODUCT(subdev)) { |
| 540 | case 0x00dd: |
| 541 | return 0; |
| 542 | } |
| 543 | break; |
| 544 | } |
| 545 | return 1; |
| 546 | |
| 547 | case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU: |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static bool |
| 555 | neo_resume(device_t dv, const pmf_qual_t *qual) |
| 556 | { |
| 557 | struct neo_softc *sc = device_private(dv); |
| 558 | |
| 559 | mutex_enter(&sc->lock); |
| 560 | mutex_spin_enter(&sc->intr_lock); |
| 561 | nm_init(sc); |
| 562 | mutex_spin_exit(&sc->intr_lock); |
| 563 | sc->codec_if->vtbl->restore_ports(sc->codec_if); |
| 564 | mutex_exit(&sc->lock); |
| 565 | |
| 566 | return true; |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | neo_attach(device_t parent, device_t self, void *aux) |
| 571 | { |
| 572 | struct neo_softc *sc; |
| 573 | struct pci_attach_args *pa; |
| 574 | pci_chipset_tag_t pc; |
| 575 | char const *intrstr; |
| 576 | pci_intr_handle_t ih; |
| 577 | pcireg_t csr; |
| 578 | int error; |
| 579 | char intrbuf[PCI_INTRSTR_LEN]; |
| 580 | |
| 581 | sc = device_private(self); |
| 582 | pa = aux; |
| 583 | pc = pa->pa_pc; |
| 584 | |
| 585 | sc->type = PCI_PRODUCT(pa->pa_id); |
| 586 | |
| 587 | printf(": NeoMagic 256%s audio\n" , |
| 588 | sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX" ); |
| 589 | |
| 590 | /* Map I/O register */ |
| 591 | if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0, |
| 592 | &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) { |
| 593 | aprint_error_dev(self, "can't map buffer\n" ); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM, |
| 598 | BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) { |
| 599 | aprint_error_dev(self, "can't map registers\n" ); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | /* Map and establish the interrupt. */ |
| 604 | if (pci_intr_map(pa, &ih)) { |
| 605 | aprint_error_dev(self, "couldn't map interrupt\n" ); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE); |
| 610 | mutex_init(&sc->intr_lock, MUTEX_DEFAULT, IPL_AUDIO); |
| 611 | |
| 612 | intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); |
| 613 | sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc); |
| 614 | |
| 615 | if (sc->ih == NULL) { |
| 616 | aprint_error_dev(self, "couldn't establish interrupt" ); |
| 617 | if (intrstr != NULL) |
| 618 | aprint_error(" at %s" , intrstr); |
| 619 | aprint_error("\n" ); |
| 620 | mutex_destroy(&sc->lock); |
| 621 | mutex_destroy(&sc->intr_lock); |
| 622 | return; |
| 623 | } |
| 624 | aprint_normal_dev(self, "interrupting at %s\n" , intrstr); |
| 625 | |
| 626 | mutex_spin_enter(&sc->intr_lock); |
| 627 | error = nm_init(sc); |
| 628 | mutex_spin_exit(&sc->intr_lock); |
| 629 | if (error != 0) { |
| 630 | mutex_destroy(&sc->lock); |
| 631 | mutex_destroy(&sc->intr_lock); |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | /* Enable the device. */ |
| 636 | csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); |
| 637 | pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, |
| 638 | csr | PCI_COMMAND_MASTER_ENABLE); |
| 639 | |
| 640 | sc->host_if.arg = sc; |
| 641 | sc->host_if.attach = neo_attach_codec; |
| 642 | sc->host_if.read = neo_read_codec; |
| 643 | sc->host_if.write = neo_write_codec; |
| 644 | sc->host_if.reset = neo_reset_codec; |
| 645 | sc->host_if.flags = neo_flags_codec; |
| 646 | |
| 647 | if (ac97_attach(&sc->host_if, self, &sc->lock) != 0) { |
| 648 | mutex_destroy(&sc->lock); |
| 649 | mutex_destroy(&sc->intr_lock); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | if (!pmf_device_register(self, NULL, neo_resume)) |
| 654 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
| 655 | |
| 656 | audio_attach_mi(&neo_hw_if, sc, self); |
| 657 | } |
| 658 | |
| 659 | static int |
| 660 | neo_read_codec(void *v, uint8_t a, uint16_t *d) |
| 661 | { |
| 662 | struct neo_softc *sc; |
| 663 | |
| 664 | sc = v; |
| 665 | if (!nm_waitcd(sc)) { |
| 666 | *d = nm_rd_2(sc, sc->ac97_base + a); |
| 667 | DELAY(1000); |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | return ENXIO; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | static int |
| 676 | neo_write_codec(void *v, u_int8_t a, u_int16_t d) |
| 677 | { |
| 678 | struct neo_softc *sc; |
| 679 | int cnt; |
| 680 | |
| 681 | sc = v; |
| 682 | cnt = 3; |
| 683 | if (!nm_waitcd(sc)) { |
| 684 | while (cnt-- > 0) { |
| 685 | nm_wr_2(sc, sc->ac97_base + a, d); |
| 686 | if (!nm_waitcd(sc)) { |
| 687 | DELAY(1000); |
| 688 | return 0; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return ENXIO; |
| 694 | } |
| 695 | |
| 696 | static int |
| 697 | neo_attach_codec(void *v, struct ac97_codec_if *codec_if) |
| 698 | { |
| 699 | struct neo_softc *sc; |
| 700 | |
| 701 | sc = v; |
| 702 | sc->codec_if = codec_if; |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static int |
| 707 | neo_reset_codec(void *v) |
| 708 | { |
| 709 | struct neo_softc *sc; |
| 710 | |
| 711 | sc = v; |
| 712 | nm_wr_1(sc, 0x6c0, 0x01); |
| 713 | nm_wr_1(sc, 0x6cc, 0x87); |
| 714 | nm_wr_1(sc, 0x6cc, 0x80); |
| 715 | nm_wr_1(sc, 0x6cc, 0x00); |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | static enum ac97_host_flags |
| 720 | neo_flags_codec(void *v) |
| 721 | { |
| 722 | |
| 723 | return AC97_HOST_DONT_READ; |
| 724 | } |
| 725 | |
| 726 | static int |
| 727 | neo_query_encoding(void *addr, struct audio_encoding *fp) |
| 728 | { |
| 729 | |
| 730 | switch (fp->index) { |
| 731 | case 0: |
| 732 | strcpy(fp->name, AudioEulinear); |
| 733 | fp->encoding = AUDIO_ENCODING_ULINEAR; |
| 734 | fp->precision = 8; |
| 735 | fp->flags = 0; |
| 736 | return 0; |
| 737 | case 1: |
| 738 | strcpy(fp->name, AudioEmulaw); |
| 739 | fp->encoding = AUDIO_ENCODING_ULAW; |
| 740 | fp->precision = 8; |
| 741 | fp->flags = AUDIO_ENCODINGFLAG_EMULATED; |
| 742 | return 0; |
| 743 | case 2: |
| 744 | strcpy(fp->name, AudioEalaw); |
| 745 | fp->encoding = AUDIO_ENCODING_ALAW; |
| 746 | fp->precision = 8; |
| 747 | fp->flags = AUDIO_ENCODINGFLAG_EMULATED; |
| 748 | return 0; |
| 749 | case 3: |
| 750 | strcpy(fp->name, AudioEslinear); |
| 751 | fp->encoding = AUDIO_ENCODING_SLINEAR; |
| 752 | fp->precision = 8; |
| 753 | fp->flags = AUDIO_ENCODINGFLAG_EMULATED; |
| 754 | return (0); |
| 755 | case 4: |
| 756 | strcpy(fp->name, AudioEslinear_le); |
| 757 | fp->encoding = AUDIO_ENCODING_SLINEAR_LE; |
| 758 | fp->precision = 16; |
| 759 | fp->flags = 0; |
| 760 | return 0; |
| 761 | case 5: |
| 762 | strcpy(fp->name, AudioEulinear_le); |
| 763 | fp->encoding = AUDIO_ENCODING_ULINEAR_LE; |
| 764 | fp->precision = 16; |
| 765 | fp->flags = AUDIO_ENCODINGFLAG_EMULATED; |
| 766 | return 0; |
| 767 | case 6: |
| 768 | strcpy(fp->name, AudioEslinear_be); |
| 769 | fp->encoding = AUDIO_ENCODING_SLINEAR_BE; |
| 770 | fp->precision = 16; |
| 771 | fp->flags = AUDIO_ENCODINGFLAG_EMULATED; |
| 772 | return 0; |
| 773 | case 7: |
| 774 | strcpy(fp->name, AudioEulinear_be); |
| 775 | fp->encoding = AUDIO_ENCODING_ULINEAR_BE; |
| 776 | fp->precision = 16; |
| 777 | fp->flags = AUDIO_ENCODINGFLAG_EMULATED; |
| 778 | return 0; |
| 779 | default: |
| 780 | return EINVAL; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /* Todo: don't commit settings to card until we've verified all parameters */ |
| 785 | static int |
| 786 | neo_set_params(void *addr, int setmode, int usemode, |
| 787 | audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil, |
| 788 | stream_filter_list_t *rfil) |
| 789 | { |
| 790 | struct neo_softc *sc; |
| 791 | audio_params_t *p; |
| 792 | stream_filter_list_t *fil; |
| 793 | uint32_t base; |
| 794 | uint8_t x; |
| 795 | int mode, i; |
| 796 | |
| 797 | sc = addr; |
| 798 | for (mode = AUMODE_RECORD; mode != -1; |
| 799 | mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { |
| 800 | if ((setmode & mode) == 0) |
| 801 | continue; |
| 802 | |
| 803 | p = mode == AUMODE_PLAY ? play : rec; |
| 804 | |
| 805 | if (p == NULL) continue; |
| 806 | |
| 807 | for (x = 0; x < 8; x++) { |
| 808 | if (p->sample_rate < |
| 809 | (samplerates[x] + samplerates[x + 1]) / 2) |
| 810 | break; |
| 811 | } |
| 812 | if (x == 8) |
| 813 | return EINVAL; |
| 814 | |
| 815 | p->sample_rate = samplerates[x]; |
| 816 | nm_loadcoeff(sc, mode, x); |
| 817 | |
| 818 | x <<= 4; |
| 819 | x &= NM_RATE_MASK; |
| 820 | if (p->precision == 16) |
| 821 | x |= NM_RATE_BITS_16; |
| 822 | if (p->channels == 2) |
| 823 | x |= NM_RATE_STEREO; |
| 824 | |
| 825 | base = (mode == AUMODE_PLAY)? |
| 826 | NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET; |
| 827 | nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x); |
| 828 | |
| 829 | fil = mode == AUMODE_PLAY ? pfil : rfil; |
| 830 | i = auconv_set_converter(neo_formats, NEO_NFORMATS, |
| 831 | mode, p, FALSE, fil); |
| 832 | if (i < 0) |
| 833 | return EINVAL; |
| 834 | } |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static int |
| 840 | neo_round_blocksize(void *addr, int blk, int mode, |
| 841 | const audio_params_t *param) |
| 842 | { |
| 843 | |
| 844 | return NM_BUFFSIZE / 2; |
| 845 | } |
| 846 | |
| 847 | static int |
| 848 | neo_trigger_output(void *addr, void *start, void *end, int blksize, |
| 849 | void (*intr)(void *), void *arg, const audio_params_t *param) |
| 850 | { |
| 851 | struct neo_softc *sc; |
| 852 | int ssz; |
| 853 | |
| 854 | sc = addr; |
| 855 | sc->pintr = intr; |
| 856 | sc->parg = arg; |
| 857 | |
| 858 | ssz = (param->precision == 16) ? 2 : 1; |
| 859 | if (param->channels == 2) |
| 860 | ssz <<= 1; |
| 861 | |
| 862 | sc->pbufsize = ((char*)end - (char *)start); |
| 863 | sc->pblksize = blksize; |
| 864 | sc->pwmark = blksize; |
| 865 | |
| 866 | nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf); |
| 867 | nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz); |
| 868 | nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf); |
| 869 | nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark); |
| 870 | nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN | |
| 871 | NM_PLAYBACK_ENABLE_FLAG); |
| 872 | nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0); |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static int |
| 878 | neo_trigger_input(void *addr, void *start, void *end, int blksize, |
| 879 | void (*intr)(void *), void *arg, const audio_params_t *param) |
| 880 | { |
| 881 | struct neo_softc *sc; |
| 882 | int ssz; |
| 883 | |
| 884 | sc = addr; |
| 885 | sc->rintr = intr; |
| 886 | sc->rarg = arg; |
| 887 | |
| 888 | ssz = (param->precision == 16) ? 2 : 1; |
| 889 | if (param->channels == 2) |
| 890 | ssz <<= 1; |
| 891 | |
| 892 | sc->rbufsize = ((char*)end - (char *)start); |
| 893 | sc->rblksize = blksize; |
| 894 | sc->rwmark = blksize; |
| 895 | |
| 896 | nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf); |
| 897 | nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize); |
| 898 | nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf); |
| 899 | nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark); |
| 900 | nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN | |
| 901 | NM_RECORD_ENABLE_FLAG); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static int |
| 907 | neo_halt_output(void *addr) |
| 908 | { |
| 909 | struct neo_softc *sc; |
| 910 | |
| 911 | sc = (struct neo_softc *)addr; |
| 912 | nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0); |
| 913 | nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH); |
| 914 | sc->pintr = 0; |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | static int |
| 920 | neo_halt_input(void *addr) |
| 921 | { |
| 922 | struct neo_softc *sc; |
| 923 | |
| 924 | sc = (struct neo_softc *)addr; |
| 925 | nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0); |
| 926 | sc->rintr = 0; |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static int |
| 932 | neo_getdev(void *addr, struct audio_device *retp) |
| 933 | { |
| 934 | |
| 935 | *retp = neo_device; |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static int |
| 940 | neo_mixer_set_port(void *addr, mixer_ctrl_t *cp) |
| 941 | { |
| 942 | struct neo_softc *sc; |
| 943 | |
| 944 | sc = addr; |
| 945 | return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp); |
| 946 | } |
| 947 | |
| 948 | static int |
| 949 | neo_mixer_get_port(void *addr, mixer_ctrl_t *cp) |
| 950 | { |
| 951 | struct neo_softc *sc; |
| 952 | |
| 953 | sc = addr; |
| 954 | return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp); |
| 955 | } |
| 956 | |
| 957 | static int |
| 958 | neo_query_devinfo(void *addr, mixer_devinfo_t *dip) |
| 959 | { |
| 960 | struct neo_softc *sc; |
| 961 | |
| 962 | sc = addr; |
| 963 | return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip); |
| 964 | } |
| 965 | |
| 966 | static void * |
| 967 | neo_malloc(void *addr, int direction, size_t size) |
| 968 | { |
| 969 | struct neo_softc *sc; |
| 970 | void *rv; |
| 971 | |
| 972 | sc = addr; |
| 973 | rv = NULL; |
| 974 | switch (direction) { |
| 975 | case AUMODE_PLAY: |
| 976 | if (sc->pbuf_allocated == 0) { |
| 977 | rv = (void *) sc->pbuf_vaddr; |
| 978 | sc->pbuf_allocated = 1; |
| 979 | } |
| 980 | break; |
| 981 | |
| 982 | case AUMODE_RECORD: |
| 983 | if (sc->rbuf_allocated == 0) { |
| 984 | rv = (void *) sc->rbuf_vaddr; |
| 985 | sc->rbuf_allocated = 1; |
| 986 | } |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | return rv; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | neo_free(void *addr, void *ptr, size_t size) |
| 995 | { |
| 996 | struct neo_softc *sc; |
| 997 | vaddr_t v; |
| 998 | |
| 999 | sc = addr; |
| 1000 | v = (vaddr_t)ptr; |
| 1001 | if (v == sc->pbuf_vaddr) |
| 1002 | sc->pbuf_allocated = 0; |
| 1003 | else if (v == sc->rbuf_vaddr) |
| 1004 | sc->rbuf_allocated = 0; |
| 1005 | else |
| 1006 | printf("neo_free: bad address %p\n" , ptr); |
| 1007 | } |
| 1008 | |
| 1009 | static size_t |
| 1010 | neo_round_buffersize(void *addr, int direction, size_t size) |
| 1011 | { |
| 1012 | |
| 1013 | return NM_BUFFSIZE; |
| 1014 | } |
| 1015 | |
| 1016 | static paddr_t |
| 1017 | neo_mappage(void *addr, void *mem, off_t off, int prot) |
| 1018 | { |
| 1019 | struct neo_softc *sc; |
| 1020 | vaddr_t v; |
| 1021 | bus_addr_t pciaddr; |
| 1022 | |
| 1023 | sc = addr; |
| 1024 | v = (vaddr_t)mem; |
| 1025 | if (v == sc->pbuf_vaddr) |
| 1026 | pciaddr = sc->pbuf_pciaddr; |
| 1027 | else if (v == sc->rbuf_vaddr) |
| 1028 | pciaddr = sc->rbuf_pciaddr; |
| 1029 | else |
| 1030 | return -1; |
| 1031 | |
| 1032 | return bus_space_mmap(sc->bufiot, pciaddr, off, prot, |
| 1033 | BUS_SPACE_MAP_LINEAR); |
| 1034 | } |
| 1035 | |
| 1036 | static int |
| 1037 | neo_get_props(void *addr) |
| 1038 | { |
| 1039 | |
| 1040 | return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP | |
| 1041 | AUDIO_PROP_FULLDUPLEX; |
| 1042 | } |
| 1043 | |
| 1044 | static void |
| 1045 | neo_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread) |
| 1046 | { |
| 1047 | struct neo_softc *sc; |
| 1048 | |
| 1049 | sc = addr; |
| 1050 | *intr = &sc->intr_lock; |
| 1051 | *thread = &sc->lock; |
| 1052 | } |
| 1053 | |