Home | History | Annotate | Line # | Download | only in pci
amdsmn.c revision 1.7
      1  1.7    simonb /*	$NetBSD: amdsmn.c,v 1.7 2020/04/20 11:03:02 simonb Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.7    simonb  * Copyright (c) 2017, 2019 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.7    simonb __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.7 2020/04/20 11:03:02 simonb Exp $ ");
     33  1.1  christos 
     34  1.1  christos /*
     35  1.7    simonb  * Driver for the AMD Family 15h (model 60+) and 17h CPU
     36  1.7    simonb  * System Management Network.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos #include <sys/param.h>
     40  1.1  christos #include <sys/device.h>
     41  1.1  christos #include <sys/errno.h>
     42  1.1  christos #include <sys/mutex.h>
     43  1.1  christos #include <sys/systm.h>
     44  1.1  christos #include <sys/cpu.h>
     45  1.2  pgoyette #include <sys/module.h>
     46  1.1  christos 
     47  1.1  christos #include <machine/specialreg.h>
     48  1.1  christos 
     49  1.1  christos #include <dev/pci/pcireg.h>
     50  1.1  christos #include <dev/pci/pcivar.h>
     51  1.1  christos #include <dev/pci/pcidevs.h>
     52  1.1  christos 
     53  1.1  christos #include "amdsmn.h"
     54  1.2  pgoyette #include "ioconf.h"
     55  1.1  christos 
     56  1.7    simonb #define	F15H_SMN_ADDR_REG	0xb8
     57  1.7    simonb #define	F15H_SMN_DATA_REG	0xbc
     58  1.7    simonb #define	F17H_SMN_ADDR_REG	0x60
     59  1.7    simonb #define	F17H_SMN_DATA_REG	0x64
     60  1.1  christos 
     61  1.1  christos struct amdsmn_softc {
     62  1.1  christos 	kmutex_t smn_lock;
     63  1.7    simonb 	uint8_t smn_addr_reg;
     64  1.7    simonb 	uint8_t smn_data_reg;
     65  1.1  christos 	struct pci_attach_args pa;
     66  1.1  christos 	pci_chipset_tag_t pc;
     67  1.1  christos 	pcitag_t pcitag;
     68  1.1  christos };
     69  1.1  christos 
     70  1.4   msaitoh static const struct pciid {
     71  1.4   msaitoh 	uint16_t	amdsmn_deviceid;
     72  1.7    simonb 	uint8_t		amdsmn_addr_reg;
     73  1.7    simonb 	uint8_t		amdsmn_data_reg;
     74  1.4   msaitoh } amdsmn_ids[] = {
     75  1.7    simonb 	{
     76  1.7    simonb 		.amdsmn_deviceid = PCI_PRODUCT_AMD_F15_60_RC,
     77  1.7    simonb 		.amdsmn_addr_reg = F15H_SMN_ADDR_REG,
     78  1.7    simonb 		.amdsmn_data_reg = F15H_SMN_DATA_REG,
     79  1.7    simonb 	},
     80  1.7    simonb 	{
     81  1.7    simonb 		.amdsmn_deviceid = PCI_PRODUCT_AMD_F17_RC,
     82  1.7    simonb 		.amdsmn_addr_reg = F17H_SMN_ADDR_REG,
     83  1.7    simonb 		.amdsmn_data_reg = F17H_SMN_DATA_REG,
     84  1.7    simonb 	},
     85  1.7    simonb 	{
     86  1.7    simonb 		.amdsmn_deviceid = PCI_PRODUCT_AMD_F17_1X_RC,
     87  1.7    simonb 		.amdsmn_addr_reg = F17H_SMN_ADDR_REG,
     88  1.7    simonb 		.amdsmn_data_reg = F17H_SMN_DATA_REG,
     89  1.7    simonb 	},
     90  1.7    simonb 	{
     91  1.7    simonb 		.amdsmn_deviceid = PCI_PRODUCT_AMD_F17_7X_RC,
     92  1.7    simonb 		.amdsmn_addr_reg = F17H_SMN_ADDR_REG,
     93  1.7    simonb 		.amdsmn_data_reg = F17H_SMN_DATA_REG,
     94  1.7    simonb 	},
     95  1.4   msaitoh };
     96  1.4   msaitoh 
     97  1.1  christos static int amdsmn_match(device_t, cfdata_t, void *);
     98  1.1  christos static void amdsmn_attach(device_t, device_t, void *);
     99  1.2  pgoyette static int amdsmn_rescan(device_t, const char *, const int *);
    100  1.1  christos static int amdsmn_detach(device_t, int);
    101  1.1  christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
    102  1.1  christos 
    103  1.2  pgoyette CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
    104  1.2  pgoyette     amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0);
    105  1.6   msaitoh 
    106  1.1  christos static int
    107  1.6   msaitoh amdsmn_match(device_t parent, cfdata_t match, void *aux)
    108  1.1  christos {
    109  1.1  christos 	struct pci_attach_args *pa = aux;
    110  1.5   msaitoh 	unsigned int i;
    111  1.4   msaitoh 
    112  1.4   msaitoh 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
    113  1.4   msaitoh 		return 0;
    114  1.4   msaitoh 
    115  1.5   msaitoh 	for (i = 0; i < __arraycount(amdsmn_ids); i++)
    116  1.4   msaitoh 		if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid)
    117  1.4   msaitoh 			return 2;
    118  1.4   msaitoh 
    119  1.4   msaitoh 	return 0;
    120  1.1  christos }
    121  1.1  christos 
    122  1.6   msaitoh static int
    123  1.6   msaitoh amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
    124  1.1  christos {
    125  1.1  christos 	if (config_match(parent, cf, aux))
    126  1.1  christos 		config_attach_loc(parent, cf, locs, aux, NULL);
    127  1.1  christos 
    128  1.1  christos 	return 0;
    129  1.1  christos }
    130  1.1  christos 
    131  1.6   msaitoh static void
    132  1.6   msaitoh amdsmn_attach(device_t parent, device_t self, void *aux)
    133  1.1  christos {
    134  1.1  christos 	struct amdsmn_softc *sc = device_private(self);
    135  1.1  christos 	struct pci_attach_args *pa = aux;
    136  1.2  pgoyette 	int flags = 0;
    137  1.7    simonb 	int i;
    138  1.1  christos 
    139  1.1  christos 	mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
    140  1.1  christos 	sc->pa = *pa;
    141  1.1  christos 	sc->pc = pa->pa_pc;
    142  1.1  christos 	sc->pcitag = pa->pa_tag;
    143  1.7    simonb 
    144  1.7    simonb 	for (i = 0; i < __arraycount(amdsmn_ids); i++)
    145  1.7    simonb 		if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid) {
    146  1.7    simonb 			sc->smn_addr_reg = amdsmn_ids[i].amdsmn_addr_reg;
    147  1.7    simonb 			sc->smn_data_reg = amdsmn_ids[i].amdsmn_data_reg;
    148  1.7    simonb 		}
    149  1.7    simonb 
    150  1.7    simonb 	// aprint_normal(": AMD Family 17h System Management Network\n");
    151  1.7    simonb 	aprint_normal(": AMD System Management Network\n");
    152  1.3    kardel 	amdsmn_rescan(self, "amdsmnbus", &flags);
    153  1.2  pgoyette }
    154  1.2  pgoyette 
    155  1.2  pgoyette static int
    156  1.2  pgoyette amdsmn_rescan(device_t self, const char *ifattr, const int *flags)
    157  1.2  pgoyette {
    158  1.2  pgoyette 	struct amdsmn_softc *sc = device_private(self);
    159  1.2  pgoyette 
    160  1.2  pgoyette 	config_search_loc(amdsmn_misc_search, self, ifattr, NULL, &sc->pa);
    161  1.2  pgoyette 
    162  1.2  pgoyette 	return 0;
    163  1.1  christos }
    164  1.1  christos 
    165  1.1  christos static int
    166  1.6   msaitoh amdsmn_detach(device_t self, int flags)
    167  1.1  christos {
    168  1.1  christos 	struct amdsmn_softc *sc = device_private(self);
    169  1.1  christos 
    170  1.1  christos 	mutex_destroy(&sc->smn_lock);
    171  1.1  christos 	aprint_normal_dev(self,"detach!\n");
    172  1.1  christos 
    173  1.1  christos 	return 0;
    174  1.1  christos }
    175  1.1  christos 
    176  1.1  christos int
    177  1.1  christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
    178  1.1  christos {
    179  1.1  christos 	struct amdsmn_softc *sc = device_private(dev);
    180  1.1  christos 
    181  1.1  christos 	mutex_enter(&sc->smn_lock);
    182  1.7    simonb 	pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
    183  1.7    simonb 	*value = pci_conf_read(sc->pc, sc->pcitag, sc->smn_data_reg);
    184  1.1  christos 	mutex_exit(&sc->smn_lock);
    185  1.1  christos 
    186  1.1  christos 	return 0;
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos int
    190  1.1  christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
    191  1.1  christos {
    192  1.1  christos 	struct amdsmn_softc *sc = device_private(dev);
    193  1.1  christos 
    194  1.1  christos 	mutex_enter(&sc->smn_lock);
    195  1.7    simonb 	pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
    196  1.7    simonb 	pci_conf_write(sc->pc, sc->pcitag, sc->smn_data_reg, value);
    197  1.1  christos 	mutex_exit(&sc->smn_lock);
    198  1.1  christos 
    199  1.1  christos 	return 0;
    200  1.1  christos }
    201  1.2  pgoyette 
    202  1.2  pgoyette MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci");
    203  1.2  pgoyette 
    204  1.2  pgoyette #ifdef _MODULE
    205  1.2  pgoyette #include "ioconf.c"
    206  1.2  pgoyette #endif
    207  1.2  pgoyette 
    208  1.2  pgoyette static int
    209  1.2  pgoyette amdsmn_modcmd(modcmd_t cmd, void *opaque)
    210  1.2  pgoyette {
    211  1.2  pgoyette 	int error = 0;
    212  1.2  pgoyette 
    213  1.2  pgoyette #ifdef _MODULE
    214  1.2  pgoyette 	switch (cmd) {
    215  1.2  pgoyette 	case MODULE_CMD_INIT:
    216  1.2  pgoyette 		error = config_init_component(cfdriver_ioconf_amdsmn,
    217  1.2  pgoyette 		    cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
    218  1.2  pgoyette 		break;
    219  1.2  pgoyette 	case MODULE_CMD_FINI:
    220  1.2  pgoyette 		error = config_fini_component(cfdriver_ioconf_amdsmn,
    221  1.2  pgoyette 		    cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
    222  1.2  pgoyette 		break;
    223  1.2  pgoyette 	default:
    224  1.2  pgoyette 		error = ENOTTY;
    225  1.2  pgoyette 		break;
    226  1.2  pgoyette 	}
    227  1.2  pgoyette #endif
    228  1.2  pgoyette 
    229  1.2  pgoyette 	return error;
    230  1.2  pgoyette }
    231  1.2  pgoyette 
    232