1/* $NetBSD: ug_isa.c,v 1.10 2022/09/25 17:09:36 thorpej 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.10 2022/09/25 17:09:36 thorpej 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/errno.h>
43#include <sys/conf.h>
44#include <sys/envsys.h>
45#include <sys/time.h>
46
47#include <sys/bus.h>
48#include <sys/intr.h>
49
50#include <dev/isa/isareg.h>
51#include <dev/isa/isavar.h>
52
53#include <dev/sysmon/sysmonvar.h>
54
55#include <dev/ic/ugreg.h>
56#include <dev/ic/ugvar.h>
57
58/* autoconf(9) functions */
59static int  ug_isa_match(device_t, cfdata_t, void *);
60static void ug_isa_attach(device_t, device_t, void *);
61static int  ug_isa_detach(device_t, int);
62
63CFATTACH_DECL_NEW(ug_isa, sizeof(struct ug_softc),
64    ug_isa_match, ug_isa_attach, ug_isa_detach, NULL);
65
66extern uint8_t ug_ver;
67
68static int
69ug_isa_match(device_t parent, cfdata_t match, void *aux)
70{
71	struct isa_attach_args *ia = aux;
72	bus_space_handle_t bsh;
73	uint8_t valc, vald;
74
75	if (ia->ia_nio < 1)     /* need base addr */
76		return 0;
77
78	if (ISA_DIRECT_CONFIG(ia))
79		return 0;
80
81	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
82		return 0;
83
84	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 8, 0, &bsh))
85		return 0;
86
87	valc = bus_space_read_1(ia->ia_iot, bsh, UG_CMD);
88	vald = bus_space_read_1(ia->ia_iot, bsh, UG_DATA);
89
90	ug_ver = 0;
91
92	/* Check for uGuru 2003 */
93
94	if (((vald == 0) || (vald == 8)) && (valc == 0xAC))
95		ug_ver = 1;
96
97	/* Check for uGuru 2005 */
98	if (ug2_sync(ia->ia_iot, bsh) == 1)
99		ug_ver = 2;
100
101	/* unmap, prepare ia and bye */
102	bus_space_unmap(ia->ia_iot, bsh, 8);
103
104	if (ug_ver != 0) {
105		ia->ia_nio = 1;
106		ia->ia_io[0].ir_size = 8;
107		ia->ia_niomem = 0;
108		ia->ia_nirq = 0;
109		ia->ia_ndrq = 0;
110		return 1;
111	}
112
113	return 0;
114
115}
116
117static void
118ug_isa_attach(device_t parent, device_t self, void *aux)
119{
120	struct ug_softc *sc = device_private(self);
121	struct isa_attach_args *ia = aux;
122	int i;
123
124	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
125	    8, 0, &sc->sc_ioh)) {
126		aprint_error(": can't map i/o space\n");
127		return;
128	}
129
130	ia->ia_iot = sc->sc_iot;
131	sc->version = ug_ver;
132
133	if (sc->version == 2) {
134		ug2_attach(self);
135		return;
136	}
137
138	aprint_normal(": Abit uGuru system monitor\n");
139
140	if (!ug_reset(sc))
141		aprint_error_dev(self, "reset failed.\n");
142
143	ug_setup_sensors(sc);
144	sc->sc_sme = sysmon_envsys_create();
145
146	for (i = 0; i < UG_NUM_SENSORS; i++) {
147		if (sysmon_envsys_sensor_attach(sc->sc_sme,
148						&sc->sc_sensor[i])) {
149			sysmon_envsys_destroy(sc->sc_sme);
150			sc->sc_sme = NULL;
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		sc->sc_sme = NULL;
162		goto out;
163	}
164
165	return;
166
167out:
168	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8);
169
170}
171
172static int
173ug_isa_detach(device_t self, int flags)
174{
175	struct ug_softc *sc = device_private(self);
176
177	if (sc->sc_sme != NULL)
178		sysmon_envsys_unregister(sc->sc_sme);
179	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 8);
180	return 0;
181}
182
183