neo.c revision 1.52 1 /* $NetBSD: neo.c,v 1.52 2019/03/16 12:09:58 isaki 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.52 2019/03/16 12:09:58 isaki 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 .query_encoding = neo_query_encoding,
247 .set_params = neo_set_params,
248 .round_blocksize = neo_round_blocksize,
249 .halt_output = neo_halt_output,
250 .halt_input = neo_halt_input,
251 .getdev = neo_getdev,
252 .set_port = neo_mixer_set_port,
253 .get_port = neo_mixer_get_port,
254 .query_devinfo = neo_query_devinfo,
255 .allocm = neo_malloc,
256 .freem = neo_free,
257 .round_buffersize = neo_round_buffersize,
258 .mappage = neo_mappage,
259 .get_props = neo_get_props,
260 .trigger_output = neo_trigger_output,
261 .trigger_input = neo_trigger_input,
262 .get_locks = neo_get_locks,
263 };
264
265 /* -------------------------------------------------------------------- */
266
267 #define nm_rd_1(sc, regno) \
268 bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
269
270 #define nm_rd_2(sc, regno) \
271 bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
272
273 #define nm_rd_4(sc, regno) \
274 bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
275
276 #define nm_wr_1(sc, regno, val) \
277 bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
278
279 #define nm_wr_2(sc, regno, val) \
280 bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
281
282 #define nm_wr_4(sc, regno, val) \
283 bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
284
285 #define nm_rdbuf_4(sc, regno) \
286 bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
287
288 #define nm_wrbuf_1(sc, regno, val) \
289 bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
290
291 /* ac97 codec */
292 static int
293 nm_waitcd(struct neo_softc *sc)
294 {
295 int cnt;
296 int fail;
297
298 cnt = 10;
299 fail = 1;
300 while (cnt-- > 0) {
301 if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
302 DELAY(100);
303 else {
304 fail = 0;
305 break;
306 }
307 }
308 return fail;
309 }
310
311 static void
312 nm_ackint(struct neo_softc *sc, uint32_t num)
313 {
314
315 switch (sc->type) {
316 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
317 nm_wr_2(sc, NM_INT_REG, num << 1);
318 break;
319
320 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
321 nm_wr_4(sc, NM_INT_REG, num);
322 break;
323 }
324 }
325
326 static int
327 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
328 {
329 int ofs, sz, i;
330 uint32_t addr;
331
332 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
333 if (dir == AUMODE_RECORD)
334 num += 8;
335 sz = coefficientSizes[num];
336 ofs = 0;
337 while (num-- > 0)
338 ofs+= coefficientSizes[num];
339 for (i = 0; i < sz; i++)
340 nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
341 nm_wr_4(sc, addr, sc->cbuf);
342 if (dir == AUMODE_PLAY)
343 sz--;
344 nm_wr_4(sc, addr + 4, sc->cbuf + sz);
345 return 0;
346 }
347
348 /* The interrupt handler */
349 static int
350 neo_intr(void *p)
351 {
352 struct neo_softc *sc;
353 int status, x;
354 int rv;
355
356 sc = (struct neo_softc *)p;
357 mutex_spin_enter(&sc->intr_lock);
358
359 rv = 0;
360 status = (sc->irsz == 2) ?
361 nm_rd_2(sc, NM_INT_REG) :
362 nm_rd_4(sc, NM_INT_REG);
363
364 if (status & sc->playint) {
365 status &= ~sc->playint;
366
367 sc->pwmark += sc->pblksize;
368 sc->pwmark %= sc->pbufsize;
369
370 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
371
372 nm_ackint(sc, sc->playint);
373
374 if (sc->pintr)
375 (*sc->pintr)(sc->parg);
376
377 rv = 1;
378 }
379 if (status & sc->recint) {
380 status &= ~sc->recint;
381
382 sc->rwmark += sc->rblksize;
383 sc->rwmark %= sc->rbufsize;
384 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
385 nm_ackint(sc, sc->recint);
386 if (sc->rintr)
387 (*sc->rintr)(sc->rarg);
388
389 rv = 1;
390 }
391 if (status & sc->misc1int) {
392 status &= ~sc->misc1int;
393 nm_ackint(sc, sc->misc1int);
394 x = nm_rd_1(sc, 0x400);
395 nm_wr_1(sc, 0x400, x | 2);
396 printf("%s: misc int 1\n", device_xname(sc->dev));
397 rv = 1;
398 }
399 if (status & sc->misc2int) {
400 status &= ~sc->misc2int;
401 nm_ackint(sc, sc->misc2int);
402 x = nm_rd_1(sc, 0x400);
403 nm_wr_1(sc, 0x400, x & ~2);
404 printf("%s: misc int 2\n", device_xname(sc->dev));
405 rv = 1;
406 }
407 if (status) {
408 status &= ~sc->misc2int;
409 nm_ackint(sc, sc->misc2int);
410 printf("%s: unknown int\n", device_xname(sc->dev));
411 rv = 1;
412 }
413
414 mutex_spin_exit(&sc->intr_lock);
415 return rv;
416 }
417
418 /* -------------------------------------------------------------------- */
419
420 /*
421 * Probe and attach the card
422 */
423
424 static int
425 nm_init(struct neo_softc *sc)
426 {
427 uint32_t ofs, i;
428
429 switch (sc->type) {
430 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
431 sc->ac97_base = NM_MIXER_OFFSET;
432 sc->ac97_status = NM_MIXER_STATUS_OFFSET;
433 sc->ac97_busy = NM_MIXER_READY_MASK;
434
435 sc->buftop = 2560 * 1024;
436
437 sc->irsz = 2;
438 sc->playint = NM_PLAYBACK_INT;
439 sc->recint = NM_RECORD_INT;
440 sc->misc1int = NM_MISC_INT_1;
441 sc->misc2int = NM_MISC_INT_2;
442 break;
443
444 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
445 sc->ac97_base = NM_MIXER_OFFSET;
446 sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
447 sc->ac97_busy = NM2_MIXER_READY_MASK;
448
449 sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
450
451 sc->irsz = 4;
452 sc->playint = NM2_PLAYBACK_INT;
453 sc->recint = NM2_RECORD_INT;
454 sc->misc1int = NM2_MISC_INT_1;
455 sc->misc2int = NM2_MISC_INT_2;
456 break;
457 #ifdef DIAGNOSTIC
458 default:
459 panic("nm_init: impossible");
460 #endif
461 }
462
463 sc->badintr = 0;
464 ofs = sc->buftop - 0x0400;
465 sc->buftop -= 0x1400;
466
467 if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
468 i = nm_rdbuf_4(sc, ofs + 4);
469 if (i != 0 && i != 0xffffffff)
470 sc->buftop = i;
471 }
472
473 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
474 sc->rbuf = sc->cbuf - NM_BUFFSIZE;
475 sc->pbuf = sc->rbuf - NM_BUFFSIZE;
476 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
477
478 sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
479 sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
480 sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
481
482 sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
483 sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
484
485 nm_wr_1(sc, 0, 0x11);
486 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
487 nm_wr_2(sc, 0x214, 0);
488
489 return 0;
490 }
491
492 static int
493 neo_match(device_t parent, cfdata_t match, void *aux)
494 {
495 struct pci_attach_args *pa;
496 pcireg_t subdev;
497
498 pa = aux;
499 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
500 return 0;
501
502 subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
503
504 switch (PCI_PRODUCT(pa->pa_id)) {
505 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
506 /*
507 * We have to weed-out the non-AC'97 versions of
508 * the chip (i.e. the ones that are known to work
509 * in WSS emulation mode), as they won't work with
510 * this driver.
511 */
512 switch (PCI_VENDOR(subdev)) {
513 case PCI_VENDOR_DELL:
514 switch (PCI_PRODUCT(subdev)) {
515 case 0x008f:
516 return 0;
517 }
518 break;
519
520 case PCI_VENDOR_HP:
521 switch (PCI_PRODUCT(subdev)) {
522 case 0x0007:
523 return 0;
524 }
525 break;
526
527 case PCI_VENDOR_IBM:
528 switch (PCI_PRODUCT(subdev)) {
529 case 0x00dd:
530 return 0;
531 }
532 break;
533 }
534 return 1;
535
536 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
537 return 1;
538 }
539
540 return 0;
541 }
542
543 static bool
544 neo_resume(device_t dv, const pmf_qual_t *qual)
545 {
546 struct neo_softc *sc = device_private(dv);
547
548 mutex_enter(&sc->lock);
549 mutex_spin_enter(&sc->intr_lock);
550 nm_init(sc);
551 mutex_spin_exit(&sc->intr_lock);
552 sc->codec_if->vtbl->restore_ports(sc->codec_if);
553 mutex_exit(&sc->lock);
554
555 return true;
556 }
557
558 static void
559 neo_attach(device_t parent, device_t self, void *aux)
560 {
561 struct neo_softc *sc;
562 struct pci_attach_args *pa;
563 pci_chipset_tag_t pc;
564 char const *intrstr;
565 pci_intr_handle_t ih;
566 pcireg_t csr;
567 int error;
568 char intrbuf[PCI_INTRSTR_LEN];
569
570 sc = device_private(self);
571 pa = aux;
572 pc = pa->pa_pc;
573
574 sc->type = PCI_PRODUCT(pa->pa_id);
575
576 printf(": NeoMagic 256%s audio\n",
577 sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
578
579 /* Map I/O register */
580 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
581 &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
582 aprint_error_dev(self, "can't map buffer\n");
583 return;
584 }
585
586 if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
587 BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) {
588 aprint_error_dev(self, "can't map registers\n");
589 return;
590 }
591
592 /* Map and establish the interrupt. */
593 if (pci_intr_map(pa, &ih)) {
594 aprint_error_dev(self, "couldn't map interrupt\n");
595 return;
596 }
597
598 mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
599 mutex_init(&sc->intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
600
601 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
602 sc->ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, neo_intr, sc,
603 device_xname(self));
604
605 if (sc->ih == NULL) {
606 aprint_error_dev(self, "couldn't establish interrupt");
607 if (intrstr != NULL)
608 aprint_error(" at %s", intrstr);
609 aprint_error("\n");
610 mutex_destroy(&sc->lock);
611 mutex_destroy(&sc->intr_lock);
612 return;
613 }
614 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
615
616 mutex_spin_enter(&sc->intr_lock);
617 error = nm_init(sc);
618 mutex_spin_exit(&sc->intr_lock);
619 if (error != 0) {
620 mutex_destroy(&sc->lock);
621 mutex_destroy(&sc->intr_lock);
622 return;
623 }
624
625 /* Enable the device. */
626 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
627 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
628 csr | PCI_COMMAND_MASTER_ENABLE);
629
630 sc->host_if.arg = sc;
631 sc->host_if.attach = neo_attach_codec;
632 sc->host_if.read = neo_read_codec;
633 sc->host_if.write = neo_write_codec;
634 sc->host_if.reset = neo_reset_codec;
635 sc->host_if.flags = neo_flags_codec;
636
637 if (ac97_attach(&sc->host_if, self, &sc->lock) != 0) {
638 mutex_destroy(&sc->lock);
639 mutex_destroy(&sc->intr_lock);
640 return;
641 }
642
643 if (!pmf_device_register(self, NULL, neo_resume))
644 aprint_error_dev(self, "couldn't establish power handler\n");
645
646 audio_attach_mi(&neo_hw_if, sc, self);
647 }
648
649 static int
650 neo_read_codec(void *v, uint8_t a, uint16_t *d)
651 {
652 struct neo_softc *sc;
653
654 sc = v;
655 if (!nm_waitcd(sc)) {
656 *d = nm_rd_2(sc, sc->ac97_base + a);
657 DELAY(1000);
658 return 0;
659 }
660
661 return ENXIO;
662 }
663
664
665 static int
666 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
667 {
668 struct neo_softc *sc;
669 int cnt;
670
671 sc = v;
672 cnt = 3;
673 if (!nm_waitcd(sc)) {
674 while (cnt-- > 0) {
675 nm_wr_2(sc, sc->ac97_base + a, d);
676 if (!nm_waitcd(sc)) {
677 DELAY(1000);
678 return 0;
679 }
680 }
681 }
682
683 return ENXIO;
684 }
685
686 static int
687 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
688 {
689 struct neo_softc *sc;
690
691 sc = v;
692 sc->codec_if = codec_if;
693 return 0;
694 }
695
696 static int
697 neo_reset_codec(void *v)
698 {
699 struct neo_softc *sc;
700
701 sc = v;
702 nm_wr_1(sc, 0x6c0, 0x01);
703 nm_wr_1(sc, 0x6cc, 0x87);
704 nm_wr_1(sc, 0x6cc, 0x80);
705 nm_wr_1(sc, 0x6cc, 0x00);
706 return 0;
707 }
708
709 static enum ac97_host_flags
710 neo_flags_codec(void *v)
711 {
712
713 return AC97_HOST_DONT_READ;
714 }
715
716 static int
717 neo_query_encoding(void *addr, struct audio_encoding *fp)
718 {
719
720 switch (fp->index) {
721 case 0:
722 strcpy(fp->name, AudioEulinear);
723 fp->encoding = AUDIO_ENCODING_ULINEAR;
724 fp->precision = 8;
725 fp->flags = 0;
726 return 0;
727 case 1:
728 strcpy(fp->name, AudioEmulaw);
729 fp->encoding = AUDIO_ENCODING_ULAW;
730 fp->precision = 8;
731 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
732 return 0;
733 case 2:
734 strcpy(fp->name, AudioEalaw);
735 fp->encoding = AUDIO_ENCODING_ALAW;
736 fp->precision = 8;
737 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
738 return 0;
739 case 3:
740 strcpy(fp->name, AudioEslinear);
741 fp->encoding = AUDIO_ENCODING_SLINEAR;
742 fp->precision = 8;
743 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
744 return (0);
745 case 4:
746 strcpy(fp->name, AudioEslinear_le);
747 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
748 fp->precision = 16;
749 fp->flags = 0;
750 return 0;
751 case 5:
752 strcpy(fp->name, AudioEulinear_le);
753 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
754 fp->precision = 16;
755 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
756 return 0;
757 case 6:
758 strcpy(fp->name, AudioEslinear_be);
759 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
760 fp->precision = 16;
761 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
762 return 0;
763 case 7:
764 strcpy(fp->name, AudioEulinear_be);
765 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
766 fp->precision = 16;
767 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
768 return 0;
769 default:
770 return EINVAL;
771 }
772 }
773
774 /* Todo: don't commit settings to card until we've verified all parameters */
775 static int
776 neo_set_params(void *addr, int setmode, int usemode,
777 audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
778 stream_filter_list_t *rfil)
779 {
780 struct neo_softc *sc;
781 audio_params_t *p;
782 stream_filter_list_t *fil;
783 uint32_t base;
784 uint8_t x;
785 int mode, i;
786
787 sc = addr;
788 for (mode = AUMODE_RECORD; mode != -1;
789 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
790 if ((setmode & mode) == 0)
791 continue;
792
793 p = mode == AUMODE_PLAY ? play : rec;
794
795 if (p == NULL) continue;
796
797 for (x = 0; x < 8; x++) {
798 if (p->sample_rate <
799 (samplerates[x] + samplerates[x + 1]) / 2)
800 break;
801 }
802 if (x == 8)
803 return EINVAL;
804
805 p->sample_rate = samplerates[x];
806 nm_loadcoeff(sc, mode, x);
807
808 x <<= 4;
809 x &= NM_RATE_MASK;
810 if (p->precision == 16)
811 x |= NM_RATE_BITS_16;
812 if (p->channels == 2)
813 x |= NM_RATE_STEREO;
814
815 base = (mode == AUMODE_PLAY)?
816 NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
817 nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
818
819 fil = mode == AUMODE_PLAY ? pfil : rfil;
820 i = auconv_set_converter(neo_formats, NEO_NFORMATS,
821 mode, p, FALSE, fil);
822 if (i < 0)
823 return EINVAL;
824 }
825
826 return 0;
827 }
828
829 static int
830 neo_round_blocksize(void *addr, int blk, int mode,
831 const audio_params_t *param)
832 {
833
834 return NM_BUFFSIZE / 2;
835 }
836
837 static int
838 neo_trigger_output(void *addr, void *start, void *end, int blksize,
839 void (*intr)(void *), void *arg, const audio_params_t *param)
840 {
841 struct neo_softc *sc;
842 int ssz;
843
844 sc = addr;
845 sc->pintr = intr;
846 sc->parg = arg;
847
848 ssz = (param->precision == 16) ? 2 : 1;
849 if (param->channels == 2)
850 ssz <<= 1;
851
852 sc->pbufsize = ((char*)end - (char *)start);
853 sc->pblksize = blksize;
854 sc->pwmark = blksize;
855
856 nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
857 nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
858 nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
859 nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
860 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
861 NM_PLAYBACK_ENABLE_FLAG);
862 nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
863
864 return 0;
865 }
866
867 static int
868 neo_trigger_input(void *addr, void *start, void *end, int blksize,
869 void (*intr)(void *), void *arg, const audio_params_t *param)
870 {
871 struct neo_softc *sc;
872 int ssz;
873
874 sc = addr;
875 sc->rintr = intr;
876 sc->rarg = arg;
877
878 ssz = (param->precision == 16) ? 2 : 1;
879 if (param->channels == 2)
880 ssz <<= 1;
881
882 sc->rbufsize = ((char*)end - (char *)start);
883 sc->rblksize = blksize;
884 sc->rwmark = blksize;
885
886 nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
887 nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
888 nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
889 nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
890 nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
891 NM_RECORD_ENABLE_FLAG);
892
893 return 0;
894 }
895
896 static int
897 neo_halt_output(void *addr)
898 {
899 struct neo_softc *sc;
900
901 sc = (struct neo_softc *)addr;
902 nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
903 nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
904 sc->pintr = 0;
905
906 return 0;
907 }
908
909 static int
910 neo_halt_input(void *addr)
911 {
912 struct neo_softc *sc;
913
914 sc = (struct neo_softc *)addr;
915 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
916 sc->rintr = 0;
917
918 return 0;
919 }
920
921 static int
922 neo_getdev(void *addr, struct audio_device *retp)
923 {
924
925 *retp = neo_device;
926 return 0;
927 }
928
929 static int
930 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
931 {
932 struct neo_softc *sc;
933
934 sc = addr;
935 return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
936 }
937
938 static int
939 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
940 {
941 struct neo_softc *sc;
942
943 sc = addr;
944 return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
945 }
946
947 static int
948 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
949 {
950 struct neo_softc *sc;
951
952 sc = addr;
953 return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
954 }
955
956 static void *
957 neo_malloc(void *addr, int direction, size_t size)
958 {
959 struct neo_softc *sc;
960 void *rv;
961
962 sc = addr;
963 rv = NULL;
964 switch (direction) {
965 case AUMODE_PLAY:
966 if (sc->pbuf_allocated == 0) {
967 rv = (void *) sc->pbuf_vaddr;
968 sc->pbuf_allocated = 1;
969 }
970 break;
971
972 case AUMODE_RECORD:
973 if (sc->rbuf_allocated == 0) {
974 rv = (void *) sc->rbuf_vaddr;
975 sc->rbuf_allocated = 1;
976 }
977 break;
978 }
979
980 return rv;
981 }
982
983 static void
984 neo_free(void *addr, void *ptr, size_t size)
985 {
986 struct neo_softc *sc;
987 vaddr_t v;
988
989 sc = addr;
990 v = (vaddr_t)ptr;
991 if (v == sc->pbuf_vaddr)
992 sc->pbuf_allocated = 0;
993 else if (v == sc->rbuf_vaddr)
994 sc->rbuf_allocated = 0;
995 else
996 printf("neo_free: bad address %p\n", ptr);
997 }
998
999 static size_t
1000 neo_round_buffersize(void *addr, int direction, size_t size)
1001 {
1002
1003 return NM_BUFFSIZE;
1004 }
1005
1006 static paddr_t
1007 neo_mappage(void *addr, void *mem, off_t off, int prot)
1008 {
1009 struct neo_softc *sc;
1010 vaddr_t v;
1011 bus_addr_t pciaddr;
1012
1013 sc = addr;
1014 v = (vaddr_t)mem;
1015 if (v == sc->pbuf_vaddr)
1016 pciaddr = sc->pbuf_pciaddr;
1017 else if (v == sc->rbuf_vaddr)
1018 pciaddr = sc->rbuf_pciaddr;
1019 else
1020 return -1;
1021
1022 return bus_space_mmap(sc->bufiot, pciaddr, off, prot,
1023 BUS_SPACE_MAP_LINEAR);
1024 }
1025
1026 static int
1027 neo_get_props(void *addr)
1028 {
1029
1030 return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
1031 AUDIO_PROP_FULLDUPLEX;
1032 }
1033
1034 static void
1035 neo_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
1036 {
1037 struct neo_softc *sc;
1038
1039 sc = addr;
1040 *intr = &sc->intr_lock;
1041 *thread = &sc->lock;
1042 }
1043