neo.c revision 1.39.16.2 1 /* $NetBSD: neo.c,v 1.39.16.2 2008/12/12 23:06:58 ad 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.39.16.2 2008/12/12 23:06:58 ad 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 secodn 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 struct device 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(struct device *, struct cfdata *, void *);
177 static void neo_attach(struct device *, struct device *, 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(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 NULL,
274 neo_get_locks,
275 };
276
277 /* -------------------------------------------------------------------- */
278
279 #define nm_rd_1(sc, regno) \
280 bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
281
282 #define nm_rd_2(sc, regno) \
283 bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
284
285 #define nm_rd_4(sc, regno) \
286 bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
287
288 #define nm_wr_1(sc, regno, val) \
289 bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
290
291 #define nm_wr_2(sc, regno, val) \
292 bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
293
294 #define nm_wr_4(sc, regno, val) \
295 bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
296
297 #define nm_rdbuf_4(sc, regno) \
298 bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
299
300 #define nm_wrbuf_1(sc, regno, val) \
301 bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
302
303 /* ac97 codec */
304 static int
305 nm_waitcd(struct neo_softc *sc)
306 {
307 int cnt;
308 int fail;
309
310 cnt = 10;
311 fail = 1;
312 while (cnt-- > 0) {
313 if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
314 DELAY(100);
315 else {
316 fail = 0;
317 break;
318 }
319 }
320 return fail;
321 }
322
323 static void
324 nm_ackint(struct neo_softc *sc, uint32_t num)
325 {
326
327 switch (sc->type) {
328 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
329 nm_wr_2(sc, NM_INT_REG, num << 1);
330 break;
331
332 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
333 nm_wr_4(sc, NM_INT_REG, num);
334 break;
335 }
336 }
337
338 static int
339 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
340 {
341 int ofs, sz, i;
342 uint32_t addr;
343
344 addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
345 if (dir == AUMODE_RECORD)
346 num += 8;
347 sz = coefficientSizes[num];
348 ofs = 0;
349 while (num-- > 0)
350 ofs+= coefficientSizes[num];
351 for (i = 0; i < sz; i++)
352 nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
353 nm_wr_4(sc, addr, sc->cbuf);
354 if (dir == AUMODE_PLAY)
355 sz--;
356 nm_wr_4(sc, addr + 4, sc->cbuf + sz);
357 return 0;
358 }
359
360 /* The interrupt handler */
361 static int
362 neo_intr(void *p)
363 {
364 struct neo_softc *sc;
365 int status, x;
366 int rv;
367
368 sc = (struct neo_softc *)p;
369 mutex_spin_enter(&sc->intr_lock);
370
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", device_xname(&sc->dev));
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", device_xname(&sc->dev));
417 rv = 1;
418 }
419 if (status) {
420 status &= ~sc->misc2int;
421 nm_ackint(sc, sc->misc2int);
422 printf("%s: unknown int\n", device_xname(&sc->dev));
423 rv = 1;
424 }
425
426 mutex_spin_exit(&sc->intr_lock);
427 return rv;
428 }
429
430 /* -------------------------------------------------------------------- */
431
432 /*
433 * Probe and attach the card
434 */
435
436 static int
437 nm_init(struct neo_softc *sc)
438 {
439 uint32_t ofs, i;
440
441 switch (sc->type) {
442 case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
443 sc->ac97_base = NM_MIXER_OFFSET;
444 sc->ac97_status = NM_MIXER_STATUS_OFFSET;
445 sc->ac97_busy = NM_MIXER_READY_MASK;
446
447 sc->buftop = 2560 * 1024;
448
449 sc->irsz = 2;
450 sc->playint = NM_PLAYBACK_INT;
451 sc->recint = NM_RECORD_INT;
452 sc->misc1int = NM_MISC_INT_1;
453 sc->misc2int = NM_MISC_INT_2;
454 break;
455
456 case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
457 sc->ac97_base = NM_MIXER_OFFSET;
458 sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
459 sc->ac97_busy = NM2_MIXER_READY_MASK;
460
461 sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
462
463 sc->irsz = 4;
464 sc->playint = NM2_PLAYBACK_INT;
465 sc->recint = NM2_RECORD_INT;
466 sc->misc1int = NM2_MISC_INT_1;
467 sc->misc2int = NM2_MISC_INT_2;
468 break;
469 #ifdef DIAGNOSTIC
470 default:
471 panic("nm_init: impossible");
472 #endif
473 }
474
475 sc->badintr = 0;
476 ofs = sc->buftop - 0x0400;
477 sc->buftop -= 0x1400;
478
479 if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
480 i = nm_rdbuf_4(sc, ofs + 4);
481 if (i != 0 && i != 0xffffffff)
482 sc->buftop = i;
483 }
484
485 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
486 sc->rbuf = sc->cbuf - NM_BUFFSIZE;
487 sc->pbuf = sc->rbuf - NM_BUFFSIZE;
488 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
489
490 sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
491 sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
492 sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
493
494 sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
495 sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
496
497 nm_wr_1(sc, 0, 0x11);
498 nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
499 nm_wr_2(sc, 0x214, 0);
500
501 return 0;
502 }
503
504 static int
505 neo_match(struct device *parent, struct cfdata *match, 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 bool
556 neo_resume(device_t dv PMF_FN_ARGS)
557 {
558 struct neo_softc *sc = device_private(dv);
559
560 mutex_enter(&sc->lock);
561 mutex_spin_enter(&sc->intr_lock);
562 nm_init(sc);
563 mutex_spin_exit(&sc->intr_lock);
564 sc->codec_if->vtbl->restore_ports(sc->codec_if);
565 mutex_exit(&sc->lock);
566
567 return true;
568 }
569
570 static void
571 neo_attach(struct device *parent, struct device *self, void *aux)
572 {
573 struct neo_softc *sc;
574 struct pci_attach_args *pa;
575 pci_chipset_tag_t pc;
576 char const *intrstr;
577 pci_intr_handle_t ih;
578 pcireg_t csr;
579 int error;
580
581 sc = (struct neo_softc *)self;
582 pa = (struct pci_attach_args *)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(&sc->dev, "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(&sc->dev, "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(&sc->dev, "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_SCHED);
611
612 intrstr = pci_intr_string(pc, ih);
613 sc->ih = pci_intr_establish(pc, ih, IPL_SCHED, neo_intr, sc);
614
615 if (sc->ih == NULL) {
616 aprint_error_dev(&sc->dev, "couldn't establish interrupt");
617 if (intrstr != NULL)
618 printf(" at %s", intrstr);
619 printf("\n");
620 mutex_destroy(&sc->lock);
621 mutex_destroy(&sc->intr_lock);
622 return;
623 }
624 printf("%s: interrupting at %s\n", device_xname(&sc->dev), intrstr);
625
626 /* Enable the device. */
627 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
628 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
629 csr | PCI_COMMAND_MASTER_ENABLE);
630
631 mutex_spin_enter(&sc->intr_lock);
632 error = nm_init(sc);
633 mutex_spin_exit(&sc->intr_lock);
634
635 if (error != 0) {
636 mutex_destroy(&sc->lock);
637 mutex_destroy(&sc->intr_lock);
638 return;
639 }
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, &sc->dev);
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