neo.c revision 1.51 1 /* $NetBSD: neo.c,v 1.51 2018/12/09 11:14:02 jdolecek 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.51 2018/12/09 11:14:02 jdolecek 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_xname(pc, ih, IPL_AUDIO, neo_intr, sc,
614 device_xname(self));
615
616 if (sc->ih == NULL) {
617 aprint_error_dev(self, "couldn't establish interrupt");
618 if (intrstr != NULL)
619 aprint_error(" at %s", intrstr);
620 aprint_error("\n");
621 mutex_destroy(&sc->lock);
622 mutex_destroy(&sc->intr_lock);
623 return;
624 }
625 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
626
627 mutex_spin_enter(&sc->intr_lock);
628 error = nm_init(sc);
629 mutex_spin_exit(&sc->intr_lock);
630 if (error != 0) {
631 mutex_destroy(&sc->lock);
632 mutex_destroy(&sc->intr_lock);
633 return;
634 }
635
636 /* Enable the device. */
637 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
638 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
639 csr | PCI_COMMAND_MASTER_ENABLE);
640
641 sc->host_if.arg = sc;
642 sc->host_if.attach = neo_attach_codec;
643 sc->host_if.read = neo_read_codec;
644 sc->host_if.write = neo_write_codec;
645 sc->host_if.reset = neo_reset_codec;
646 sc->host_if.flags = neo_flags_codec;
647
648 if (ac97_attach(&sc->host_if, self, &sc->lock) != 0) {
649 mutex_destroy(&sc->lock);
650 mutex_destroy(&sc->intr_lock);
651 return;
652 }
653
654 if (!pmf_device_register(self, NULL, neo_resume))
655 aprint_error_dev(self, "couldn't establish power handler\n");
656
657 audio_attach_mi(&neo_hw_if, sc, self);
658 }
659
660 static int
661 neo_read_codec(void *v, uint8_t a, uint16_t *d)
662 {
663 struct neo_softc *sc;
664
665 sc = v;
666 if (!nm_waitcd(sc)) {
667 *d = nm_rd_2(sc, sc->ac97_base + a);
668 DELAY(1000);
669 return 0;
670 }
671
672 return ENXIO;
673 }
674
675
676 static int
677 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
678 {
679 struct neo_softc *sc;
680 int cnt;
681
682 sc = v;
683 cnt = 3;
684 if (!nm_waitcd(sc)) {
685 while (cnt-- > 0) {
686 nm_wr_2(sc, sc->ac97_base + a, d);
687 if (!nm_waitcd(sc)) {
688 DELAY(1000);
689 return 0;
690 }
691 }
692 }
693
694 return ENXIO;
695 }
696
697 static int
698 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
699 {
700 struct neo_softc *sc;
701
702 sc = v;
703 sc->codec_if = codec_if;
704 return 0;
705 }
706
707 static int
708 neo_reset_codec(void *v)
709 {
710 struct neo_softc *sc;
711
712 sc = v;
713 nm_wr_1(sc, 0x6c0, 0x01);
714 nm_wr_1(sc, 0x6cc, 0x87);
715 nm_wr_1(sc, 0x6cc, 0x80);
716 nm_wr_1(sc, 0x6cc, 0x00);
717 return 0;
718 }
719
720 static enum ac97_host_flags
721 neo_flags_codec(void *v)
722 {
723
724 return AC97_HOST_DONT_READ;
725 }
726
727 static int
728 neo_query_encoding(void *addr, struct audio_encoding *fp)
729 {
730
731 switch (fp->index) {
732 case 0:
733 strcpy(fp->name, AudioEulinear);
734 fp->encoding = AUDIO_ENCODING_ULINEAR;
735 fp->precision = 8;
736 fp->flags = 0;
737 return 0;
738 case 1:
739 strcpy(fp->name, AudioEmulaw);
740 fp->encoding = AUDIO_ENCODING_ULAW;
741 fp->precision = 8;
742 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
743 return 0;
744 case 2:
745 strcpy(fp->name, AudioEalaw);
746 fp->encoding = AUDIO_ENCODING_ALAW;
747 fp->precision = 8;
748 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
749 return 0;
750 case 3:
751 strcpy(fp->name, AudioEslinear);
752 fp->encoding = AUDIO_ENCODING_SLINEAR;
753 fp->precision = 8;
754 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
755 return (0);
756 case 4:
757 strcpy(fp->name, AudioEslinear_le);
758 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
759 fp->precision = 16;
760 fp->flags = 0;
761 return 0;
762 case 5:
763 strcpy(fp->name, AudioEulinear_le);
764 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
765 fp->precision = 16;
766 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
767 return 0;
768 case 6:
769 strcpy(fp->name, AudioEslinear_be);
770 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
771 fp->precision = 16;
772 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
773 return 0;
774 case 7:
775 strcpy(fp->name, AudioEulinear_be);
776 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
777 fp->precision = 16;
778 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
779 return 0;
780 default:
781 return EINVAL;
782 }
783 }
784
785 /* Todo: don't commit settings to card until we've verified all parameters */
786 static int
787 neo_set_params(void *addr, int setmode, int usemode,
788 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
789 stream_filter_list_t *rfil)
790 {
791 struct neo_softc *sc;
792 audio_params_t *p;
793 stream_filter_list_t *fil;
794 uint32_t base;
795 uint8_t x;
796 int mode, i;
797
798 sc = addr;
799 for (mode = AUMODE_RECORD; mode != -1;
800 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
801 if ((setmode & mode) == 0)
802 continue;
803
804 p = mode == AUMODE_PLAY ? play : rec;
805
806 if (p == NULL) continue;
807
808 for (x = 0; x < 8; x++) {
809 if (p->sample_rate <
810 (samplerates[x] + samplerates[x + 1]) / 2)
811 break;
812 }
813 if (x == 8)
814 return EINVAL;
815
816 p->sample_rate = samplerates[x];
817 nm_loadcoeff(sc, mode, x);
818
819 x <<= 4;
820 x &= NM_RATE_MASK;
821 if (p->precision == 16)
822 x |= NM_RATE_BITS_16;
823 if (p->channels == 2)
824 x |= NM_RATE_STEREO;
825
826 base = (mode == AUMODE_PLAY)?
827 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
828 nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
829
830 fil = mode == AUMODE_PLAY ? pfil : rfil;
831 i = auconv_set_converter(neo_formats, NEO_NFORMATS,
832 mode, p, FALSE, fil);
833 if (i < 0)
834 return EINVAL;
835 }
836
837 return 0;
838 }
839
840 static int
841 neo_round_blocksize(void *addr, int blk, int mode,
842 const audio_params_t *param)
843 {
844
845 return NM_BUFFSIZE / 2;
846 }
847
848 static int
849 neo_trigger_output(void *addr, void *start, void *end, int blksize,
850 void (*intr)(void *), void *arg, const audio_params_t *param)
851 {
852 struct neo_softc *sc;
853 int ssz;
854
855 sc = addr;
856 sc->pintr = intr;
857 sc->parg = arg;
858
859 ssz = (param->precision == 16) ? 2 : 1;
860 if (param->channels == 2)
861 ssz <<= 1;
862
863 sc->pbufsize = ((char*)end - (char *)start);
864 sc->pblksize = blksize;
865 sc->pwmark = blksize;
866
867 nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
868 nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
869 nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
870 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
871 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
872 NM_PLAYBACK_ENABLE_FLAG);
873 nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
874
875 return 0;
876 }
877
878 static int
879 neo_trigger_input(void *addr, void *start, void *end, int blksize,
880 void (*intr)(void *), void *arg, const audio_params_t *param)
881 {
882 struct neo_softc *sc;
883 int ssz;
884
885 sc = addr;
886 sc->rintr = intr;
887 sc->rarg = arg;
888
889 ssz = (param->precision == 16) ? 2 : 1;
890 if (param->channels == 2)
891 ssz <<= 1;
892
893 sc->rbufsize = ((char*)end - (char *)start);
894 sc->rblksize = blksize;
895 sc->rwmark = blksize;
896
897 nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
898 nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
899 nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
900 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
901 nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
902 NM_RECORD_ENABLE_FLAG);
903
904 return 0;
905 }
906
907 static int
908 neo_halt_output(void *addr)
909 {
910 struct neo_softc *sc;
911
912 sc = (struct neo_softc *)addr;
913 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
914 nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
915 sc->pintr = 0;
916
917 return 0;
918 }
919
920 static int
921 neo_halt_input(void *addr)
922 {
923 struct neo_softc *sc;
924
925 sc = (struct neo_softc *)addr;
926 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
927 sc->rintr = 0;
928
929 return 0;
930 }
931
932 static int
933 neo_getdev(void *addr, struct audio_device *retp)
934 {
935
936 *retp = neo_device;
937 return 0;
938 }
939
940 static int
941 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
942 {
943 struct neo_softc *sc;
944
945 sc = addr;
946 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
947 }
948
949 static int
950 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
951 {
952 struct neo_softc *sc;
953
954 sc = addr;
955 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
956 }
957
958 static int
959 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
960 {
961 struct neo_softc *sc;
962
963 sc = addr;
964 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
965 }
966
967 static void *
968 neo_malloc(void *addr, int direction, size_t size)
969 {
970 struct neo_softc *sc;
971 void *rv;
972
973 sc = addr;
974 rv = NULL;
975 switch (direction) {
976 case AUMODE_PLAY:
977 if (sc->pbuf_allocated == 0) {
978 rv = (void *) sc->pbuf_vaddr;
979 sc->pbuf_allocated = 1;
980 }
981 break;
982
983 case AUMODE_RECORD:
984 if (sc->rbuf_allocated == 0) {
985 rv = (void *) sc->rbuf_vaddr;
986 sc->rbuf_allocated = 1;
987 }
988 break;
989 }
990
991 return rv;
992 }
993
994 static void
995 neo_free(void *addr, void *ptr, size_t size)
996 {
997 struct neo_softc *sc;
998 vaddr_t v;
999
1000 sc = addr;
1001 v = (vaddr_t)ptr;
1002 if (v == sc->pbuf_vaddr)
1003 sc->pbuf_allocated = 0;
1004 else if (v == sc->rbuf_vaddr)
1005 sc->rbuf_allocated = 0;
1006 else
1007 printf("neo_free: bad address %p\n", ptr);
1008 }
1009
1010 static size_t
1011 neo_round_buffersize(void *addr, int direction, size_t size)
1012 {
1013
1014 return NM_BUFFSIZE;
1015 }
1016
1017 static paddr_t
1018 neo_mappage(void *addr, void *mem, off_t off, int prot)
1019 {
1020 struct neo_softc *sc;
1021 vaddr_t v;
1022 bus_addr_t pciaddr;
1023
1024 sc = addr;
1025 v = (vaddr_t)mem;
1026 if (v == sc->pbuf_vaddr)
1027 pciaddr = sc->pbuf_pciaddr;
1028 else if (v == sc->rbuf_vaddr)
1029 pciaddr = sc->rbuf_pciaddr;
1030 else
1031 return -1;
1032
1033 return bus_space_mmap(sc->bufiot, pciaddr, off, prot,
1034 BUS_SPACE_MAP_LINEAR);
1035 }
1036
1037 static int
1038 neo_get_props(void *addr)
1039 {
1040
1041 return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
1042 AUDIO_PROP_FULLDUPLEX;
1043 }
1044
1045 static void
1046 neo_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
1047 {
1048 struct neo_softc *sc;
1049
1050 sc = addr;
1051 *intr = &sc->intr_lock;
1052 *thread = &sc->lock;
1053 }
1054