1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016-2022 Stormshield 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 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/rman.h> 35 #include <sys/sysctl.h> 36 #include <sys/watchdog.h> 37 38 #include <dev/superio/superio.h> 39 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 43 #define NCTHWM_FAN_MAX 5 44 45 #define NCTHWM_BANK_SELECT 0x4e 46 #define NCTHWM_VENDOR_ID 0x4f 47 48 #define NCTHWM_VERBOSE_PRINTF(dev, ...) \ 49 do { \ 50 if (__predict_false(bootverbose)) \ 51 device_printf(dev, __VA_ARGS__); \ 52 } while (0) 53 54 struct ncthwm_softc { 55 device_t dev; 56 struct ncthwm_device *nctdevp; 57 struct resource *iores; 58 int iorid; 59 }; 60 61 struct ncthwm_fan_info 62 { 63 const char *name; 64 uint8_t low_byte_offset; 65 uint8_t high_byte_offset; 66 }; 67 68 struct ncthwm_device { 69 uint16_t devid; 70 const char *descr; 71 uint8_t base_offset; 72 uint8_t fan_bank; 73 uint8_t fan_count; 74 struct ncthwm_fan_info fan_info[NCTHWM_FAN_MAX]; 75 } ncthwm_devices[] = { 76 { 77 .devid = 0xc562, 78 .descr = "HWM on Nuvoton NCT6779D", 79 .base_offset = 5, 80 .fan_bank = 4, 81 .fan_count = 5, 82 .fan_info = { 83 { .name = "SYSFAN", .low_byte_offset = 0xc1, .high_byte_offset = 0xc0 }, 84 { .name = "CPUFAN", .low_byte_offset = 0xc3, .high_byte_offset = 0xc2 }, 85 { .name = "AUXFAN0", .low_byte_offset = 0xc5, .high_byte_offset = 0xc4 }, 86 { .name = "AUXFAN1", .low_byte_offset = 0xc7, .high_byte_offset = 0xc6 }, 87 { .name = "AUXFAN2", .low_byte_offset = 0xc9, .high_byte_offset = 0xc8 }, 88 }, 89 }, { 90 .devid = 0xd42a, 91 .descr = "HWM on Nuvoton NCT6796D-E", 92 .base_offset = 5, 93 .fan_bank = 4, 94 .fan_count = 5, 95 .fan_info = { 96 { .name = "SYSFAN", .low_byte_offset = 0xc1, .high_byte_offset = 0xc0 }, 97 { .name = "CPUFAN", .low_byte_offset = 0xc3, .high_byte_offset = 0xc2 }, 98 { .name = "AUXFAN0", .low_byte_offset = 0xc5, .high_byte_offset = 0xc4 }, 99 { .name = "AUXFAN1", .low_byte_offset = 0xc7, .high_byte_offset = 0xc6 }, 100 { .name = "AUXFAN2", .low_byte_offset = 0xc9, .high_byte_offset = 0xc8 }, 101 }, 102 } 103 }; 104 105 static struct ncthwm_device * 106 ncthwm_lookup_device(device_t dev) 107 { 108 int i; 109 uint16_t devid; 110 111 devid = superio_devid(dev); 112 for (i = 0; i < nitems(ncthwm_devices); i++) { 113 if (devid == ncthwm_devices[i].devid) 114 return (ncthwm_devices + i); 115 } 116 return (NULL); 117 } 118 119 static void 120 ncthwm_write(struct ncthwm_softc *sc, uint8_t reg, uint8_t val) 121 { 122 bus_write_1(sc->iores, 0, reg); 123 bus_write_1(sc->iores, 1, val); 124 } 125 126 static uint8_t 127 ncthwm_read(struct ncthwm_softc *sc, uint8_t reg) 128 { 129 bus_write_1(sc->iores, 0, reg); 130 return (bus_read_1(sc->iores, 1)); 131 } 132 133 static int 134 ncthwm_query_fan_speed(SYSCTL_HANDLER_ARGS) 135 { 136 struct ncthwm_softc *sc; 137 struct ncthwm_fan_info *fan; 138 uint16_t val; 139 140 sc = arg1; 141 if (sc == NULL) 142 return (EINVAL); 143 144 KASSERT(sc->nctdevp != NULL, ("Unreachable")); 145 146 if (sc->nctdevp->fan_count <= arg2) 147 return (EINVAL); 148 fan = &sc->nctdevp->fan_info[arg2]; 149 150 KASSERT(sc->iores != NULL, ("Unreachable")); 151 152 ncthwm_write(sc, NCTHWM_BANK_SELECT, sc->nctdevp->fan_bank); 153 val = ncthwm_read(sc, fan->high_byte_offset) << 8; 154 val |= ncthwm_read(sc, fan->low_byte_offset); 155 156 NCTHWM_VERBOSE_PRINTF(sc->dev, "%s: read %u from bank %u offset 0x%x-0x%x\n", 157 fan->name, val, sc->nctdevp->fan_bank, fan->high_byte_offset, fan->low_byte_offset); 158 159 return (sysctl_handle_16(oidp, &val, 0, req)); 160 } 161 162 static int 163 ncthwm_probe(device_t dev) 164 { 165 struct ncthwm_device *nctdevp; 166 uint8_t ldn; 167 168 ldn = superio_get_ldn(dev); 169 170 if (superio_vendor(dev) != SUPERIO_VENDOR_NUVOTON) { 171 NCTHWM_VERBOSE_PRINTF(dev, "ldn 0x%x not a Nuvoton device\n", ldn); 172 return (ENXIO); 173 } 174 if (superio_get_type(dev) != SUPERIO_DEV_HWM) { 175 NCTHWM_VERBOSE_PRINTF(dev, "ldn 0x%x not a HWM device\n", ldn); 176 return (ENXIO); 177 } 178 179 nctdevp = ncthwm_lookup_device(dev); 180 if (nctdevp == NULL) { 181 NCTHWM_VERBOSE_PRINTF(dev, "ldn 0x%x not supported\n", ldn); 182 return (ENXIO); 183 } 184 device_set_desc(dev, nctdevp->descr); 185 return (BUS_PROBE_DEFAULT); 186 } 187 188 static int 189 ncthwm_attach(device_t dev) 190 { 191 struct ncthwm_softc *sc; 192 int i; 193 uint16_t iobase; 194 195 sc = device_get_softc(dev); 196 sc->dev = dev; 197 198 sc->nctdevp = ncthwm_lookup_device(dev); 199 if (sc->nctdevp == NULL) { 200 device_printf(dev, "device not supported\n"); 201 return (ENXIO); 202 } 203 204 iobase = superio_get_iobase(dev) + sc->nctdevp->base_offset; 205 sc->iorid = 0; 206 if (bus_set_resource(dev, SYS_RES_IOPORT, sc->iorid, iobase, 2) != 0) { 207 device_printf(dev, "failed to set I/O port resource at 0x%x\n", iobase); 208 return (ENXIO); 209 } 210 sc->iores = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 211 &sc->iorid, RF_ACTIVE); 212 if (sc->iores == NULL) { 213 device_printf(dev, "can't map I/O space at 0x%x\n", iobase); 214 return (ENXIO); 215 } 216 NCTHWM_VERBOSE_PRINTF(dev, "iobase 0x%x iores %p\n", iobase, sc->iores); 217 218 /* Register FAN sysctl */ 219 for (i = 0; i < sc->nctdevp->fan_count; i++) { 220 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 221 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 222 sc->nctdevp->fan_info[i].name, 223 CTLTYPE_U16 | CTLFLAG_RD, sc, i, 224 ncthwm_query_fan_speed, "SU", "Fan speed in RPM"); 225 } 226 227 return (0); 228 } 229 230 static int 231 ncthwm_detach(device_t dev) 232 { 233 struct ncthwm_softc *sc = device_get_softc(dev); 234 235 if (sc->iores) 236 bus_release_resource(dev, SYS_RES_IOPORT, sc->iorid, sc->iores); 237 238 return (0); 239 } 240 241 static device_method_t ncthwm_methods[] = { 242 /* Methods from the device interface */ 243 DEVMETHOD(device_probe, ncthwm_probe), 244 DEVMETHOD(device_attach, ncthwm_attach), 245 DEVMETHOD(device_detach, ncthwm_detach), 246 247 /* Terminate method list */ 248 { 0, 0 } 249 }; 250 251 static driver_t ncthwm_driver = { 252 "ncthwm", 253 ncthwm_methods, 254 sizeof (struct ncthwm_softc) 255 }; 256 257 DRIVER_MODULE(ncthwm, superio, ncthwm_driver, NULL, NULL); 258 MODULE_DEPEND(ncthwm, superio, 1, 1, 1); 259 MODULE_VERSION(ncthwm, 1); 260