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