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