obiofan.c revision 1.3 1 1.3 macallan /* $NetBSD: obiofan.c,v 1.3 2025/07/01 06:36:35 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.2 macallan * Copyright (c) 2021 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/param.h>
30 1.1 macallan #include <sys/device.h>
31 1.1 macallan #include <sys/systm.h>
32 1.1 macallan #include <sys/mutex.h>
33 1.1 macallan #include <sys/callout.h>
34 1.1 macallan #include <sys/time.h>
35 1.1 macallan
36 1.1 macallan #include <dev/ofw/openfirm.h>
37 1.1 macallan #include <dev/sysmon/sysmonvar.h>
38 1.1 macallan #include <machine/autoconf.h>
39 1.1 macallan #include <macppc/dev/obiovar.h>
40 1.1 macallan
41 1.1 macallan #include "opt_obiofan.h"
42 1.1 macallan
43 1.1 macallan #ifdef OBIOFAN_DEBUG
44 1.1 macallan #define DPRINTF printf
45 1.1 macallan #else
46 1.1 macallan #define DPRINTF if (0) printf
47 1.1 macallan #endif
48 1.1 macallan
49 1.1 macallan struct obiofan_softc {
50 1.1 macallan device_t sc_dev;
51 1.1 macallan struct sysmon_envsys *sc_sme;
52 1.1 macallan envsys_data_t sc_sensors[4];
53 1.1 macallan callout_t sc_callout;
54 1.1 macallan time_t sc_stamp;
55 1.1 macallan int sc_node;
56 1.3 macallan int sc_reg[4], sc_shift[4];
57 1.3 macallan int sc_count[4], sc_rpm[4];
58 1.3 macallan int sc_fans;
59 1.1 macallan };
60 1.1 macallan
61 1.1 macallan int obiofan_match(device_t, cfdata_t, void *);
62 1.1 macallan void obiofan_attach(device_t, device_t, void *);
63 1.1 macallan static void obiofan_refresh(struct sysmon_envsys *, envsys_data_t *);
64 1.1 macallan static void obiofan_update(void *);
65 1.1 macallan
66 1.1 macallan CFATTACH_DECL_NEW(obiofan, sizeof(struct obiofan_softc), obiofan_match,
67 1.1 macallan obiofan_attach, NULL, NULL);
68 1.1 macallan
69 1.1 macallan int
70 1.1 macallan obiofan_match(device_t parent, cfdata_t match, void *aux)
71 1.1 macallan {
72 1.1 macallan struct confargs *ca = aux;
73 1.1 macallan
74 1.1 macallan if (strcmp(ca->ca_name, "fans") == 0)
75 1.1 macallan return 1;
76 1.1 macallan
77 1.1 macallan return 0;
78 1.1 macallan }
79 1.1 macallan
80 1.1 macallan void
81 1.1 macallan obiofan_attach(device_t parent, device_t self, void *aux)
82 1.1 macallan {
83 1.1 macallan struct obiofan_softc *sc = device_private(self);
84 1.1 macallan struct confargs *ca = aux;
85 1.1 macallan char descr[32] = "fan";
86 1.3 macallan int node = ca->ca_node, tc_node, f, cnt;
87 1.3 macallan uint32_t regs[8], tc_regs[32];
88 1.1 macallan
89 1.1 macallan printf("\n");
90 1.1 macallan
91 1.1 macallan if (OF_getprop(node, "reg", regs, sizeof(regs)) <= 0)
92 1.1 macallan return;
93 1.1 macallan
94 1.1 macallan sc->sc_node = node;
95 1.1 macallan
96 1.1 macallan #ifdef OBIOFAN_DEBUG
97 1.1 macallan int i;
98 1.1 macallan for (i = 0; i < 7; i += 2)
99 1.1 macallan printf("%02x: %08x\n", regs[i], obio_read_4(regs[i]));
100 1.1 macallan #endif
101 1.1 macallan
102 1.1 macallan tc_node = OF_parent(node);
103 1.1 macallan
104 1.3 macallan if (OF_getprop(tc_node, "platform-do-getTACHCount", tc_regs, 128) <= 0) {
105 1.1 macallan aprint_error("no platform-do-getTACHCount\n");
106 1.1 macallan return;
107 1.1 macallan }
108 1.1 macallan
109 1.1 macallan /*
110 1.1 macallan * XXX this is guesswork based on poking around & observation
111 1.1 macallan *
112 1.1 macallan * the parameter format seems to be:
113 1.1 macallan * reg[0] - node number for the fan, or pointer into OF space
114 1.1 macallan * reg[1] - 0x08000000
115 1.1 macallan * reg[2] - 0x0000001a - varies with different platform-do-*
116 1.1 macallan * reg[3] - register number in obio space
117 1.1 macallan * reg[4] - bitmask in the register
118 1.1 macallan * reg[5] - unknown
119 1.1 macallan * reg[6] - unknown
120 1.1 macallan */
121 1.1 macallan
122 1.1 macallan sc->sc_sme = sysmon_envsys_create();
123 1.1 macallan sc->sc_sme->sme_name = device_xname(self);
124 1.1 macallan sc->sc_sme->sme_cookie = sc;
125 1.1 macallan sc->sc_sme->sme_refresh = obiofan_refresh;
126 1.1 macallan
127 1.3 macallan /* now look for which fan register(s) we can use */
128 1.3 macallan f = OF_child(node);
129 1.3 macallan cnt = 0;
130 1.3 macallan while (f != 0) {
131 1.3 macallan int num = -1;
132 1.3 macallan OF_getprop(f, "reg", &num, 4);
133 1.3 macallan if (OF_getprop(f, "hwctrl-location", descr, 32) <= 0)
134 1.3 macallan goto skip;
135 1.3 macallan sc->sc_reg[cnt] = tc_regs[num * 7 + 3];
136 1.3 macallan sc->sc_shift[cnt] = ffs(tc_regs[num * 7 + 4]) - 1;
137 1.3 macallan
138 1.3 macallan DPRINTF("%s: %d %02x %d\n", descr, num, sc->sc_reg[cnt], sc->sc_shift[cnt]);
139 1.3 macallan
140 1.3 macallan sc->sc_stamp = time_second;
141 1.3 macallan sc->sc_count[cnt] =
142 1.3 macallan obio_read_4(sc->sc_reg[cnt]) >> sc->sc_shift[cnt];
143 1.3 macallan sc->sc_rpm[cnt] = -1;
144 1.3 macallan
145 1.3 macallan sc->sc_sensors[cnt].units = ENVSYS_SFANRPM;
146 1.3 macallan sc->sc_sensors[cnt].state = ENVSYS_SINVALID;
147 1.3 macallan sc->sc_sensors[cnt].private = cnt;
148 1.3 macallan strcpy(sc->sc_sensors[cnt].desc, descr);
149 1.3 macallan sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[cnt]);
150 1.3 macallan cnt++;
151 1.3 macallan skip:
152 1.3 macallan f = OF_peer(f);
153 1.3 macallan }
154 1.3 macallan sc->sc_fans = cnt;
155 1.1 macallan sysmon_envsys_register(sc->sc_sme);
156 1.1 macallan
157 1.1 macallan callout_init(&sc->sc_callout, 0);
158 1.1 macallan callout_setfunc(&sc->sc_callout, obiofan_update, sc);
159 1.1 macallan callout_schedule(&sc->sc_callout, mstohz(5000));
160 1.1 macallan }
161 1.1 macallan
162 1.1 macallan static void
163 1.1 macallan obiofan_update(void *cookie)
164 1.1 macallan {
165 1.1 macallan struct obiofan_softc *sc = cookie;
166 1.1 macallan time_t now = time_second, diff;
167 1.3 macallan int spin, spins, i;
168 1.3 macallan for (i = 0; i < sc->sc_fans; i++) {
169 1.3 macallan diff = now - sc->sc_stamp;
170 1.3 macallan if (diff < 5) return;
171 1.3 macallan spin = obio_read_4(sc->sc_reg[i]) >> sc->sc_shift[i];
172 1.3 macallan spins = (spin - sc->sc_count[i]) & 0xffff;
173 1.3 macallan sc->sc_rpm[i] = spins * 60 / diff;
174 1.3 macallan sc->sc_count[i] = spin;
175 1.3 macallan DPRINTF("%s %lld %d\n", __func__, diff, spins);
176 1.3 macallan }
177 1.1 macallan sc->sc_stamp = now;
178 1.1 macallan callout_schedule(&sc->sc_callout, mstohz(5000));
179 1.1 macallan }
180 1.1 macallan
181 1.1 macallan static void
182 1.1 macallan obiofan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
183 1.1 macallan {
184 1.1 macallan struct obiofan_softc *sc = sme->sme_cookie;
185 1.3 macallan int i = edata->private;
186 1.3 macallan
187 1.3 macallan if (sc->sc_rpm[i] >= 0) {
188 1.1 macallan edata->state = ENVSYS_SVALID;
189 1.3 macallan edata->value_cur = sc->sc_rpm[i];
190 1.1 macallan }
191 1.1 macallan }
192