1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2018 Rubicon Communications, LLC (Netgate) 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 AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/rman.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/sysctl.h> 41 42 #include <machine/bus.h> 43 #include <machine/resource.h> 44 #include <machine/intr.h> 45 #include <dev/extres/syscon/syscon.h> 46 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 50 #include "syscon_if.h" 51 52 #define CONTROL0 0 /* Offset in config->regs[] array */ 53 #define CONTROL0_TSEN_START (1 << 0) 54 #define CONTROL0_TSEN_RESET (1 << 1) 55 #define CONTROL0_TSEN_EN (1 << 2) 56 #define CONTROL0_CHANNEL_SHIFT 13 57 #define CONTROL0_CHANNEL_MASK 0xF 58 #define CONTROL0_OSR_SHIFT 24 59 #define CONTROL0_OSR_MAX 3 /* OSR = 512 * 4uS = ~2mS */ 60 #define CONTROL0_MODE_SHIFT 30 61 #define CONTROL0_MODE_EXTERNAL 0x2 62 #define CONTROL0_MODE_MASK 0x3 63 64 #define CONTROL1 1 /* Offset in config->regs[] array */ 65 /* This doesn't seems to work */ 66 #define CONTROL1_TSEN_SENS_SHIFT 21 67 #define CONTROL1_TSEN_SENS_MASK 0x7 68 69 #define STATUS 2 /* Offset in config->regs[] array */ 70 #define STATUS_TEMP_MASK 0x3FF 71 72 enum mv_thermal_type { 73 MV_AP806 = 1, 74 MV_CP110, 75 }; 76 77 struct mv_thermal_config { 78 enum mv_thermal_type type; 79 int regs[3]; 80 int ncpus; 81 int64_t calib_mul; 82 int64_t calib_add; 83 int64_t calib_div; 84 uint32_t valid_mask; 85 bool signed_value; 86 }; 87 88 struct mv_thermal_softc { 89 device_t dev; 90 struct syscon *syscon; 91 struct mtx mtx; 92 93 struct mv_thermal_config *config; 94 int cur_sensor; 95 }; 96 97 static struct mv_thermal_config mv_ap806_config = { 98 .type = MV_AP806, 99 .regs = {0x84, 0x88, 0x8C}, 100 .ncpus = 4, 101 .calib_mul = 423, 102 .calib_add = -150000, 103 .calib_div = 100, 104 .valid_mask = (1 << 16), 105 .signed_value = true, 106 }; 107 108 static struct mv_thermal_config mv_cp110_config = { 109 .type = MV_CP110, 110 .regs = {0x70, 0x74, 0x78}, 111 .calib_mul = 2000096, 112 .calib_add = 1172499100, 113 .calib_div = 420100, 114 .valid_mask = (1 << 10), 115 .signed_value = false, 116 }; 117 118 static struct ofw_compat_data compat_data[] = { 119 {"marvell,armada-ap806-thermal", (uintptr_t) &mv_ap806_config}, 120 {"marvell,armada-cp110-thermal", (uintptr_t) &mv_cp110_config}, 121 {NULL, 0} 122 }; 123 124 #define RD4(sc, reg) \ 125 SYSCON_READ_4((sc)->syscon, sc->config->regs[reg]) 126 #define WR4(sc, reg, val) \ 127 SYSCON_WRITE_4((sc)->syscon, sc->config->regs[reg], (val)) 128 129 static inline int32_t sign_extend(uint32_t value, int index) 130 { 131 uint8_t shift; 132 133 shift = 31 - index; 134 return ((int32_t)(value << shift) >> shift); 135 } 136 137 static int 138 mv_thermal_wait_sensor(struct mv_thermal_softc *sc) 139 { 140 uint32_t reg; 141 uint32_t timeout; 142 143 timeout = 100000; 144 while (--timeout > 0) { 145 reg = RD4(sc, STATUS); 146 if ((reg & sc->config->valid_mask) == sc->config->valid_mask) 147 break; 148 DELAY(100); 149 } 150 if (timeout == 0) { 151 return (ETIMEDOUT); 152 } 153 154 return (0); 155 } 156 157 static int 158 mv_thermal_select_sensor(struct mv_thermal_softc *sc, int sensor) 159 { 160 uint32_t reg; 161 162 if (sc->cur_sensor == sensor) 163 return (0); 164 165 /* Stop the current reading and reset the module */ 166 reg = RD4(sc, CONTROL0); 167 reg &= ~(CONTROL0_TSEN_START | CONTROL0_TSEN_EN); 168 WR4(sc, CONTROL0, reg); 169 170 /* Switch to the selected sensor */ 171 /* 172 * NOTE : Datasheet says to use CONTROL1 for selecting 173 * but when doing so the sensors >0 are never ready 174 * Do what Linux does using undocumented bits in CONTROL0 175 */ 176 /* This reset automatically to the sensor 0 */ 177 reg &= ~(CONTROL0_MODE_MASK << CONTROL0_MODE_SHIFT); 178 if (sensor) { 179 /* Select external sensor */ 180 reg |= CONTROL0_MODE_EXTERNAL << CONTROL0_MODE_SHIFT; 181 reg &= ~(CONTROL0_CHANNEL_MASK << CONTROL0_CHANNEL_SHIFT); 182 reg |= (sensor - 1) << CONTROL0_CHANNEL_SHIFT; 183 } 184 WR4(sc, CONTROL0, reg); 185 sc->cur_sensor = sensor; 186 187 /* Start the reading */ 188 reg = RD4(sc, CONTROL0); 189 reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN; 190 WR4(sc, CONTROL0, reg); 191 192 return (mv_thermal_wait_sensor(sc)); 193 } 194 195 static int 196 mv_thermal_read_sensor(struct mv_thermal_softc *sc, int sensor, int *temp) 197 { 198 uint32_t reg; 199 int64_t sample, rv; 200 201 rv = mv_thermal_select_sensor(sc, sensor); 202 if (rv != 0) 203 return (rv); 204 205 reg = RD4(sc, STATUS) & STATUS_TEMP_MASK; 206 207 if (sc->config->signed_value) 208 sample = sign_extend(reg, fls(STATUS_TEMP_MASK) - 1); 209 else 210 sample = reg; 211 212 *temp = ((sample * sc->config->calib_mul) - sc->config->calib_add) / 213 sc->config->calib_div; 214 215 return (0); 216 } 217 218 static int 219 ap806_init(struct mv_thermal_softc *sc) 220 { 221 uint32_t reg; 222 223 /* Start the temp capture/conversion */ 224 reg = RD4(sc, CONTROL0); 225 reg &= ~CONTROL0_TSEN_RESET; 226 reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN; 227 228 /* Sample every ~2ms */ 229 reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT; 230 231 WR4(sc, CONTROL0, reg); 232 233 /* Since we just started the module wait for the sensor to be ready */ 234 mv_thermal_wait_sensor(sc); 235 236 return (0); 237 } 238 239 static int 240 cp110_init(struct mv_thermal_softc *sc) 241 { 242 uint32_t reg; 243 244 reg = RD4(sc, CONTROL1); 245 reg &= (1 << 7); 246 reg |= (1 << 8); 247 WR4(sc, CONTROL1, reg); 248 249 /* Sample every ~2ms */ 250 reg = RD4(sc, CONTROL0); 251 reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT; 252 WR4(sc, CONTROL0, reg); 253 254 return (0); 255 } 256 257 static int 258 mv_thermal_sysctl(SYSCTL_HANDLER_ARGS) 259 { 260 struct mv_thermal_softc *sc; 261 device_t dev = arg1; 262 int sensor = arg2; 263 int val = 0; 264 265 sc = device_get_softc(dev); 266 mtx_lock(&(sc)->mtx); 267 268 if (mv_thermal_read_sensor(sc, sensor, &val) == 0) { 269 /* Convert to Kelvin */ 270 val = val + 2732; 271 } else { 272 device_printf(dev, "Timeout waiting for sensor\n"); 273 } 274 275 mtx_unlock(&(sc)->mtx); 276 return sysctl_handle_opaque(oidp, &val, sizeof(val), req); 277 } 278 279 static int 280 mv_thermal_probe(device_t dev) 281 { 282 283 if (!ofw_bus_status_okay(dev)) 284 return (ENXIO); 285 286 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 287 return (ENXIO); 288 289 device_set_desc(dev, "Marvell Thermal Sensor Controller"); 290 return (BUS_PROBE_DEFAULT); 291 } 292 293 static int 294 mv_thermal_attach(device_t dev) 295 { 296 struct mv_thermal_softc *sc; 297 struct sysctl_ctx_list *ctx; 298 struct sysctl_oid_list *oid; 299 char name[255]; 300 char desc[255]; 301 int i; 302 303 sc = device_get_softc(dev); 304 sc->dev = dev; 305 306 sc->config = (struct mv_thermal_config *) 307 ofw_bus_search_compatible(dev, compat_data)->ocd_data; 308 309 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 310 311 if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 || 312 sc->syscon == NULL) { 313 device_printf(dev, "cannot get syscon for device\n"); 314 return (ENXIO); 315 } 316 317 sc->cur_sensor = -1; 318 switch (sc->config->type) { 319 case MV_AP806: 320 ap806_init(sc); 321 break; 322 case MV_CP110: 323 cp110_init(sc); 324 break; 325 } 326 327 ctx = device_get_sysctl_ctx(dev); 328 oid = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 329 /* There is always at least one sensor */ 330 SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, "internal", 331 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 332 dev, 0, mv_thermal_sysctl, 333 "IK", 334 "Internal Temperature"); 335 336 for (i = 0; i < sc->config->ncpus; i++) { 337 snprintf(name, sizeof(name), "cpu%d", i); 338 snprintf(desc, sizeof(desc), "CPU%d Temperature", i); 339 SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, name, 340 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 341 dev, i + 1, mv_thermal_sysctl, 342 "IK", 343 desc); 344 } 345 346 return (0); 347 } 348 349 static int 350 mv_thermal_detach(device_t dev) 351 { 352 return (0); 353 } 354 355 static device_method_t mv_thermal_methods[] = { 356 /* Device interface */ 357 DEVMETHOD(device_probe, mv_thermal_probe), 358 DEVMETHOD(device_attach, mv_thermal_attach), 359 DEVMETHOD(device_detach, mv_thermal_detach), 360 361 DEVMETHOD_END 362 }; 363 364 static driver_t mv_thermal_driver = { 365 "mv_thermal", 366 mv_thermal_methods, 367 sizeof(struct mv_thermal_softc), 368 }; 369 370 DRIVER_MODULE(mv_thermal, simplebus, mv_thermal_driver, 0, 0); 371