obiofan.c revision 1.2 1 1.2 macallan /* $NetBSD: obiofan.c,v 1.2 2021/09/11 16:29:18 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.1 macallan int sc_reg, sc_shift;
57 1.1 macallan int sc_count, sc_rpm;
58 1.1 macallan };
59 1.1 macallan
60 1.1 macallan int obiofan_match(device_t, cfdata_t, void *);
61 1.1 macallan void obiofan_attach(device_t, device_t, void *);
62 1.1 macallan static void obiofan_refresh(struct sysmon_envsys *, envsys_data_t *);
63 1.1 macallan static void obiofan_update(void *);
64 1.1 macallan
65 1.1 macallan CFATTACH_DECL_NEW(obiofan, sizeof(struct obiofan_softc), obiofan_match,
66 1.1 macallan obiofan_attach, NULL, NULL);
67 1.1 macallan
68 1.1 macallan int
69 1.1 macallan obiofan_match(device_t parent, cfdata_t match, void *aux)
70 1.1 macallan {
71 1.1 macallan struct confargs *ca = aux;
72 1.1 macallan
73 1.1 macallan if (strcmp(ca->ca_name, "fans") == 0)
74 1.1 macallan return 1;
75 1.1 macallan
76 1.1 macallan return 0;
77 1.1 macallan }
78 1.1 macallan
79 1.1 macallan void
80 1.1 macallan obiofan_attach(device_t parent, device_t self, void *aux)
81 1.1 macallan {
82 1.1 macallan struct obiofan_softc *sc = device_private(self);
83 1.1 macallan struct confargs *ca = aux;
84 1.1 macallan char descr[32] = "fan";
85 1.1 macallan int node = ca->ca_node, tc_node;
86 1.1 macallan int regs[8], tc_regs[32];
87 1.1 macallan
88 1.1 macallan printf("\n");
89 1.1 macallan
90 1.1 macallan if (OF_getprop(node, "reg", regs, sizeof(regs)) <= 0)
91 1.1 macallan return;
92 1.1 macallan
93 1.1 macallan sc->sc_node = node;
94 1.1 macallan
95 1.1 macallan #ifdef OBIOFAN_DEBUG
96 1.1 macallan int i;
97 1.1 macallan for (i = 0; i < 7; i += 2)
98 1.1 macallan printf("%02x: %08x\n", regs[i], obio_read_4(regs[i]));
99 1.1 macallan #endif
100 1.1 macallan
101 1.1 macallan tc_node = OF_parent(node);
102 1.1 macallan
103 1.1 macallan if (OF_getprop(tc_node, "platform-do-getTACHCount", tc_regs, 32) <= 0) {
104 1.1 macallan aprint_error("no platform-do-getTACHCount\n");
105 1.1 macallan return;
106 1.1 macallan }
107 1.1 macallan
108 1.1 macallan /*
109 1.1 macallan * XXX this is guesswork based on poking around & observation
110 1.1 macallan *
111 1.1 macallan * the parameter format seems to be:
112 1.1 macallan * reg[0] - node number for the fan, or pointer into OF space
113 1.1 macallan * reg[1] - 0x08000000
114 1.1 macallan * reg[2] - 0x0000001a - varies with different platform-do-*
115 1.1 macallan * reg[3] - register number in obio space
116 1.1 macallan * reg[4] - bitmask in the register
117 1.1 macallan * reg[5] - unknown
118 1.1 macallan * reg[6] - unknown
119 1.1 macallan *
120 1.1 macallan * for now only get the parameters for the 1st fan since that's all we
121 1.1 macallan * have on my 7,3
122 1.1 macallan */
123 1.1 macallan
124 1.1 macallan sc->sc_reg = tc_regs[3];
125 1.1 macallan sc->sc_shift = ffs(tc_regs[4]) - 1;
126 1.1 macallan
127 1.1 macallan OF_getprop(tc_regs[0], "location", descr, 32);
128 1.1 macallan DPRINTF("%s: %02x %d\n", descr, sc->sc_reg, sc->sc_shift);
129 1.1 macallan
130 1.1 macallan sc->sc_stamp = time_second;
131 1.1 macallan sc->sc_count = obio_read_4(sc->sc_reg) >> sc->sc_shift;
132 1.1 macallan sc->sc_rpm = -1;
133 1.1 macallan
134 1.1 macallan sc->sc_sme = sysmon_envsys_create();
135 1.1 macallan sc->sc_sme->sme_name = device_xname(self);
136 1.1 macallan sc->sc_sme->sme_cookie = sc;
137 1.1 macallan sc->sc_sme->sme_refresh = obiofan_refresh;
138 1.1 macallan
139 1.1 macallan sc->sc_sensors[0].units = ENVSYS_SFANRPM;
140 1.1 macallan sc->sc_sensors[0].state = ENVSYS_SINVALID;
141 1.1 macallan strcpy(sc->sc_sensors[0].desc, descr);
142 1.1 macallan sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[0]);
143 1.1 macallan sysmon_envsys_register(sc->sc_sme);
144 1.1 macallan
145 1.1 macallan callout_init(&sc->sc_callout, 0);
146 1.1 macallan callout_setfunc(&sc->sc_callout, obiofan_update, sc);
147 1.1 macallan callout_schedule(&sc->sc_callout, mstohz(5000));
148 1.1 macallan }
149 1.1 macallan
150 1.1 macallan static void
151 1.1 macallan obiofan_update(void *cookie)
152 1.1 macallan {
153 1.1 macallan struct obiofan_softc *sc = cookie;
154 1.1 macallan time_t now = time_second, diff;
155 1.1 macallan int spin, spins;
156 1.1 macallan diff = now - sc->sc_stamp;
157 1.1 macallan if (diff < 5) return;
158 1.1 macallan spin = obio_read_4(sc->sc_reg) >> sc->sc_shift;
159 1.1 macallan spins = (spin - sc->sc_count) & 0xffff;
160 1.1 macallan sc->sc_rpm = spins * 60 / diff;
161 1.1 macallan sc->sc_count = spin;
162 1.1 macallan sc->sc_stamp = now;
163 1.1 macallan DPRINTF("%s %lld %d\n", __func__, diff, spins);
164 1.1 macallan callout_schedule(&sc->sc_callout, mstohz(5000));
165 1.1 macallan }
166 1.1 macallan
167 1.1 macallan static void
168 1.1 macallan obiofan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
169 1.1 macallan {
170 1.1 macallan struct obiofan_softc *sc = sme->sme_cookie;
171 1.1 macallan if (sc->sc_rpm >= 0) {
172 1.1 macallan edata->state = ENVSYS_SVALID;
173 1.1 macallan edata->value_cur = sc->sc_rpm;
174 1.1 macallan }
175 1.1 macallan }
176