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