Home | History | Annotate | Line # | Download | only in pci
amdsmn.c revision 1.4
      1  1.4   msaitoh /*	$NetBSD: amdsmn.c,v 1.4 2019/07/18 08:53:41 msaitoh Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2017 Conrad Meyer <cem (at) FreeBSD.org>
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * NetBSD port by Ian Clark <mrrooster (at) gmail.com>
      8  1.1  christos  *
      9  1.1  christos  * Redistribution and use in source and binary forms, with or without
     10  1.1  christos  * modification, are permitted provided that the following conditions
     11  1.1  christos  * are met:
     12  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  christos  *    documentation and/or other materials provided with the distribution.
     17  1.1  christos  *
     18  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.1  christos  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     22  1.1  christos  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  1.1  christos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     26  1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     27  1.1  christos  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #include <sys/cdefs.h>
     32  1.4   msaitoh __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.4 2019/07/18 08:53:41 msaitoh Exp $ ");
     33  1.1  christos 
     34  1.1  christos /*
     35  1.1  christos  * Driver for the AMD Family 17h CPU System Management Network.
     36  1.1  christos  */
     37  1.1  christos 
     38  1.1  christos #include <sys/param.h>
     39  1.1  christos #include <sys/device.h>
     40  1.1  christos #include <sys/errno.h>
     41  1.1  christos #include <sys/mutex.h>
     42  1.1  christos #include <sys/systm.h>
     43  1.1  christos #include <sys/cpu.h>
     44  1.2  pgoyette #include <sys/module.h>
     45  1.1  christos 
     46  1.1  christos #include <machine/specialreg.h>
     47  1.1  christos 
     48  1.1  christos #include <dev/pci/pcireg.h>
     49  1.1  christos #include <dev/pci/pcivar.h>
     50  1.1  christos #include <dev/pci/pcidevs.h>
     51  1.1  christos 
     52  1.1  christos #include "amdsmn.h"
     53  1.2  pgoyette #include "ioconf.h"
     54  1.1  christos 
     55  1.1  christos #define	SMN_ADDR_REG	0x60
     56  1.1  christos #define	SMN_DATA_REG	0x64
     57  1.1  christos 
     58  1.1  christos struct amdsmn_softc {
     59  1.1  christos 	kmutex_t smn_lock;
     60  1.1  christos 	struct pci_attach_args pa;
     61  1.1  christos 	pci_chipset_tag_t pc;
     62  1.1  christos 	pcitag_t pcitag;
     63  1.1  christos };
     64  1.1  christos 
     65  1.4   msaitoh static const struct pciid {
     66  1.4   msaitoh 	uint16_t	amdsmn_deviceid;
     67  1.4   msaitoh } amdsmn_ids[] = {
     68  1.4   msaitoh 	{ PCI_PRODUCT_AMD_F17_RC },
     69  1.4   msaitoh 	{ PCI_PRODUCT_AMD_F17_1X_RC },
     70  1.4   msaitoh 	{ PCI_PRODUCT_AMD_F17_7X_RC },
     71  1.4   msaitoh };
     72  1.4   msaitoh 
     73  1.1  christos static int amdsmn_match(device_t, cfdata_t, void *);
     74  1.1  christos static void amdsmn_attach(device_t, device_t, void *);
     75  1.2  pgoyette static int amdsmn_rescan(device_t, const char *, const int *);
     76  1.1  christos static int amdsmn_detach(device_t, int);
     77  1.1  christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
     78  1.1  christos 
     79  1.2  pgoyette CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
     80  1.2  pgoyette     amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0);
     81  1.1  christos 
     82  1.1  christos static int
     83  1.1  christos amdsmn_match(device_t parent, cfdata_t match, void *aux)
     84  1.1  christos {
     85  1.1  christos 	struct pci_attach_args *pa = aux;
     86  1.4   msaitoh 	int i;
     87  1.4   msaitoh 
     88  1.4   msaitoh 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
     89  1.4   msaitoh 		return 0;
     90  1.4   msaitoh 
     91  1.4   msaitoh 	for (i = 0; i <  __arraycount(amdsmn_ids); i++)
     92  1.4   msaitoh 		if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid)
     93  1.4   msaitoh 			return 2;
     94  1.4   msaitoh 
     95  1.4   msaitoh 	return 0;
     96  1.1  christos }
     97  1.1  christos 
     98  1.1  christos static int
     99  1.1  christos amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
    100  1.1  christos {
    101  1.1  christos 	if (config_match(parent, cf, aux))
    102  1.1  christos 		config_attach_loc(parent, cf, locs, aux, NULL);
    103  1.1  christos 
    104  1.1  christos 	return 0;
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos static void
    108  1.1  christos amdsmn_attach(device_t parent, device_t self, void *aux)
    109  1.1  christos {
    110  1.1  christos 	struct amdsmn_softc *sc = device_private(self);
    111  1.1  christos 	struct pci_attach_args *pa = aux;
    112  1.2  pgoyette 	int flags = 0;
    113  1.1  christos 
    114  1.1  christos 	mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
    115  1.1  christos 	sc->pa = *pa;
    116  1.1  christos 	sc->pc = pa->pa_pc;
    117  1.1  christos 	sc->pcitag = pa->pa_tag;
    118  1.1  christos 	aprint_normal(": AMD Family 17h System Management Network\n");
    119  1.3    kardel 	amdsmn_rescan(self, "amdsmnbus", &flags);
    120  1.2  pgoyette }
    121  1.2  pgoyette 
    122  1.2  pgoyette static int
    123  1.2  pgoyette amdsmn_rescan(device_t self, const char *ifattr, const int *flags)
    124  1.2  pgoyette {
    125  1.2  pgoyette 	struct amdsmn_softc *sc = device_private(self);
    126  1.2  pgoyette 
    127  1.2  pgoyette 	config_search_loc(amdsmn_misc_search, self, ifattr, NULL, &sc->pa);
    128  1.2  pgoyette 
    129  1.2  pgoyette 	return 0;
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos static int
    133  1.1  christos amdsmn_detach(device_t self, int flags)
    134  1.1  christos {
    135  1.1  christos 	struct amdsmn_softc *sc = device_private(self);
    136  1.1  christos 
    137  1.1  christos 	mutex_destroy(&sc->smn_lock);
    138  1.1  christos 	aprint_normal_dev(self,"detach!\n");
    139  1.1  christos 
    140  1.1  christos 	return 0;
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos int
    144  1.1  christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
    145  1.1  christos {
    146  1.1  christos 	struct amdsmn_softc *sc = device_private(dev);
    147  1.1  christos 
    148  1.1  christos 	mutex_enter(&sc->smn_lock);
    149  1.1  christos 	pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
    150  1.1  christos 	*value = pci_conf_read(sc->pc, sc->pcitag, SMN_DATA_REG);
    151  1.1  christos 	mutex_exit(&sc->smn_lock);
    152  1.1  christos 
    153  1.1  christos 	return 0;
    154  1.1  christos }
    155  1.1  christos 
    156  1.1  christos int
    157  1.1  christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
    158  1.1  christos {
    159  1.1  christos 	struct amdsmn_softc *sc = device_private(dev);
    160  1.1  christos 
    161  1.1  christos 	mutex_enter(&sc->smn_lock);
    162  1.1  christos 	pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
    163  1.1  christos 	pci_conf_write(sc->pc, sc->pcitag, SMN_DATA_REG, value);
    164  1.1  christos 	mutex_exit(&sc->smn_lock);
    165  1.1  christos 
    166  1.1  christos 	return 0;
    167  1.1  christos }
    168  1.2  pgoyette 
    169  1.2  pgoyette MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci");
    170  1.2  pgoyette 
    171  1.2  pgoyette #ifdef _MODULE
    172  1.2  pgoyette #include "ioconf.c"
    173  1.2  pgoyette #endif
    174  1.2  pgoyette 
    175  1.2  pgoyette static int
    176  1.2  pgoyette amdsmn_modcmd(modcmd_t cmd, void *opaque)
    177  1.2  pgoyette {
    178  1.2  pgoyette 	int error = 0;
    179  1.2  pgoyette 
    180  1.2  pgoyette #ifdef _MODULE
    181  1.2  pgoyette 	switch (cmd) {
    182  1.2  pgoyette 	case MODULE_CMD_INIT:
    183  1.2  pgoyette 		error = config_init_component(cfdriver_ioconf_amdsmn,
    184  1.2  pgoyette 		    cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
    185  1.2  pgoyette 		break;
    186  1.2  pgoyette 	case MODULE_CMD_FINI:
    187  1.2  pgoyette 		error = config_fini_component(cfdriver_ioconf_amdsmn,
    188  1.2  pgoyette 		    cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
    189  1.2  pgoyette 		break;
    190  1.2  pgoyette 	default:
    191  1.2  pgoyette 		error = ENOTTY;
    192  1.2  pgoyette 		break;
    193  1.2  pgoyette 	}
    194  1.2  pgoyette #endif
    195  1.2  pgoyette 
    196  1.2  pgoyette 	return error;
    197  1.2  pgoyette }
    198  1.2  pgoyette 
    199