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