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