1*e3722b78SAndriy Gapon /* 2*e3722b78SAndriy Gapon * Copyright (c) 2016 Andriy Gapon 3*e3722b78SAndriy Gapon * All rights reserved. 4*e3722b78SAndriy Gapon * 5*e3722b78SAndriy Gapon * Redistribution and use in source and binary forms, with or without 6*e3722b78SAndriy Gapon * modification, are permitted provided that the following conditions 7*e3722b78SAndriy Gapon * are met: 8*e3722b78SAndriy Gapon * 1. Redistributions of source code must retain the above copyright 9*e3722b78SAndriy Gapon * notice, this list of conditions and the following disclaimer. 10*e3722b78SAndriy Gapon * 2. Redistributions in binary form must reproduce the above copyright 11*e3722b78SAndriy Gapon * notice, this list of conditions and the following disclaimer in the 12*e3722b78SAndriy Gapon * documentation and/or other materials provided with the distribution. 13*e3722b78SAndriy Gapon * 14*e3722b78SAndriy Gapon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND 15*e3722b78SAndriy Gapon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*e3722b78SAndriy Gapon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*e3722b78SAndriy Gapon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*e3722b78SAndriy Gapon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*e3722b78SAndriy Gapon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*e3722b78SAndriy Gapon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*e3722b78SAndriy Gapon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*e3722b78SAndriy Gapon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*e3722b78SAndriy Gapon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*e3722b78SAndriy Gapon * SUCH DAMAGE. 25*e3722b78SAndriy Gapon */ 26*e3722b78SAndriy Gapon 27*e3722b78SAndriy Gapon #include <sys/cdefs.h> 28*e3722b78SAndriy Gapon __FBSDID("$FreeBSD$"); 29*e3722b78SAndriy Gapon 30*e3722b78SAndriy Gapon #include <sys/param.h> 31*e3722b78SAndriy Gapon #include <sys/systm.h> 32*e3722b78SAndriy Gapon #include <sys/bus.h> 33*e3722b78SAndriy Gapon #include <sys/conf.h> 34*e3722b78SAndriy Gapon #include <sys/kernel.h> 35*e3722b78SAndriy Gapon #include <sys/lock.h> 36*e3722b78SAndriy Gapon #include <sys/mutex.h> 37*e3722b78SAndriy Gapon #include <sys/malloc.h> 38*e3722b78SAndriy Gapon #include <sys/module.h> 39*e3722b78SAndriy Gapon #include <sys/proc.h> 40*e3722b78SAndriy Gapon #include <sys/rman.h> 41*e3722b78SAndriy Gapon #include <sys/time.h> 42*e3722b78SAndriy Gapon 43*e3722b78SAndriy Gapon #include <machine/bus.h> 44*e3722b78SAndriy Gapon #include <machine/resource.h> 45*e3722b78SAndriy Gapon #include <machine/stdarg.h> 46*e3722b78SAndriy Gapon 47*e3722b78SAndriy Gapon #include <isa/isavar.h> 48*e3722b78SAndriy Gapon 49*e3722b78SAndriy Gapon #include <dev/superio/superio.h> 50*e3722b78SAndriy Gapon 51*e3722b78SAndriy Gapon #include "isa_if.h" 52*e3722b78SAndriy Gapon 53*e3722b78SAndriy Gapon 54*e3722b78SAndriy Gapon typedef void (*sio_conf_enter_f)(struct resource*, uint16_t); 55*e3722b78SAndriy Gapon typedef void (*sio_conf_exit_f)(struct resource*, uint16_t); 56*e3722b78SAndriy Gapon 57*e3722b78SAndriy Gapon struct sio_conf_methods { 58*e3722b78SAndriy Gapon sio_conf_enter_f enter; 59*e3722b78SAndriy Gapon sio_conf_exit_f exit; 60*e3722b78SAndriy Gapon superio_vendor_t vendor; 61*e3722b78SAndriy Gapon }; 62*e3722b78SAndriy Gapon 63*e3722b78SAndriy Gapon struct sio_device { 64*e3722b78SAndriy Gapon uint8_t ldn; 65*e3722b78SAndriy Gapon superio_dev_type_t type; 66*e3722b78SAndriy Gapon }; 67*e3722b78SAndriy Gapon 68*e3722b78SAndriy Gapon struct superio_devinfo { 69*e3722b78SAndriy Gapon STAILQ_ENTRY(superio_devinfo) link; 70*e3722b78SAndriy Gapon struct resource_list resources; 71*e3722b78SAndriy Gapon device_t dev; 72*e3722b78SAndriy Gapon uint8_t ldn; 73*e3722b78SAndriy Gapon superio_dev_type_t type; 74*e3722b78SAndriy Gapon uint16_t iobase; 75*e3722b78SAndriy Gapon uint16_t iobase2; 76*e3722b78SAndriy Gapon uint8_t irq; 77*e3722b78SAndriy Gapon uint8_t dma; 78*e3722b78SAndriy Gapon }; 79*e3722b78SAndriy Gapon 80*e3722b78SAndriy Gapon struct siosc { 81*e3722b78SAndriy Gapon struct mtx conf_lock; 82*e3722b78SAndriy Gapon STAILQ_HEAD(, superio_devinfo) devlist; 83*e3722b78SAndriy Gapon struct resource* io_res; 84*e3722b78SAndriy Gapon int io_rid; 85*e3722b78SAndriy Gapon uint16_t io_port; 86*e3722b78SAndriy Gapon const struct sio_conf_methods *methods; 87*e3722b78SAndriy Gapon const struct sio_device *known_devices; 88*e3722b78SAndriy Gapon superio_vendor_t vendor; 89*e3722b78SAndriy Gapon uint16_t devid; 90*e3722b78SAndriy Gapon uint8_t revid; 91*e3722b78SAndriy Gapon uint8_t current_ldn; 92*e3722b78SAndriy Gapon uint8_t ldn_reg; 93*e3722b78SAndriy Gapon uint8_t enable_reg; 94*e3722b78SAndriy Gapon }; 95*e3722b78SAndriy Gapon 96*e3722b78SAndriy Gapon #define NUMPORTS 2 97*e3722b78SAndriy Gapon 98*e3722b78SAndriy Gapon static uint8_t 99*e3722b78SAndriy Gapon sio_read(struct resource* res, uint8_t reg) 100*e3722b78SAndriy Gapon { 101*e3722b78SAndriy Gapon bus_write_1(res, 0, reg); 102*e3722b78SAndriy Gapon return (bus_read_1(res, 1)); 103*e3722b78SAndriy Gapon } 104*e3722b78SAndriy Gapon 105*e3722b78SAndriy Gapon /* Read a word from two one-byte registers, big endian. */ 106*e3722b78SAndriy Gapon static uint16_t 107*e3722b78SAndriy Gapon sio_readw(struct resource* res, uint8_t reg) 108*e3722b78SAndriy Gapon { 109*e3722b78SAndriy Gapon uint16_t v; 110*e3722b78SAndriy Gapon 111*e3722b78SAndriy Gapon v = sio_read(res, reg); 112*e3722b78SAndriy Gapon v <<= 8; 113*e3722b78SAndriy Gapon v |= sio_read(res, reg + 1); 114*e3722b78SAndriy Gapon return (v); 115*e3722b78SAndriy Gapon } 116*e3722b78SAndriy Gapon 117*e3722b78SAndriy Gapon static void 118*e3722b78SAndriy Gapon sio_write(struct resource* res, uint8_t reg, uint8_t val) 119*e3722b78SAndriy Gapon { 120*e3722b78SAndriy Gapon bus_write_1(res, 0, reg); 121*e3722b78SAndriy Gapon bus_write_1(res, 1, val); 122*e3722b78SAndriy Gapon } 123*e3722b78SAndriy Gapon 124*e3722b78SAndriy Gapon static void 125*e3722b78SAndriy Gapon sio_ldn_select(struct siosc *sc, uint8_t ldn) 126*e3722b78SAndriy Gapon { 127*e3722b78SAndriy Gapon mtx_assert(&sc->conf_lock, MA_OWNED); 128*e3722b78SAndriy Gapon if (ldn == sc->current_ldn) 129*e3722b78SAndriy Gapon return; 130*e3722b78SAndriy Gapon sio_write(sc->io_res, sc->ldn_reg, ldn); 131*e3722b78SAndriy Gapon sc->current_ldn = ldn; 132*e3722b78SAndriy Gapon } 133*e3722b78SAndriy Gapon 134*e3722b78SAndriy Gapon static uint8_t 135*e3722b78SAndriy Gapon sio_ldn_read(struct siosc *sc, uint8_t ldn, uint8_t reg) 136*e3722b78SAndriy Gapon { 137*e3722b78SAndriy Gapon mtx_assert(&sc->conf_lock, MA_OWNED); 138*e3722b78SAndriy Gapon if (reg >= sc->enable_reg) { 139*e3722b78SAndriy Gapon sio_ldn_select(sc, ldn); 140*e3722b78SAndriy Gapon KASSERT(sc->current_ldn == ldn, ("sio_ldn_select failed")); 141*e3722b78SAndriy Gapon } 142*e3722b78SAndriy Gapon return (sio_read(sc->io_res, reg)); 143*e3722b78SAndriy Gapon } 144*e3722b78SAndriy Gapon 145*e3722b78SAndriy Gapon static uint16_t 146*e3722b78SAndriy Gapon sio_ldn_readw(struct siosc *sc, uint8_t ldn, uint8_t reg) 147*e3722b78SAndriy Gapon { 148*e3722b78SAndriy Gapon mtx_assert(&sc->conf_lock, MA_OWNED); 149*e3722b78SAndriy Gapon if (reg >= sc->enable_reg) { 150*e3722b78SAndriy Gapon sio_ldn_select(sc, ldn); 151*e3722b78SAndriy Gapon KASSERT(sc->current_ldn == ldn, ("sio_ldn_select failed")); 152*e3722b78SAndriy Gapon } 153*e3722b78SAndriy Gapon return (sio_readw(sc->io_res, reg)); 154*e3722b78SAndriy Gapon } 155*e3722b78SAndriy Gapon 156*e3722b78SAndriy Gapon static void 157*e3722b78SAndriy Gapon sio_ldn_write(struct siosc *sc, uint8_t ldn, uint8_t reg, uint8_t val) 158*e3722b78SAndriy Gapon { 159*e3722b78SAndriy Gapon mtx_assert(&sc->conf_lock, MA_OWNED); 160*e3722b78SAndriy Gapon if (reg <= sc->ldn_reg) { 161*e3722b78SAndriy Gapon printf("ignored attempt to write special register 0x%x\n", reg); 162*e3722b78SAndriy Gapon return; 163*e3722b78SAndriy Gapon } 164*e3722b78SAndriy Gapon sio_ldn_select(sc, ldn); 165*e3722b78SAndriy Gapon KASSERT(sc->current_ldn == ldn, ("sio_ldn_select failed")); 166*e3722b78SAndriy Gapon sio_write(sc->io_res, reg, val); 167*e3722b78SAndriy Gapon } 168*e3722b78SAndriy Gapon 169*e3722b78SAndriy Gapon static void 170*e3722b78SAndriy Gapon sio_conf_enter(struct siosc *sc) 171*e3722b78SAndriy Gapon { 172*e3722b78SAndriy Gapon mtx_lock(&sc->conf_lock); 173*e3722b78SAndriy Gapon sc->methods->enter(sc->io_res, sc->io_port); 174*e3722b78SAndriy Gapon } 175*e3722b78SAndriy Gapon 176*e3722b78SAndriy Gapon static void 177*e3722b78SAndriy Gapon sio_conf_exit(struct siosc *sc) 178*e3722b78SAndriy Gapon { 179*e3722b78SAndriy Gapon sc->methods->exit(sc->io_res, sc->io_port); 180*e3722b78SAndriy Gapon mtx_unlock(&sc->conf_lock); 181*e3722b78SAndriy Gapon } 182*e3722b78SAndriy Gapon 183*e3722b78SAndriy Gapon static void 184*e3722b78SAndriy Gapon ite_conf_enter(struct resource* res, uint16_t port) 185*e3722b78SAndriy Gapon { 186*e3722b78SAndriy Gapon bus_write_1(res, 0, 0x87); 187*e3722b78SAndriy Gapon bus_write_1(res, 0, 0x01); 188*e3722b78SAndriy Gapon bus_write_1(res, 0, 0x55); 189*e3722b78SAndriy Gapon bus_write_1(res, 0, port == 0x2e ? 0x55 : 0xaa); 190*e3722b78SAndriy Gapon } 191*e3722b78SAndriy Gapon 192*e3722b78SAndriy Gapon static void 193*e3722b78SAndriy Gapon ite_conf_exit(struct resource* res, uint16_t port) 194*e3722b78SAndriy Gapon { 195*e3722b78SAndriy Gapon sio_write(res, 0x02, 0x02); 196*e3722b78SAndriy Gapon } 197*e3722b78SAndriy Gapon 198*e3722b78SAndriy Gapon static const struct sio_conf_methods ite_conf_methods = { 199*e3722b78SAndriy Gapon .enter = ite_conf_enter, 200*e3722b78SAndriy Gapon .exit = ite_conf_exit, 201*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE 202*e3722b78SAndriy Gapon }; 203*e3722b78SAndriy Gapon 204*e3722b78SAndriy Gapon static void 205*e3722b78SAndriy Gapon nvt_conf_enter(struct resource* res, uint16_t port) 206*e3722b78SAndriy Gapon { 207*e3722b78SAndriy Gapon bus_write_1(res, 0, 0x87); 208*e3722b78SAndriy Gapon bus_write_1(res, 0, 0x87); 209*e3722b78SAndriy Gapon } 210*e3722b78SAndriy Gapon 211*e3722b78SAndriy Gapon static void 212*e3722b78SAndriy Gapon nvt_conf_exit(struct resource* res, uint16_t port) 213*e3722b78SAndriy Gapon { 214*e3722b78SAndriy Gapon bus_write_1(res, 0, 0xaa); 215*e3722b78SAndriy Gapon } 216*e3722b78SAndriy Gapon 217*e3722b78SAndriy Gapon static const struct sio_conf_methods nvt_conf_methods = { 218*e3722b78SAndriy Gapon .enter = nvt_conf_enter, 219*e3722b78SAndriy Gapon .exit = nvt_conf_exit, 220*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON 221*e3722b78SAndriy Gapon }; 222*e3722b78SAndriy Gapon 223*e3722b78SAndriy Gapon static const struct sio_conf_methods * const methods_table[] = { 224*e3722b78SAndriy Gapon &ite_conf_methods, 225*e3722b78SAndriy Gapon &nvt_conf_methods, 226*e3722b78SAndriy Gapon NULL 227*e3722b78SAndriy Gapon }; 228*e3722b78SAndriy Gapon 229*e3722b78SAndriy Gapon static const uint16_t ports_table[] = { 230*e3722b78SAndriy Gapon 0x2e, 0x4e, 0 231*e3722b78SAndriy Gapon }; 232*e3722b78SAndriy Gapon 233*e3722b78SAndriy Gapon const struct sio_device ite_devices[] = { 234*e3722b78SAndriy Gapon { .ldn = 4, .type = SUPERIO_DEV_HWM }, 235*e3722b78SAndriy Gapon { .ldn = 7, .type = SUPERIO_DEV_WDT }, 236*e3722b78SAndriy Gapon { .type = SUPERIO_DEV_NONE }, 237*e3722b78SAndriy Gapon }; 238*e3722b78SAndriy Gapon 239*e3722b78SAndriy Gapon const struct sio_device nvt_devices[] = { 240*e3722b78SAndriy Gapon { .ldn = 8, .type = SUPERIO_DEV_WDT }, 241*e3722b78SAndriy Gapon { .type = SUPERIO_DEV_NONE }, 242*e3722b78SAndriy Gapon }; 243*e3722b78SAndriy Gapon 244*e3722b78SAndriy Gapon const struct sio_device nct5104_devices[] = { 245*e3722b78SAndriy Gapon { .ldn = 7, .type = SUPERIO_DEV_GPIO }, 246*e3722b78SAndriy Gapon { .ldn = 8, .type = SUPERIO_DEV_WDT }, 247*e3722b78SAndriy Gapon { .ldn = 15, .type = SUPERIO_DEV_GPIO }, 248*e3722b78SAndriy Gapon { .type = SUPERIO_DEV_NONE }, 249*e3722b78SAndriy Gapon }; 250*e3722b78SAndriy Gapon 251*e3722b78SAndriy Gapon static const struct { 252*e3722b78SAndriy Gapon superio_vendor_t vendor; 253*e3722b78SAndriy Gapon uint16_t devid; 254*e3722b78SAndriy Gapon uint16_t mask; 255*e3722b78SAndriy Gapon const char *descr; 256*e3722b78SAndriy Gapon const struct sio_device *devices; 257*e3722b78SAndriy Gapon } superio_table[] = { 258*e3722b78SAndriy Gapon { 259*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8712, 260*e3722b78SAndriy Gapon .devices = ite_devices, 261*e3722b78SAndriy Gapon }, 262*e3722b78SAndriy Gapon { 263*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8716, 264*e3722b78SAndriy Gapon .devices = ite_devices, 265*e3722b78SAndriy Gapon }, 266*e3722b78SAndriy Gapon { 267*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8718, 268*e3722b78SAndriy Gapon .devices = ite_devices, 269*e3722b78SAndriy Gapon }, 270*e3722b78SAndriy Gapon { 271*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8720, 272*e3722b78SAndriy Gapon .devices = ite_devices, 273*e3722b78SAndriy Gapon }, 274*e3722b78SAndriy Gapon { 275*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8721, 276*e3722b78SAndriy Gapon .devices = ite_devices, 277*e3722b78SAndriy Gapon }, 278*e3722b78SAndriy Gapon { 279*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8726, 280*e3722b78SAndriy Gapon .devices = ite_devices, 281*e3722b78SAndriy Gapon }, 282*e3722b78SAndriy Gapon { 283*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8728, 284*e3722b78SAndriy Gapon .devices = ite_devices, 285*e3722b78SAndriy Gapon }, 286*e3722b78SAndriy Gapon { 287*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_ITE, .devid = 0x8771, 288*e3722b78SAndriy Gapon .devices = ite_devices, 289*e3722b78SAndriy Gapon }, 290*e3722b78SAndriy Gapon { 291*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x1061, .mask = 0x00, 292*e3722b78SAndriy Gapon .descr = "Nuvoton NCT5104D/NCT6102D/NCT6106D (rev. A)", 293*e3722b78SAndriy Gapon .devices = nct5104_devices, 294*e3722b78SAndriy Gapon }, 295*e3722b78SAndriy Gapon { 296*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x5200, .mask = 0xff, 297*e3722b78SAndriy Gapon .descr = "Winbond 83627HF/F/HG/G", 298*e3722b78SAndriy Gapon .devices = nvt_devices, 299*e3722b78SAndriy Gapon }, 300*e3722b78SAndriy Gapon { 301*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x5900, .mask = 0xff, 302*e3722b78SAndriy Gapon .descr = "Winbond 83627S", 303*e3722b78SAndriy Gapon .devices = nvt_devices, 304*e3722b78SAndriy Gapon }, 305*e3722b78SAndriy Gapon { 306*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x6000, .mask = 0xff, 307*e3722b78SAndriy Gapon .descr = "Winbond 83697HF", 308*e3722b78SAndriy Gapon .devices = nvt_devices, 309*e3722b78SAndriy Gapon }, 310*e3722b78SAndriy Gapon { 311*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x6800, .mask = 0xff, 312*e3722b78SAndriy Gapon .descr = "Winbond 83697UG", 313*e3722b78SAndriy Gapon .devices = nvt_devices, 314*e3722b78SAndriy Gapon }, 315*e3722b78SAndriy Gapon { 316*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x7000, .mask = 0xff, 317*e3722b78SAndriy Gapon .descr = "Winbond 83637HF", 318*e3722b78SAndriy Gapon .devices = nvt_devices, 319*e3722b78SAndriy Gapon }, 320*e3722b78SAndriy Gapon { 321*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x8200, .mask = 0xff, 322*e3722b78SAndriy Gapon .descr = "Winbond 83627THF", 323*e3722b78SAndriy Gapon .devices = nvt_devices, 324*e3722b78SAndriy Gapon }, 325*e3722b78SAndriy Gapon { 326*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x8500, .mask = 0xff, 327*e3722b78SAndriy Gapon .descr = "Winbond 83687THF", 328*e3722b78SAndriy Gapon .devices = nvt_devices, 329*e3722b78SAndriy Gapon }, 330*e3722b78SAndriy Gapon { 331*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0x8800, .mask = 0xff, 332*e3722b78SAndriy Gapon .descr = "Winbond 83627EHF", 333*e3722b78SAndriy Gapon .devices = nvt_devices, 334*e3722b78SAndriy Gapon }, 335*e3722b78SAndriy Gapon { 336*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xa000, .mask = 0xff, 337*e3722b78SAndriy Gapon .descr = "Winbond 83627DHG", 338*e3722b78SAndriy Gapon .devices = nvt_devices, 339*e3722b78SAndriy Gapon }, 340*e3722b78SAndriy Gapon { 341*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xa200, .mask = 0xff, 342*e3722b78SAndriy Gapon .descr = "Winbond 83627UHG", 343*e3722b78SAndriy Gapon .devices = nvt_devices, 344*e3722b78SAndriy Gapon }, 345*e3722b78SAndriy Gapon { 346*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xa500, .mask = 0xff, 347*e3722b78SAndriy Gapon .descr = "Winbond 83667HG", 348*e3722b78SAndriy Gapon .devices = nvt_devices, 349*e3722b78SAndriy Gapon }, 350*e3722b78SAndriy Gapon { 351*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xb000, .mask = 0xff, 352*e3722b78SAndriy Gapon .descr = "Winbond 83627DHG-P", 353*e3722b78SAndriy Gapon .devices = nvt_devices, 354*e3722b78SAndriy Gapon }, 355*e3722b78SAndriy Gapon { 356*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xb300, .mask = 0xff, 357*e3722b78SAndriy Gapon .descr = "Winbond 83667HG-B", 358*e3722b78SAndriy Gapon .devices = nvt_devices, 359*e3722b78SAndriy Gapon }, 360*e3722b78SAndriy Gapon { 361*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xb400, .mask = 0xff, 362*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6775", 363*e3722b78SAndriy Gapon .devices = nvt_devices, 364*e3722b78SAndriy Gapon }, 365*e3722b78SAndriy Gapon { 366*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc300, .mask = 0xff, 367*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6776", 368*e3722b78SAndriy Gapon .devices = nvt_devices, 369*e3722b78SAndriy Gapon }, 370*e3722b78SAndriy Gapon { 371*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc400, .mask = 0xff, 372*e3722b78SAndriy Gapon .descr = "Nuvoton NCT5104D/NCT6102D/NCT6106D (rev. B+)", 373*e3722b78SAndriy Gapon .devices = nct5104_devices, 374*e3722b78SAndriy Gapon }, 375*e3722b78SAndriy Gapon { 376*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc500, .mask = 0xff, 377*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6779", 378*e3722b78SAndriy Gapon .devices = nvt_devices, 379*e3722b78SAndriy Gapon }, 380*e3722b78SAndriy Gapon { 381*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc800, .mask = 0xff, 382*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6791", 383*e3722b78SAndriy Gapon .devices = nvt_devices, 384*e3722b78SAndriy Gapon }, 385*e3722b78SAndriy Gapon { 386*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xc900, .mask = 0xff, 387*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6792", 388*e3722b78SAndriy Gapon .devices = nvt_devices, 389*e3722b78SAndriy Gapon }, 390*e3722b78SAndriy Gapon { 391*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xd100, .mask = 0xff, 392*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6793", 393*e3722b78SAndriy Gapon .devices = nvt_devices, 394*e3722b78SAndriy Gapon }, 395*e3722b78SAndriy Gapon { 396*e3722b78SAndriy Gapon .vendor = SUPERIO_VENDOR_NUVOTON, .devid = 0xd300, .mask = 0xff, 397*e3722b78SAndriy Gapon .descr = "Nuvoton NCT6795", 398*e3722b78SAndriy Gapon .devices = nvt_devices, 399*e3722b78SAndriy Gapon }, 400*e3722b78SAndriy Gapon { 0, 0 } 401*e3722b78SAndriy Gapon }; 402*e3722b78SAndriy Gapon 403*e3722b78SAndriy Gapon static const char * 404*e3722b78SAndriy Gapon devtype_to_str(superio_dev_type_t type) 405*e3722b78SAndriy Gapon { 406*e3722b78SAndriy Gapon switch (type) { 407*e3722b78SAndriy Gapon case SUPERIO_DEV_NONE: 408*e3722b78SAndriy Gapon return ("invalid"); 409*e3722b78SAndriy Gapon case SUPERIO_DEV_HWM: 410*e3722b78SAndriy Gapon return ("HWM"); 411*e3722b78SAndriy Gapon case SUPERIO_DEV_WDT: 412*e3722b78SAndriy Gapon return ("WDT"); 413*e3722b78SAndriy Gapon case SUPERIO_DEV_GPIO: 414*e3722b78SAndriy Gapon return ("GPIO"); 415*e3722b78SAndriy Gapon case SUPERIO_DEV_MAX: 416*e3722b78SAndriy Gapon return ("invalid"); 417*e3722b78SAndriy Gapon } 418*e3722b78SAndriy Gapon } 419*e3722b78SAndriy Gapon 420*e3722b78SAndriy Gapon static int 421*e3722b78SAndriy Gapon superio_detect(device_t dev, bool claim, struct siosc *sc) 422*e3722b78SAndriy Gapon { 423*e3722b78SAndriy Gapon struct resource *res; 424*e3722b78SAndriy Gapon rman_res_t port; 425*e3722b78SAndriy Gapon rman_res_t count; 426*e3722b78SAndriy Gapon uint16_t devid; 427*e3722b78SAndriy Gapon uint8_t revid; 428*e3722b78SAndriy Gapon int error; 429*e3722b78SAndriy Gapon int rid; 430*e3722b78SAndriy Gapon int i, m; 431*e3722b78SAndriy Gapon 432*e3722b78SAndriy Gapon error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &port, &count); 433*e3722b78SAndriy Gapon if (error != 0) 434*e3722b78SAndriy Gapon return (error); 435*e3722b78SAndriy Gapon if (port > UINT16_MAX || count < NUMPORTS) { 436*e3722b78SAndriy Gapon device_printf(dev, "unexpected I/O range size\n"); 437*e3722b78SAndriy Gapon return (ENXIO); 438*e3722b78SAndriy Gapon } 439*e3722b78SAndriy Gapon 440*e3722b78SAndriy Gapon /* 441*e3722b78SAndriy Gapon * Make a temporary resource reservation for hardware probing. 442*e3722b78SAndriy Gapon * If we can't get the resources we need then 443*e3722b78SAndriy Gapon * we need to abort. Possibly this indicates 444*e3722b78SAndriy Gapon * the resources were used by another device 445*e3722b78SAndriy Gapon * in which case the probe would have failed anyhow. 446*e3722b78SAndriy Gapon */ 447*e3722b78SAndriy Gapon rid = 0; 448*e3722b78SAndriy Gapon res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE); 449*e3722b78SAndriy Gapon if (res == NULL) { 450*e3722b78SAndriy Gapon if (claim) 451*e3722b78SAndriy Gapon device_printf(dev, "failed to allocate I/O resource\n"); 452*e3722b78SAndriy Gapon return (ENXIO); 453*e3722b78SAndriy Gapon } 454*e3722b78SAndriy Gapon 455*e3722b78SAndriy Gapon for (m = 0; methods_table[m] != NULL; m++) { 456*e3722b78SAndriy Gapon methods_table[m]->enter(res, port); 457*e3722b78SAndriy Gapon if (methods_table[m]->vendor == SUPERIO_VENDOR_ITE) { 458*e3722b78SAndriy Gapon devid = sio_readw(res, 0x20); 459*e3722b78SAndriy Gapon revid = sio_read(res, 0x22); 460*e3722b78SAndriy Gapon } else if (methods_table[m]->vendor == SUPERIO_VENDOR_NUVOTON) { 461*e3722b78SAndriy Gapon devid = sio_read(res, 0x20); 462*e3722b78SAndriy Gapon revid = sio_read(res, 0x21); 463*e3722b78SAndriy Gapon devid = (devid << 8) | revid; 464*e3722b78SAndriy Gapon } else { 465*e3722b78SAndriy Gapon continue; 466*e3722b78SAndriy Gapon } 467*e3722b78SAndriy Gapon methods_table[m]->exit(res, port); 468*e3722b78SAndriy Gapon for (i = 0; superio_table[i].vendor != 0; i++) { 469*e3722b78SAndriy Gapon uint16_t mask; 470*e3722b78SAndriy Gapon 471*e3722b78SAndriy Gapon mask = superio_table[i].mask; 472*e3722b78SAndriy Gapon if (superio_table[i].vendor != 473*e3722b78SAndriy Gapon methods_table[m]->vendor) 474*e3722b78SAndriy Gapon continue; 475*e3722b78SAndriy Gapon if ((superio_table[i].devid & ~mask) != (devid & ~mask)) 476*e3722b78SAndriy Gapon continue; 477*e3722b78SAndriy Gapon break; 478*e3722b78SAndriy Gapon } 479*e3722b78SAndriy Gapon 480*e3722b78SAndriy Gapon /* Found a matching SuperIO entry. */ 481*e3722b78SAndriy Gapon if (superio_table[i].vendor != 0) 482*e3722b78SAndriy Gapon break; 483*e3722b78SAndriy Gapon } 484*e3722b78SAndriy Gapon 485*e3722b78SAndriy Gapon if (methods_table[m] == NULL) 486*e3722b78SAndriy Gapon error = ENXIO; 487*e3722b78SAndriy Gapon else 488*e3722b78SAndriy Gapon error = 0; 489*e3722b78SAndriy Gapon if (!claim || error != 0) { 490*e3722b78SAndriy Gapon bus_release_resource(dev, SYS_RES_IOPORT, rid, res); 491*e3722b78SAndriy Gapon return (error); 492*e3722b78SAndriy Gapon } 493*e3722b78SAndriy Gapon 494*e3722b78SAndriy Gapon sc->methods = methods_table[m]; 495*e3722b78SAndriy Gapon sc->vendor = sc->methods->vendor; 496*e3722b78SAndriy Gapon sc->known_devices = superio_table[i].devices; 497*e3722b78SAndriy Gapon sc->io_res = res; 498*e3722b78SAndriy Gapon sc->io_rid = rid; 499*e3722b78SAndriy Gapon sc->io_port = port; 500*e3722b78SAndriy Gapon sc->devid = devid; 501*e3722b78SAndriy Gapon sc->revid = revid; 502*e3722b78SAndriy Gapon 503*e3722b78SAndriy Gapon KASSERT(sc->vendor == SUPERIO_VENDOR_ITE || 504*e3722b78SAndriy Gapon sc->vendor == SUPERIO_VENDOR_NUVOTON, 505*e3722b78SAndriy Gapon ("Only ITE and Nuvoton SuperIO-s are supported")); 506*e3722b78SAndriy Gapon sc->ldn_reg = 0x07; 507*e3722b78SAndriy Gapon sc->enable_reg = 0x30; 508*e3722b78SAndriy Gapon sc->current_ldn = 0xff; /* no device should have this */ 509*e3722b78SAndriy Gapon 510*e3722b78SAndriy Gapon if (superio_table[i].descr != NULL) { 511*e3722b78SAndriy Gapon device_set_desc(dev, superio_table[i].descr); 512*e3722b78SAndriy Gapon } else if (sc->vendor == SUPERIO_VENDOR_ITE) { 513*e3722b78SAndriy Gapon char descr[64]; 514*e3722b78SAndriy Gapon 515*e3722b78SAndriy Gapon snprintf(descr, sizeof(descr), 516*e3722b78SAndriy Gapon "ITE IT%4x SuperIO (revision 0x%02x)", 517*e3722b78SAndriy Gapon sc->devid, sc->revid); 518*e3722b78SAndriy Gapon device_set_desc_copy(dev, descr); 519*e3722b78SAndriy Gapon } 520*e3722b78SAndriy Gapon return (0); 521*e3722b78SAndriy Gapon } 522*e3722b78SAndriy Gapon 523*e3722b78SAndriy Gapon static void 524*e3722b78SAndriy Gapon superio_identify(driver_t *driver, device_t parent) 525*e3722b78SAndriy Gapon { 526*e3722b78SAndriy Gapon device_t child; 527*e3722b78SAndriy Gapon int i; 528*e3722b78SAndriy Gapon 529*e3722b78SAndriy Gapon /* 530*e3722b78SAndriy Gapon * Don't create child devices if any already exist. 531*e3722b78SAndriy Gapon * Those could be created via isa hints or if this 532*e3722b78SAndriy Gapon * driver is loaded, unloaded and then loaded again. 533*e3722b78SAndriy Gapon */ 534*e3722b78SAndriy Gapon if (device_find_child(parent, "superio", -1)) { 535*e3722b78SAndriy Gapon if (bootverbose) 536*e3722b78SAndriy Gapon printf("superio: device(s) already created\n"); 537*e3722b78SAndriy Gapon return; 538*e3722b78SAndriy Gapon } 539*e3722b78SAndriy Gapon 540*e3722b78SAndriy Gapon /* 541*e3722b78SAndriy Gapon * Create a child for each candidate port. 542*e3722b78SAndriy Gapon * It would be nice if we could somehow clean up those 543*e3722b78SAndriy Gapon * that this driver fails to probe. 544*e3722b78SAndriy Gapon */ 545*e3722b78SAndriy Gapon for (i = 0; ports_table[i] != 0; i++) { 546*e3722b78SAndriy Gapon child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, 547*e3722b78SAndriy Gapon "superio", -1); 548*e3722b78SAndriy Gapon if (child == NULL) { 549*e3722b78SAndriy Gapon device_printf(parent, "failed to add superio child\n"); 550*e3722b78SAndriy Gapon continue; 551*e3722b78SAndriy Gapon } 552*e3722b78SAndriy Gapon bus_set_resource(child, SYS_RES_IOPORT, 0, ports_table[i], 2); 553*e3722b78SAndriy Gapon if (superio_detect(child, false, NULL) != 0) 554*e3722b78SAndriy Gapon device_delete_child(parent, child); 555*e3722b78SAndriy Gapon } 556*e3722b78SAndriy Gapon } 557*e3722b78SAndriy Gapon 558*e3722b78SAndriy Gapon static int 559*e3722b78SAndriy Gapon superio_probe(device_t dev) 560*e3722b78SAndriy Gapon { 561*e3722b78SAndriy Gapon struct siosc *sc; 562*e3722b78SAndriy Gapon int error; 563*e3722b78SAndriy Gapon 564*e3722b78SAndriy Gapon /* Make sure we do not claim some ISA PNP device. */ 565*e3722b78SAndriy Gapon if (isa_get_logicalid(dev) != 0) 566*e3722b78SAndriy Gapon return (ENXIO); 567*e3722b78SAndriy Gapon 568*e3722b78SAndriy Gapon /* 569*e3722b78SAndriy Gapon * XXX We can populate the softc now only because we return 570*e3722b78SAndriy Gapon * BUS_PROBE_SPECIFIC 571*e3722b78SAndriy Gapon */ 572*e3722b78SAndriy Gapon sc = device_get_softc(dev); 573*e3722b78SAndriy Gapon error = superio_detect(dev, true, sc); 574*e3722b78SAndriy Gapon if (error != 0) 575*e3722b78SAndriy Gapon return (error); 576*e3722b78SAndriy Gapon return (BUS_PROBE_SPECIFIC); 577*e3722b78SAndriy Gapon } 578*e3722b78SAndriy Gapon 579*e3722b78SAndriy Gapon static void 580*e3722b78SAndriy Gapon superio_add_known_child(device_t dev, superio_dev_type_t type, uint8_t ldn) 581*e3722b78SAndriy Gapon { 582*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(dev); 583*e3722b78SAndriy Gapon struct superio_devinfo *dinfo; 584*e3722b78SAndriy Gapon device_t child; 585*e3722b78SAndriy Gapon 586*e3722b78SAndriy Gapon child = BUS_ADD_CHILD(dev, 0, NULL, -1); 587*e3722b78SAndriy Gapon if (child == NULL) { 588*e3722b78SAndriy Gapon device_printf(dev, "failed to add child for ldn %d, type %s\n", 589*e3722b78SAndriy Gapon ldn, devtype_to_str(type)); 590*e3722b78SAndriy Gapon return; 591*e3722b78SAndriy Gapon } 592*e3722b78SAndriy Gapon dinfo = device_get_ivars(child); 593*e3722b78SAndriy Gapon dinfo->ldn = ldn; 594*e3722b78SAndriy Gapon dinfo->type = type; 595*e3722b78SAndriy Gapon sio_conf_enter(sc); 596*e3722b78SAndriy Gapon dinfo->iobase = sio_ldn_readw(sc, ldn, 0x60); 597*e3722b78SAndriy Gapon dinfo->iobase2 = sio_ldn_readw(sc, ldn, 0x62); 598*e3722b78SAndriy Gapon dinfo->irq = sio_ldn_readw(sc, ldn, 0x70); 599*e3722b78SAndriy Gapon dinfo->dma = sio_ldn_readw(sc, ldn, 0x74); 600*e3722b78SAndriy Gapon sio_conf_exit(sc); 601*e3722b78SAndriy Gapon STAILQ_INSERT_TAIL(&sc->devlist, dinfo, link); 602*e3722b78SAndriy Gapon } 603*e3722b78SAndriy Gapon 604*e3722b78SAndriy Gapon static int 605*e3722b78SAndriy Gapon superio_attach(device_t dev) 606*e3722b78SAndriy Gapon { 607*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(dev); 608*e3722b78SAndriy Gapon int i; 609*e3722b78SAndriy Gapon 610*e3722b78SAndriy Gapon mtx_init(&sc->conf_lock, device_get_nameunit(dev), "superio", MTX_DEF); 611*e3722b78SAndriy Gapon STAILQ_INIT(&sc->devlist); 612*e3722b78SAndriy Gapon 613*e3722b78SAndriy Gapon for (i = 0; sc->known_devices[i].type != SUPERIO_DEV_NONE; i++) { 614*e3722b78SAndriy Gapon superio_add_known_child(dev, sc->known_devices[i].type, 615*e3722b78SAndriy Gapon sc->known_devices[i].ldn); 616*e3722b78SAndriy Gapon } 617*e3722b78SAndriy Gapon 618*e3722b78SAndriy Gapon bus_generic_probe(dev); 619*e3722b78SAndriy Gapon bus_generic_attach(dev); 620*e3722b78SAndriy Gapon return (0); 621*e3722b78SAndriy Gapon } 622*e3722b78SAndriy Gapon 623*e3722b78SAndriy Gapon static int 624*e3722b78SAndriy Gapon superio_detach(device_t dev) 625*e3722b78SAndriy Gapon { 626*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(dev); 627*e3722b78SAndriy Gapon int error; 628*e3722b78SAndriy Gapon 629*e3722b78SAndriy Gapon error = bus_generic_detach(dev); 630*e3722b78SAndriy Gapon if (error != 0) 631*e3722b78SAndriy Gapon return (error); 632*e3722b78SAndriy Gapon device_delete_children(dev); 633*e3722b78SAndriy Gapon bus_release_resource(dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res); 634*e3722b78SAndriy Gapon mtx_destroy(&sc->conf_lock); 635*e3722b78SAndriy Gapon return (0); 636*e3722b78SAndriy Gapon } 637*e3722b78SAndriy Gapon 638*e3722b78SAndriy Gapon static device_t 639*e3722b78SAndriy Gapon superio_add_child(device_t dev, u_int order, const char *name, int unit) 640*e3722b78SAndriy Gapon { 641*e3722b78SAndriy Gapon struct superio_devinfo *dinfo; 642*e3722b78SAndriy Gapon device_t child; 643*e3722b78SAndriy Gapon 644*e3722b78SAndriy Gapon child = device_add_child_ordered(dev, order, name, unit); 645*e3722b78SAndriy Gapon if (child == NULL) 646*e3722b78SAndriy Gapon return (NULL); 647*e3722b78SAndriy Gapon dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO); 648*e3722b78SAndriy Gapon if (dinfo == NULL) { 649*e3722b78SAndriy Gapon device_delete_child(dev, child); 650*e3722b78SAndriy Gapon return (NULL); 651*e3722b78SAndriy Gapon } 652*e3722b78SAndriy Gapon dinfo->ldn = 0xff; 653*e3722b78SAndriy Gapon dinfo->type = SUPERIO_DEV_NONE; 654*e3722b78SAndriy Gapon dinfo->dev = child; 655*e3722b78SAndriy Gapon resource_list_init(&dinfo->resources); 656*e3722b78SAndriy Gapon device_set_ivars(child, dinfo); 657*e3722b78SAndriy Gapon return (child); 658*e3722b78SAndriy Gapon } 659*e3722b78SAndriy Gapon 660*e3722b78SAndriy Gapon static int 661*e3722b78SAndriy Gapon superio_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 662*e3722b78SAndriy Gapon { 663*e3722b78SAndriy Gapon struct superio_devinfo *dinfo; 664*e3722b78SAndriy Gapon 665*e3722b78SAndriy Gapon dinfo = device_get_ivars(child); 666*e3722b78SAndriy Gapon switch (which) { 667*e3722b78SAndriy Gapon case SUPERIO_IVAR_LDN: 668*e3722b78SAndriy Gapon *result = dinfo->ldn; 669*e3722b78SAndriy Gapon break; 670*e3722b78SAndriy Gapon case SUPERIO_IVAR_TYPE: 671*e3722b78SAndriy Gapon *result = dinfo->type; 672*e3722b78SAndriy Gapon break; 673*e3722b78SAndriy Gapon case SUPERIO_IVAR_IOBASE: 674*e3722b78SAndriy Gapon *result = dinfo->iobase; 675*e3722b78SAndriy Gapon break; 676*e3722b78SAndriy Gapon case SUPERIO_IVAR_IOBASE2: 677*e3722b78SAndriy Gapon *result = dinfo->iobase2; 678*e3722b78SAndriy Gapon break; 679*e3722b78SAndriy Gapon case SUPERIO_IVAR_IRQ: 680*e3722b78SAndriy Gapon *result = dinfo->irq; 681*e3722b78SAndriy Gapon break; 682*e3722b78SAndriy Gapon case SUPERIO_IVAR_DMA: 683*e3722b78SAndriy Gapon *result = dinfo->dma; 684*e3722b78SAndriy Gapon break; 685*e3722b78SAndriy Gapon default: 686*e3722b78SAndriy Gapon return (ENOENT); 687*e3722b78SAndriy Gapon } 688*e3722b78SAndriy Gapon return (0); 689*e3722b78SAndriy Gapon } 690*e3722b78SAndriy Gapon 691*e3722b78SAndriy Gapon static int 692*e3722b78SAndriy Gapon superio_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 693*e3722b78SAndriy Gapon { 694*e3722b78SAndriy Gapon 695*e3722b78SAndriy Gapon switch (which) { 696*e3722b78SAndriy Gapon case SUPERIO_IVAR_LDN: 697*e3722b78SAndriy Gapon case SUPERIO_IVAR_TYPE: 698*e3722b78SAndriy Gapon case SUPERIO_IVAR_IOBASE: 699*e3722b78SAndriy Gapon case SUPERIO_IVAR_IOBASE2: 700*e3722b78SAndriy Gapon case SUPERIO_IVAR_IRQ: 701*e3722b78SAndriy Gapon case SUPERIO_IVAR_DMA: 702*e3722b78SAndriy Gapon return (EINVAL); 703*e3722b78SAndriy Gapon default: 704*e3722b78SAndriy Gapon return (ENOENT); 705*e3722b78SAndriy Gapon } 706*e3722b78SAndriy Gapon } 707*e3722b78SAndriy Gapon 708*e3722b78SAndriy Gapon static struct resource_list * 709*e3722b78SAndriy Gapon superio_get_resource_list(device_t dev, device_t child) 710*e3722b78SAndriy Gapon { 711*e3722b78SAndriy Gapon struct superio_devinfo *dinfo = device_get_ivars(child); 712*e3722b78SAndriy Gapon 713*e3722b78SAndriy Gapon return (&dinfo->resources); 714*e3722b78SAndriy Gapon } 715*e3722b78SAndriy Gapon 716*e3722b78SAndriy Gapon static int 717*e3722b78SAndriy Gapon superio_printf(struct superio_devinfo *dinfo, const char *fmt, ...) 718*e3722b78SAndriy Gapon { 719*e3722b78SAndriy Gapon va_list ap; 720*e3722b78SAndriy Gapon int retval; 721*e3722b78SAndriy Gapon 722*e3722b78SAndriy Gapon retval = printf("superio:%s@ldn%0x2x: ", 723*e3722b78SAndriy Gapon devtype_to_str(dinfo->type), dinfo->ldn); 724*e3722b78SAndriy Gapon va_start(ap, fmt); 725*e3722b78SAndriy Gapon retval += vprintf(fmt, ap); 726*e3722b78SAndriy Gapon va_end(ap); 727*e3722b78SAndriy Gapon return (retval); 728*e3722b78SAndriy Gapon } 729*e3722b78SAndriy Gapon 730*e3722b78SAndriy Gapon static void 731*e3722b78SAndriy Gapon superio_child_detached(device_t dev, device_t child) 732*e3722b78SAndriy Gapon { 733*e3722b78SAndriy Gapon struct superio_devinfo *dinfo; 734*e3722b78SAndriy Gapon struct resource_list *rl; 735*e3722b78SAndriy Gapon 736*e3722b78SAndriy Gapon dinfo = device_get_ivars(child); 737*e3722b78SAndriy Gapon rl = &dinfo->resources; 738*e3722b78SAndriy Gapon 739*e3722b78SAndriy Gapon if (resource_list_release_active(rl, dev, child, SYS_RES_IRQ) != 0) 740*e3722b78SAndriy Gapon superio_printf(dinfo, "Device leaked IRQ resources\n"); 741*e3722b78SAndriy Gapon if (resource_list_release_active(rl, dev, child, SYS_RES_MEMORY) != 0) 742*e3722b78SAndriy Gapon superio_printf(dinfo, "Device leaked memory resources\n"); 743*e3722b78SAndriy Gapon if (resource_list_release_active(rl, dev, child, SYS_RES_IOPORT) != 0) 744*e3722b78SAndriy Gapon superio_printf(dinfo, "Device leaked I/O resources\n"); 745*e3722b78SAndriy Gapon } 746*e3722b78SAndriy Gapon 747*e3722b78SAndriy Gapon static int 748*e3722b78SAndriy Gapon superio_child_location_str(device_t parent, device_t child, char *buf, 749*e3722b78SAndriy Gapon size_t buflen) 750*e3722b78SAndriy Gapon { 751*e3722b78SAndriy Gapon uint8_t ldn; 752*e3722b78SAndriy Gapon 753*e3722b78SAndriy Gapon ldn = superio_get_ldn(child); 754*e3722b78SAndriy Gapon snprintf(buf, buflen, "ldn=0x%02x", ldn); 755*e3722b78SAndriy Gapon return (0); 756*e3722b78SAndriy Gapon } 757*e3722b78SAndriy Gapon 758*e3722b78SAndriy Gapon static int 759*e3722b78SAndriy Gapon superio_child_pnp_str(device_t parent, device_t child, char *buf, 760*e3722b78SAndriy Gapon size_t buflen) 761*e3722b78SAndriy Gapon { 762*e3722b78SAndriy Gapon superio_dev_type_t type; 763*e3722b78SAndriy Gapon 764*e3722b78SAndriy Gapon type = superio_get_type(child); 765*e3722b78SAndriy Gapon snprintf(buf, buflen, "type=%s", devtype_to_str(type)); 766*e3722b78SAndriy Gapon return (0); 767*e3722b78SAndriy Gapon } 768*e3722b78SAndriy Gapon 769*e3722b78SAndriy Gapon static int 770*e3722b78SAndriy Gapon superio_print_child(device_t parent, device_t child) 771*e3722b78SAndriy Gapon { 772*e3722b78SAndriy Gapon superio_dev_type_t type; 773*e3722b78SAndriy Gapon uint8_t ldn; 774*e3722b78SAndriy Gapon int retval; 775*e3722b78SAndriy Gapon 776*e3722b78SAndriy Gapon ldn = superio_get_ldn(child); 777*e3722b78SAndriy Gapon type = superio_get_type(child); 778*e3722b78SAndriy Gapon 779*e3722b78SAndriy Gapon retval = bus_print_child_header(parent, child); 780*e3722b78SAndriy Gapon retval += printf(" at %s ldn 0x%02x", devtype_to_str(type), ldn); 781*e3722b78SAndriy Gapon retval += bus_print_child_footer(parent, child); 782*e3722b78SAndriy Gapon 783*e3722b78SAndriy Gapon return (retval); 784*e3722b78SAndriy Gapon } 785*e3722b78SAndriy Gapon 786*e3722b78SAndriy Gapon superio_vendor_t 787*e3722b78SAndriy Gapon superio_vendor(device_t dev) 788*e3722b78SAndriy Gapon { 789*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 790*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 791*e3722b78SAndriy Gapon 792*e3722b78SAndriy Gapon return (sc->vendor); 793*e3722b78SAndriy Gapon } 794*e3722b78SAndriy Gapon 795*e3722b78SAndriy Gapon uint16_t 796*e3722b78SAndriy Gapon superio_devid(device_t dev) 797*e3722b78SAndriy Gapon { 798*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 799*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 800*e3722b78SAndriy Gapon 801*e3722b78SAndriy Gapon return (sc->devid); 802*e3722b78SAndriy Gapon } 803*e3722b78SAndriy Gapon 804*e3722b78SAndriy Gapon uint8_t 805*e3722b78SAndriy Gapon superio_revid(device_t dev) 806*e3722b78SAndriy Gapon { 807*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 808*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 809*e3722b78SAndriy Gapon 810*e3722b78SAndriy Gapon return (sc->revid); 811*e3722b78SAndriy Gapon } 812*e3722b78SAndriy Gapon 813*e3722b78SAndriy Gapon uint8_t 814*e3722b78SAndriy Gapon superio_read(device_t dev, uint8_t reg) 815*e3722b78SAndriy Gapon { 816*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 817*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 818*e3722b78SAndriy Gapon struct superio_devinfo *dinfo = device_get_ivars(dev); 819*e3722b78SAndriy Gapon uint8_t v; 820*e3722b78SAndriy Gapon 821*e3722b78SAndriy Gapon sio_conf_enter(sc); 822*e3722b78SAndriy Gapon v = sio_ldn_read(sc, dinfo->ldn, reg); 823*e3722b78SAndriy Gapon sio_conf_exit(sc); 824*e3722b78SAndriy Gapon return (v); 825*e3722b78SAndriy Gapon } 826*e3722b78SAndriy Gapon 827*e3722b78SAndriy Gapon void 828*e3722b78SAndriy Gapon superio_write(device_t dev, uint8_t reg, uint8_t val) 829*e3722b78SAndriy Gapon { 830*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 831*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 832*e3722b78SAndriy Gapon struct superio_devinfo *dinfo = device_get_ivars(dev); 833*e3722b78SAndriy Gapon 834*e3722b78SAndriy Gapon sio_conf_enter(sc); 835*e3722b78SAndriy Gapon sio_ldn_write(sc, dinfo->ldn, reg, val); 836*e3722b78SAndriy Gapon sio_conf_exit(sc); 837*e3722b78SAndriy Gapon } 838*e3722b78SAndriy Gapon 839*e3722b78SAndriy Gapon bool 840*e3722b78SAndriy Gapon superio_dev_enabled(device_t dev, uint8_t mask) 841*e3722b78SAndriy Gapon { 842*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 843*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 844*e3722b78SAndriy Gapon struct superio_devinfo *dinfo = device_get_ivars(dev); 845*e3722b78SAndriy Gapon uint8_t v; 846*e3722b78SAndriy Gapon 847*e3722b78SAndriy Gapon /* GPIO device is always active in ITE chips. */ 848*e3722b78SAndriy Gapon if (sc->vendor == SUPERIO_VENDOR_ITE && dinfo->ldn == 7) 849*e3722b78SAndriy Gapon return (true); 850*e3722b78SAndriy Gapon 851*e3722b78SAndriy Gapon v = superio_read(dev, sc->enable_reg); 852*e3722b78SAndriy Gapon return ((v & mask) != 0); 853*e3722b78SAndriy Gapon } 854*e3722b78SAndriy Gapon 855*e3722b78SAndriy Gapon void 856*e3722b78SAndriy Gapon superio_dev_enable(device_t dev, uint8_t mask) 857*e3722b78SAndriy Gapon { 858*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 859*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 860*e3722b78SAndriy Gapon struct superio_devinfo *dinfo = device_get_ivars(dev); 861*e3722b78SAndriy Gapon uint8_t v; 862*e3722b78SAndriy Gapon 863*e3722b78SAndriy Gapon /* GPIO device is always active in ITE chips. */ 864*e3722b78SAndriy Gapon if (sc->vendor == SUPERIO_VENDOR_ITE && dinfo->ldn == 7) 865*e3722b78SAndriy Gapon return; 866*e3722b78SAndriy Gapon 867*e3722b78SAndriy Gapon sio_conf_enter(sc); 868*e3722b78SAndriy Gapon v = sio_ldn_read(sc, dinfo->ldn, sc->enable_reg); 869*e3722b78SAndriy Gapon v |= mask; 870*e3722b78SAndriy Gapon sio_ldn_write(sc, dinfo->ldn, sc->enable_reg, v); 871*e3722b78SAndriy Gapon sio_conf_exit(sc); 872*e3722b78SAndriy Gapon } 873*e3722b78SAndriy Gapon 874*e3722b78SAndriy Gapon void 875*e3722b78SAndriy Gapon superio_dev_disable(device_t dev, uint8_t mask) 876*e3722b78SAndriy Gapon { 877*e3722b78SAndriy Gapon device_t sio_dev = device_get_parent(dev); 878*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(sio_dev); 879*e3722b78SAndriy Gapon struct superio_devinfo *dinfo = device_get_ivars(dev); 880*e3722b78SAndriy Gapon uint8_t v; 881*e3722b78SAndriy Gapon 882*e3722b78SAndriy Gapon /* GPIO device is always active in ITE chips. */ 883*e3722b78SAndriy Gapon if (sc->vendor == SUPERIO_VENDOR_ITE && dinfo->ldn == 7) 884*e3722b78SAndriy Gapon return; 885*e3722b78SAndriy Gapon 886*e3722b78SAndriy Gapon sio_conf_enter(sc); 887*e3722b78SAndriy Gapon v = sio_ldn_read(sc, dinfo->ldn, sc->enable_reg); 888*e3722b78SAndriy Gapon v &= ~mask; 889*e3722b78SAndriy Gapon sio_ldn_write(sc, dinfo->ldn, sc->enable_reg, v); 890*e3722b78SAndriy Gapon sio_conf_exit(sc); 891*e3722b78SAndriy Gapon } 892*e3722b78SAndriy Gapon 893*e3722b78SAndriy Gapon device_t 894*e3722b78SAndriy Gapon superio_find_dev(device_t superio, superio_dev_type_t type, int ldn) 895*e3722b78SAndriy Gapon { 896*e3722b78SAndriy Gapon struct siosc *sc = device_get_softc(superio); 897*e3722b78SAndriy Gapon struct superio_devinfo *dinfo; 898*e3722b78SAndriy Gapon 899*e3722b78SAndriy Gapon if (ldn < -1 || ldn > UINT8_MAX) 900*e3722b78SAndriy Gapon return (NULL); /* ERANGE */ 901*e3722b78SAndriy Gapon if (type == SUPERIO_DEV_NONE && ldn == -1) 902*e3722b78SAndriy Gapon return (NULL); /* EINVAL */ 903*e3722b78SAndriy Gapon 904*e3722b78SAndriy Gapon STAILQ_FOREACH(dinfo, &sc->devlist, link) { 905*e3722b78SAndriy Gapon if (ldn != -1 && dinfo->ldn != ldn) 906*e3722b78SAndriy Gapon continue; 907*e3722b78SAndriy Gapon if (type != SUPERIO_DEV_NONE && dinfo->type != type) 908*e3722b78SAndriy Gapon continue; 909*e3722b78SAndriy Gapon return (dinfo->dev); 910*e3722b78SAndriy Gapon } 911*e3722b78SAndriy Gapon return (NULL); 912*e3722b78SAndriy Gapon } 913*e3722b78SAndriy Gapon 914*e3722b78SAndriy Gapon static devclass_t superio_devclass; 915*e3722b78SAndriy Gapon 916*e3722b78SAndriy Gapon static device_method_t superio_methods[] = { 917*e3722b78SAndriy Gapon DEVMETHOD(device_identify, superio_identify), 918*e3722b78SAndriy Gapon DEVMETHOD(device_probe, superio_probe), 919*e3722b78SAndriy Gapon DEVMETHOD(device_attach, superio_attach), 920*e3722b78SAndriy Gapon DEVMETHOD(device_detach, superio_detach), 921*e3722b78SAndriy Gapon DEVMETHOD(device_shutdown, bus_generic_shutdown), 922*e3722b78SAndriy Gapon DEVMETHOD(device_suspend, bus_generic_suspend), 923*e3722b78SAndriy Gapon DEVMETHOD(device_resume, bus_generic_resume), 924*e3722b78SAndriy Gapon 925*e3722b78SAndriy Gapon DEVMETHOD(bus_add_child, superio_add_child), 926*e3722b78SAndriy Gapon DEVMETHOD(bus_child_detached, superio_child_detached), 927*e3722b78SAndriy Gapon DEVMETHOD(bus_child_location_str, superio_child_location_str), 928*e3722b78SAndriy Gapon DEVMETHOD(bus_child_pnpinfo_str, superio_child_pnp_str), 929*e3722b78SAndriy Gapon DEVMETHOD(bus_print_child, superio_print_child), 930*e3722b78SAndriy Gapon DEVMETHOD(bus_read_ivar, superio_read_ivar), 931*e3722b78SAndriy Gapon DEVMETHOD(bus_write_ivar, superio_write_ivar), 932*e3722b78SAndriy Gapon DEVMETHOD(bus_get_resource_list, superio_get_resource_list), 933*e3722b78SAndriy Gapon DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 934*e3722b78SAndriy Gapon DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 935*e3722b78SAndriy Gapon DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 936*e3722b78SAndriy Gapon DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 937*e3722b78SAndriy Gapon DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource), 938*e3722b78SAndriy Gapon DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 939*e3722b78SAndriy Gapon DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 940*e3722b78SAndriy Gapon DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 941*e3722b78SAndriy Gapon DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 942*e3722b78SAndriy Gapon 943*e3722b78SAndriy Gapon DEVMETHOD_END 944*e3722b78SAndriy Gapon }; 945*e3722b78SAndriy Gapon 946*e3722b78SAndriy Gapon static driver_t superio_driver = { 947*e3722b78SAndriy Gapon "superio", 948*e3722b78SAndriy Gapon superio_methods, 949*e3722b78SAndriy Gapon sizeof(struct siosc) 950*e3722b78SAndriy Gapon }; 951*e3722b78SAndriy Gapon 952*e3722b78SAndriy Gapon DRIVER_MODULE(superio, isa, superio_driver, superio_devclass, 0, 0); 953*e3722b78SAndriy Gapon MODULE_VERSION(superio, 1); 954