neo.c revision 1.34 1 /* $NetBSD: neo.c,v 1.34 2006/10/12 01:31:32 christos 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.34 2006/10/12 01:31:32 christos 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 uint32_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 uint32_t ac97_base, ac97_status, ac97_busy;
150 uint32_t buftop, pbuf, rbuf, cbuf, acbuf;
151 uint32_t playint, recint, misc1int, misc2int;
152 uint32_t irsz, badintr;
153
154 uint32_t pbufsize;
155 uint32_t rbufsize;
156
157 uint32_t pblksize;
158 uint32_t rblksize;
159
160 uint32_t pwmark;
161 uint32_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 *);
176 static int nm_loadcoeff(struct neo_softc *, int, int);
177 static int nm_init(struct neo_softc *);
178
179 static int neo_match(struct device *, struct cfdata *, void *);
180 static void neo_attach(struct device *, struct device *, void *);
181 static int neo_intr(void *);
182
183 static int neo_query_encoding(void *, struct audio_encoding *);
184 static int neo_set_params(void *, int, int, audio_params_t *,
185 audio_params_t *, stream_filter_list_t *,
186 stream_filter_list_t *);
187 static int neo_round_blocksize(void *, int, int, const audio_params_t *);
188 static int neo_trigger_output(void *, void *, void *, int,
189 void (*)(void *), void *,
190 const audio_params_t *);
191 static int neo_trigger_input(void *, void *, void *, int,
192 void (*)(void *), void *,
193 const audio_params_t *);
194 static int neo_halt_output(void *);
195 static int neo_halt_input(void *);
196 static int neo_getdev(void *, struct audio_device *);
197 static int neo_mixer_set_port(void *, mixer_ctrl_t *);
198 static int neo_mixer_get_port(void *, mixer_ctrl_t *);
199 static int neo_attach_codec(void *, struct ac97_codec_if *);
200 static int neo_read_codec(void *, uint8_t, uint16_t *);
201 static int neo_write_codec(void *, uint8_t, uint16_t);
202 static int neo_reset_codec(void *);
203 static enum ac97_host_flags neo_flags_codec(void *);
204 static int neo_query_devinfo(void *, mixer_devinfo_t *);
205 static void * neo_malloc(void *, int, size_t, struct malloc_type *, int);
206 static void neo_free(void *, void *, struct malloc_type *);
207 static size_t neo_round_buffersize(void *, int, size_t);
208 static paddr_t neo_mappage(void *, void *, off_t, int);
209 static int neo_get_props(void *);
210 static void neo_power(int, void *);
211
212 CFATTACH_DECL(neo, sizeof(struct neo_softc),
213 neo_match, neo_attach, NULL, NULL);
214
215 static struct audio_device neo_device = {
216 "NeoMagic 256",
217 "",
218 "neo"
219 };
220
221 /* The actual rates supported by the card. */
222 static const int samplerates[9] = {
223 8000,
224 11025,
225 16000,
226 22050,
227 24000,
228 32000,
229 44100,
230 48000,
231 99999999
232 };
233
234 #define NEO_NFORMATS 4
235 static const struct audio_format neo_formats[NEO_NFORMATS] = {
236 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
237 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
238 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
239 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
240 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
241 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
242 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
243 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
244 };
245
246 /* -------------------------------------------------------------------- */
247
248 static const struct audio_hw_if neo_hw_if = {
249 NULL, /* open */
250 NULL, /* close */
251 NULL, /* drain */
252 neo_query_encoding,
253 neo_set_params,
254 neo_round_blocksize,
255 NULL, /* commit_setting */
256 NULL, /* init_output */
257 NULL, /* init_input */
258 NULL, /* start_output */
259 NULL, /* start_input */
260 neo_halt_output,
261 neo_halt_input,
262 NULL, /* speaker_ctl */
263 neo_getdev,
264 NULL, /* getfd */
265 neo_mixer_set_port,
266 neo_mixer_get_port,
267 neo_query_devinfo,
268 neo_malloc,
269 neo_free,
270 neo_round_buffersize,
271 neo_mappage,
272 neo_get_props,
273 neo_trigger_output,
274 neo_trigger_input,
275 NULL,
276 NULL,
277 };
278
279 /* -------------------------------------------------------------------- */
280
281 #define nm_rd_1(sc, regno) \
282 bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
283
284 #define nm_rd_2(sc, regno) \
285 bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
286
287 #define nm_rd_4(sc, regno) \
288 bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
289
290 #define nm_wr_1(sc, regno, val) \
291 bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
292
293 #define nm_wr_2(sc, regno, val) \
294 bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
295
296 #define nm_wr_4(sc, regno, val) \
297 bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
298
299 #define nm_rdbuf_4(sc, regno) \
300 bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
301
302 #define nm_wrbuf_1(sc, regno, val) \
303 bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
304
305 /* ac97 codec */
306 static int
307 nm_waitcd(struct neo_softc *sc)
308 {
309 int cnt;
310 int fail;
311
312 cnt = 10;
313 fail = 1;
314 while (cnt-- > 0) {
315 if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
316 DELAY(100);
317 else {
318 fail = 0;
319 break;
320 }
321 }
322 return fail;
323 }
324
325 static void
326 nm_ackint(struct neo_softc *sc, uint32_t num)
327 {
328
329 switch (sc->type) {
330 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
331 nm_wr_2(sc, NM_INT_REG, num << 1);
332 break;
333
334 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
335 nm_wr_4(sc, NM_INT_REG, num);
336 break;
337 }
338 }
339
340 static int
341 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
342 {
343 int ofs, sz, i;
344 uint32_t addr;
345
346 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
347 if (dir == AUMODE_RECORD)
348 num += 8;
349 sz = coefficientSizes[num];
350 ofs = 0;
351 while (num-- > 0)
352 ofs+= coefficientSizes[num];
353 for (i = 0; i < sz; i++)
354 nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
355 nm_wr_4(sc, addr, sc->cbuf);
356 if (dir == AUMODE_PLAY)
357 sz--;
358 nm_wr_4(sc, addr + 4, sc->cbuf + sz);
359 return 0;
360 }
361
362 /* The interrupt handler */
363 static int
364 neo_intr(void *p)
365 {
366 struct neo_softc *sc;
367 int status, x;
368 int rv;
369
370 sc = (struct neo_softc *)p;
371 rv = 0;
372 status = (sc->irsz == 2) ?
373 nm_rd_2(sc, NM_INT_REG) :
374 nm_rd_4(sc, NM_INT_REG);
375
376 if (status & sc->playint) {
377 status &= ~sc->playint;
378
379 sc->pwmark += sc->pblksize;
380 sc->pwmark %= sc->pbufsize;
381
382 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
383
384 nm_ackint(sc, sc->playint);
385
386 if (sc->pintr)
387 (*sc->pintr)(sc->parg);
388
389 rv = 1;
390 }
391 if (status & sc->recint) {
392 status &= ~sc->recint;
393
394 sc->rwmark += sc->rblksize;
395 sc->rwmark %= sc->rbufsize;
396 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
397 nm_ackint(sc, sc->recint);
398 if (sc->rintr)
399 (*sc->rintr)(sc->rarg);
400
401 rv = 1;
402 }
403 if (status & sc->misc1int) {
404 status &= ~sc->misc1int;
405 nm_ackint(sc, sc->misc1int);
406 x = nm_rd_1(sc, 0x400);
407 nm_wr_1(sc, 0x400, x | 2);
408 printf("%s: misc int 1\n", sc->dev.dv_xname);
409 rv = 1;
410 }
411 if (status & sc->misc2int) {
412 status &= ~sc->misc2int;
413 nm_ackint(sc, sc->misc2int);
414 x = nm_rd_1(sc, 0x400);
415 nm_wr_1(sc, 0x400, x & ~2);
416 printf("%s: misc int 2\n", sc->dev.dv_xname);
417 rv = 1;
418 }
419 if (status) {
420 status &= ~sc->misc2int;
421 nm_ackint(sc, sc->misc2int);
422 printf("%s: unknown int\n", sc->dev.dv_xname);
423 rv = 1;
424 }
425
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(struct device *parent __unused, struct cfdata *match __unused,
505 void *aux)
506 {
507 struct pci_attach_args *pa;
508 pcireg_t subdev;
509
510 pa = aux;
511 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
512 return 0;
513
514 subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
515
516 switch (PCI_PRODUCT(pa->pa_id)) {
517 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
518 /*
519 * We have to weed-out the non-AC'97 versions of
520 * the chip (i.e. the ones that are known to work
521 * in WSS emulation mode), as they won't work with
522 * this driver.
523 */
524 switch (PCI_VENDOR(subdev)) {
525 case PCI_VENDOR_DELL:
526 switch (PCI_PRODUCT(subdev)) {
527 case 0x008f:
528 return 0;
529 }
530 break;
531
532 case PCI_VENDOR_HP:
533 switch (PCI_PRODUCT(subdev)) {
534 case 0x0007:
535 return 0;
536 }
537 break;
538
539 case PCI_VENDOR_IBM:
540 switch (PCI_PRODUCT(subdev)) {
541 case 0x00dd:
542 return 0;
543 }
544 break;
545 }
546 return 1;
547
548 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
549 return 1;
550 }
551
552 return 0;
553 }
554
555 static void
556 neo_power(int why, void *addr)
557 {
558 struct neo_softc *sc;
559
560 sc = (struct neo_softc *)addr;
561 if (why == PWR_RESUME) {
562 nm_init(sc);
563 sc->codec_if->vtbl->restore_ports(sc->codec_if);
564 }
565 }
566
567 static void
568 neo_attach(struct device *parent __unused, struct device *self, void *aux)
569 {
570 struct neo_softc *sc;
571 struct pci_attach_args *pa;
572 pci_chipset_tag_t pc;
573 char const *intrstr;
574 pci_intr_handle_t ih;
575 pcireg_t csr;
576
577 sc = (struct neo_softc *)self;
578 pa = (struct pci_attach_args *)aux;
579 pc = pa->pa_pc;
580
581 sc->type = PCI_PRODUCT(pa->pa_id);
582
583 printf(": NeoMagic 256%s audio\n",
584 sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
585
586 /* Map I/O register */
587 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
588 &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
589 printf("%s: can't map buffer\n", sc->dev.dv_xname);
590 return;
591 }
592
593 if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
594 BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) {
595 printf("%s: can't map registers\n", sc->dev.dv_xname);
596 return;
597 }
598
599 /* Map and establish the interrupt. */
600 if (pci_intr_map(pa, &ih)) {
601 printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
602 return;
603 }
604
605 intrstr = pci_intr_string(pc, ih);
606 sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
607
608 if (sc->ih == NULL) {
609 printf("%s: couldn't establish interrupt",
610 sc->dev.dv_xname);
611 if (intrstr != NULL)
612 printf(" at %s", intrstr);
613 printf("\n");
614 return;
615 }
616 printf("%s: interrupting at %s\n", sc->dev.dv_xname, intrstr);
617
618 if (nm_init(sc) != 0)
619 return;
620
621 /* Enable the device. */
622 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
623 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
624 csr | PCI_COMMAND_MASTER_ENABLE);
625
626 sc->host_if.arg = sc;
627
628 sc->host_if.attach = neo_attach_codec;
629 sc->host_if.read = neo_read_codec;
630 sc->host_if.write = neo_write_codec;
631 sc->host_if.reset = neo_reset_codec;
632 sc->host_if.flags = neo_flags_codec;
633
634 if (ac97_attach(&sc->host_if, self) != 0)
635 return;
636
637 sc->powerhook = powerhook_establish(sc->dev.dv_xname, neo_power, sc);
638
639 audio_attach_mi(&neo_hw_if, sc, &sc->dev);
640 }
641
642 static int
643 neo_read_codec(void *v, uint8_t a, uint16_t *d)
644 {
645 struct neo_softc *sc;
646
647 sc = v;
648 if (!nm_waitcd(sc)) {
649 *d = nm_rd_2(sc, sc->ac97_base + a);
650 DELAY(1000);
651 return 0;
652 }
653
654 return ENXIO;
655 }
656
657
658 static int
659 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
660 {
661 struct neo_softc *sc;
662 int cnt;
663
664 sc = v;
665 cnt = 3;
666 if (!nm_waitcd(sc)) {
667 while (cnt-- > 0) {
668 nm_wr_2(sc, sc->ac97_base + a, d);
669 if (!nm_waitcd(sc)) {
670 DELAY(1000);
671 return 0;
672 }
673 }
674 }
675
676 return ENXIO;
677 }
678
679 static int
680 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
681 {
682 struct neo_softc *sc;
683
684 sc = v;
685 sc->codec_if = codec_if;
686 return 0;
687 }
688
689 static int
690 neo_reset_codec(void *v)
691 {
692 struct neo_softc *sc;
693
694 sc = v;
695 nm_wr_1(sc, 0x6c0, 0x01);
696 nm_wr_1(sc, 0x6cc, 0x87);
697 nm_wr_1(sc, 0x6cc, 0x80);
698 nm_wr_1(sc, 0x6cc, 0x00);
699 return 0;
700 }
701
702 static enum ac97_host_flags
703 neo_flags_codec(void *v __unused)
704 {
705
706 return AC97_HOST_DONT_READ;
707 }
708
709 static int
710 neo_query_encoding(void *addr __unused, struct audio_encoding *fp)
711 {
712
713 switch (fp->index) {
714 case 0:
715 strcpy(fp->name, AudioEulinear);
716 fp->encoding = AUDIO_ENCODING_ULINEAR;
717 fp->precision = 8;
718 fp->flags = 0;
719 return 0;
720 case 1:
721 strcpy(fp->name, AudioEmulaw);
722 fp->encoding = AUDIO_ENCODING_ULAW;
723 fp->precision = 8;
724 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
725 return 0;
726 case 2:
727 strcpy(fp->name, AudioEalaw);
728 fp->encoding = AUDIO_ENCODING_ALAW;
729 fp->precision = 8;
730 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
731 return 0;
732 case 3:
733 strcpy(fp->name, AudioEslinear);
734 fp->encoding = AUDIO_ENCODING_SLINEAR;
735 fp->precision = 8;
736 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
737 return (0);
738 case 4:
739 strcpy(fp->name, AudioEslinear_le);
740 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
741 fp->precision = 16;
742 fp->flags = 0;
743 return 0;
744 case 5:
745 strcpy(fp->name, AudioEulinear_le);
746 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
747 fp->precision = 16;
748 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
749 return 0;
750 case 6:
751 strcpy(fp->name, AudioEslinear_be);
752 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
753 fp->precision = 16;
754 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
755 return 0;
756 case 7:
757 strcpy(fp->name, AudioEulinear_be);
758 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
759 fp->precision = 16;
760 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
761 return 0;
762 default:
763 return EINVAL;
764 }
765 }
766
767 /* Todo: don't commit settings to card until we've verified all parameters */
768 static int
769 neo_set_params(void *addr, int setmode, int usemode __unused,
770 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
771 stream_filter_list_t *rfil)
772 {
773 struct neo_softc *sc;
774 audio_params_t *p;
775 stream_filter_list_t *fil;
776 uint32_t base;
777 uint8_t x;
778 int mode, i;
779
780 sc = addr;
781 for (mode = AUMODE_RECORD; mode != -1;
782 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
783 if ((setmode & mode) == 0)
784 continue;
785
786 p = mode == AUMODE_PLAY ? play : rec;
787
788 if (p == NULL) continue;
789
790 for (x = 0; x < 8; x++) {
791 if (p->sample_rate <
792 (samplerates[x] + samplerates[x + 1]) / 2)
793 break;
794 }
795 if (x == 8)
796 return EINVAL;
797
798 p->sample_rate = samplerates[x];
799 nm_loadcoeff(sc, mode, x);
800
801 x <<= 4;
802 x &= NM_RATE_MASK;
803 if (p->precision == 16)
804 x |= NM_RATE_BITS_16;
805 if (p->channels == 2)
806 x |= NM_RATE_STEREO;
807
808 base = (mode == AUMODE_PLAY)?
809 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
810 nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
811
812 fil = mode == AUMODE_PLAY ? pfil : rfil;
813 i = auconv_set_converter(neo_formats, NEO_NFORMATS,
814 mode, p, FALSE, fil);
815 if (i < 0)
816 return EINVAL;
817 }
818
819 return 0;
820 }
821
822 static int
823 neo_round_blocksize(void *addr __unused, int blk __unused, int mode __unused,
824 const audio_params_t *param __unused)
825 {
826
827 return NM_BUFFSIZE / 2;
828 }
829
830 static int
831 neo_trigger_output(void *addr, void *start, void *end, int blksize,
832 void (*intr)(void *), void *arg, const audio_params_t *param)
833 {
834 struct neo_softc *sc;
835 int ssz;
836
837 sc = addr;
838 sc->pintr = intr;
839 sc->parg = arg;
840
841 ssz = (param->precision == 16) ? 2 : 1;
842 if (param->channels == 2)
843 ssz <<= 1;
844
845 sc->pbufsize = ((char*)end - (char *)start);
846 sc->pblksize = blksize;
847 sc->pwmark = blksize;
848
849 nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
850 nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
851 nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
852 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
853 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
854 NM_PLAYBACK_ENABLE_FLAG);
855 nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
856
857 return 0;
858 }
859
860 static int
861 neo_trigger_input(void *addr, void *start, void *end, int blksize,
862 void (*intr)(void *), void *arg, const audio_params_t *param)
863 {
864 struct neo_softc *sc;
865 int ssz;
866
867 sc = addr;
868 sc->rintr = intr;
869 sc->rarg = arg;
870
871 ssz = (param->precision == 16) ? 2 : 1;
872 if (param->channels == 2)
873 ssz <<= 1;
874
875 sc->rbufsize = ((char*)end - (char *)start);
876 sc->rblksize = blksize;
877 sc->rwmark = blksize;
878
879 nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
880 nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
881 nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
882 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
883 nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
884 NM_RECORD_ENABLE_FLAG);
885
886 return 0;
887 }
888
889 static int
890 neo_halt_output(void *addr)
891 {
892 struct neo_softc *sc;
893
894 sc = (struct neo_softc *)addr;
895 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
896 nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
897 sc->pintr = 0;
898
899 return 0;
900 }
901
902 static int
903 neo_halt_input(void *addr)
904 {
905 struct neo_softc *sc;
906
907 sc = (struct neo_softc *)addr;
908 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
909 sc->rintr = 0;
910
911 return 0;
912 }
913
914 static int
915 neo_getdev(void *addr __unused, struct audio_device *retp)
916 {
917
918 *retp = neo_device;
919 return 0;
920 }
921
922 static int
923 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
924 {
925 struct neo_softc *sc;
926
927 sc = addr;
928 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
929 }
930
931 static int
932 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
933 {
934 struct neo_softc *sc;
935
936 sc = addr;
937 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
938 }
939
940 static int
941 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
942 {
943 struct neo_softc *sc;
944
945 sc = addr;
946 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
947 }
948
949 static void *
950 neo_malloc(void *addr, int direction, size_t size __unused,
951 struct malloc_type *pool __unused, int flags __unused)
952 {
953 struct neo_softc *sc;
954 void *rv;
955
956 sc = addr;
957 rv = NULL;
958 switch (direction) {
959 case AUMODE_PLAY:
960 if (sc->pbuf_allocated == 0) {
961 rv = (void *) sc->pbuf_vaddr;
962 sc->pbuf_allocated = 1;
963 }
964 break;
965
966 case AUMODE_RECORD:
967 if (sc->rbuf_allocated == 0) {
968 rv = (void *) sc->rbuf_vaddr;
969 sc->rbuf_allocated = 1;
970 }
971 break;
972 }
973
974 return rv;
975 }
976
977 static void
978 neo_free(void *addr, void *ptr, struct malloc_type *pool __unused)
979 {
980 struct neo_softc *sc;
981 vaddr_t v;
982
983 sc = addr;
984 v = (vaddr_t)ptr;
985 if (v == sc->pbuf_vaddr)
986 sc->pbuf_allocated = 0;
987 else if (v == sc->rbuf_vaddr)
988 sc->rbuf_allocated = 0;
989 else
990 printf("neo_free: bad address %p\n", ptr);
991 }
992
993 static size_t
994 neo_round_buffersize(void *addr __unused, int direction __unused,
995 size_t size __unused)
996 {
997
998 return NM_BUFFSIZE;
999 }
1000
1001 static paddr_t
1002 neo_mappage(void *addr, void *mem, off_t off, int prot)
1003 {
1004 struct neo_softc *sc;
1005 vaddr_t v;
1006 bus_addr_t pciaddr;
1007
1008 sc = addr;
1009 v = (vaddr_t)mem;
1010 if (v == sc->pbuf_vaddr)
1011 pciaddr = sc->pbuf_pciaddr;
1012 else if (v == sc->rbuf_vaddr)
1013 pciaddr = sc->rbuf_pciaddr;
1014 else
1015 return -1;
1016
1017 return bus_space_mmap(sc->bufiot, pciaddr, off, prot,
1018 BUS_SPACE_MAP_LINEAR);
1019 }
1020
1021 static int
1022 neo_get_props(void *addr __unused)
1023 {
1024
1025 return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
1026 AUDIO_PROP_FULLDUPLEX;
1027 }
1028