nslm7x.c revision 1.4 1 1.4 thorpej /* $NetBSD: nslm7x.c,v 1.4 2000/06/24 00:37:19 thorpej Exp $ */
2 1.1 groo
3 1.1 groo /*-
4 1.1 groo * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 groo * All rights reserved.
6 1.1 groo *
7 1.1 groo * This code is derived from software contributed to The NetBSD Foundation
8 1.1 groo * by Bill Squier.
9 1.1 groo *
10 1.1 groo * Redistribution and use in source and binary forms, with or without
11 1.1 groo * modification, are permitted provided that the following conditions
12 1.1 groo * are met:
13 1.1 groo * 1. Redistributions of source code must retain the above copyright
14 1.1 groo * notice, this list of conditions and the following disclaimer.
15 1.1 groo * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 groo * notice, this list of conditions and the following disclaimer in the
17 1.1 groo * documentation and/or other materials provided with the distribution.
18 1.1 groo * 3. All advertising materials mentioning features or use of this software
19 1.1 groo * must display the following acknowledgement:
20 1.1 groo * This product includes software developed by the NetBSD
21 1.1 groo * Foundation, Inc. and its contributors.
22 1.1 groo * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 groo * contributors may be used to endorse or promote products derived
24 1.1 groo * from this software without specific prior written permission.
25 1.1 groo *
26 1.1 groo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 groo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 groo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 groo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 groo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 groo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 groo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 groo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 groo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 groo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 groo * POSSIBILITY OF SUCH DAMAGE.
37 1.1 groo */
38 1.1 groo
39 1.1 groo #include <sys/param.h>
40 1.1 groo #include <sys/systm.h>
41 1.1 groo #include <sys/kernel.h>
42 1.1 groo #include <sys/proc.h>
43 1.1 groo #include <sys/device.h>
44 1.1 groo #include <sys/malloc.h>
45 1.1 groo #include <sys/errno.h>
46 1.1 groo #include <sys/queue.h>
47 1.1 groo #include <sys/lock.h>
48 1.1 groo #include <sys/ioctl.h>
49 1.1 groo #include <sys/conf.h>
50 1.1 groo #include <sys/time.h>
51 1.1 groo
52 1.1 groo #include <machine/bus.h>
53 1.1 groo
54 1.1 groo #include <dev/isa/isareg.h>
55 1.1 groo #include <dev/isa/isavar.h>
56 1.1 groo
57 1.4 thorpej #include <dev/sysmon/sysmonvar.h>
58 1.4 thorpej
59 1.1 groo #include <dev/ic/nslm7xvar.h>
60 1.1 groo
61 1.1 groo #include <machine/intr.h>
62 1.1 groo #include <machine/bus.h>
63 1.1 groo
64 1.1 groo #if defined(LMDEBUG)
65 1.1 groo #define DPRINTF(x) do { printf x; } while (0)
66 1.1 groo #else
67 1.1 groo #define DPRINTF(x)
68 1.1 groo #endif
69 1.1 groo
70 1.4 thorpej const struct envsys_range lm_ranges[] = { /* sc->sensors sub-intervals */
71 1.4 thorpej /* for each unit type */
72 1.1 groo { 7, 7, ENVSYS_STEMP },
73 1.1 groo { 8, 10, ENVSYS_SFANRPM },
74 1.1 groo { 1, 0, ENVSYS_SVOLTS_AC }, /* None */
75 1.1 groo { 0, 6, ENVSYS_SVOLTS_DC },
76 1.1 groo { 1, 0, ENVSYS_SOHMS }, /* None */
77 1.1 groo { 1, 0, ENVSYS_SWATTS }, /* None */
78 1.1 groo { 1, 0, ENVSYS_SAMPS } /* None */
79 1.1 groo };
80 1.1 groo
81 1.1 groo
82 1.1 groo u_int8_t lm_readreg __P((struct lm_softc *, int));
83 1.1 groo void lm_writereg __P((struct lm_softc *, int, int));
84 1.1 groo void lm_refresh_sensor_data __P((struct lm_softc *));
85 1.4 thorpej int lm_gtredata __P((struct sysmon_envsys *, struct envsys_tre_data *));
86 1.4 thorpej int lm_streinfo __P((struct sysmon_envsys *, struct envsys_basic_info *));
87 1.1 groo
88 1.1 groo u_int8_t
89 1.1 groo lm_readreg(sc, reg)
90 1.1 groo struct lm_softc *sc;
91 1.1 groo int reg;
92 1.1 groo {
93 1.1 groo bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
94 1.1 groo return (bus_space_read_1(sc->lm_iot, sc->lm_ioh, LMC_DATA));
95 1.1 groo }
96 1.1 groo
97 1.1 groo void
98 1.1 groo lm_writereg(sc, reg, val)
99 1.1 groo struct lm_softc *sc;
100 1.1 groo int reg;
101 1.1 groo int val;
102 1.1 groo {
103 1.1 groo bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
104 1.1 groo bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_DATA, val);
105 1.1 groo }
106 1.1 groo
107 1.1 groo
108 1.1 groo /*
109 1.2 groo * bus independent probe
110 1.2 groo */
111 1.2 groo int
112 1.2 groo lm_probe(iot, ioh)
113 1.2 groo bus_space_tag_t iot;
114 1.2 groo bus_space_handle_t ioh;
115 1.2 groo {
116 1.2 groo u_int8_t cr;
117 1.2 groo int rv;
118 1.2 groo
119 1.2 groo /* Check for some power-on defaults */
120 1.2 groo bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
121 1.2 groo
122 1.2 groo /* Perform LM78 reset */
123 1.2 groo bus_space_write_1(iot, ioh, LMC_DATA, 0x80);
124 1.2 groo
125 1.2 groo /* XXX - Why do I have to reselect the register? */
126 1.2 groo bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
127 1.2 groo cr = bus_space_read_1(iot, ioh, LMC_DATA);
128 1.2 groo
129 1.2 groo /* XXX - spec says *only* 0x08! */
130 1.2 groo if ((cr == 0x08) || (cr == 0x01))
131 1.2 groo rv = 1;
132 1.2 groo else
133 1.2 groo rv = 0;
134 1.2 groo
135 1.2 groo DPRINTF(("lm: rv = %d, cr = %x\n", rv, cr));
136 1.2 groo
137 1.2 groo return (rv);
138 1.2 groo }
139 1.2 groo
140 1.2 groo
141 1.2 groo /*
142 1.1 groo * pre: lmsc contains valid busspace tag and handle
143 1.1 groo */
144 1.1 groo void
145 1.1 groo lm_attach(lmsc)
146 1.1 groo struct lm_softc *lmsc;
147 1.1 groo {
148 1.1 groo int i;
149 1.1 groo
150 1.1 groo /* See if we have an LM78 or LM79 */
151 1.1 groo i = lm_readreg(lmsc, LMD_CHIPID) & LM_ID_MASK;
152 1.1 groo printf(": LM7");
153 1.1 groo if (i == LM_ID_LM78)
154 1.1 groo printf("8\n");
155 1.1 groo else if (i == LM_ID_LM78J)
156 1.1 groo printf("8J\n");
157 1.1 groo else if (i == LM_ID_LM79)
158 1.1 groo printf("9\n");
159 1.1 groo else
160 1.1 groo printf("? - Unknown chip ID (%x)\n", i);
161 1.1 groo
162 1.1 groo /* Start the monitoring loop */
163 1.1 groo lm_writereg(lmsc, LMD_CONFIG, 0x01);
164 1.1 groo
165 1.1 groo /* Indicate we have never read the registers */
166 1.1 groo timerclear(&lmsc->lastread);
167 1.1 groo
168 1.1 groo /* Initialize sensors */
169 1.1 groo for (i = 0; i < LM_NUM_SENSORS; ++i) {
170 1.1 groo lmsc->sensors[i].sensor = lmsc->info[i].sensor = i;
171 1.1 groo lmsc->sensors[i].validflags = (ENVSYS_FVALID|ENVSYS_FCURVALID);
172 1.1 groo lmsc->info[i].validflags = ENVSYS_FVALID;
173 1.1 groo lmsc->sensors[i].warnflags = ENVSYS_WARN_OK;
174 1.1 groo }
175 1.1 groo
176 1.1 groo for (i = 0; i < 7; ++i) {
177 1.1 groo lmsc->sensors[i].units = lmsc->info[i].units =
178 1.1 groo ENVSYS_SVOLTS_DC;
179 1.1 groo
180 1.1 groo lmsc->info[i].desc[0] = 'I';
181 1.1 groo lmsc->info[i].desc[1] = 'N';
182 1.1 groo lmsc->info[i].desc[2] = i + '0';
183 1.1 groo lmsc->info[i].desc[3] = 0;
184 1.1 groo }
185 1.1 groo
186 1.3 groo /* default correction factors for resistors on higher voltage inputs */
187 1.3 groo lmsc->info[0].rfact = lmsc->info[1].rfact =
188 1.3 groo lmsc->info[2].rfact = 10000;
189 1.3 groo lmsc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
190 1.3 groo lmsc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
191 1.3 groo lmsc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
192 1.3 groo lmsc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
193 1.3 groo
194 1.1 groo lmsc->sensors[7].units = ENVSYS_STEMP;
195 1.2 groo strcpy(lmsc->info[7].desc, "Temp");
196 1.1 groo
197 1.1 groo for (i = 8; i < 11; ++i) {
198 1.1 groo lmsc->sensors[i].units = lmsc->info[i].units = ENVSYS_SFANRPM;
199 1.1 groo
200 1.1 groo lmsc->info[i].desc[0] = 'F';
201 1.1 groo lmsc->info[i].desc[1] = 'a';
202 1.1 groo lmsc->info[i].desc[2] = 'n';
203 1.1 groo lmsc->info[i].desc[3] = ' ';
204 1.1 groo lmsc->info[i].desc[4] = i - 7 + '0';
205 1.1 groo lmsc->info[i].desc[5] = 0;
206 1.1 groo }
207 1.1 groo
208 1.4 thorpej /*
209 1.4 thorpej * Hook into the System Monitor.
210 1.4 thorpej */
211 1.4 thorpej lmsc->sc_sysmon.sme_ranges = lm_ranges;
212 1.4 thorpej lmsc->sc_sysmon.sme_sensor_info = lmsc->info;
213 1.4 thorpej lmsc->sc_sysmon.sme_sensor_data = lmsc->sensors;
214 1.4 thorpej lmsc->sc_sysmon.sme_cookie = lmsc;
215 1.4 thorpej
216 1.4 thorpej lmsc->sc_sysmon.sme_gtredata = lm_gtredata;
217 1.4 thorpej lmsc->sc_sysmon.sme_streinfo = lm_streinfo;
218 1.4 thorpej
219 1.4 thorpej lmsc->sc_sysmon.sme_nsensors = LM_NUM_SENSORS;
220 1.4 thorpej lmsc->sc_sysmon.sme_envsys_version = 1000;
221 1.4 thorpej
222 1.4 thorpej if (sysmon_envsys_register(&lmsc->sc_sysmon))
223 1.4 thorpej printf("%s: unable to register with sysmon\n",
224 1.4 thorpej lmsc->sc_dev.dv_xname);
225 1.1 groo }
226 1.1 groo
227 1.1 groo
228 1.1 groo int
229 1.4 thorpej lm_gtredata(sme, tred)
230 1.4 thorpej struct sysmon_envsys *sme;
231 1.4 thorpej struct envsys_tre_data *tred;
232 1.1 groo {
233 1.4 thorpej static const struct timeval onepointfive = { 1, 500000 };
234 1.4 thorpej struct timeval t;
235 1.4 thorpej struct lm_softc *sc = sme->sme_cookie;
236 1.4 thorpej int i, s;
237 1.1 groo
238 1.4 thorpej /* read new values at most once every 1.5 seconds */
239 1.4 thorpej timeradd(&sc->lastread, &onepointfive, &t);
240 1.4 thorpej s = splclock();
241 1.4 thorpej i = timercmp(&mono_time, &t, >);
242 1.4 thorpej if (i) {
243 1.4 thorpej sc->lastread.tv_sec = mono_time.tv_sec;
244 1.4 thorpej sc->lastread.tv_usec = mono_time.tv_usec;
245 1.4 thorpej }
246 1.4 thorpej splx(s);
247 1.4 thorpej
248 1.4 thorpej if (i)
249 1.4 thorpej lm_refresh_sensor_data(sc);
250 1.1 groo
251 1.4 thorpej *tred = sc->sensors[tred->sensor];
252 1.1 groo
253 1.4 thorpej return (0);
254 1.1 groo }
255 1.1 groo
256 1.1 groo
257 1.1 groo int
258 1.4 thorpej lm_streinfo(sme, binfo)
259 1.4 thorpej struct sysmon_envsys *sme;
260 1.4 thorpej struct envsys_basic_info *binfo;
261 1.1 groo {
262 1.4 thorpej struct lm_softc *sc = sme->sme_cookie;
263 1.4 thorpej int divisor;
264 1.1 groo u_int8_t sdata;
265 1.1 groo
266 1.4 thorpej if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
267 1.4 thorpej sc->info[binfo->sensor].rfact = binfo->rfact;
268 1.4 thorpej else {
269 1.4 thorpej /* FAN1 and FAN2 can have divisors set, but not FAN3 */
270 1.4 thorpej if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
271 1.4 thorpej && (binfo->sensor != 10)) {
272 1.4 thorpej
273 1.4 thorpej if (binfo->rpms == 0) {
274 1.4 thorpej binfo->validflags = 0;
275 1.4 thorpej return (0);
276 1.1 groo }
277 1.1 groo
278 1.4 thorpej /* 153 is the nominal FAN speed value */
279 1.4 thorpej divisor = 1350000 / (binfo->rpms * 153);
280 1.1 groo
281 1.4 thorpej /* ...but we need lg(divisor) */
282 1.4 thorpej if (divisor <= 1)
283 1.4 thorpej divisor = 0;
284 1.4 thorpej else if (divisor <= 2)
285 1.4 thorpej divisor = 1;
286 1.4 thorpej else if (divisor <= 4)
287 1.4 thorpej divisor = 2;
288 1.4 thorpej else
289 1.4 thorpej divisor = 3;
290 1.4 thorpej
291 1.4 thorpej /*
292 1.4 thorpej * FAN1 div is in bits <5:4>, FAN2 div is
293 1.4 thorpej * in <7:6>
294 1.4 thorpej */
295 1.4 thorpej sdata = lm_readreg(sc, LMD_VIDFAN);
296 1.4 thorpej if ( binfo->sensor == 8 ) { /* FAN1 */
297 1.4 thorpej divisor <<= 4;
298 1.4 thorpej sdata = (sdata & 0xCF) | divisor;
299 1.4 thorpej } else { /* FAN2 */
300 1.4 thorpej divisor <<= 6;
301 1.4 thorpej sdata = (sdata & 0x3F) | divisor;
302 1.1 groo }
303 1.1 groo
304 1.4 thorpej lm_writereg(sc, LMD_VIDFAN, sdata);
305 1.1 groo }
306 1.1 groo
307 1.4 thorpej memcpy(sc->info[binfo->sensor].desc, binfo->desc,
308 1.4 thorpej sizeof(sc->info[binfo->sensor].desc));
309 1.4 thorpej sc->info[binfo->sensor].desc[
310 1.4 thorpej sizeof(sc->info[binfo->sensor].desc) - 1] = '\0';
311 1.1 groo
312 1.4 thorpej binfo->validflags = ENVSYS_FVALID;
313 1.1 groo }
314 1.4 thorpej return (0);
315 1.1 groo }
316 1.1 groo
317 1.1 groo
318 1.1 groo /*
319 1.1 groo * pre: last read occured >= 1.5 seconds ago
320 1.1 groo * post: sensors[] current data are the latest from the chip
321 1.1 groo */
322 1.1 groo void
323 1.1 groo lm_refresh_sensor_data(sc)
324 1.1 groo struct lm_softc *sc;
325 1.1 groo {
326 1.1 groo u_int8_t sdata;
327 1.1 groo int i, divisor;
328 1.1 groo
329 1.1 groo /* Refresh our stored data for every sensor */
330 1.1 groo for (i = 0; i < LM_NUM_SENSORS; ++i) {
331 1.1 groo sdata = lm_readreg(sc, LMD_SENSORBASE + i);
332 1.1 groo
333 1.1 groo switch (sc->sensors[i].units) {
334 1.1 groo case ENVSYS_STEMP:
335 1.1 groo /* temp is given in deg. C, we convert to uK */
336 1.1 groo sc->sensors[i].cur.data_us = sdata * 1000000 +
337 1.1 groo 273150000;
338 1.1 groo break;
339 1.1 groo
340 1.1 groo case ENVSYS_SVOLTS_DC:
341 1.1 groo /* voltage returned as (mV >> 4), we convert to uVDC */
342 1.3 groo sc->sensors[i].cur.data_s = (sdata << 4);
343 1.3 groo /* rfact is (factor * 10^4) */
344 1.3 groo sc->sensors[i].cur.data_s *= sc->info[i].rfact;
345 1.3 groo /* division by 10 gets us back to uVDC */
346 1.3 groo sc->sensors[i].cur.data_s /= 10;
347 1.1 groo
348 1.1 groo /* these two are negative voltages */
349 1.1 groo if ( (i == 5) || (i == 6) )
350 1.1 groo sc->sensors[i].cur.data_s *= -1;
351 1.3 groo
352 1.1 groo break;
353 1.1 groo
354 1.1 groo case ENVSYS_SFANRPM:
355 1.1 groo if (i == 10)
356 1.1 groo divisor = 2; /* Fixed divisor for FAN3 */
357 1.1 groo else if (i == 9) /* Bits 7 & 6 of VID/FAN */
358 1.1 groo divisor = (lm_readreg(sc, LMD_VIDFAN) >> 6) &
359 1.1 groo 0x3;
360 1.1 groo else
361 1.1 groo divisor = (lm_readreg(sc, LMD_VIDFAN) >> 4) &
362 1.1 groo 0x3;
363 1.1 groo
364 1.1 groo sc->sensors[i].cur.data_us = 1350000 /
365 1.1 groo (sdata << divisor);
366 1.1 groo
367 1.1 groo break;
368 1.1 groo
369 1.1 groo default:
370 1.1 groo /* XXX - debug log something? */
371 1.1 groo sc->sensors[i].validflags = 0;
372 1.1 groo
373 1.1 groo break;
374 1.1 groo }
375 1.1 groo }
376 1.1 groo }
377