1 /* $Id: bbc_envctrl.c,v 1.4 2001/04/06 16:48:08 davem Exp $
2 * bbc_envctrl.c: UltraSPARC-III environment control driver.
4 * Copyright (C) 2001 David S. Miller (davem@redhat.com)
7 #define __KERNEL_SYSCALLS__
9 #include <linux/kernel.h>
10 #include <linux/kthread.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <asm/oplib.h>
17 #include <asm/unistd.h>
24 /* WARNING: Making changes to this driver is very dangerous.
25 * If you misprogram the sensor chips they can
26 * cut the power on you instantly.
29 /* Two temperature sensors exist in the SunBLADE-1000 enclosure.
30 * Both are implemented using max1617 i2c devices. Each max1617
31 * monitors 2 temperatures, one for one of the cpu dies and the other
32 * for the ambient temperature.
34 * The max1617 is capable of being programmed with power-off
35 * temperature values, one low limit and one high limit. These
36 * can be controlled independently for the cpu or ambient temperature.
37 * If a limit is violated, the power is simply shut off. The frequency
38 * with which the max1617 does temperature sampling can be controlled
41 * Three fans exist inside the machine, all three are controlled with
42 * an i2c digital to analog converter. There is a fan directed at the
43 * two processor slots, another for the rest of the enclosure, and the
44 * third is for the power supply. The first two fans may be speed
45 * controlled by changing the voltage fed to them. The third fan may
46 * only be completely off or on. The third fan is meant to only be
47 * disabled/enabled when entering/exiting the lowest power-saving
48 * mode of the machine.
50 * An environmental control kernel thread periodically monitors all
51 * temperature sensors. Based upon the samples it will adjust the
52 * fan speeds to try and keep the system within a certain temperature
53 * range (the goal being to make the fans as quiet as possible without
54 * allowing the system to get too hot).
56 * If the temperature begins to rise/fall outside of the acceptable
57 * operating range, a periodic warning will be sent to the kernel log.
58 * The fans will be put on full blast to attempt to deal with this
59 * situation. After exceeding the acceptable operating range by a
60 * certain threshold, the kernel thread will shut down the system.
61 * Here, the thread is attempting to shut the machine down cleanly
62 * before the hardware based power-off event is triggered.
65 /* These settings are in Celsius. We use these defaults only
66 * if we cannot interrogate the cpu-fru SEEPROM.
69 s8 high_pwroff
, high_shutdown
, high_warn
;
70 s8 low_warn
, low_shutdown
, low_pwroff
;
73 static struct temp_limits cpu_temp_limits
[2] = {
74 { 100, 85, 80, 5, -5, -10 },
75 { 100, 85, 80, 5, -5, -10 },
78 static struct temp_limits amb_temp_limits
[2] = {
79 { 65, 55, 40, 5, -5, -10 },
80 { 65, 55, 40, 5, -5, -10 },
83 enum fan_action
{ FAN_SLOWER
, FAN_SAME
, FAN_FASTER
, FAN_FULLBLAST
, FAN_STATE_MAX
};
85 struct bbc_cpu_temperature
{
86 struct bbc_cpu_temperature
*next
;
88 struct bbc_i2c_client
*client
;
91 /* Current readings, and history. */
101 enum fan_action fan_todo
[2];
102 #define FAN_AMBIENT 0
106 struct bbc_cpu_temperature
*all_bbc_temps
;
108 struct bbc_fan_control
{
109 struct bbc_fan_control
*next
;
111 struct bbc_i2c_client
*client
;
116 int system_fan_speed
;
119 struct bbc_fan_control
*all_bbc_fans
;
121 #define CPU_FAN_REG 0xf0
122 #define SYS_FAN_REG 0xf2
123 #define PSUPPLY_FAN_REG 0xf4
125 #define FAN_SPEED_MIN 0x0c
126 #define FAN_SPEED_MAX 0x3f
128 #define PSUPPLY_FAN_ON 0x1f
129 #define PSUPPLY_FAN_OFF 0x00
131 static void set_fan_speeds(struct bbc_fan_control
*fp
)
133 /* Put temperatures into range so we don't mis-program
136 if (fp
->cpu_fan_speed
< FAN_SPEED_MIN
)
137 fp
->cpu_fan_speed
= FAN_SPEED_MIN
;
138 if (fp
->cpu_fan_speed
> FAN_SPEED_MAX
)
139 fp
->cpu_fan_speed
= FAN_SPEED_MAX
;
140 if (fp
->system_fan_speed
< FAN_SPEED_MIN
)
141 fp
->system_fan_speed
= FAN_SPEED_MIN
;
142 if (fp
->system_fan_speed
> FAN_SPEED_MAX
)
143 fp
->system_fan_speed
= FAN_SPEED_MAX
;
145 printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
147 fp
->cpu_fan_speed
, fp
->system_fan_speed
);
150 bbc_i2c_writeb(fp
->client
, fp
->cpu_fan_speed
, CPU_FAN_REG
);
151 bbc_i2c_writeb(fp
->client
, fp
->system_fan_speed
, SYS_FAN_REG
);
152 bbc_i2c_writeb(fp
->client
,
153 (fp
->psupply_fan_on
?
154 PSUPPLY_FAN_ON
: PSUPPLY_FAN_OFF
),
158 static void get_current_temps(struct bbc_cpu_temperature
*tp
)
160 tp
->prev_amb_temp
= tp
->curr_amb_temp
;
161 bbc_i2c_readb(tp
->client
,
162 (unsigned char *) &tp
->curr_amb_temp
,
164 tp
->prev_cpu_temp
= tp
->curr_cpu_temp
;
165 bbc_i2c_readb(tp
->client
,
166 (unsigned char *) &tp
->curr_cpu_temp
,
169 printk("temp%d: cpu(%d C) amb(%d C)\n",
171 (int) tp
->curr_cpu_temp
, (int) tp
->curr_amb_temp
);
176 static void do_envctrl_shutdown(struct bbc_cpu_temperature
*tp
)
178 static int shutting_down
= 0;
179 static char *envp
[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL
};
180 char *argv
[] = { "/sbin/shutdown", "-h", "now", NULL
};
184 if (shutting_down
!= 0)
187 if (tp
->curr_amb_temp
>= amb_temp_limits
[tp
->index
].high_shutdown
||
188 tp
->curr_amb_temp
< amb_temp_limits
[tp
->index
].low_shutdown
) {
190 val
= tp
->curr_amb_temp
;
191 } else if (tp
->curr_cpu_temp
>= cpu_temp_limits
[tp
->index
].high_shutdown
||
192 tp
->curr_cpu_temp
< cpu_temp_limits
[tp
->index
].low_shutdown
) {
194 val
= tp
->curr_cpu_temp
;
197 printk(KERN_CRIT
"temp%d: Outside of safe %s "
198 "operating temperature, %d C.\n",
199 tp
->index
, type
, val
);
201 printk(KERN_CRIT
"kenvctrld: Shutting down the system now.\n");
204 if (execve("/sbin/shutdown", argv
, envp
) < 0)
205 printk(KERN_CRIT
"envctrl: shutdown execution failed\n");
208 #define WARN_INTERVAL (30 * HZ)
210 static void analyze_ambient_temp(struct bbc_cpu_temperature
*tp
, unsigned long *last_warn
, int tick
)
214 if (time_after(jiffies
, (*last_warn
+ WARN_INTERVAL
))) {
215 if (tp
->curr_amb_temp
>=
216 amb_temp_limits
[tp
->index
].high_warn
) {
217 printk(KERN_WARNING
"temp%d: "
218 "Above safe ambient operating temperature, %d C.\n",
219 tp
->index
, (int) tp
->curr_amb_temp
);
221 } else if (tp
->curr_amb_temp
<
222 amb_temp_limits
[tp
->index
].low_warn
) {
223 printk(KERN_WARNING
"temp%d: "
224 "Below safe ambient operating temperature, %d C.\n",
225 tp
->index
, (int) tp
->curr_amb_temp
);
229 *last_warn
= jiffies
;
230 } else if (tp
->curr_amb_temp
>= amb_temp_limits
[tp
->index
].high_warn
||
231 tp
->curr_amb_temp
< amb_temp_limits
[tp
->index
].low_warn
)
234 /* Now check the shutdown limits. */
235 if (tp
->curr_amb_temp
>= amb_temp_limits
[tp
->index
].high_shutdown
||
236 tp
->curr_amb_temp
< amb_temp_limits
[tp
->index
].low_shutdown
) {
237 do_envctrl_shutdown(tp
);
242 tp
->fan_todo
[FAN_AMBIENT
] = FAN_FULLBLAST
;
243 } else if ((tick
& (8 - 1)) == 0) {
244 s8 amb_goal_hi
= amb_temp_limits
[tp
->index
].high_warn
- 10;
247 amb_goal_lo
= amb_goal_hi
- 3;
249 /* We do not try to avoid 'too cold' events. Basically we
250 * only try to deal with over-heating and fan noise reduction.
252 if (tp
->avg_amb_temp
< amb_goal_hi
) {
253 if (tp
->avg_amb_temp
>= amb_goal_lo
)
254 tp
->fan_todo
[FAN_AMBIENT
] = FAN_SAME
;
256 tp
->fan_todo
[FAN_AMBIENT
] = FAN_SLOWER
;
258 tp
->fan_todo
[FAN_AMBIENT
] = FAN_FASTER
;
261 tp
->fan_todo
[FAN_AMBIENT
] = FAN_SAME
;
265 static void analyze_cpu_temp(struct bbc_cpu_temperature
*tp
, unsigned long *last_warn
, int tick
)
269 if (time_after(jiffies
, (*last_warn
+ WARN_INTERVAL
))) {
270 if (tp
->curr_cpu_temp
>=
271 cpu_temp_limits
[tp
->index
].high_warn
) {
272 printk(KERN_WARNING
"temp%d: "
273 "Above safe CPU operating temperature, %d C.\n",
274 tp
->index
, (int) tp
->curr_cpu_temp
);
276 } else if (tp
->curr_cpu_temp
<
277 cpu_temp_limits
[tp
->index
].low_warn
) {
278 printk(KERN_WARNING
"temp%d: "
279 "Below safe CPU operating temperature, %d C.\n",
280 tp
->index
, (int) tp
->curr_cpu_temp
);
284 *last_warn
= jiffies
;
285 } else if (tp
->curr_cpu_temp
>= cpu_temp_limits
[tp
->index
].high_warn
||
286 tp
->curr_cpu_temp
< cpu_temp_limits
[tp
->index
].low_warn
)
289 /* Now check the shutdown limits. */
290 if (tp
->curr_cpu_temp
>= cpu_temp_limits
[tp
->index
].high_shutdown
||
291 tp
->curr_cpu_temp
< cpu_temp_limits
[tp
->index
].low_shutdown
) {
292 do_envctrl_shutdown(tp
);
297 tp
->fan_todo
[FAN_CPU
] = FAN_FULLBLAST
;
298 } else if ((tick
& (8 - 1)) == 0) {
299 s8 cpu_goal_hi
= cpu_temp_limits
[tp
->index
].high_warn
- 10;
302 cpu_goal_lo
= cpu_goal_hi
- 3;
304 /* We do not try to avoid 'too cold' events. Basically we
305 * only try to deal with over-heating and fan noise reduction.
307 if (tp
->avg_cpu_temp
< cpu_goal_hi
) {
308 if (tp
->avg_cpu_temp
>= cpu_goal_lo
)
309 tp
->fan_todo
[FAN_CPU
] = FAN_SAME
;
311 tp
->fan_todo
[FAN_CPU
] = FAN_SLOWER
;
313 tp
->fan_todo
[FAN_CPU
] = FAN_FASTER
;
316 tp
->fan_todo
[FAN_CPU
] = FAN_SAME
;
320 static void analyze_temps(struct bbc_cpu_temperature
*tp
, unsigned long *last_warn
)
322 tp
->avg_amb_temp
= (s8
)((int)((int)tp
->avg_amb_temp
+ (int)tp
->curr_amb_temp
) / 2);
323 tp
->avg_cpu_temp
= (s8
)((int)((int)tp
->avg_cpu_temp
+ (int)tp
->curr_cpu_temp
) / 2);
325 analyze_ambient_temp(tp
, last_warn
, tp
->sample_tick
);
326 analyze_cpu_temp(tp
, last_warn
, tp
->sample_tick
);
331 static enum fan_action
prioritize_fan_action(int which_fan
)
333 struct bbc_cpu_temperature
*tp
;
334 enum fan_action decision
= FAN_STATE_MAX
;
336 /* Basically, prioritize what the temperature sensors
337 * recommend we do, and perform that action on all the
340 for (tp
= all_bbc_temps
; tp
; tp
= tp
->next
) {
341 if (tp
->fan_todo
[which_fan
] == FAN_FULLBLAST
) {
342 decision
= FAN_FULLBLAST
;
345 if (tp
->fan_todo
[which_fan
] == FAN_SAME
&&
346 decision
!= FAN_FASTER
)
348 else if (tp
->fan_todo
[which_fan
] == FAN_FASTER
)
349 decision
= FAN_FASTER
;
350 else if (decision
!= FAN_FASTER
&&
351 decision
!= FAN_SAME
&&
352 tp
->fan_todo
[which_fan
] == FAN_SLOWER
)
353 decision
= FAN_SLOWER
;
355 if (decision
== FAN_STATE_MAX
)
361 static int maybe_new_ambient_fan_speed(struct bbc_fan_control
*fp
)
363 enum fan_action decision
= prioritize_fan_action(FAN_AMBIENT
);
366 if (decision
== FAN_SAME
)
370 if (decision
== FAN_FULLBLAST
) {
371 if (fp
->system_fan_speed
>= FAN_SPEED_MAX
)
374 fp
->system_fan_speed
= FAN_SPEED_MAX
;
376 if (decision
== FAN_FASTER
) {
377 if (fp
->system_fan_speed
>= FAN_SPEED_MAX
)
380 fp
->system_fan_speed
+= 2;
382 int orig_speed
= fp
->system_fan_speed
;
384 if (orig_speed
<= FAN_SPEED_MIN
||
385 orig_speed
<= (fp
->cpu_fan_speed
- 3))
388 fp
->system_fan_speed
-= 1;
395 static int maybe_new_cpu_fan_speed(struct bbc_fan_control
*fp
)
397 enum fan_action decision
= prioritize_fan_action(FAN_CPU
);
400 if (decision
== FAN_SAME
)
404 if (decision
== FAN_FULLBLAST
) {
405 if (fp
->cpu_fan_speed
>= FAN_SPEED_MAX
)
408 fp
->cpu_fan_speed
= FAN_SPEED_MAX
;
410 if (decision
== FAN_FASTER
) {
411 if (fp
->cpu_fan_speed
>= FAN_SPEED_MAX
)
414 fp
->cpu_fan_speed
+= 2;
415 if (fp
->system_fan_speed
<
416 (fp
->cpu_fan_speed
- 3))
417 fp
->system_fan_speed
=
418 fp
->cpu_fan_speed
- 3;
421 if (fp
->cpu_fan_speed
<= FAN_SPEED_MIN
)
424 fp
->cpu_fan_speed
-= 1;
431 static void maybe_new_fan_speeds(struct bbc_fan_control
*fp
)
435 new = maybe_new_ambient_fan_speed(fp
);
436 new |= maybe_new_cpu_fan_speed(fp
);
442 static void fans_full_blast(void)
444 struct bbc_fan_control
*fp
;
446 /* Since we will not be monitoring things anymore, put
447 * the fans on full blast.
449 for (fp
= all_bbc_fans
; fp
; fp
= fp
->next
) {
450 fp
->cpu_fan_speed
= FAN_SPEED_MAX
;
451 fp
->system_fan_speed
= FAN_SPEED_MAX
;
452 fp
->psupply_fan_on
= 1;
457 #define POLL_INTERVAL (5 * 1000)
458 static unsigned long last_warning_jiffies
;
459 static struct task_struct
*kenvctrld_task
;
461 static int kenvctrld(void *__unused
)
463 printk(KERN_INFO
"bbc_envctrl: kenvctrld starting...\n");
464 last_warning_jiffies
= jiffies
- WARN_INTERVAL
;
466 struct bbc_cpu_temperature
*tp
;
467 struct bbc_fan_control
*fp
;
469 msleep_interruptible(POLL_INTERVAL
);
470 if (kthread_should_stop())
473 for (tp
= all_bbc_temps
; tp
; tp
= tp
->next
) {
474 get_current_temps(tp
);
475 analyze_temps(tp
, &last_warning_jiffies
);
477 for (fp
= all_bbc_fans
; fp
; fp
= fp
->next
)
478 maybe_new_fan_speeds(fp
);
480 printk(KERN_INFO
"bbc_envctrl: kenvctrld exiting...\n");
487 static void attach_one_temp(struct linux_ebus_child
*echild
, int temp_idx
)
489 struct bbc_cpu_temperature
*tp
= kmalloc(sizeof(*tp
), GFP_KERNEL
);
493 memset(tp
, 0, sizeof(*tp
));
494 tp
->client
= bbc_i2c_attach(echild
);
500 tp
->index
= temp_idx
;
502 struct bbc_cpu_temperature
**tpp
= &all_bbc_temps
;
504 tpp
= &((*tpp
)->next
);
509 /* Tell it to convert once every 5 seconds, clear all cfg
512 bbc_i2c_writeb(tp
->client
, 0x00, MAX1617_WR_CFG_BYTE
);
513 bbc_i2c_writeb(tp
->client
, 0x02, MAX1617_WR_CVRATE_BYTE
);
515 /* Program the hard temperature limits into the chip. */
516 bbc_i2c_writeb(tp
->client
, amb_temp_limits
[tp
->index
].high_pwroff
,
517 MAX1617_WR_AMB_HIGHLIM
);
518 bbc_i2c_writeb(tp
->client
, amb_temp_limits
[tp
->index
].low_pwroff
,
519 MAX1617_WR_AMB_LOWLIM
);
520 bbc_i2c_writeb(tp
->client
, cpu_temp_limits
[tp
->index
].high_pwroff
,
521 MAX1617_WR_CPU_HIGHLIM
);
522 bbc_i2c_writeb(tp
->client
, cpu_temp_limits
[tp
->index
].low_pwroff
,
523 MAX1617_WR_CPU_LOWLIM
);
525 get_current_temps(tp
);
526 tp
->prev_cpu_temp
= tp
->avg_cpu_temp
= tp
->curr_cpu_temp
;
527 tp
->prev_amb_temp
= tp
->avg_amb_temp
= tp
->curr_amb_temp
;
529 tp
->fan_todo
[FAN_AMBIENT
] = FAN_SAME
;
530 tp
->fan_todo
[FAN_CPU
] = FAN_SAME
;
533 static void attach_one_fan(struct linux_ebus_child
*echild
, int fan_idx
)
535 struct bbc_fan_control
*fp
= kmalloc(sizeof(*fp
), GFP_KERNEL
);
539 memset(fp
, 0, sizeof(*fp
));
540 fp
->client
= bbc_i2c_attach(echild
);
549 struct bbc_fan_control
**fpp
= &all_bbc_fans
;
551 fpp
= &((*fpp
)->next
);
556 /* The i2c device controlling the fans is write-only.
557 * So the only way to keep track of the current power
558 * level fed to the fans is via software. Choose half
559 * power for cpu/system and 'on' fo the powersupply fan
562 fp
->psupply_fan_on
= 1;
563 fp
->cpu_fan_speed
= (FAN_SPEED_MAX
- FAN_SPEED_MIN
) / 2;
564 fp
->cpu_fan_speed
+= FAN_SPEED_MIN
;
565 fp
->system_fan_speed
= (FAN_SPEED_MAX
- FAN_SPEED_MIN
) / 2;
566 fp
->system_fan_speed
+= FAN_SPEED_MIN
;
571 int bbc_envctrl_init(void)
573 struct linux_ebus_child
*echild
;
578 while ((echild
= bbc_i2c_getdev(devidx
++)) != NULL
) {
579 if (!strcmp(echild
->prom_name
, "temperature"))
580 attach_one_temp(echild
, temp_index
++);
581 if (!strcmp(echild
->prom_name
, "fan-control"))
582 attach_one_fan(echild
, fan_index
++);
584 if (temp_index
!= 0 && fan_index
!= 0) {
585 kenvctrld_task
= kthread_run(kenvctrld
, NULL
, "kenvctrld");
586 if (IS_ERR(kenvctrld_task
))
587 return PTR_ERR(kenvctrld_task
);
593 static void destroy_one_temp(struct bbc_cpu_temperature
*tp
)
595 bbc_i2c_detach(tp
->client
);
599 static void destroy_one_fan(struct bbc_fan_control
*fp
)
601 bbc_i2c_detach(fp
->client
);
605 void bbc_envctrl_cleanup(void)
607 struct bbc_cpu_temperature
*tp
;
608 struct bbc_fan_control
*fp
;
610 kthread_stop(kenvctrld_task
);
614 struct bbc_cpu_temperature
*next
= tp
->next
;
615 destroy_one_temp(tp
);
618 all_bbc_temps
= NULL
;
622 struct bbc_fan_control
*next
= fp
->next
;