Home | History | Annotate | Line # | Download | only in pci
hdaudio_pci.c revision 1.11
      1  1.11   msaitoh /* $NetBSD: hdaudio_pci.c,v 1.11 2021/10/28 09:15:35 msaitoh Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*
      4   1.1  jmcneill  * Copyright (c) 2009 Precedence Technologies Ltd <support (at) precedence.co.uk>
      5   1.1  jmcneill  * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
      6   1.1  jmcneill  * All rights reserved.
      7   1.1  jmcneill  *
      8   1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      9   1.1  jmcneill  * by Precedence Technologies Ltd
     10   1.1  jmcneill  *
     11   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     12   1.1  jmcneill  * modification, are permitted provided that the following conditions
     13   1.1  jmcneill  * are met:
     14   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     15   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     16   1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     17   1.1  jmcneill  *    derived from this software without specific prior written permission.
     18   1.1  jmcneill  *
     19   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1  jmcneill  * SUCH DAMAGE.
     30   1.1  jmcneill  */
     31   1.1  jmcneill 
     32   1.1  jmcneill /*
     33   1.1  jmcneill  * Intel High Definition Audio (Revision 1.0a) device driver.
     34   1.1  jmcneill  */
     35   1.1  jmcneill 
     36   1.1  jmcneill #include <sys/cdefs.h>
     37  1.11   msaitoh __KERNEL_RCSID(0, "$NetBSD: hdaudio_pci.c,v 1.11 2021/10/28 09:15:35 msaitoh Exp $");
     38   1.1  jmcneill 
     39   1.1  jmcneill #include <sys/types.h>
     40   1.1  jmcneill #include <sys/param.h>
     41   1.1  jmcneill #include <sys/systm.h>
     42   1.1  jmcneill #include <sys/device.h>
     43   1.1  jmcneill #include <sys/conf.h>
     44   1.1  jmcneill #include <sys/bus.h>
     45   1.1  jmcneill #include <sys/intr.h>
     46   1.1  jmcneill #include <sys/module.h>
     47   1.1  jmcneill 
     48   1.1  jmcneill #include <dev/pci/pcidevs.h>
     49   1.1  jmcneill #include <dev/pci/pcivar.h>
     50   1.1  jmcneill 
     51   1.1  jmcneill #include <dev/hdaudio/hdaudioreg.h>
     52   1.1  jmcneill #include <dev/hdaudio/hdaudiovar.h>
     53   1.1  jmcneill #include <dev/pci/hdaudio_pci.h>
     54   1.1  jmcneill 
     55   1.1  jmcneill struct hdaudio_pci_softc {
     56   1.1  jmcneill 	struct hdaudio_softc	sc_hdaudio;	/* must be first */
     57   1.1  jmcneill 	pcitag_t		sc_tag;
     58   1.1  jmcneill 	pci_chipset_tag_t	sc_pc;
     59   1.1  jmcneill 	void			*sc_ih;
     60   1.1  jmcneill 	pcireg_t		sc_id;
     61   1.5    nonaka 	pci_intr_handle_t	*sc_pihp;
     62   1.1  jmcneill };
     63   1.1  jmcneill 
     64   1.3   msaitoh static int	hdaudio_pci_match(device_t, cfdata_t, void *);
     65   1.3   msaitoh static void	hdaudio_pci_attach(device_t, device_t, void *);
     66   1.3   msaitoh static int	hdaudio_pci_detach(device_t, int);
     67   1.3   msaitoh static int	hdaudio_pci_rescan(device_t, const char *, const int *);
     68   1.3   msaitoh static void	hdaudio_pci_childdet(device_t, device_t);
     69   1.1  jmcneill 
     70   1.3   msaitoh static int	hdaudio_pci_intr(void *);
     71   1.3   msaitoh static void	hdaudio_pci_reinit(struct hdaudio_pci_softc *);
     72   1.1  jmcneill 
     73   1.1  jmcneill /* power management */
     74   1.3   msaitoh static bool	hdaudio_pci_resume(device_t, const pmf_qual_t *);
     75   1.1  jmcneill 
     76   1.1  jmcneill CFATTACH_DECL2_NEW(
     77   1.1  jmcneill     hdaudio_pci,
     78   1.1  jmcneill     sizeof(struct hdaudio_pci_softc),
     79   1.1  jmcneill     hdaudio_pci_match,
     80   1.1  jmcneill     hdaudio_pci_attach,
     81   1.1  jmcneill     hdaudio_pci_detach,
     82   1.1  jmcneill     NULL,
     83   1.1  jmcneill     hdaudio_pci_rescan,
     84   1.1  jmcneill     hdaudio_pci_childdet
     85   1.1  jmcneill );
     86   1.1  jmcneill 
     87  1.11   msaitoh /* Some devices' sublcass is not PCI_SUBCLASS_MULTIMEDIA_HDAUDIO. */
     88  1.11   msaitoh static const struct device_compatible_entry compat_data[] = {
     89  1.11   msaitoh 	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_2HS_U_HDA) },
     90  1.11   msaitoh 	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_3HS_U_HDA) },
     91  1.11   msaitoh 	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_4HS_H_CAVS) },
     92  1.11   msaitoh 	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_5HS_LP_HDA) },
     93  1.11   msaitoh 	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_JSL_CAVS) },
     94  1.11   msaitoh 
     95  1.11   msaitoh 	PCI_COMPAT_EOL
     96  1.11   msaitoh };
     97  1.11   msaitoh 
     98   1.1  jmcneill /*
     99   1.1  jmcneill  * NetBSD autoconfiguration
    100   1.1  jmcneill  */
    101   1.1  jmcneill 
    102   1.1  jmcneill static int
    103   1.1  jmcneill hdaudio_pci_match(device_t parent, cfdata_t match, void *opaque)
    104   1.1  jmcneill {
    105   1.1  jmcneill 	struct pci_attach_args *pa = opaque;
    106   1.1  jmcneill 
    107  1.11   msaitoh 	if ((PCI_CLASS(pa->pa_class) == PCI_CLASS_MULTIMEDIA) &&
    108  1.11   msaitoh 	    (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MULTIMEDIA_HDAUDIO))
    109  1.11   msaitoh 		return 10;
    110  1.11   msaitoh 	if (pci_compatible_match(pa, compat_data) != 0)
    111  1.11   msaitoh 		return 10;
    112   1.1  jmcneill 
    113  1.11   msaitoh 	return 0;
    114   1.1  jmcneill }
    115   1.1  jmcneill 
    116   1.1  jmcneill static void
    117   1.1  jmcneill hdaudio_pci_attach(device_t parent, device_t self, void *opaque)
    118   1.1  jmcneill {
    119   1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    120   1.1  jmcneill 	struct pci_attach_args *pa = opaque;
    121   1.1  jmcneill 	const char *intrstr;
    122   1.9       mrg 	pcireg_t csr, maptype;
    123   1.9       mrg 	int err, reg;
    124   1.1  jmcneill 	char intrbuf[PCI_INTRSTR_LEN];
    125   1.1  jmcneill 
    126   1.1  jmcneill 	aprint_naive("\n");
    127   1.1  jmcneill 	aprint_normal(": HD Audio Controller\n");
    128   1.1  jmcneill 
    129   1.1  jmcneill 	sc->sc_pc = pa->pa_pc;
    130   1.1  jmcneill 	sc->sc_tag = pa->pa_tag;
    131   1.1  jmcneill 	sc->sc_id = pa->pa_id;
    132   1.1  jmcneill 
    133   1.1  jmcneill 	sc->sc_hdaudio.sc_subsystem = pci_conf_read(sc->sc_pc, sc->sc_tag,
    134   1.1  jmcneill 	    PCI_SUBSYS_ID_REG);
    135   1.1  jmcneill 
    136   1.1  jmcneill 	/* Enable busmastering and MMIO access */
    137   1.1  jmcneill 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
    138   1.1  jmcneill 	csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE;
    139   1.1  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
    140   1.1  jmcneill 
    141   1.1  jmcneill 	/* Map MMIO registers */
    142   1.9       mrg 	reg = HDAUDIO_PCI_AZBARL;
    143   1.9       mrg 	maptype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, reg);
    144   1.9       mrg 	err = pci_mapreg_map(pa, reg, maptype, 0,
    145   1.1  jmcneill 			     &sc->sc_hdaudio.sc_memt,
    146   1.1  jmcneill 			     &sc->sc_hdaudio.sc_memh,
    147   1.1  jmcneill 			     &sc->sc_hdaudio.sc_membase,
    148   1.1  jmcneill 			     &sc->sc_hdaudio.sc_memsize);
    149   1.1  jmcneill 	if (err) {
    150   1.1  jmcneill 		aprint_error_dev(self, "couldn't map mmio space\n");
    151   1.1  jmcneill 		return;
    152   1.1  jmcneill 	}
    153   1.1  jmcneill 	sc->sc_hdaudio.sc_memvalid = true;
    154  1.10       mrg 	if (pci_dma64_available(pa))
    155  1.10       mrg 		sc->sc_hdaudio.sc_dmat = pa->pa_dmat64;
    156  1.10       mrg 	else
    157  1.10       mrg 		sc->sc_hdaudio.sc_dmat = pa->pa_dmat;
    158   1.1  jmcneill 
    159   1.1  jmcneill 	/* Map interrupt and establish handler */
    160   1.5    nonaka 	if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
    161   1.1  jmcneill 		aprint_error_dev(self, "couldn't map interrupt\n");
    162   1.1  jmcneill 		return;
    163   1.1  jmcneill 	}
    164   1.5    nonaka 	intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf,
    165   1.5    nonaka 	    sizeof(intrbuf));
    166   1.7   msaitoh 	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0],
    167   1.7   msaitoh 	    IPL_AUDIO, hdaudio_pci_intr, sc, device_xname(self));
    168   1.1  jmcneill 	if (sc->sc_ih == NULL) {
    169   1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt");
    170   1.1  jmcneill 		if (intrstr)
    171   1.1  jmcneill 			aprint_error(" at %s", intrstr);
    172   1.1  jmcneill 		aprint_error("\n");
    173   1.1  jmcneill 		return;
    174   1.1  jmcneill 	}
    175   1.1  jmcneill 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    176   1.1  jmcneill 
    177   1.1  jmcneill 	hdaudio_pci_reinit(sc);
    178   1.1  jmcneill 
    179   1.1  jmcneill 	/* Attach bus-independent HD audio layer */
    180   1.1  jmcneill 	if (hdaudio_attach(self, &sc->sc_hdaudio)) {
    181   1.1  jmcneill 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    182   1.5    nonaka 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    183   1.1  jmcneill 		sc->sc_ih = NULL;
    184   1.1  jmcneill 		bus_space_unmap(sc->sc_hdaudio.sc_memt,
    185   1.1  jmcneill 				sc->sc_hdaudio.sc_memh,
    186   1.1  jmcneill 				sc->sc_hdaudio.sc_memsize);
    187   1.1  jmcneill 		sc->sc_hdaudio.sc_memvalid = false;
    188   1.4   msaitoh 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    189   1.4   msaitoh 		    PCI_COMMAND_STATUS_REG);
    190   1.1  jmcneill 		csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
    191   1.4   msaitoh 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    192   1.4   msaitoh 		    PCI_COMMAND_STATUS_REG, csr);
    193   1.6   khorben 
    194   1.6   khorben 		if (!pmf_device_register(self, NULL, NULL))
    195   1.6   khorben 			aprint_error_dev(self, "couldn't establish power handler\n");
    196   1.1  jmcneill 	}
    197   1.6   khorben 	else if (!pmf_device_register(self, NULL, hdaudio_pci_resume))
    198   1.6   khorben 		aprint_error_dev(self, "couldn't establish power handler\n");
    199   1.1  jmcneill }
    200   1.1  jmcneill 
    201   1.1  jmcneill static int
    202   1.1  jmcneill hdaudio_pci_rescan(device_t self, const char *ifattr, const int *locs)
    203   1.1  jmcneill {
    204   1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    205   1.1  jmcneill 
    206   1.1  jmcneill 	return hdaudio_rescan(&sc->sc_hdaudio, ifattr, locs);
    207   1.1  jmcneill }
    208   1.1  jmcneill 
    209   1.1  jmcneill void
    210   1.1  jmcneill hdaudio_pci_childdet(device_t self, device_t child)
    211   1.1  jmcneill {
    212   1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    213   1.1  jmcneill 
    214   1.1  jmcneill 	hdaudio_childdet(&sc->sc_hdaudio, child);
    215   1.1  jmcneill }
    216   1.1  jmcneill 
    217   1.1  jmcneill static int
    218   1.1  jmcneill hdaudio_pci_detach(device_t self, int flags)
    219   1.1  jmcneill {
    220   1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    221   1.1  jmcneill 	pcireg_t csr;
    222   1.1  jmcneill 
    223   1.1  jmcneill 	hdaudio_detach(&sc->sc_hdaudio, flags);
    224   1.1  jmcneill 
    225   1.1  jmcneill 	if (sc->sc_ih != NULL) {
    226   1.1  jmcneill 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    227   1.5    nonaka 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    228   1.1  jmcneill 		sc->sc_ih = NULL;
    229   1.1  jmcneill 	}
    230   1.1  jmcneill 	if (sc->sc_hdaudio.sc_memvalid == true) {
    231   1.1  jmcneill 		bus_space_unmap(sc->sc_hdaudio.sc_memt,
    232   1.1  jmcneill 				sc->sc_hdaudio.sc_memh,
    233   1.1  jmcneill 				sc->sc_hdaudio.sc_memsize);
    234   1.1  jmcneill 		sc->sc_hdaudio.sc_memvalid = false;
    235   1.1  jmcneill 	}
    236   1.1  jmcneill 
    237   1.1  jmcneill 	/* Disable busmastering and MMIO access */
    238   1.1  jmcneill 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
    239   1.1  jmcneill 	csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
    240   1.1  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
    241   1.1  jmcneill 
    242   1.1  jmcneill 	pmf_device_deregister(self);
    243   1.1  jmcneill 
    244   1.1  jmcneill 	return 0;
    245   1.1  jmcneill }
    246   1.1  jmcneill 
    247   1.1  jmcneill static int
    248   1.1  jmcneill hdaudio_pci_intr(void *opaque)
    249   1.1  jmcneill {
    250   1.1  jmcneill 	struct hdaudio_pci_softc *sc = opaque;
    251   1.1  jmcneill 
    252   1.1  jmcneill 	return hdaudio_intr(&sc->sc_hdaudio);
    253   1.1  jmcneill }
    254   1.1  jmcneill 
    255   1.1  jmcneill 
    256   1.1  jmcneill static void
    257   1.1  jmcneill hdaudio_pci_reinit(struct hdaudio_pci_softc *sc)
    258   1.1  jmcneill {
    259   1.1  jmcneill 	pcireg_t val;
    260   1.1  jmcneill 
    261   1.1  jmcneill 	/* stops playback static */
    262   1.1  jmcneill 	val = pci_conf_read(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL);
    263   1.1  jmcneill 	val &= ~7;
    264   1.1  jmcneill 	val |= 0;
    265   1.1  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL, val);
    266   1.1  jmcneill 
    267   1.1  jmcneill 	switch (PCI_VENDOR(sc->sc_id)) {
    268   1.1  jmcneill 	case PCI_VENDOR_NVIDIA:
    269   1.1  jmcneill 		/* enable snooping */
    270   1.1  jmcneill 		val = pci_conf_read(sc->sc_pc, sc->sc_tag,
    271   1.1  jmcneill 		    HDAUDIO_NV_REG_SNOOP);
    272   1.1  jmcneill 		val &= ~HDAUDIO_NV_SNOOP_MASK;
    273   1.1  jmcneill 		val |= HDAUDIO_NV_SNOOP_ENABLE;
    274   1.1  jmcneill 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    275   1.1  jmcneill 		    HDAUDIO_NV_REG_SNOOP, val);
    276   1.1  jmcneill 		break;
    277   1.1  jmcneill 	}
    278   1.1  jmcneill }
    279   1.1  jmcneill 
    280   1.1  jmcneill static bool
    281   1.1  jmcneill hdaudio_pci_resume(device_t self, const pmf_qual_t *qual)
    282   1.1  jmcneill {
    283   1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    284   1.1  jmcneill 
    285   1.1  jmcneill 	hdaudio_pci_reinit(sc);
    286   1.1  jmcneill 	return hdaudio_resume(&sc->sc_hdaudio);
    287   1.1  jmcneill }
    288   1.1  jmcneill 
    289   1.8  pgoyette MODULE(MODULE_CLASS_DRIVER, hdaudio_pci, "pci,hdaudio,audio");
    290   1.1  jmcneill 
    291   1.1  jmcneill #ifdef _MODULE
    292   1.8  pgoyette /*
    293   1.8  pgoyette  * XXX Don't allow ioconf.c to redefine the "struct cfdriver hdaudio_cd"
    294   1.8  pgoyette  * XXX it will be defined in the common hdaudio module
    295   1.8  pgoyette  */
    296   1.8  pgoyette 
    297   1.8  pgoyette #undef CFDRIVER_DECL
    298   1.8  pgoyette #define CFDRIVER_DECL(name, class, attr) /* nothing */
    299   1.1  jmcneill #include "ioconf.c"
    300   1.1  jmcneill #endif
    301   1.1  jmcneill 
    302   1.1  jmcneill static int
    303   1.1  jmcneill hdaudio_pci_modcmd(modcmd_t cmd, void *opaque)
    304   1.1  jmcneill {
    305   1.8  pgoyette #ifdef _MODULE
    306   1.8  pgoyette 	/*
    307   1.8  pgoyette 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    308   1.8  pgoyette 	 * the cfdrivers are attached already.
    309   1.8  pgoyette 	 */
    310   1.8  pgoyette 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    311   1.8  pgoyette #endif
    312   1.1  jmcneill 	int error = 0;
    313   1.1  jmcneill 
    314   1.1  jmcneill 	switch (cmd) {
    315   1.1  jmcneill 	case MODULE_CMD_INIT:
    316   1.1  jmcneill #ifdef _MODULE
    317   1.8  pgoyette 		error = config_init_component(no_cfdriver_vec,
    318   1.1  jmcneill 		    cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci);
    319   1.1  jmcneill #endif
    320   1.1  jmcneill 		return error;
    321   1.1  jmcneill 	case MODULE_CMD_FINI:
    322   1.1  jmcneill #ifdef _MODULE
    323   1.8  pgoyette 		error = config_fini_component(no_cfdriver_vec,
    324   1.1  jmcneill 		    cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci);
    325   1.1  jmcneill #endif
    326   1.1  jmcneill 		return error;
    327   1.1  jmcneill 	default:
    328   1.1  jmcneill 		return ENOTTY;
    329   1.1  jmcneill 	}
    330   1.1  jmcneill }
    331