1 /* $NetBSD: mvsocts.c,v 1.3 2023/06/19 08:40:29 msaitoh Exp $ */
2 /*
3 * Copyright (c) 2012, 2015 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: mvsocts.c,v 1.3 2023/06/19 08:40:29 msaitoh Exp $");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/device.h>
33 #include <sys/errno.h>
34
35 #include <dev/sysmon/sysmonvar.h>
36
37 #include <dev/marvell/marvellreg.h>
38 #include <dev/marvell/marvellvar.h>
39
40 #define TS_STATUS 0x0
41 #define STATUS_VAL(v) (((v) >> 10) & 0x1ff)
42
43 struct mvsocts_softc {
44 device_t sc_dev;
45
46 bool (*sc_isvalid)(int);
47 int (*sc_val2uc)(int);
48
49 struct sysmon_envsys *sc_sme;
50 envsys_data_t sc_sensor;
51
52 bus_space_tag_t sc_iot;
53 bus_space_handle_t sc_ioh;
54 };
55
56
57 static int mvsocts_match(device_t, struct cfdata *, void *);
58 static void mvsocts_attach(device_t, device_t, void *);
59
60 static void mvsocts_refresh(struct sysmon_envsys *, envsys_data_t *);
61
62 static bool mvsocts_isvalid_88f6282(int);
63 static int mvsocts_val2uc_88f6282(int);
64
65 static bool mvsocts_isvalid_armada(int);
66 static int mvsocts_val2uc_armada(int);
67
68 CFATTACH_DECL_NEW(mvsocts, sizeof(struct mvsocts_softc),
69 mvsocts_match, mvsocts_attach, NULL, NULL);
70
71
72 /* ARGSUSED */
73 static int
mvsocts_match(device_t parent,struct cfdata * match,void * aux)74 mvsocts_match(device_t parent, struct cfdata *match, void *aux)
75 {
76 struct marvell_attach_args *mva = aux;
77
78 if (strcmp(mva->mva_name, match->cf_name) != 0)
79 return 0;
80 return 1;
81 }
82
83 /* ARGSUSED */
84 static void
mvsocts_attach(device_t parent,device_t self,void * aux)85 mvsocts_attach(device_t parent, device_t self, void *aux)
86 {
87 struct mvsocts_softc *sc = device_private(self);
88 struct marvell_attach_args *mva = aux;
89
90 aprint_naive("\n");
91 aprint_normal(": Marvell SoC Thermal Sensor\n");
92
93 sc->sc_dev = self;
94 sc->sc_iot = mva->mva_iot;
95 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
96 mva->mva_offset, sizeof(uint32_t), &sc->sc_ioh))
97 panic("%s: Cannot map registers", device_xname(self));
98
99 switch (mva->mva_model) {
100 case MARVELL_KIRKWOOD_88F6282:
101 sc->sc_isvalid = mvsocts_isvalid_88f6282;
102 sc->sc_val2uc = mvsocts_val2uc_88f6282;
103 break;
104 case MARVELL_ARMADAXP_MV78130:
105 case MARVELL_ARMADAXP_MV78160:
106 case MARVELL_ARMADAXP_MV78230:
107 case MARVELL_ARMADAXP_MV78260:
108 case MARVELL_ARMADAXP_MV78460:
109 /*
110 case MARVELL_ARMADA370_MV6707:
111 case MARVELL_ARMADA370_MV6710:
112 case MARVELL_ARMADA370_MV6W11:
113 */
114 sc->sc_isvalid = mvsocts_isvalid_armada;
115 sc->sc_val2uc = mvsocts_val2uc_armada;
116 break;
117
118 default:
119 aprint_error_dev(self, "unknown model: 0x%x", mva->mva_model);
120 return;
121 }
122
123 sc->sc_sme = sysmon_envsys_create();
124 /* Initialize sensor data. */
125 sc->sc_sensor.units = ENVSYS_STEMP;
126 sc->sc_sensor.state = ENVSYS_SINVALID;
127 strlcpy(sc->sc_sensor.desc, device_xname(self),
128 sizeof(sc->sc_sensor.desc));
129 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
130 aprint_error_dev(self, "Unable to attach sysmon\n");
131 sysmon_envsys_destroy(sc->sc_sme);
132 return;
133 }
134
135 /* Hook into system monitor. */
136 sc->sc_sme->sme_name = device_xname(self);
137 sc->sc_sme->sme_cookie = sc;
138 sc->sc_sme->sme_refresh = mvsocts_refresh;
139 if (sysmon_envsys_register(sc->sc_sme)) {
140 aprint_error_dev(self, "Unable to register with sysmon\n");
141 sysmon_envsys_destroy(sc->sc_sme);
142 }
143 }
144
145 static void
mvsocts_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)146 mvsocts_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
147 {
148 struct mvsocts_softc *sc = sme->sme_cookie;
149 uint32_t val, uc, uk;
150
151 val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, TS_STATUS);
152 if (!sc->sc_isvalid(val)) {
153 aprint_error_dev(sc->sc_dev, "status value is invalid\n");
154 return;
155 }
156 uc = sc->sc_val2uc(STATUS_VAL(val)); /* uC */
157 uk = uc + 273150000; /* convert to uKelvin */
158 sc->sc_sensor.value_cur = uk;
159 sc->sc_sensor.state = ENVSYS_SVALID;
160 }
161
162 static bool
mvsocts_isvalid_88f6282(int v)163 mvsocts_isvalid_88f6282(int v)
164 {
165
166 return (v & (1 << 9)) != 0;
167 }
168
169 static int
mvsocts_val2uc_88f6282(int v)170 mvsocts_val2uc_88f6282(int v)
171 {
172
173 return (322 - v) * 10 * 1000000 / 13625 * 1000;
174 }
175
176 static bool
mvsocts_isvalid_armada(int v)177 mvsocts_isvalid_armada(int v)
178 {
179
180 return (v & (1 << 0)) != 0;
181 }
182
183 static int
mvsocts_val2uc_armada(int v)184 mvsocts_val2uc_armada(int v)
185 {
186
187 return (3153 - v * 10) * 1000000 / 13825 * 1000;
188 }
189