nslm7x.c revision 1.3 1 1.3 groo /* $NetBSD: nslm7x.c,v 1.3 2000/03/09 04:20:58 groo 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 <sys/envsys.h>
53 1.1 groo
54 1.1 groo #include <machine/bus.h>
55 1.1 groo
56 1.1 groo #include <dev/isa/isareg.h>
57 1.1 groo #include <dev/isa/isavar.h>
58 1.1 groo
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.1 groo struct envsys_range ranges[] = { /* sc->sensors sub-intervals */
71 1.1 groo /* 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 #define SCFLAG_OREAD 0x00000001
83 1.1 groo #define SCFLAG_OWRITE 0x00000002
84 1.1 groo #define SCFLAG_OPEN (SCFLAG_OREAD|SCFLAG_OWRITE)
85 1.1 groo
86 1.1 groo u_int8_t lm_readreg __P((struct lm_softc *, int));
87 1.1 groo void lm_writereg __P((struct lm_softc *, int, int));
88 1.1 groo void lm_refresh_sensor_data __P((struct lm_softc *));
89 1.1 groo
90 1.1 groo cdev_decl(lm);
91 1.1 groo
92 1.1 groo extern struct cfdriver lm_cd;
93 1.1 groo
94 1.1 groo #define LMUNIT(x) (minor(x))
95 1.1 groo
96 1.1 groo u_int8_t
97 1.1 groo lm_readreg(sc, reg)
98 1.1 groo struct lm_softc *sc;
99 1.1 groo int reg;
100 1.1 groo {
101 1.1 groo bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
102 1.1 groo return (bus_space_read_1(sc->lm_iot, sc->lm_ioh, LMC_DATA));
103 1.1 groo }
104 1.1 groo
105 1.1 groo void
106 1.1 groo lm_writereg(sc, reg, val)
107 1.1 groo struct lm_softc *sc;
108 1.1 groo int reg;
109 1.1 groo int val;
110 1.1 groo {
111 1.1 groo bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
112 1.1 groo bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_DATA, val);
113 1.1 groo }
114 1.1 groo
115 1.1 groo
116 1.1 groo /*
117 1.2 groo * bus independent probe
118 1.2 groo */
119 1.2 groo int
120 1.2 groo lm_probe(iot, ioh)
121 1.2 groo bus_space_tag_t iot;
122 1.2 groo bus_space_handle_t ioh;
123 1.2 groo {
124 1.2 groo u_int8_t cr;
125 1.2 groo int rv;
126 1.2 groo
127 1.2 groo /* Check for some power-on defaults */
128 1.2 groo bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
129 1.2 groo
130 1.2 groo /* Perform LM78 reset */
131 1.2 groo bus_space_write_1(iot, ioh, LMC_DATA, 0x80);
132 1.2 groo
133 1.2 groo /* XXX - Why do I have to reselect the register? */
134 1.2 groo bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
135 1.2 groo cr = bus_space_read_1(iot, ioh, LMC_DATA);
136 1.2 groo
137 1.2 groo /* XXX - spec says *only* 0x08! */
138 1.2 groo if ((cr == 0x08) || (cr == 0x01))
139 1.2 groo rv = 1;
140 1.2 groo else
141 1.2 groo rv = 0;
142 1.2 groo
143 1.2 groo DPRINTF(("lm: rv = %d, cr = %x\n", rv, cr));
144 1.2 groo
145 1.2 groo return (rv);
146 1.2 groo }
147 1.2 groo
148 1.2 groo
149 1.2 groo /*
150 1.1 groo * pre: lmsc contains valid busspace tag and handle
151 1.1 groo */
152 1.1 groo void
153 1.1 groo lm_attach(lmsc)
154 1.1 groo struct lm_softc *lmsc;
155 1.1 groo {
156 1.1 groo int i;
157 1.1 groo
158 1.1 groo /* See if we have an LM78 or LM79 */
159 1.1 groo i = lm_readreg(lmsc, LMD_CHIPID) & LM_ID_MASK;
160 1.1 groo printf(": LM7");
161 1.1 groo if (i == LM_ID_LM78)
162 1.1 groo printf("8\n");
163 1.1 groo else if (i == LM_ID_LM78J)
164 1.1 groo printf("8J\n");
165 1.1 groo else if (i == LM_ID_LM79)
166 1.1 groo printf("9\n");
167 1.1 groo else
168 1.1 groo printf("? - Unknown chip ID (%x)\n", i);
169 1.1 groo
170 1.1 groo /* Start the monitoring loop */
171 1.1 groo lm_writereg(lmsc, LMD_CONFIG, 0x01);
172 1.1 groo
173 1.1 groo /* Indicate we have never read the registers */
174 1.1 groo timerclear(&lmsc->lastread);
175 1.1 groo
176 1.1 groo /* Initialize sensors */
177 1.1 groo for (i = 0; i < LM_NUM_SENSORS; ++i) {
178 1.1 groo lmsc->sensors[i].sensor = lmsc->info[i].sensor = i;
179 1.1 groo lmsc->sensors[i].validflags = (ENVSYS_FVALID|ENVSYS_FCURVALID);
180 1.1 groo lmsc->info[i].validflags = ENVSYS_FVALID;
181 1.1 groo lmsc->sensors[i].warnflags = ENVSYS_WARN_OK;
182 1.1 groo }
183 1.1 groo
184 1.1 groo for (i = 0; i < 7; ++i) {
185 1.1 groo lmsc->sensors[i].units = lmsc->info[i].units =
186 1.1 groo ENVSYS_SVOLTS_DC;
187 1.1 groo
188 1.1 groo lmsc->info[i].desc[0] = 'I';
189 1.1 groo lmsc->info[i].desc[1] = 'N';
190 1.1 groo lmsc->info[i].desc[2] = i + '0';
191 1.1 groo lmsc->info[i].desc[3] = 0;
192 1.1 groo }
193 1.1 groo
194 1.3 groo /* default correction factors for resistors on higher voltage inputs */
195 1.3 groo lmsc->info[0].rfact = lmsc->info[1].rfact =
196 1.3 groo lmsc->info[2].rfact = 10000;
197 1.3 groo lmsc->info[3].rfact = (int)(( 90.9 / 60.4) * 10000);
198 1.3 groo lmsc->info[4].rfact = (int)(( 38.0 / 10.0) * 10000);
199 1.3 groo lmsc->info[5].rfact = (int)((210.0 / 60.4) * 10000);
200 1.3 groo lmsc->info[6].rfact = (int)(( 90.9 / 60.4) * 10000);
201 1.3 groo
202 1.1 groo lmsc->sensors[7].units = ENVSYS_STEMP;
203 1.2 groo strcpy(lmsc->info[7].desc, "Temp");
204 1.1 groo
205 1.1 groo for (i = 8; i < 11; ++i) {
206 1.1 groo lmsc->sensors[i].units = lmsc->info[i].units = ENVSYS_SFANRPM;
207 1.1 groo
208 1.1 groo lmsc->info[i].desc[0] = 'F';
209 1.1 groo lmsc->info[i].desc[1] = 'a';
210 1.1 groo lmsc->info[i].desc[2] = 'n';
211 1.1 groo lmsc->info[i].desc[3] = ' ';
212 1.1 groo lmsc->info[i].desc[4] = i - 7 + '0';
213 1.1 groo lmsc->info[i].desc[5] = 0;
214 1.1 groo }
215 1.1 groo }
216 1.1 groo
217 1.1 groo
218 1.1 groo int
219 1.1 groo lmopen(dev, flag, mode, p)
220 1.1 groo dev_t dev;
221 1.1 groo int flag, mode;
222 1.1 groo struct proc *p;
223 1.1 groo {
224 1.1 groo int unit = LMUNIT(dev);
225 1.1 groo struct lm_softc *sc;
226 1.1 groo
227 1.1 groo if (unit >= lm_cd.cd_ndevs)
228 1.1 groo return (ENXIO);
229 1.1 groo sc = lm_cd.cd_devs[unit];
230 1.1 groo if (sc == 0)
231 1.1 groo return (ENXIO);
232 1.1 groo
233 1.1 groo /* XXX - add spinlocks instead! */
234 1.1 groo if (sc->sc_flags & SCFLAG_OPEN)
235 1.1 groo return (EBUSY);
236 1.1 groo
237 1.1 groo sc->sc_flags |= SCFLAG_OPEN;
238 1.1 groo
239 1.1 groo return 0;
240 1.1 groo }
241 1.1 groo
242 1.1 groo
243 1.1 groo int
244 1.1 groo lmclose(dev, flag, mode, p)
245 1.1 groo dev_t dev;
246 1.1 groo int flag, mode;
247 1.1 groo struct proc *p;
248 1.1 groo {
249 1.1 groo struct lm_softc *sc = lm_cd.cd_devs[LMUNIT(dev)];
250 1.1 groo
251 1.1 groo DPRINTF(("lmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
252 1.1 groo
253 1.1 groo sc->sc_flags &= ~SCFLAG_OPEN;
254 1.1 groo
255 1.1 groo return 0;
256 1.1 groo }
257 1.1 groo
258 1.1 groo
259 1.1 groo int
260 1.1 groo lmioctl(dev, cmd, data, flag, p)
261 1.1 groo dev_t dev;
262 1.1 groo u_long cmd;
263 1.1 groo caddr_t data;
264 1.1 groo int flag;
265 1.1 groo struct proc *p;
266 1.1 groo {
267 1.1 groo struct lm_softc *sc = lm_cd.cd_devs[LMUNIT(dev)];
268 1.1 groo struct envsys_range *rng;
269 1.1 groo struct envsys_tre_data *tred;
270 1.1 groo struct envsys_basic_info *binfo;
271 1.1 groo struct timeval t, onepointfive = { 1, 500000 };
272 1.1 groo u_int8_t sdata;
273 1.1 groo int32_t *vers;
274 1.1 groo int i, s;
275 1.1 groo int divisor;
276 1.1 groo
277 1.1 groo switch (cmd) {
278 1.1 groo case ENVSYS_VERSION:
279 1.1 groo vers = (int32_t *)data;
280 1.1 groo *vers = 1000;
281 1.1 groo
282 1.1 groo return (0);
283 1.1 groo
284 1.1 groo case ENVSYS_GRANGE:
285 1.1 groo rng = (struct envsys_range *)data;
286 1.1 groo if ((rng->units < ENVSYS_STEMP) ||
287 1.1 groo (rng->units > ENVSYS_SAMPS) ) {
288 1.1 groo /* Return empty range for unsupp sensor types */
289 1.1 groo rng->low = 1;
290 1.1 groo rng->high = 0;
291 1.1 groo } else {
292 1.1 groo rng->low = ranges[rng->units].low;
293 1.1 groo rng->high = ranges[rng->units].high;
294 1.1 groo }
295 1.1 groo
296 1.1 groo return (0);
297 1.1 groo
298 1.1 groo case ENVSYS_GTREDATA:
299 1.1 groo tred = (struct envsys_tre_data *)data;
300 1.1 groo tred->validflags = 0;
301 1.1 groo
302 1.1 groo if (tred->sensor < LM_NUM_SENSORS) {
303 1.1 groo /* read new values at most once every 1.5 seconds */
304 1.1 groo s = splclock();
305 1.1 groo
306 1.1 groo timeradd(&sc->lastread, &onepointfive, &t);
307 1.1 groo
308 1.1 groo i = timercmp(&mono_time, &t, >);
309 1.1 groo if (i) {
310 1.1 groo sc->lastread.tv_sec = mono_time.tv_sec;
311 1.1 groo sc->lastread.tv_usec = mono_time.tv_usec;
312 1.1 groo }
313 1.1 groo splx(s);
314 1.1 groo
315 1.1 groo if (i) {
316 1.1 groo lm_refresh_sensor_data(sc);
317 1.1 groo }
318 1.1 groo
319 1.1 groo bcopy(&sc->sensors[tred->sensor], tred,
320 1.1 groo sizeof(struct envsys_tre_data));
321 1.1 groo }
322 1.1 groo
323 1.1 groo return (0);
324 1.1 groo
325 1.1 groo case ENVSYS_GTREINFO:
326 1.1 groo binfo = (struct envsys_basic_info *)data;
327 1.1 groo
328 1.1 groo if (binfo->sensor >= LM_NUM_SENSORS)
329 1.1 groo binfo->validflags = 0;
330 1.1 groo else
331 1.1 groo bcopy(&sc->info[binfo->sensor], binfo,
332 1.1 groo sizeof(struct envsys_basic_info));
333 1.1 groo
334 1.1 groo return (0);
335 1.1 groo
336 1.1 groo case ENVSYS_STREINFO:
337 1.1 groo binfo = (struct envsys_basic_info *)data;
338 1.1 groo
339 1.1 groo if (binfo->sensor >= LM_NUM_SENSORS)
340 1.1 groo binfo->validflags = 0;
341 1.3 groo else if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
342 1.3 groo sc->info[binfo->sensor].rfact = binfo->rfact;
343 1.1 groo else {
344 1.1 groo /* FAN1 and FAN2 can have divisors set, but not FAN3 */
345 1.1 groo if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
346 1.1 groo && (binfo->sensor != 10)) {
347 1.1 groo
348 1.1 groo if (binfo->rpms == 0) {
349 1.1 groo binfo->validflags = 0;
350 1.1 groo return (0);
351 1.1 groo }
352 1.1 groo
353 1.1 groo /* 153 is the nominal FAN speed value */
354 1.1 groo divisor = 1350000 / (binfo->rpms * 153);
355 1.1 groo
356 1.1 groo /* ...but we need lg(divisor) */
357 1.1 groo if (divisor <= 1)
358 1.1 groo divisor = 0;
359 1.1 groo else if (divisor <= 2)
360 1.1 groo divisor = 1;
361 1.1 groo else if (divisor <= 4)
362 1.1 groo divisor = 2;
363 1.1 groo else
364 1.1 groo divisor = 3;
365 1.1 groo
366 1.1 groo /*
367 1.1 groo * FAN1 div is in bits <5:4>, FAN2 div is
368 1.1 groo * in <7:6>
369 1.1 groo */
370 1.1 groo sdata = lm_readreg(sc, LMD_VIDFAN);
371 1.1 groo if ( binfo->sensor == 8 ) { /* FAN1 */
372 1.1 groo divisor <<= 4;
373 1.1 groo sdata = (sdata & 0xCF) | divisor;
374 1.1 groo } else { /* FAN2 */
375 1.1 groo divisor <<= 6;
376 1.1 groo sdata = (sdata & 0x3F) | divisor;
377 1.1 groo }
378 1.1 groo
379 1.1 groo lm_writereg(sc, LMD_VIDFAN, sdata);
380 1.1 groo }
381 1.1 groo
382 1.1 groo bcopy(binfo->desc, sc->info[binfo->sensor].desc, 33);
383 1.1 groo sc->info[binfo->sensor].desc[32] = 0;
384 1.1 groo
385 1.1 groo binfo->validflags = ENVSYS_FVALID;
386 1.1 groo }
387 1.1 groo
388 1.1 groo return (0);
389 1.1 groo
390 1.1 groo default:
391 1.1 groo return (ENOTTY);
392 1.1 groo }
393 1.1 groo }
394 1.1 groo
395 1.1 groo
396 1.1 groo /*
397 1.1 groo * pre: last read occured >= 1.5 seconds ago
398 1.1 groo * post: sensors[] current data are the latest from the chip
399 1.1 groo */
400 1.1 groo void
401 1.1 groo lm_refresh_sensor_data(sc)
402 1.1 groo struct lm_softc *sc;
403 1.1 groo {
404 1.1 groo u_int8_t sdata;
405 1.1 groo int i, divisor;
406 1.1 groo
407 1.1 groo /* Refresh our stored data for every sensor */
408 1.1 groo for (i = 0; i < LM_NUM_SENSORS; ++i) {
409 1.1 groo sdata = lm_readreg(sc, LMD_SENSORBASE + i);
410 1.1 groo
411 1.1 groo switch (sc->sensors[i].units) {
412 1.1 groo case ENVSYS_STEMP:
413 1.1 groo /* temp is given in deg. C, we convert to uK */
414 1.1 groo sc->sensors[i].cur.data_us = sdata * 1000000 +
415 1.1 groo 273150000;
416 1.1 groo break;
417 1.1 groo
418 1.1 groo case ENVSYS_SVOLTS_DC:
419 1.1 groo /* voltage returned as (mV >> 4), we convert to uVDC */
420 1.3 groo sc->sensors[i].cur.data_s = (sdata << 4);
421 1.3 groo /* rfact is (factor * 10^4) */
422 1.3 groo sc->sensors[i].cur.data_s *= sc->info[i].rfact;
423 1.3 groo /* division by 10 gets us back to uVDC */
424 1.3 groo sc->sensors[i].cur.data_s /= 10;
425 1.1 groo
426 1.1 groo /* these two are negative voltages */
427 1.1 groo if ( (i == 5) || (i == 6) )
428 1.1 groo sc->sensors[i].cur.data_s *= -1;
429 1.3 groo
430 1.1 groo break;
431 1.1 groo
432 1.1 groo case ENVSYS_SFANRPM:
433 1.1 groo if (i == 10)
434 1.1 groo divisor = 2; /* Fixed divisor for FAN3 */
435 1.1 groo else if (i == 9) /* Bits 7 & 6 of VID/FAN */
436 1.1 groo divisor = (lm_readreg(sc, LMD_VIDFAN) >> 6) &
437 1.1 groo 0x3;
438 1.1 groo else
439 1.1 groo divisor = (lm_readreg(sc, LMD_VIDFAN) >> 4) &
440 1.1 groo 0x3;
441 1.1 groo
442 1.1 groo sc->sensors[i].cur.data_us = 1350000 /
443 1.1 groo (sdata << divisor);
444 1.1 groo
445 1.1 groo break;
446 1.1 groo
447 1.1 groo default:
448 1.1 groo /* XXX - debug log something? */
449 1.1 groo sc->sensors[i].validflags = 0;
450 1.1 groo
451 1.1 groo break;
452 1.1 groo }
453 1.1 groo }
454 1.1 groo }
455