Home | History | Annotate | Line # | Download | only in pci
hdaudio_pci.c revision 1.8.8.1
      1  1.8.8.1  christos /* $NetBSD: hdaudio_pci.c,v 1.8.8.1 2019/06/10 22:07:15 christos 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.8.8.1  christos __KERNEL_RCSID(0, "$NetBSD: hdaudio_pci.c,v 1.8.8.1 2019/06/10 22:07:15 christos 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.1  jmcneill /*
     88      1.1  jmcneill  * NetBSD autoconfiguration
     89      1.1  jmcneill  */
     90      1.1  jmcneill 
     91      1.1  jmcneill static int
     92      1.1  jmcneill hdaudio_pci_match(device_t parent, cfdata_t match, void *opaque)
     93      1.1  jmcneill {
     94      1.1  jmcneill 	struct pci_attach_args *pa = opaque;
     95      1.1  jmcneill 
     96      1.1  jmcneill 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_MULTIMEDIA)
     97      1.1  jmcneill 		return 0;
     98      1.1  jmcneill 	if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MULTIMEDIA_HDAUDIO)
     99      1.1  jmcneill 		return 0;
    100      1.1  jmcneill 
    101      1.1  jmcneill 	return 10;
    102      1.1  jmcneill }
    103      1.1  jmcneill 
    104      1.1  jmcneill static void
    105      1.1  jmcneill hdaudio_pci_attach(device_t parent, device_t self, void *opaque)
    106      1.1  jmcneill {
    107      1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    108      1.1  jmcneill 	struct pci_attach_args *pa = opaque;
    109      1.1  jmcneill 	const char *intrstr;
    110  1.8.8.1  christos 	pcireg_t csr, maptype;
    111  1.8.8.1  christos 	int err, reg;
    112      1.1  jmcneill 	char intrbuf[PCI_INTRSTR_LEN];
    113      1.1  jmcneill 
    114      1.1  jmcneill 	aprint_naive("\n");
    115      1.1  jmcneill 	aprint_normal(": HD Audio Controller\n");
    116      1.1  jmcneill 
    117      1.1  jmcneill 	sc->sc_pc = pa->pa_pc;
    118      1.1  jmcneill 	sc->sc_tag = pa->pa_tag;
    119      1.1  jmcneill 	sc->sc_id = pa->pa_id;
    120      1.1  jmcneill 
    121      1.1  jmcneill 	sc->sc_hdaudio.sc_subsystem = pci_conf_read(sc->sc_pc, sc->sc_tag,
    122      1.1  jmcneill 	    PCI_SUBSYS_ID_REG);
    123      1.1  jmcneill 
    124      1.1  jmcneill 	/* Enable busmastering and MMIO access */
    125      1.1  jmcneill 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
    126      1.1  jmcneill 	csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE;
    127      1.1  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
    128      1.1  jmcneill 
    129      1.1  jmcneill 	/* Map MMIO registers */
    130  1.8.8.1  christos 	reg = HDAUDIO_PCI_AZBARL;
    131  1.8.8.1  christos 	maptype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, reg);
    132  1.8.8.1  christos 	err = pci_mapreg_map(pa, reg, maptype, 0,
    133      1.1  jmcneill 			     &sc->sc_hdaudio.sc_memt,
    134      1.1  jmcneill 			     &sc->sc_hdaudio.sc_memh,
    135      1.1  jmcneill 			     &sc->sc_hdaudio.sc_membase,
    136      1.1  jmcneill 			     &sc->sc_hdaudio.sc_memsize);
    137      1.1  jmcneill 	if (err) {
    138      1.1  jmcneill 		aprint_error_dev(self, "couldn't map mmio space\n");
    139      1.1  jmcneill 		return;
    140      1.1  jmcneill 	}
    141      1.1  jmcneill 	sc->sc_hdaudio.sc_memvalid = true;
    142  1.8.8.1  christos 	if (pci_dma64_available(pa))
    143  1.8.8.1  christos 		sc->sc_hdaudio.sc_dmat = pa->pa_dmat64;
    144  1.8.8.1  christos 	else
    145  1.8.8.1  christos 		sc->sc_hdaudio.sc_dmat = pa->pa_dmat;
    146      1.1  jmcneill 
    147      1.1  jmcneill 	/* Map interrupt and establish handler */
    148      1.5    nonaka 	if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
    149      1.1  jmcneill 		aprint_error_dev(self, "couldn't map interrupt\n");
    150      1.1  jmcneill 		return;
    151      1.1  jmcneill 	}
    152      1.5    nonaka 	intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf,
    153      1.5    nonaka 	    sizeof(intrbuf));
    154      1.7   msaitoh 	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0],
    155      1.7   msaitoh 	    IPL_AUDIO, hdaudio_pci_intr, sc, device_xname(self));
    156      1.1  jmcneill 	if (sc->sc_ih == NULL) {
    157      1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt");
    158      1.1  jmcneill 		if (intrstr)
    159      1.1  jmcneill 			aprint_error(" at %s", intrstr);
    160      1.1  jmcneill 		aprint_error("\n");
    161      1.1  jmcneill 		return;
    162      1.1  jmcneill 	}
    163      1.1  jmcneill 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    164      1.1  jmcneill 
    165      1.1  jmcneill 	hdaudio_pci_reinit(sc);
    166      1.1  jmcneill 
    167      1.1  jmcneill 	/* Attach bus-independent HD audio layer */
    168      1.1  jmcneill 	if (hdaudio_attach(self, &sc->sc_hdaudio)) {
    169      1.1  jmcneill 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    170      1.5    nonaka 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    171      1.1  jmcneill 		sc->sc_ih = NULL;
    172      1.1  jmcneill 		bus_space_unmap(sc->sc_hdaudio.sc_memt,
    173      1.1  jmcneill 				sc->sc_hdaudio.sc_memh,
    174      1.1  jmcneill 				sc->sc_hdaudio.sc_memsize);
    175      1.1  jmcneill 		sc->sc_hdaudio.sc_memvalid = false;
    176      1.4   msaitoh 		csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
    177      1.4   msaitoh 		    PCI_COMMAND_STATUS_REG);
    178      1.1  jmcneill 		csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
    179      1.4   msaitoh 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    180      1.4   msaitoh 		    PCI_COMMAND_STATUS_REG, csr);
    181      1.6   khorben 
    182      1.6   khorben 		if (!pmf_device_register(self, NULL, NULL))
    183      1.6   khorben 			aprint_error_dev(self, "couldn't establish power handler\n");
    184      1.1  jmcneill 	}
    185      1.6   khorben 	else if (!pmf_device_register(self, NULL, hdaudio_pci_resume))
    186      1.6   khorben 		aprint_error_dev(self, "couldn't establish power handler\n");
    187      1.1  jmcneill }
    188      1.1  jmcneill 
    189      1.1  jmcneill static int
    190      1.1  jmcneill hdaudio_pci_rescan(device_t self, const char *ifattr, const int *locs)
    191      1.1  jmcneill {
    192      1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    193      1.1  jmcneill 
    194      1.1  jmcneill 	return hdaudio_rescan(&sc->sc_hdaudio, ifattr, locs);
    195      1.1  jmcneill }
    196      1.1  jmcneill 
    197      1.1  jmcneill void
    198      1.1  jmcneill hdaudio_pci_childdet(device_t self, device_t child)
    199      1.1  jmcneill {
    200      1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    201      1.1  jmcneill 
    202      1.1  jmcneill 	hdaudio_childdet(&sc->sc_hdaudio, child);
    203      1.1  jmcneill }
    204      1.1  jmcneill 
    205      1.1  jmcneill static int
    206      1.1  jmcneill hdaudio_pci_detach(device_t self, int flags)
    207      1.1  jmcneill {
    208      1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    209      1.1  jmcneill 	pcireg_t csr;
    210      1.1  jmcneill 
    211      1.1  jmcneill 	hdaudio_detach(&sc->sc_hdaudio, flags);
    212      1.1  jmcneill 
    213      1.1  jmcneill 	if (sc->sc_ih != NULL) {
    214      1.1  jmcneill 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    215      1.5    nonaka 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    216      1.1  jmcneill 		sc->sc_ih = NULL;
    217      1.1  jmcneill 	}
    218      1.1  jmcneill 	if (sc->sc_hdaudio.sc_memvalid == true) {
    219      1.1  jmcneill 		bus_space_unmap(sc->sc_hdaudio.sc_memt,
    220      1.1  jmcneill 				sc->sc_hdaudio.sc_memh,
    221      1.1  jmcneill 				sc->sc_hdaudio.sc_memsize);
    222      1.1  jmcneill 		sc->sc_hdaudio.sc_memvalid = false;
    223      1.1  jmcneill 	}
    224      1.1  jmcneill 
    225      1.1  jmcneill 	/* Disable busmastering and MMIO access */
    226      1.1  jmcneill 	csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
    227      1.1  jmcneill 	csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
    228      1.1  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr);
    229      1.1  jmcneill 
    230      1.1  jmcneill 	pmf_device_deregister(self);
    231      1.1  jmcneill 
    232      1.1  jmcneill 	return 0;
    233      1.1  jmcneill }
    234      1.1  jmcneill 
    235      1.1  jmcneill static int
    236      1.1  jmcneill hdaudio_pci_intr(void *opaque)
    237      1.1  jmcneill {
    238      1.1  jmcneill 	struct hdaudio_pci_softc *sc = opaque;
    239      1.1  jmcneill 
    240      1.1  jmcneill 	return hdaudio_intr(&sc->sc_hdaudio);
    241      1.1  jmcneill }
    242      1.1  jmcneill 
    243      1.1  jmcneill 
    244      1.1  jmcneill static void
    245      1.1  jmcneill hdaudio_pci_reinit(struct hdaudio_pci_softc *sc)
    246      1.1  jmcneill {
    247      1.1  jmcneill 	pcireg_t val;
    248      1.1  jmcneill 
    249      1.1  jmcneill 	/* stops playback static */
    250      1.1  jmcneill 	val = pci_conf_read(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL);
    251      1.1  jmcneill 	val &= ~7;
    252      1.1  jmcneill 	val |= 0;
    253      1.1  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL, val);
    254      1.1  jmcneill 
    255      1.1  jmcneill 	switch (PCI_VENDOR(sc->sc_id)) {
    256      1.1  jmcneill 	case PCI_VENDOR_NVIDIA:
    257      1.1  jmcneill 		/* enable snooping */
    258      1.1  jmcneill 		val = pci_conf_read(sc->sc_pc, sc->sc_tag,
    259      1.1  jmcneill 		    HDAUDIO_NV_REG_SNOOP);
    260      1.1  jmcneill 		val &= ~HDAUDIO_NV_SNOOP_MASK;
    261      1.1  jmcneill 		val |= HDAUDIO_NV_SNOOP_ENABLE;
    262      1.1  jmcneill 		pci_conf_write(sc->sc_pc, sc->sc_tag,
    263      1.1  jmcneill 		    HDAUDIO_NV_REG_SNOOP, val);
    264      1.1  jmcneill 		break;
    265      1.1  jmcneill 	}
    266      1.1  jmcneill }
    267      1.1  jmcneill 
    268      1.1  jmcneill static bool
    269      1.1  jmcneill hdaudio_pci_resume(device_t self, const pmf_qual_t *qual)
    270      1.1  jmcneill {
    271      1.1  jmcneill 	struct hdaudio_pci_softc *sc = device_private(self);
    272      1.1  jmcneill 
    273      1.1  jmcneill 	hdaudio_pci_reinit(sc);
    274      1.1  jmcneill 	return hdaudio_resume(&sc->sc_hdaudio);
    275      1.1  jmcneill }
    276      1.1  jmcneill 
    277      1.8  pgoyette MODULE(MODULE_CLASS_DRIVER, hdaudio_pci, "pci,hdaudio,audio");
    278      1.1  jmcneill 
    279      1.1  jmcneill #ifdef _MODULE
    280      1.8  pgoyette /*
    281      1.8  pgoyette  * XXX Don't allow ioconf.c to redefine the "struct cfdriver hdaudio_cd"
    282      1.8  pgoyette  * XXX it will be defined in the common hdaudio module
    283      1.8  pgoyette  */
    284      1.8  pgoyette 
    285      1.8  pgoyette #undef CFDRIVER_DECL
    286      1.8  pgoyette #define CFDRIVER_DECL(name, class, attr) /* nothing */
    287      1.1  jmcneill #include "ioconf.c"
    288      1.1  jmcneill #endif
    289      1.1  jmcneill 
    290      1.1  jmcneill static int
    291      1.1  jmcneill hdaudio_pci_modcmd(modcmd_t cmd, void *opaque)
    292      1.1  jmcneill {
    293      1.8  pgoyette #ifdef _MODULE
    294      1.8  pgoyette 	/*
    295      1.8  pgoyette 	 * We ignore the cfdriver_vec[] that ioconf provides, since
    296      1.8  pgoyette 	 * the cfdrivers are attached already.
    297      1.8  pgoyette 	 */
    298      1.8  pgoyette 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
    299      1.8  pgoyette #endif
    300      1.1  jmcneill 	int error = 0;
    301      1.1  jmcneill 
    302      1.1  jmcneill 	switch (cmd) {
    303      1.1  jmcneill 	case MODULE_CMD_INIT:
    304      1.1  jmcneill #ifdef _MODULE
    305      1.8  pgoyette 		error = config_init_component(no_cfdriver_vec,
    306      1.1  jmcneill 		    cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci);
    307      1.1  jmcneill #endif
    308      1.1  jmcneill 		return error;
    309      1.1  jmcneill 	case MODULE_CMD_FINI:
    310      1.1  jmcneill #ifdef _MODULE
    311      1.8  pgoyette 		error = config_fini_component(no_cfdriver_vec,
    312      1.1  jmcneill 		    cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci);
    313      1.1  jmcneill #endif
    314      1.1  jmcneill 		return error;
    315      1.1  jmcneill 	default:
    316      1.1  jmcneill 		return ENOTTY;
    317      1.1  jmcneill 	}
    318      1.1  jmcneill }
    319