2018-09-12 12:49:03 +03:00
|
|
|
/* $NetBSD: hdaudio_pci.c,v 1.10 2018/09/12 09:49:03 mrg Exp $ */
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
/*
|
2009-09-06 21:33:53 +04:00
|
|
|
* Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk>
|
2009-09-06 21:25:55 +04:00
|
|
|
* Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Precedence Technologies Ltd
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-02-12 18:15:34 +03:00
|
|
|
* Intel High Definition Audio (Revision 1.0a) device driver.
|
2009-09-06 21:25:55 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2018-09-12 12:49:03 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: hdaudio_pci.c,v 1.10 2018/09/12 09:49:03 mrg Exp $");
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/intr.h>
|
2015-03-28 17:09:58 +03:00
|
|
|
#include <sys/module.h>
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
#include <dev/pci/pcidevs.h>
|
|
|
|
#include <dev/pci/pcivar.h>
|
|
|
|
|
2015-03-28 17:09:58 +03:00
|
|
|
#include <dev/hdaudio/hdaudioreg.h>
|
|
|
|
#include <dev/hdaudio/hdaudiovar.h>
|
|
|
|
#include <dev/pci/hdaudio_pci.h>
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
struct hdaudio_pci_softc {
|
|
|
|
struct hdaudio_softc sc_hdaudio; /* must be first */
|
|
|
|
pcitag_t sc_tag;
|
|
|
|
pci_chipset_tag_t sc_pc;
|
|
|
|
void *sc_ih;
|
2012-02-01 20:56:34 +04:00
|
|
|
pcireg_t sc_id;
|
2016-12-16 14:34:52 +03:00
|
|
|
pci_intr_handle_t *sc_pihp;
|
2009-09-06 21:25:55 +04:00
|
|
|
};
|
|
|
|
|
2016-07-07 09:55:38 +03:00
|
|
|
static int hdaudio_pci_match(device_t, cfdata_t, void *);
|
|
|
|
static void hdaudio_pci_attach(device_t, device_t, void *);
|
|
|
|
static int hdaudio_pci_detach(device_t, int);
|
|
|
|
static int hdaudio_pci_rescan(device_t, const char *, const int *);
|
|
|
|
static void hdaudio_pci_childdet(device_t, device_t);
|
2009-09-06 21:25:55 +04:00
|
|
|
|
2016-07-07 09:55:38 +03:00
|
|
|
static int hdaudio_pci_intr(void *);
|
|
|
|
static void hdaudio_pci_reinit(struct hdaudio_pci_softc *);
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
/* power management */
|
2016-07-07 09:55:38 +03:00
|
|
|
static bool hdaudio_pci_resume(device_t, const pmf_qual_t *);
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
CFATTACH_DECL2_NEW(
|
|
|
|
hdaudio_pci,
|
|
|
|
sizeof(struct hdaudio_pci_softc),
|
|
|
|
hdaudio_pci_match,
|
|
|
|
hdaudio_pci_attach,
|
|
|
|
hdaudio_pci_detach,
|
|
|
|
NULL,
|
2011-01-07 18:30:29 +03:00
|
|
|
hdaudio_pci_rescan,
|
2009-09-06 21:25:55 +04:00
|
|
|
hdaudio_pci_childdet
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NetBSD autoconfiguration
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
hdaudio_pci_match(device_t parent, cfdata_t match, void *opaque)
|
|
|
|
{
|
|
|
|
struct pci_attach_args *pa = opaque;
|
|
|
|
|
|
|
|
if (PCI_CLASS(pa->pa_class) != PCI_CLASS_MULTIMEDIA)
|
|
|
|
return 0;
|
|
|
|
if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MULTIMEDIA_HDAUDIO)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hdaudio_pci_attach(device_t parent, device_t self, void *opaque)
|
|
|
|
{
|
|
|
|
struct hdaudio_pci_softc *sc = device_private(self);
|
|
|
|
struct pci_attach_args *pa = opaque;
|
|
|
|
const char *intrstr;
|
2018-09-12 06:23:38 +03:00
|
|
|
pcireg_t csr, maptype;
|
|
|
|
int err, reg;
|
2014-03-29 23:28:24 +04:00
|
|
|
char intrbuf[PCI_INTRSTR_LEN];
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
aprint_naive("\n");
|
|
|
|
aprint_normal(": HD Audio Controller\n");
|
|
|
|
|
|
|
|
sc->sc_pc = pa->pa_pc;
|
|
|
|
sc->sc_tag = pa->pa_tag;
|
2012-02-01 20:56:34 +04:00
|
|
|
sc->sc_id = pa->pa_id;
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
sc->sc_hdaudio.sc_subsystem = pci_conf_read(sc->sc_pc, sc->sc_tag,
|
|
|
|
PCI_SUBSYS_ID_REG);
|
|
|
|
|
|
|
|
/* Enable busmastering and MMIO access */
|
|
|
|
csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
|
|
|
|
csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE;
|
|
|
|
pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
|
|
|
|
|
|
|
|
/* Map MMIO registers */
|
2018-09-12 06:23:38 +03:00
|
|
|
reg = HDAUDIO_PCI_AZBARL;
|
|
|
|
maptype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, reg);
|
|
|
|
err = pci_mapreg_map(pa, reg, maptype, 0,
|
2009-09-06 21:25:55 +04:00
|
|
|
&sc->sc_hdaudio.sc_memt,
|
|
|
|
&sc->sc_hdaudio.sc_memh,
|
|
|
|
&sc->sc_hdaudio.sc_membase,
|
|
|
|
&sc->sc_hdaudio.sc_memsize);
|
|
|
|
if (err) {
|
|
|
|
aprint_error_dev(self, "couldn't map mmio space\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sc->sc_hdaudio.sc_memvalid = true;
|
2018-09-12 12:49:03 +03:00
|
|
|
if (pci_dma64_available(pa))
|
|
|
|
sc->sc_hdaudio.sc_dmat = pa->pa_dmat64;
|
|
|
|
else
|
|
|
|
sc->sc_hdaudio.sc_dmat = pa->pa_dmat;
|
2009-09-06 21:25:55 +04:00
|
|
|
|
|
|
|
/* Map interrupt and establish handler */
|
2016-12-16 14:34:52 +03:00
|
|
|
if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
|
2009-09-06 21:25:55 +04:00
|
|
|
aprint_error_dev(self, "couldn't map interrupt\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-12-16 14:34:52 +03:00
|
|
|
intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf,
|
|
|
|
sizeof(intrbuf));
|
2017-04-27 13:01:53 +03:00
|
|
|
sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0],
|
|
|
|
IPL_AUDIO, hdaudio_pci_intr, sc, device_xname(self));
|
2009-09-06 21:25:55 +04:00
|
|
|
if (sc->sc_ih == NULL) {
|
|
|
|
aprint_error_dev(self, "couldn't establish interrupt");
|
|
|
|
if (intrstr)
|
2009-12-04 14:13:04 +03:00
|
|
|
aprint_error(" at %s", intrstr);
|
|
|
|
aprint_error("\n");
|
2009-09-06 21:25:55 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
aprint_normal_dev(self, "interrupting at %s\n", intrstr);
|
|
|
|
|
2012-02-01 20:56:34 +04:00
|
|
|
hdaudio_pci_reinit(sc);
|
2010-08-07 20:59:48 +04:00
|
|
|
|
2009-09-06 21:25:55 +04:00
|
|
|
/* Attach bus-independent HD audio layer */
|
2014-10-28 05:49:52 +03:00
|
|
|
if (hdaudio_attach(self, &sc->sc_hdaudio)) {
|
|
|
|
pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
|
2016-12-16 14:34:52 +03:00
|
|
|
pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
|
2014-10-28 05:49:52 +03:00
|
|
|
sc->sc_ih = NULL;
|
|
|
|
bus_space_unmap(sc->sc_hdaudio.sc_memt,
|
|
|
|
sc->sc_hdaudio.sc_memh,
|
|
|
|
sc->sc_hdaudio.sc_memsize);
|
|
|
|
sc->sc_hdaudio.sc_memvalid = false;
|
2016-07-14 07:19:26 +03:00
|
|
|
csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
|
|
|
|
PCI_COMMAND_STATUS_REG);
|
2014-10-28 05:49:52 +03:00
|
|
|
csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
|
2016-07-14 07:19:26 +03:00
|
|
|
pci_conf_write(sc->sc_pc, sc->sc_tag,
|
|
|
|
PCI_COMMAND_STATUS_REG, csr);
|
2017-01-31 03:58:15 +03:00
|
|
|
|
|
|
|
if (!pmf_device_register(self, NULL, NULL))
|
|
|
|
aprint_error_dev(self, "couldn't establish power handler\n");
|
2014-10-28 05:49:52 +03:00
|
|
|
}
|
2017-01-31 03:58:15 +03:00
|
|
|
else if (!pmf_device_register(self, NULL, hdaudio_pci_resume))
|
|
|
|
aprint_error_dev(self, "couldn't establish power handler\n");
|
2009-09-06 21:25:55 +04:00
|
|
|
}
|
|
|
|
|
2011-01-07 18:30:29 +03:00
|
|
|
static int
|
|
|
|
hdaudio_pci_rescan(device_t self, const char *ifattr, const int *locs)
|
|
|
|
{
|
|
|
|
struct hdaudio_pci_softc *sc = device_private(self);
|
|
|
|
|
|
|
|
return hdaudio_rescan(&sc->sc_hdaudio, ifattr, locs);
|
|
|
|
}
|
|
|
|
|
2009-09-06 21:25:55 +04:00
|
|
|
void
|
|
|
|
hdaudio_pci_childdet(device_t self, device_t child)
|
|
|
|
{
|
|
|
|
struct hdaudio_pci_softc *sc = device_private(self);
|
|
|
|
|
|
|
|
hdaudio_childdet(&sc->sc_hdaudio, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hdaudio_pci_detach(device_t self, int flags)
|
|
|
|
{
|
|
|
|
struct hdaudio_pci_softc *sc = device_private(self);
|
|
|
|
pcireg_t csr;
|
|
|
|
|
|
|
|
hdaudio_detach(&sc->sc_hdaudio, flags);
|
|
|
|
|
|
|
|
if (sc->sc_ih != NULL) {
|
|
|
|
pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
|
2016-12-16 14:34:52 +03:00
|
|
|
pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
|
2009-09-06 21:25:55 +04:00
|
|
|
sc->sc_ih = NULL;
|
|
|
|
}
|
|
|
|
if (sc->sc_hdaudio.sc_memvalid == true) {
|
|
|
|
bus_space_unmap(sc->sc_hdaudio.sc_memt,
|
|
|
|
sc->sc_hdaudio.sc_memh,
|
|
|
|
sc->sc_hdaudio.sc_memsize);
|
|
|
|
sc->sc_hdaudio.sc_memvalid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable busmastering and MMIO access */
|
|
|
|
csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
|
|
|
|
csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
|
|
|
|
pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
|
|
|
|
|
|
|
|
pmf_device_deregister(self);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hdaudio_pci_intr(void *opaque)
|
|
|
|
{
|
|
|
|
struct hdaudio_pci_softc *sc = opaque;
|
|
|
|
|
|
|
|
return hdaudio_intr(&sc->sc_hdaudio);
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:56:34 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
hdaudio_pci_reinit(struct hdaudio_pci_softc *sc)
|
|
|
|
{
|
|
|
|
pcireg_t val;
|
|
|
|
|
|
|
|
/* stops playback static */
|
|
|
|
val = pci_conf_read(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL);
|
|
|
|
val &= ~7;
|
|
|
|
val |= 0;
|
|
|
|
pci_conf_write(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL, val);
|
|
|
|
|
|
|
|
switch (PCI_VENDOR(sc->sc_id)) {
|
|
|
|
case PCI_VENDOR_NVIDIA:
|
|
|
|
/* enable snooping */
|
|
|
|
val = pci_conf_read(sc->sc_pc, sc->sc_tag,
|
|
|
|
HDAUDIO_NV_REG_SNOOP);
|
|
|
|
val &= ~HDAUDIO_NV_SNOOP_MASK;
|
|
|
|
val |= HDAUDIO_NV_SNOOP_ENABLE;
|
|
|
|
pci_conf_write(sc->sc_pc, sc->sc_tag,
|
|
|
|
HDAUDIO_NV_REG_SNOOP, val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-06 21:25:55 +04:00
|
|
|
static bool
|
2010-02-25 01:37:54 +03:00
|
|
|
hdaudio_pci_resume(device_t self, const pmf_qual_t *qual)
|
2009-09-06 21:25:55 +04:00
|
|
|
{
|
|
|
|
struct hdaudio_pci_softc *sc = device_private(self);
|
|
|
|
|
2012-02-01 20:56:34 +04:00
|
|
|
hdaudio_pci_reinit(sc);
|
2009-09-06 21:25:55 +04:00
|
|
|
return hdaudio_resume(&sc->sc_hdaudio);
|
|
|
|
}
|
2015-03-28 17:09:58 +03:00
|
|
|
|
2017-06-05 02:34:55 +03:00
|
|
|
MODULE(MODULE_CLASS_DRIVER, hdaudio_pci, "pci,hdaudio,audio");
|
2015-03-28 17:09:58 +03:00
|
|
|
|
|
|
|
#ifdef _MODULE
|
2017-06-05 02:34:55 +03:00
|
|
|
/*
|
|
|
|
* XXX Don't allow ioconf.c to redefine the "struct cfdriver hdaudio_cd"
|
|
|
|
* XXX it will be defined in the common hdaudio module
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef CFDRIVER_DECL
|
|
|
|
#define CFDRIVER_DECL(name, class, attr) /* nothing */
|
2015-03-28 17:09:58 +03:00
|
|
|
#include "ioconf.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
hdaudio_pci_modcmd(modcmd_t cmd, void *opaque)
|
|
|
|
{
|
2017-06-05 02:34:55 +03:00
|
|
|
#ifdef _MODULE
|
|
|
|
/*
|
|
|
|
* We ignore the cfdriver_vec[] that ioconf provides, since
|
|
|
|
* the cfdrivers are attached already.
|
|
|
|
*/
|
|
|
|
static struct cfdriver * const no_cfdriver_vec[] = { NULL };
|
|
|
|
#endif
|
2015-03-28 17:09:58 +03:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
#ifdef _MODULE
|
2017-06-05 02:34:55 +03:00
|
|
|
error = config_init_component(no_cfdriver_vec,
|
2015-03-28 17:09:58 +03:00
|
|
|
cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci);
|
|
|
|
#endif
|
|
|
|
return error;
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
#ifdef _MODULE
|
2017-06-05 02:34:55 +03:00
|
|
|
error = config_fini_component(no_cfdriver_vec,
|
2015-03-28 17:09:58 +03:00
|
|
|
cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci);
|
|
|
|
#endif
|
|
|
|
return error;
|
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
}
|