1 /* $NetBSD: ug_isa.c,v 1.8 2020/06/24 19:24:44 jdolecek Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Mihai Chelaru <kefren@netbsd.ro> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * Driver for Abit uGuru (interface is inspired from it.c and nslm7x.c) 30 * Inspired by olle sandberg linux driver as Abit didn't care to release docs 31 * Support for uGuru 2005 from Louis Kruger and Hans de Goede linux driver 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ug_isa.c,v 1.8 2020/06/24 19:24:44 jdolecek Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/device.h> 42 #include <sys/malloc.h> 43 #include <sys/errno.h> 44 #include <sys/conf.h> 45 #include <sys/envsys.h> 46 #include <sys/time.h> 47 48 #include <sys/bus.h> 49 #include <sys/intr.h> 50 51 #include <dev/isa/isareg.h> 52 #include <dev/isa/isavar.h> 53 54 #include <dev/sysmon/sysmonvar.h> 55 56 #include <dev/ic/ugreg.h> 57 #include <dev/ic/ugvar.h> 58 59 /* autoconf(9) functions */ 60 static int ug_isa_match(device_t, cfdata_t, void *); 61 static void ug_isa_attach(device_t, device_t, void *); 62 static int ug_isa_detach(device_t, int); 63 64 CFATTACH_DECL_NEW(ug_isa, sizeof(struct ug_softc), 65 ug_isa_match, ug_isa_attach, ug_isa_detach, NULL); 66 67 extern uint8_t ug_ver; 68 69 static int 70 ug_isa_match(device_t parent, cfdata_t match, void *aux) 71 { 72 struct isa_attach_args *ia = aux; 73 bus_space_handle_t bsh; 74 uint8_t valc, vald; 75 76 if (ia->ia_nio < 1) /* need base addr */ 77 return 0; 78 79 if (ISA_DIRECT_CONFIG(ia)) 80 return 0; 81 82 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 83 return 0; 84 85 if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 8, 0, &bsh)) 86 return 0; 87 88 valc = bus_space_read_1(ia->ia_iot, bsh, UG_CMD); 89 vald = bus_space_read_1(ia->ia_iot, bsh, UG_DATA); 90 91 ug_ver = 0; 92 93 /* Check for uGuru 2003 */ 94 95 if (((vald == 0) || (vald == 8)) && (valc == 0xAC)) 96 ug_ver = 1; 97 98 /* Check for uGuru 2005 */ 99 if (ug2_sync(ia->ia_iot, bsh) == 1) 100 ug_ver = 2; 101 102 /* unmap, prepare ia and bye */ 103 bus_space_unmap(ia->ia_iot, bsh, 8); 104 105 if (ug_ver != 0) { 106 ia->ia_nio = 1; 107 ia->ia_io[0].ir_size = 8; 108 ia->ia_niomem = 0; 109 ia->ia_nirq = 0; 110 ia->ia_ndrq = 0; 111 return 1; 112 } 113 114 return 0; 115 116 } 117 118 static void 119 ug_isa_attach(device_t parent, device_t self, void *aux) 120 { 121 struct ug_softc *sc = device_private(self); 122 struct isa_attach_args *ia = aux; 123 int i; 124 125 if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 126 8, 0, &sc->sc_ioh)) { 127 aprint_error(": can't map i/o space\n"); 128 return; 129 } 130 131 ia->ia_iot = sc->sc_iot; 132 sc->version = ug_ver; 133 134 if (sc->version == 2) { 135 ug2_attach(self); 136 return; 137 } 138 139 aprint_normal(": Abit uGuru system monitor\n"); 140 141 if (!ug_reset(sc)) 142 aprint_error_dev(self, "reset failed.\n"); 143 144 ug_setup_sensors(sc); 145 sc->sc_sme = sysmon_envsys_create(); 146 147 for (i = 0; i < UG_NUM_SENSORS; i++) { 148 if (sysmon_envsys_sensor_attach(sc->sc_sme, 149 &sc->sc_sensor[i])) { 150 sysmon_envsys_destroy(sc->sc_sme); 151 goto out; 152 } 153 } 154 155 sc->sc_sme->sme_cookie = sc; 156 sc->sc_sme->sme_refresh = ug_refresh; 157 158 if (sysmon_envsys_register(sc->sc_sme)) { 159 aprint_error_dev(self, "unable to register with sysmon\n"); 160 sysmon_envsys_destroy(sc->sc_sme); 161 goto out; 162 } 163 164 return; 165 166 out: 167 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8); 168 169 } 170 171 static int 172 ug_isa_detach(device_t self, int flags) 173 { 174 struct ug_softc *sc = device_private(self); 175 176 sysmon_envsys_unregister(sc->sc_sme); 177 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8); 178 return 0; 179 } 180 181