2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
7 * Derived from the public domain Linux driver
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include <dev/sound/pcm/sound.h>
36 #include <dev/sound/pcm/ac97.h>
37 #include <dev/sound/pci/neomagic.h>
38 #include <dev/sound/pci/neomagic-coeff.h>
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
43 SND_DECLARE_FILE("$FreeBSD$");
45 /* -------------------------------------------------------------------- */
47 #define NM_BUFFSIZE 16384
49 #define NM256AV_PCI_ID 0x800510c8
50 #define NM256ZX_PCI_ID 0x800610c8
54 /* channel registers */
56 int active, spd, dir, fmt;
57 u_int32_t blksize, wmark;
58 struct snd_dbuf *buffer;
59 struct pcm_channel *channel;
60 struct sc_info *parent;
63 /* device private data */
68 struct resource *reg, *irq, *buf;
69 int regid, irqid, bufid;
72 u_int32_t ac97_base, ac97_status, ac97_busy;
73 u_int32_t buftop, pbuf, rbuf, cbuf, acbuf;
74 u_int32_t playint, recint, misc1int, misc2int;
75 u_int32_t irsz, badintr;
77 struct sc_chinfo pch, rch;
80 /* -------------------------------------------------------------------- */
87 static int nm_loadcoeff(struct sc_info *sc, int dir, int num);
88 static int nm_setch(struct sc_chinfo *ch);
89 static int nm_init(struct sc_info *);
90 static void nm_intr(void *);
92 /* talk to the card */
93 static u_int32_t nm_rd(struct sc_info *, int, int);
94 static void nm_wr(struct sc_info *, int, u_int32_t, int);
95 static u_int32_t nm_rdbuf(struct sc_info *, int, int);
96 static void nm_wrbuf(struct sc_info *, int, u_int32_t, int);
98 static u_int32_t badcards[] = {
104 #define NUM_BADCARDS (sizeof(badcards) / sizeof(u_int32_t))
106 /* The actual rates supported by the card. */
107 static int samplerates[9] = {
119 /* -------------------------------------------------------------------- */
121 static u_int32_t nm_fmt[] = {
122 SND_FORMAT(AFMT_U8, 1, 0),
123 SND_FORMAT(AFMT_U8, 2, 0),
124 SND_FORMAT(AFMT_S16_LE, 1, 0),
125 SND_FORMAT(AFMT_S16_LE, 2, 0),
128 static struct pcmchan_caps nm_caps = {4000, 48000, nm_fmt, 0};
130 /* -------------------------------------------------------------------- */
134 nm_rd(struct sc_info *sc, int regno, int size)
136 bus_space_tag_t st = rman_get_bustag(sc->reg);
137 bus_space_handle_t sh = rman_get_bushandle(sc->reg);
141 return bus_space_read_1(st, sh, regno);
143 return bus_space_read_2(st, sh, regno);
145 return bus_space_read_4(st, sh, regno);
152 nm_wr(struct sc_info *sc, int regno, u_int32_t data, int size)
154 bus_space_tag_t st = rman_get_bustag(sc->reg);
155 bus_space_handle_t sh = rman_get_bushandle(sc->reg);
159 bus_space_write_1(st, sh, regno, data);
162 bus_space_write_2(st, sh, regno, data);
165 bus_space_write_4(st, sh, regno, data);
171 nm_rdbuf(struct sc_info *sc, int regno, int size)
173 bus_space_tag_t st = rman_get_bustag(sc->buf);
174 bus_space_handle_t sh = rman_get_bushandle(sc->buf);
178 return bus_space_read_1(st, sh, regno);
180 return bus_space_read_2(st, sh, regno);
182 return bus_space_read_4(st, sh, regno);
189 nm_wrbuf(struct sc_info *sc, int regno, u_int32_t data, int size)
191 bus_space_tag_t st = rman_get_bustag(sc->buf);
192 bus_space_handle_t sh = rman_get_bushandle(sc->buf);
196 bus_space_write_1(st, sh, regno, data);
199 bus_space_write_2(st, sh, regno, data);
202 bus_space_write_4(st, sh, regno, data);
207 /* -------------------------------------------------------------------- */
210 nm_waitcd(struct sc_info *sc)
216 if (nm_rd(sc, sc->ac97_status, 2) & sc->ac97_busy) {
227 nm_initcd(kobj_t obj, void *devinfo)
229 struct sc_info *sc = (struct sc_info *)devinfo;
231 nm_wr(sc, 0x6c0, 0x01, 1);
234 * The following code-line may cause a hang for some chipsets, see
236 * In case of a bugreport without this line have a look at the PR and
237 * conditionize the code-line based upon the specific version of
240 nm_wr(sc, 0x6cc, 0x87, 1);
242 nm_wr(sc, 0x6cc, 0x80, 1);
243 nm_wr(sc, 0x6cc, 0x00, 1);
248 nm_rdcd(kobj_t obj, void *devinfo, int regno)
250 struct sc_info *sc = (struct sc_info *)devinfo;
253 if (!nm_waitcd(sc)) {
254 x = nm_rd(sc, sc->ac97_base + regno, 2);
258 device_printf(sc->dev, "ac97 codec not ready\n");
264 nm_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
266 struct sc_info *sc = (struct sc_info *)devinfo;
269 if (!nm_waitcd(sc)) {
271 nm_wr(sc, sc->ac97_base + regno, data, 2);
272 if (!nm_waitcd(sc)) {
278 device_printf(sc->dev, "ac97 codec not ready\n");
282 static kobj_method_t nm_ac97_methods[] = {
283 KOBJMETHOD(ac97_init, nm_initcd),
284 KOBJMETHOD(ac97_read, nm_rdcd),
285 KOBJMETHOD(ac97_write, nm_wrcd),
288 AC97_DECLARE(nm_ac97);
290 /* -------------------------------------------------------------------- */
293 nm_ackint(struct sc_info *sc, u_int32_t num)
295 if (sc->type == NM256AV_PCI_ID) {
296 nm_wr(sc, NM_INT_REG, num << 1, 2);
297 } else if (sc->type == NM256ZX_PCI_ID) {
298 nm_wr(sc, NM_INT_REG, num, 4);
303 nm_loadcoeff(struct sc_info *sc, int dir, int num)
308 addr = (dir == PCMDIR_PLAY)? 0x01c : 0x21c;
309 if (dir == PCMDIR_REC)
311 sz = coefficientSizes[num];
314 ofs+= coefficientSizes[num];
315 for (i = 0; i < sz; i++)
316 nm_wrbuf(sc, sc->cbuf + i, coefficients[ofs + i], 1);
317 nm_wr(sc, addr, sc->cbuf, 4);
318 if (dir == PCMDIR_PLAY)
320 nm_wr(sc, addr + 4, sc->cbuf + sz, 4);
325 nm_setch(struct sc_chinfo *ch)
327 struct sc_info *sc = ch->parent;
331 for (x = 0; x < 8; x++)
332 if (ch->spd < (samplerates[x] + samplerates[x + 1]) / 2)
335 if (x == 8) return 1;
337 ch->spd = samplerates[x];
338 nm_loadcoeff(sc, ch->dir, x);
342 if (ch->fmt & AFMT_16BIT) x |= NM_RATE_BITS_16;
343 if (AFMT_CHANNEL(ch->fmt) > 1) x |= NM_RATE_STEREO;
345 base = (ch->dir == PCMDIR_PLAY)? NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
346 nm_wr(sc, base + NM_RATE_REG_OFFSET, x, 1);
350 /* channel interface */
352 nmchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
354 struct sc_info *sc = devinfo;
355 struct sc_chinfo *ch;
358 chnbuf = (dir == PCMDIR_PLAY)? sc->pbuf : sc->rbuf;
359 ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
364 sndbuf_setup(ch->buffer, (u_int8_t *)rman_get_virtual(sc->buf) + chnbuf, NM_BUFFSIZE);
366 device_printf(sc->dev, "%s buf %p\n", (dir == PCMDIR_PLAY)?
367 "play" : "rec", sndbuf_getbuf(ch->buffer));
375 nmchan_free(kobj_t obj, void *data)
381 nmchan_setformat(kobj_t obj, void *data, u_int32_t format)
383 struct sc_chinfo *ch = data;
390 nmchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
392 struct sc_chinfo *ch = data;
395 return nm_setch(ch)? 0 : ch->spd;
399 nmchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
401 struct sc_chinfo *ch = data;
403 ch->blksize = blocksize;
409 nmchan_trigger(kobj_t obj, void *data, int go)
411 struct sc_chinfo *ch = data;
412 struct sc_info *sc = ch->parent;
415 if (!PCMTRIG_COMMON(go))
418 ssz = (ch->fmt & AFMT_16BIT)? 2 : 1;
419 if (AFMT_CHANNEL(ch->fmt) > 1)
422 if (ch->dir == PCMDIR_PLAY) {
423 if (go == PCMTRIG_START) {
425 ch->wmark = ch->blksize;
426 nm_wr(sc, NM_PBUFFER_START, sc->pbuf, 4);
427 nm_wr(sc, NM_PBUFFER_END, sc->pbuf + NM_BUFFSIZE - ssz, 4);
428 nm_wr(sc, NM_PBUFFER_CURRP, sc->pbuf, 4);
429 nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + ch->wmark, 4);
430 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
431 NM_PLAYBACK_ENABLE_FLAG, 1);
432 nm_wr(sc, NM_AUDIO_MUTE_REG, 0, 2);
435 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, 0, 1);
436 nm_wr(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH, 2);
439 if (go == PCMTRIG_START) {
441 ch->wmark = ch->blksize;
442 nm_wr(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
443 NM_RECORD_ENABLE_FLAG, 1);
444 nm_wr(sc, NM_RBUFFER_START, sc->rbuf, 4);
445 nm_wr(sc, NM_RBUFFER_END, sc->rbuf + NM_BUFFSIZE, 4);
446 nm_wr(sc, NM_RBUFFER_CURRP, sc->rbuf, 4);
447 nm_wr(sc, NM_RBUFFER_WMARK, sc->rbuf + ch->wmark, 4);
450 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1);
457 nmchan_getptr(kobj_t obj, void *data)
459 struct sc_chinfo *ch = data;
460 struct sc_info *sc = ch->parent;
462 if (ch->dir == PCMDIR_PLAY)
463 return nm_rd(sc, NM_PBUFFER_CURRP, 4) - sc->pbuf;
465 return nm_rd(sc, NM_RBUFFER_CURRP, 4) - sc->rbuf;
468 static struct pcmchan_caps *
469 nmchan_getcaps(kobj_t obj, void *data)
474 static kobj_method_t nmchan_methods[] = {
475 KOBJMETHOD(channel_init, nmchan_init),
476 KOBJMETHOD(channel_free, nmchan_free),
477 KOBJMETHOD(channel_setformat, nmchan_setformat),
478 KOBJMETHOD(channel_setspeed, nmchan_setspeed),
479 KOBJMETHOD(channel_setblocksize, nmchan_setblocksize),
480 KOBJMETHOD(channel_trigger, nmchan_trigger),
481 KOBJMETHOD(channel_getptr, nmchan_getptr),
482 KOBJMETHOD(channel_getcaps, nmchan_getcaps),
485 CHANNEL_DECLARE(nmchan);
487 /* The interrupt handler */
491 struct sc_info *sc = (struct sc_info *)p;
494 status = nm_rd(sc, NM_INT_REG, sc->irsz);
498 if (status & sc->playint) {
499 status &= ~sc->playint;
500 sc->pch.wmark += sc->pch.blksize;
501 sc->pch.wmark %= NM_BUFFSIZE;
502 nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pch.wmark, 4);
504 nm_ackint(sc, sc->playint);
505 chn_intr(sc->pch.channel);
507 if (status & sc->recint) {
508 status &= ~sc->recint;
509 sc->rch.wmark += sc->rch.blksize;
510 sc->rch.wmark %= NM_BUFFSIZE;
511 nm_wr(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rch.wmark, 4);
513 nm_ackint(sc, sc->recint);
514 chn_intr(sc->rch.channel);
516 if (status & sc->misc1int) {
517 status &= ~sc->misc1int;
518 nm_ackint(sc, sc->misc1int);
519 x = nm_rd(sc, 0x400, 1);
520 nm_wr(sc, 0x400, x | 2, 1);
521 device_printf(sc->dev, "misc int 1\n");
523 if (status & sc->misc2int) {
524 status &= ~sc->misc2int;
525 nm_ackint(sc, sc->misc2int);
526 x = nm_rd(sc, 0x400, 1);
527 nm_wr(sc, 0x400, x & ~2, 1);
528 device_printf(sc->dev, "misc int 2\n");
531 nm_ackint(sc, status);
532 device_printf(sc->dev, "unknown int\n");
536 /* -------------------------------------------------------------------- */
539 * Probe and attach the card
543 nm_init(struct sc_info *sc)
547 if (sc->type == NM256AV_PCI_ID) {
548 sc->ac97_base = NM_MIXER_OFFSET;
549 sc->ac97_status = NM_MIXER_STATUS_OFFSET;
550 sc->ac97_busy = NM_MIXER_READY_MASK;
552 sc->buftop = 2560 * 1024;
555 sc->playint = NM_PLAYBACK_INT;
556 sc->recint = NM_RECORD_INT;
557 sc->misc1int = NM_MISC_INT_1;
558 sc->misc2int = NM_MISC_INT_2;
559 } else if (sc->type == NM256ZX_PCI_ID) {
560 sc->ac97_base = NM_MIXER_OFFSET;
561 sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
562 sc->ac97_busy = NM2_MIXER_READY_MASK;
564 sc->buftop = (nm_rd(sc, 0xa0b, 2)? 6144 : 4096) * 1024;
567 sc->playint = NM2_PLAYBACK_INT;
568 sc->recint = NM2_RECORD_INT;
569 sc->misc1int = NM2_MISC_INT_1;
570 sc->misc2int = NM2_MISC_INT_2;
573 ofs = sc->buftop - 0x0400;
574 sc->buftop -= 0x1400;
577 device_printf(sc->dev, "buftop is 0x%08x\n", sc->buftop);
578 if ((nm_rdbuf(sc, ofs, 4) & NM_SIG_MASK) == NM_SIGNATURE) {
579 i = nm_rdbuf(sc, ofs + 4, 4);
580 if (i != 0 && i != 0xffffffff) {
582 device_printf(sc->dev, "buftop is changed to 0x%08x\n", i);
587 sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
588 sc->rbuf = sc->cbuf - NM_BUFFSIZE;
589 sc->pbuf = sc->rbuf - NM_BUFFSIZE;
590 sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
592 nm_wr(sc, 0, 0x11, 1);
593 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1);
594 nm_wr(sc, 0x214, 0, 2);
600 nm_pci_probe(device_t dev)
602 struct sc_info *sc = NULL;
606 subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
607 switch (pci_get_devid(dev)) {
610 while ((i < NUM_BADCARDS) && (badcards[i] != subdev))
613 /* Try to catch other non-ac97 cards */
615 if (i == NUM_BADCARDS) {
616 if (!(sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO))) {
617 device_printf(dev, "cannot allocate softc\n");
621 sc->regid = PCIR_BAR(1);
622 sc->reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
627 device_printf(dev, "unable to map register space\n");
633 * My Panasonic CF-M2EV needs resetting device
634 * before checking mixer is present or not.
635 * t.ichinoseki@nifty.com.
637 nm_wr(sc, 0, 0x11, 1); /* reset device */
638 if ((nm_rd(sc, NM_MIXER_PRESENCE, 2) &
639 NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
640 i = 0; /* non-ac97 card, but not listed */
641 DEB(device_printf(dev, "subdev = 0x%x - badcard?\n",
644 bus_release_resource(dev, SYS_RES_MEMORY, sc->regid,
649 if (i == NUM_BADCARDS)
650 s = "NeoMagic 256AV";
652 DEB(device_printf(dev, "this is a non-ac97 NM256AV, not attaching\n"));
657 s = "NeoMagic 256ZX";
661 if (s) device_set_desc(dev, s);
666 nm_pci_attach(device_t dev)
669 struct ac97_info *codec = NULL;
670 char status[SND_STATUSLEN];
672 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
674 sc->type = pci_get_devid(dev);
676 pci_enable_busmaster(dev);
678 sc->bufid = PCIR_BAR(0);
679 sc->buf = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->bufid,
681 sc->regid = PCIR_BAR(1);
682 sc->reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->regid,
685 if (!sc->buf || !sc->reg) {
686 device_printf(dev, "unable to map register space\n");
690 if (nm_init(sc) == -1) {
691 device_printf(dev, "unable to initialize the card\n");
695 codec = AC97_CREATE(dev, sc, nm_ac97);
696 if (codec == NULL) goto bad;
697 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto bad;
700 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
701 RF_ACTIVE | RF_SHAREABLE);
702 if (!sc->irq || snd_setup_intr(dev, sc->irq, 0, nm_intr, sc, &sc->ih)) {
703 device_printf(dev, "unable to map interrupt\n");
707 snprintf(status, SND_STATUSLEN, "at memory 0x%jx, 0x%jx irq %jd %s",
708 rman_get_start(sc->buf), rman_get_start(sc->reg),
709 rman_get_start(sc->irq),PCM_KLDSTRING(snd_neomagic));
711 if (pcm_register(dev, sc, 1, 1)) goto bad;
712 pcm_addchan(dev, PCMDIR_REC, &nmchan_class, sc);
713 pcm_addchan(dev, PCMDIR_PLAY, &nmchan_class, sc);
714 pcm_setstatus(dev, status);
719 if (codec) ac97_destroy(codec);
720 if (sc->buf) bus_release_resource(dev, SYS_RES_MEMORY, sc->bufid, sc->buf);
721 if (sc->reg) bus_release_resource(dev, SYS_RES_MEMORY, sc->regid, sc->reg);
722 if (sc->ih) bus_teardown_intr(dev, sc->irq, sc->ih);
723 if (sc->irq) bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
729 nm_pci_detach(device_t dev)
734 r = pcm_unregister(dev);
738 sc = pcm_getdevinfo(dev);
739 bus_release_resource(dev, SYS_RES_MEMORY, sc->bufid, sc->buf);
740 bus_release_resource(dev, SYS_RES_MEMORY, sc->regid, sc->reg);
741 bus_teardown_intr(dev, sc->irq, sc->ih);
742 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
749 nm_pci_suspend(device_t dev)
753 sc = pcm_getdevinfo(dev);
756 if (sc->pch.active) {
757 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, 0, 1);
758 nm_wr(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH, 2);
761 if (sc->rch.active) {
762 nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1);
768 nm_pci_resume(device_t dev)
772 sc = pcm_getdevinfo(dev);
775 * Reinit audio device.
776 * Don't call nm_init(). It would change buftop if X ran or
777 * is running. This makes playing and recording buffer address
778 * shift but these buffers of channel layer are not changed.
779 * As a result of this inconsistency, periodic noise will be
780 * generated while playing.
782 nm_wr(sc, 0, 0x11, 1);
783 nm_wr(sc, 0x214, 0, 2);
786 if (mixer_reinit(dev) == -1) {
787 device_printf(dev, "unable to reinitialize the mixer\n");
790 /* restart playing */
791 if (sc->pch.active) {
792 nm_wr(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
793 NM_PLAYBACK_ENABLE_FLAG, 1);
794 nm_wr(sc, NM_AUDIO_MUTE_REG, 0, 2);
796 /* restart recording */
797 if (sc->rch.active) {
798 nm_wr(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
799 NM_RECORD_ENABLE_FLAG, 1);
804 static device_method_t nm_methods[] = {
805 /* Device interface */
806 DEVMETHOD(device_probe, nm_pci_probe),
807 DEVMETHOD(device_attach, nm_pci_attach),
808 DEVMETHOD(device_detach, nm_pci_detach),
809 DEVMETHOD(device_suspend, nm_pci_suspend),
810 DEVMETHOD(device_resume, nm_pci_resume),
814 static driver_t nm_driver = {
820 DRIVER_MODULE(snd_neomagic, pci, nm_driver, pcm_devclass, 0, 0);
821 MODULE_DEPEND(snd_neomagic, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
822 MODULE_VERSION(snd_neomagic, 1);