amdsmn.c revision 1.14 1 1.14 msaitoh /* $NetBSD: amdsmn.c,v 1.14 2022/10/01 15:50:05 msaitoh 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.14 msaitoh __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.14 2022/10/01 15:50:05 msaitoh 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.13 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F15_6X_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.14 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_6X_RC,
92 1.14 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
93 1.14 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
94 1.14 msaitoh },
95 1.14 msaitoh {
96 1.7 simonb .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_7X_RC,
97 1.7 simonb .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
98 1.7 simonb .amdsmn_data_reg = F17H_SMN_DATA_REG,
99 1.7 simonb },
100 1.14 msaitoh {
101 1.14 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F19_6X_RC,
102 1.14 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
103 1.14 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
104 1.14 msaitoh },
105 1.4 msaitoh };
106 1.4 msaitoh
107 1.1 christos static int amdsmn_match(device_t, cfdata_t, void *);
108 1.1 christos static void amdsmn_attach(device_t, device_t, void *);
109 1.2 pgoyette static int amdsmn_rescan(device_t, const char *, const int *);
110 1.1 christos static int amdsmn_detach(device_t, int);
111 1.1 christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
112 1.1 christos
113 1.2 pgoyette CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
114 1.2 pgoyette amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0);
115 1.6 msaitoh
116 1.1 christos static int
117 1.6 msaitoh amdsmn_match(device_t parent, cfdata_t match, void *aux)
118 1.1 christos {
119 1.1 christos struct pci_attach_args *pa = aux;
120 1.9 simonb size_t i;
121 1.4 msaitoh
122 1.4 msaitoh if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
123 1.4 msaitoh return 0;
124 1.4 msaitoh
125 1.5 msaitoh for (i = 0; i < __arraycount(amdsmn_ids); i++)
126 1.4 msaitoh if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid)
127 1.4 msaitoh return 2;
128 1.4 msaitoh
129 1.4 msaitoh return 0;
130 1.1 christos }
131 1.1 christos
132 1.6 msaitoh static int
133 1.6 msaitoh amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
134 1.1 christos {
135 1.11 thorpej if (config_probe(parent, cf, aux))
136 1.11 thorpej config_attach(parent, cf, aux, NULL,
137 1.12 thorpej CFARGS(.locators = locs));
138 1.1 christos
139 1.1 christos return 0;
140 1.1 christos }
141 1.1 christos
142 1.6 msaitoh static void
143 1.6 msaitoh amdsmn_attach(device_t parent, device_t self, void *aux)
144 1.1 christos {
145 1.1 christos struct amdsmn_softc *sc = device_private(self);
146 1.1 christos struct pci_attach_args *pa = aux;
147 1.8 joerg size_t i;
148 1.1 christos
149 1.1 christos mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
150 1.1 christos sc->pa = *pa;
151 1.1 christos sc->pc = pa->pa_pc;
152 1.1 christos sc->pcitag = pa->pa_tag;
153 1.7 simonb
154 1.7 simonb for (i = 0; i < __arraycount(amdsmn_ids); i++)
155 1.7 simonb if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid) {
156 1.7 simonb sc->smn_addr_reg = amdsmn_ids[i].amdsmn_addr_reg;
157 1.7 simonb sc->smn_data_reg = amdsmn_ids[i].amdsmn_data_reg;
158 1.7 simonb }
159 1.7 simonb
160 1.7 simonb // aprint_normal(": AMD Family 17h System Management Network\n");
161 1.7 simonb aprint_normal(": AMD System Management Network\n");
162 1.11 thorpej amdsmn_rescan(self, NULL, NULL);
163 1.2 pgoyette }
164 1.2 pgoyette
165 1.2 pgoyette static int
166 1.11 thorpej amdsmn_rescan(device_t self, const char *ifattr, const int *locators)
167 1.2 pgoyette {
168 1.2 pgoyette struct amdsmn_softc *sc = device_private(self);
169 1.2 pgoyette
170 1.11 thorpej config_search(self, &sc->pa,
171 1.12 thorpej CFARGS(.search = amdsmn_misc_search));
172 1.2 pgoyette
173 1.2 pgoyette return 0;
174 1.1 christos }
175 1.1 christos
176 1.1 christos static int
177 1.6 msaitoh amdsmn_detach(device_t self, int flags)
178 1.1 christos {
179 1.1 christos struct amdsmn_softc *sc = device_private(self);
180 1.1 christos
181 1.1 christos mutex_destroy(&sc->smn_lock);
182 1.1 christos aprint_normal_dev(self,"detach!\n");
183 1.1 christos
184 1.1 christos return 0;
185 1.1 christos }
186 1.1 christos
187 1.1 christos int
188 1.1 christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
189 1.1 christos {
190 1.1 christos struct amdsmn_softc *sc = device_private(dev);
191 1.1 christos
192 1.1 christos mutex_enter(&sc->smn_lock);
193 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
194 1.7 simonb *value = pci_conf_read(sc->pc, sc->pcitag, sc->smn_data_reg);
195 1.1 christos mutex_exit(&sc->smn_lock);
196 1.1 christos
197 1.1 christos return 0;
198 1.1 christos }
199 1.1 christos
200 1.1 christos int
201 1.1 christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
202 1.1 christos {
203 1.1 christos struct amdsmn_softc *sc = device_private(dev);
204 1.1 christos
205 1.1 christos mutex_enter(&sc->smn_lock);
206 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
207 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_data_reg, value);
208 1.1 christos mutex_exit(&sc->smn_lock);
209 1.1 christos
210 1.1 christos return 0;
211 1.1 christos }
212 1.2 pgoyette
213 1.2 pgoyette MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci");
214 1.2 pgoyette
215 1.2 pgoyette #ifdef _MODULE
216 1.2 pgoyette #include "ioconf.c"
217 1.2 pgoyette #endif
218 1.2 pgoyette
219 1.2 pgoyette static int
220 1.2 pgoyette amdsmn_modcmd(modcmd_t cmd, void *opaque)
221 1.2 pgoyette {
222 1.2 pgoyette int error = 0;
223 1.2 pgoyette
224 1.2 pgoyette #ifdef _MODULE
225 1.2 pgoyette switch (cmd) {
226 1.2 pgoyette case MODULE_CMD_INIT:
227 1.2 pgoyette error = config_init_component(cfdriver_ioconf_amdsmn,
228 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
229 1.2 pgoyette break;
230 1.2 pgoyette case MODULE_CMD_FINI:
231 1.2 pgoyette error = config_fini_component(cfdriver_ioconf_amdsmn,
232 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
233 1.2 pgoyette break;
234 1.2 pgoyette default:
235 1.2 pgoyette error = ENOTTY;
236 1.2 pgoyette break;
237 1.2 pgoyette }
238 1.2 pgoyette #endif
239 1.2 pgoyette
240 1.2 pgoyette return error;
241 1.2 pgoyette }
242 1.2 pgoyette
243