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/sched.h>
11 #include <linux/slab.h>
12 #include <linux/delay.h>
13 #include <asm/oplib.h>
16 #include <asm/unistd.h>
23 /* WARNING: Making changes to this driver is very dangerous.
24 * If you misprogram the sensor chips they can
25 * cut the power on you instantly.
28 /* Two temperature sensors exist in the SunBLADE-1000 enclosure.
29 * Both are implemented using max1617 i2c devices. Each max1617
30 * monitors 2 temperatures, one for one of the cpu dies and the other
31 * for the ambient temperature.
33 * The max1617 is capable of being programmed with power-off
34 * temperature values, one low limit and one high limit. These
35 * can be controlled independently for the cpu or ambient temperature.
36 * If a limit is violated, the power is simply shut off. The frequency
37 * with which the max1617 does temperature sampling can be controlled
40 * Three fans exist inside the machine, all three are controlled with
41 * an i2c digital to analog converter. There is a fan directed at the
42 * two processor slots, another for the rest of the enclosure, and the
43 * third is for the power supply. The first two fans may be speed
44 * controlled by changing the voltage fed to them. The third fan may
45 * only be completely off or on. The third fan is meant to only be
46 * disabled/enabled when entering/exiting the lowest power-saving
47 * mode of the machine.
49 * An environmental control kernel thread periodically monitors all
50 * temperature sensors. Based upon the samples it will adjust the
51 * fan speeds to try and keep the system within a certain temperature
52 * range (the goal being to make the fans as quiet as possible without
53 * allowing the system to get too hot).
55 * If the temperature begins to rise/fall outside of the acceptable
56 * operating range, a periodic warning will be sent to the kernel log.
57 * The fans will be put on full blast to attempt to deal with this
58 * situation. After exceeding the acceptable operating range by a
59 * certain threshold, the kernel thread will shut down the system.
60 * Here, the thread is attempting to shut the machine down cleanly
61 * before the hardware based power-off event is triggered.
64 /* These settings are in Celsius. We use these defaults only
65 * if we cannot interrogate the cpu-fru SEEPROM.
68 s8 high_pwroff, high_shutdown, high_warn;
69 s8 low_warn, low_shutdown, low_pwroff;
72 static struct temp_limits cpu_temp_limits[2] = {
73 { 100, 85, 80, 5, -5, -10 },
74 { 100, 85, 80, 5, -5, -10 },
77 static struct temp_limits amb_temp_limits[2] = {
78 { 65, 55, 40, 5, -5, -10 },
79 { 65, 55, 40, 5, -5, -10 },
82 enum fan_action { FAN_SLOWER, FAN_SAME, FAN_FASTER, FAN_FULLBLAST, FAN_STATE_MAX };
84 struct bbc_cpu_temperature {
85 struct bbc_cpu_temperature *next;
87 struct bbc_i2c_client *client;
90 /* Current readings, and history. */
100 enum fan_action fan_todo[2];
101 #define FAN_AMBIENT 0
105 struct bbc_cpu_temperature *all_bbc_temps;
107 struct bbc_fan_control {
108 struct bbc_fan_control *next;
110 struct bbc_i2c_client *client;
115 int system_fan_speed;
118 struct bbc_fan_control *all_bbc_fans;
120 #define CPU_FAN_REG 0xf0
121 #define SYS_FAN_REG 0xf2
122 #define PSUPPLY_FAN_REG 0xf4
124 #define FAN_SPEED_MIN 0x0c
125 #define FAN_SPEED_MAX 0x3f
127 #define PSUPPLY_FAN_ON 0x1f
128 #define PSUPPLY_FAN_OFF 0x00
130 static void set_fan_speeds(struct bbc_fan_control *fp)
132 /* Put temperatures into range so we don't mis-program
135 if (fp->cpu_fan_speed < FAN_SPEED_MIN)
136 fp->cpu_fan_speed = FAN_SPEED_MIN;
137 if (fp->cpu_fan_speed > FAN_SPEED_MAX)
138 fp->cpu_fan_speed = FAN_SPEED_MAX;
139 if (fp->system_fan_speed < FAN_SPEED_MIN)
140 fp->system_fan_speed = FAN_SPEED_MIN;
141 if (fp->system_fan_speed > FAN_SPEED_MAX)
142 fp->system_fan_speed = FAN_SPEED_MAX;
144 printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
146 fp->cpu_fan_speed, fp->system_fan_speed);
149 bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG);
150 bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG);
151 bbc_i2c_writeb(fp->client,
152 (fp->psupply_fan_on ?
153 PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF),
157 static void get_current_temps(struct bbc_cpu_temperature *tp)
159 tp->prev_amb_temp = tp->curr_amb_temp;
160 bbc_i2c_readb(tp->client,
161 (unsigned char *) &tp->curr_amb_temp,
163 tp->prev_cpu_temp = tp->curr_cpu_temp;
164 bbc_i2c_readb(tp->client,
165 (unsigned char *) &tp->curr_cpu_temp,
168 printk("temp%d: cpu(%d C) amb(%d C)\n",
170 (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp);
175 static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp)
177 static int shutting_down = 0;
178 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
179 char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
183 if (shutting_down != 0)
186 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
187 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
189 val = tp->curr_amb_temp;
190 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
191 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
193 val = tp->curr_cpu_temp;
196 printk(KERN_CRIT "temp%d: Outside of safe %s "
197 "operating temperature, %d C.\n",
198 tp->index, type, val);
200 printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n");
203 if (execve("/sbin/shutdown", argv, envp) < 0)
204 printk(KERN_CRIT "envctrl: shutdown execution failed\n");
207 #define WARN_INTERVAL (30 * HZ)
209 static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
213 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
214 if (tp->curr_amb_temp >=
215 amb_temp_limits[tp->index].high_warn) {
216 printk(KERN_WARNING "temp%d: "
217 "Above safe ambient operating temperature, %d C.\n",
218 tp->index, (int) tp->curr_amb_temp);
220 } else if (tp->curr_amb_temp <
221 amb_temp_limits[tp->index].low_warn) {
222 printk(KERN_WARNING "temp%d: "
223 "Below safe ambient operating temperature, %d C.\n",
224 tp->index, (int) tp->curr_amb_temp);
228 *last_warn = jiffies;
229 } else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn ||
230 tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn)
233 /* Now check the shutdown limits. */
234 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
235 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
236 do_envctrl_shutdown(tp);
241 tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST;
242 } else if ((tick & (8 - 1)) == 0) {
243 s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10;
246 amb_goal_lo = amb_goal_hi - 3;
248 /* We do not try to avoid 'too cold' events. Basically we
249 * only try to deal with over-heating and fan noise reduction.
251 if (tp->avg_amb_temp < amb_goal_hi) {
252 if (tp->avg_amb_temp >= amb_goal_lo)
253 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
255 tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER;
257 tp->fan_todo[FAN_AMBIENT] = FAN_FASTER;
260 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
264 static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
268 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
269 if (tp->curr_cpu_temp >=
270 cpu_temp_limits[tp->index].high_warn) {
271 printk(KERN_WARNING "temp%d: "
272 "Above safe CPU operating temperature, %d C.\n",
273 tp->index, (int) tp->curr_cpu_temp);
275 } else if (tp->curr_cpu_temp <
276 cpu_temp_limits[tp->index].low_warn) {
277 printk(KERN_WARNING "temp%d: "
278 "Below safe CPU operating temperature, %d C.\n",
279 tp->index, (int) tp->curr_cpu_temp);
283 *last_warn = jiffies;
284 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn ||
285 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn)
288 /* Now check the shutdown limits. */
289 if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
290 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
291 do_envctrl_shutdown(tp);
296 tp->fan_todo[FAN_CPU] = FAN_FULLBLAST;
297 } else if ((tick & (8 - 1)) == 0) {
298 s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10;
301 cpu_goal_lo = cpu_goal_hi - 3;
303 /* We do not try to avoid 'too cold' events. Basically we
304 * only try to deal with over-heating and fan noise reduction.
306 if (tp->avg_cpu_temp < cpu_goal_hi) {
307 if (tp->avg_cpu_temp >= cpu_goal_lo)
308 tp->fan_todo[FAN_CPU] = FAN_SAME;
310 tp->fan_todo[FAN_CPU] = FAN_SLOWER;
312 tp->fan_todo[FAN_CPU] = FAN_FASTER;
315 tp->fan_todo[FAN_CPU] = FAN_SAME;
319 static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn)
321 tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2);
322 tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2);
324 analyze_ambient_temp(tp, last_warn, tp->sample_tick);
325 analyze_cpu_temp(tp, last_warn, tp->sample_tick);
330 static enum fan_action prioritize_fan_action(int which_fan)
332 struct bbc_cpu_temperature *tp;
333 enum fan_action decision = FAN_STATE_MAX;
335 /* Basically, prioritize what the temperature sensors
336 * recommend we do, and perform that action on all the
339 for (tp = all_bbc_temps; tp; tp = tp->next) {
340 if (tp->fan_todo[which_fan] == FAN_FULLBLAST) {
341 decision = FAN_FULLBLAST;
344 if (tp->fan_todo[which_fan] == FAN_SAME &&
345 decision != FAN_FASTER)
347 else if (tp->fan_todo[which_fan] == FAN_FASTER)
348 decision = FAN_FASTER;
349 else if (decision != FAN_FASTER &&
350 decision != FAN_SAME &&
351 tp->fan_todo[which_fan] == FAN_SLOWER)
352 decision = FAN_SLOWER;
354 if (decision == FAN_STATE_MAX)
360 static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp)
362 enum fan_action decision = prioritize_fan_action(FAN_AMBIENT);
365 if (decision == FAN_SAME)
369 if (decision == FAN_FULLBLAST) {
370 if (fp->system_fan_speed >= FAN_SPEED_MAX)
373 fp->system_fan_speed = FAN_SPEED_MAX;
375 if (decision == FAN_FASTER) {
376 if (fp->system_fan_speed >= FAN_SPEED_MAX)
379 fp->system_fan_speed += 2;
381 int orig_speed = fp->system_fan_speed;
383 if (orig_speed <= FAN_SPEED_MIN ||
384 orig_speed <= (fp->cpu_fan_speed - 3))
387 fp->system_fan_speed -= 1;
394 static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp)
396 enum fan_action decision = prioritize_fan_action(FAN_CPU);
399 if (decision == FAN_SAME)
403 if (decision == FAN_FULLBLAST) {
404 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
407 fp->cpu_fan_speed = FAN_SPEED_MAX;
409 if (decision == FAN_FASTER) {
410 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
413 fp->cpu_fan_speed += 2;
414 if (fp->system_fan_speed <
415 (fp->cpu_fan_speed - 3))
416 fp->system_fan_speed =
417 fp->cpu_fan_speed - 3;
420 if (fp->cpu_fan_speed <= FAN_SPEED_MIN)
423 fp->cpu_fan_speed -= 1;
430 static void maybe_new_fan_speeds(struct bbc_fan_control *fp)
434 new = maybe_new_ambient_fan_speed(fp);
435 new |= maybe_new_cpu_fan_speed(fp);
441 static void fans_full_blast(void)
443 struct bbc_fan_control *fp;
445 /* Since we will not be monitoring things anymore, put
446 * the fans on full blast.
448 for (fp = all_bbc_fans; fp; fp = fp->next) {
449 fp->cpu_fan_speed = FAN_SPEED_MAX;
450 fp->system_fan_speed = FAN_SPEED_MAX;
451 fp->psupply_fan_on = 1;
456 #define POLL_INTERVAL (5 * 1000)
457 static unsigned long last_warning_jiffies;
458 static struct task_struct *kenvctrld_task;
460 static int kenvctrld(void *__unused)
462 daemonize("kenvctrld");
463 allow_signal(SIGKILL);
464 kenvctrld_task = current;
466 printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n");
467 last_warning_jiffies = jiffies - WARN_INTERVAL;
469 struct bbc_cpu_temperature *tp;
470 struct bbc_fan_control *fp;
472 msleep_interruptible(POLL_INTERVAL);
473 if (signal_pending(current))
476 for (tp = all_bbc_temps; tp; tp = tp->next) {
477 get_current_temps(tp);
478 analyze_temps(tp, &last_warning_jiffies);
480 for (fp = all_bbc_fans; fp; fp = fp->next)
481 maybe_new_fan_speeds(fp);
483 printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n");
490 static void attach_one_temp(struct linux_ebus_child *echild, int temp_idx)
492 struct bbc_cpu_temperature *tp = kmalloc(sizeof(*tp), GFP_KERNEL);
496 memset(tp, 0, sizeof(*tp));
497 tp->client = bbc_i2c_attach(echild);
503 tp->index = temp_idx;
505 struct bbc_cpu_temperature **tpp = &all_bbc_temps;
507 tpp = &((*tpp)->next);
512 /* Tell it to convert once every 5 seconds, clear all cfg
515 bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE);
516 bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE);
518 /* Program the hard temperature limits into the chip. */
519 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff,
520 MAX1617_WR_AMB_HIGHLIM);
521 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff,
522 MAX1617_WR_AMB_LOWLIM);
523 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff,
524 MAX1617_WR_CPU_HIGHLIM);
525 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff,
526 MAX1617_WR_CPU_LOWLIM);
528 get_current_temps(tp);
529 tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp;
530 tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp;
532 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
533 tp->fan_todo[FAN_CPU] = FAN_SAME;
536 static void attach_one_fan(struct linux_ebus_child *echild, int fan_idx)
538 struct bbc_fan_control *fp = kmalloc(sizeof(*fp), GFP_KERNEL);
542 memset(fp, 0, sizeof(*fp));
543 fp->client = bbc_i2c_attach(echild);
552 struct bbc_fan_control **fpp = &all_bbc_fans;
554 fpp = &((*fpp)->next);
559 /* The i2c device controlling the fans is write-only.
560 * So the only way to keep track of the current power
561 * level fed to the fans is via software. Choose half
562 * power for cpu/system and 'on' fo the powersupply fan
565 fp->psupply_fan_on = 1;
566 fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
567 fp->cpu_fan_speed += FAN_SPEED_MIN;
568 fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
569 fp->system_fan_speed += FAN_SPEED_MIN;
574 int bbc_envctrl_init(void)
576 struct linux_ebus_child *echild;
582 while ((echild = bbc_i2c_getdev(devidx++)) != NULL) {
583 if (!strcmp(echild->prom_name, "temperature"))
584 attach_one_temp(echild, temp_index++);
585 if (!strcmp(echild->prom_name, "fan-control"))
586 attach_one_fan(echild, fan_index++);
588 if (temp_index != 0 && fan_index != 0)
589 err = kernel_thread(kenvctrld, NULL, CLONE_FS | CLONE_FILES);
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 if (kenvctrld_task != NULL) {
611 force_sig(SIGKILL, kenvctrld_task);
613 struct task_struct *p;
616 read_lock(&tasklist_lock);
617 for_each_process(p) {
618 if (p == kenvctrld_task) {
623 read_unlock(&tasklist_lock);
628 kenvctrld_task = NULL;
633 struct bbc_cpu_temperature *next = tp->next;
634 destroy_one_temp(tp);
637 all_bbc_temps = NULL;
641 struct bbc_fan_control *next = fp->next;