1 /* $OpenBSD: stftemp.c,v 1.2 2023/07/01 08:20:38 jsing Exp $ */ 2 /* 3 * Copyright (c) 2022 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/sensors.h> 22 23 #include <machine/intr.h> 24 #include <machine/bus.h> 25 #include <machine/fdt.h> 26 27 #include <dev/ofw/openfirm.h> 28 #include <dev/ofw/ofw_clock.h> 29 #include <dev/ofw/ofw_thermal.h> 30 #include <dev/ofw/fdt.h> 31 32 /* Registers */ 33 #define TEMP 0x0000 34 #define TEMP_PD (1 << 1) 35 #define TEMP_RSTN (1 << 0) 36 #define TEMP_RUN (1 << 2) 37 #define TEMP_DOUT_MASK 0x0fff0000 38 #define TEMP_DOUT_SHIFT 16 39 40 #define HREAD4(sc, reg) \ 41 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 42 #define HWRITE4(sc, reg, val) \ 43 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 44 45 struct stftemp_softc { 46 struct device sc_dev; 47 bus_space_tag_t sc_iot; 48 bus_space_handle_t sc_ioh; 49 50 struct ksensor sc_sensor; 51 struct ksensordev sc_sensordev; 52 53 struct thermal_sensor sc_ts; 54 }; 55 56 int stftemp_match(struct device *, void *, void *); 57 void stftemp_attach(struct device *, struct device *, void *); 58 59 const struct cfattach stftemp_ca = { 60 sizeof (struct stftemp_softc), stftemp_match, stftemp_attach 61 }; 62 63 struct cfdriver stftemp_cd = { 64 NULL, "stftemp", DV_DULL 65 }; 66 67 void stftemp_refresh_sensors(void *); 68 int32_t stftemp_get_temperature(void *, uint32_t *); 69 70 int 71 stftemp_match(struct device *parent, void *match, void *aux) 72 { 73 struct fdt_attach_args *faa = aux; 74 75 return OF_is_compatible(faa->fa_node, "starfive,jh7100-temp") || 76 OF_is_compatible(faa->fa_node, "starfive,jh7110-temp"); 77 } 78 79 void 80 stftemp_attach(struct device *parent, struct device *self, void *aux) 81 { 82 struct stftemp_softc *sc = (struct stftemp_softc *)self; 83 struct fdt_attach_args *faa = aux; 84 85 if (faa->fa_nreg < 1) { 86 printf(": no registers\n"); 87 return; 88 } 89 90 sc->sc_iot = faa->fa_iot; 91 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 92 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 93 printf(": can't map registers\n"); 94 return; 95 } 96 97 printf("\n"); 98 99 clock_enable(faa->fa_node, "bus"); 100 reset_deassert(faa->fa_node, "bus"); 101 102 clock_enable(faa->fa_node, "sense"); 103 reset_deassert(faa->fa_node, "sense"); 104 105 /* Power down */ 106 HWRITE4(sc, TEMP, TEMP_PD); 107 delay(1); 108 109 /* Power up with reset asserted */ 110 HWRITE4(sc, TEMP, 0); 111 delay(60); 112 113 /* Deassert reset */ 114 HWRITE4(sc, TEMP, TEMP_RSTN); 115 delay(1); 116 117 /* Start measuring */ 118 HWRITE4(sc, TEMP, TEMP_RSTN | TEMP_RUN); 119 120 /* Register sensor */ 121 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 122 sizeof(sc->sc_sensordev.xname)); 123 sc->sc_sensor.type = SENSOR_TEMP; 124 sc->sc_sensor.flags = SENSOR_FINVALID; 125 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); 126 sensordev_install(&sc->sc_sensordev); 127 sensor_task_register(sc, stftemp_refresh_sensors, 5); 128 129 sc->sc_ts.ts_node = faa->fa_node; 130 sc->sc_ts.ts_cookie = sc; 131 sc->sc_ts.ts_get_temperature = stftemp_get_temperature; 132 thermal_sensor_register(&sc->sc_ts); 133 } 134 135 int32_t 136 stftemp_get_temp(struct stftemp_softc *sc) 137 { 138 int32_t value; 139 140 value = HREAD4(sc, TEMP); 141 value = (value & TEMP_DOUT_MASK) >> TEMP_DOUT_SHIFT; 142 143 return (value * 237500) / 4094 - 81100; 144 } 145 146 void 147 stftemp_refresh_sensors(void *arg) 148 { 149 struct stftemp_softc *sc = arg; 150 151 sc->sc_sensor.value = 273150000 + 1000 * stftemp_get_temp(sc); 152 sc->sc_sensor.flags &= ~SENSOR_FINVALID; 153 } 154 155 int32_t 156 stftemp_get_temperature(void *cookie, uint32_t *cells) 157 { 158 return stftemp_get_temp(cookie); 159 } 160