amdsmn.c revision 1.3 1 1.3 kardel /* $NetBSD: amdsmn.c,v 1.3 2018/01/27 21:24:30 kardel 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.3 kardel __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.3 2018/01/27 21:24:30 kardel 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 #define AMD_17H_MANAGEMENT_NETWORK_PCI_ID 0x14501022
58 1.1 christos
59 1.1 christos struct amdsmn_softc {
60 1.1 christos kmutex_t smn_lock;
61 1.1 christos struct pci_attach_args pa;
62 1.1 christos pci_chipset_tag_t pc;
63 1.1 christos pcitag_t pcitag;
64 1.1 christos };
65 1.1 christos
66 1.1 christos static int amdsmn_match(device_t, cfdata_t, void *);
67 1.1 christos static void amdsmn_attach(device_t, device_t, void *);
68 1.2 pgoyette static int amdsmn_rescan(device_t, const char *, const int *);
69 1.1 christos static int amdsmn_detach(device_t, int);
70 1.1 christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
71 1.1 christos
72 1.2 pgoyette CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
73 1.2 pgoyette amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0);
74 1.1 christos
75 1.1 christos static int
76 1.1 christos amdsmn_match(device_t parent, cfdata_t match, void *aux)
77 1.1 christos {
78 1.1 christos struct pci_attach_args *pa = aux;
79 1.1 christos
80 1.1 christos return pa->pa_id == AMD_17H_MANAGEMENT_NETWORK_PCI_ID ? 2 : 0;
81 1.1 christos }
82 1.1 christos
83 1.1 christos static int
84 1.1 christos amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
85 1.1 christos {
86 1.1 christos if (config_match(parent, cf, aux))
87 1.1 christos config_attach_loc(parent, cf, locs, aux, NULL);
88 1.1 christos
89 1.1 christos return 0;
90 1.1 christos }
91 1.1 christos
92 1.1 christos static void
93 1.1 christos amdsmn_attach(device_t parent, device_t self, void *aux)
94 1.1 christos {
95 1.1 christos struct amdsmn_softc *sc = device_private(self);
96 1.1 christos struct pci_attach_args *pa = aux;
97 1.2 pgoyette int flags = 0;
98 1.1 christos
99 1.1 christos mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
100 1.1 christos sc->pa = *pa;
101 1.1 christos sc->pc = pa->pa_pc;
102 1.1 christos sc->pcitag = pa->pa_tag;
103 1.1 christos aprint_normal(": AMD Family 17h System Management Network\n");
104 1.3 kardel amdsmn_rescan(self, "amdsmnbus", &flags);
105 1.2 pgoyette }
106 1.2 pgoyette
107 1.2 pgoyette static int
108 1.2 pgoyette amdsmn_rescan(device_t self, const char *ifattr, const int *flags)
109 1.2 pgoyette {
110 1.2 pgoyette struct amdsmn_softc *sc = device_private(self);
111 1.2 pgoyette
112 1.2 pgoyette config_search_loc(amdsmn_misc_search, self, ifattr, NULL, &sc->pa);
113 1.2 pgoyette
114 1.2 pgoyette return 0;
115 1.1 christos }
116 1.1 christos
117 1.1 christos static int
118 1.1 christos amdsmn_detach(device_t self, int flags)
119 1.1 christos {
120 1.1 christos struct amdsmn_softc *sc = device_private(self);
121 1.1 christos
122 1.1 christos mutex_destroy(&sc->smn_lock);
123 1.1 christos aprint_normal_dev(self,"detach!\n");
124 1.1 christos
125 1.1 christos return 0;
126 1.1 christos }
127 1.1 christos
128 1.1 christos int
129 1.1 christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
130 1.1 christos {
131 1.1 christos struct amdsmn_softc *sc = device_private(dev);
132 1.1 christos
133 1.1 christos mutex_enter(&sc->smn_lock);
134 1.1 christos pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
135 1.1 christos *value = pci_conf_read(sc->pc, sc->pcitag, SMN_DATA_REG);
136 1.1 christos mutex_exit(&sc->smn_lock);
137 1.1 christos
138 1.1 christos return 0;
139 1.1 christos }
140 1.1 christos
141 1.1 christos int
142 1.1 christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
143 1.1 christos {
144 1.1 christos struct amdsmn_softc *sc = device_private(dev);
145 1.1 christos
146 1.1 christos mutex_enter(&sc->smn_lock);
147 1.1 christos pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr);
148 1.1 christos pci_conf_write(sc->pc, sc->pcitag, SMN_DATA_REG, value);
149 1.1 christos mutex_exit(&sc->smn_lock);
150 1.1 christos
151 1.1 christos return 0;
152 1.1 christos }
153 1.2 pgoyette
154 1.2 pgoyette MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci");
155 1.2 pgoyette
156 1.2 pgoyette #ifdef _MODULE
157 1.2 pgoyette #include "ioconf.c"
158 1.2 pgoyette #endif
159 1.2 pgoyette
160 1.2 pgoyette static int
161 1.2 pgoyette amdsmn_modcmd(modcmd_t cmd, void *opaque)
162 1.2 pgoyette {
163 1.2 pgoyette int error = 0;
164 1.2 pgoyette
165 1.2 pgoyette #ifdef _MODULE
166 1.2 pgoyette switch (cmd) {
167 1.2 pgoyette case MODULE_CMD_INIT:
168 1.2 pgoyette error = config_init_component(cfdriver_ioconf_amdsmn,
169 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
170 1.2 pgoyette break;
171 1.2 pgoyette case MODULE_CMD_FINI:
172 1.2 pgoyette error = config_fini_component(cfdriver_ioconf_amdsmn,
173 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
174 1.2 pgoyette break;
175 1.2 pgoyette default:
176 1.2 pgoyette error = ENOTTY;
177 1.2 pgoyette break;
178 1.2 pgoyette }
179 1.2 pgoyette #endif
180 1.2 pgoyette
181 1.2 pgoyette return error;
182 1.2 pgoyette }
183 1.2 pgoyette
184