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 #include <linux/kernel.h>
8 #include <linux/kthread.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/delay.h>
12 #include <asm/oplib.h>
20 /* WARNING: Making changes to this driver is very dangerous.
21 * If you misprogram the sensor chips they can
22 * cut the power on you instantly.
25 /* Two temperature sensors exist in the SunBLADE-1000 enclosure.
26 * Both are implemented using max1617 i2c devices. Each max1617
27 * monitors 2 temperatures, one for one of the cpu dies and the other
28 * for the ambient temperature.
30 * The max1617 is capable of being programmed with power-off
31 * temperature values, one low limit and one high limit. These
32 * can be controlled independently for the cpu or ambient temperature.
33 * If a limit is violated, the power is simply shut off. The frequency
34 * with which the max1617 does temperature sampling can be controlled
37 * Three fans exist inside the machine, all three are controlled with
38 * an i2c digital to analog converter. There is a fan directed at the
39 * two processor slots, another for the rest of the enclosure, and the
40 * third is for the power supply. The first two fans may be speed
41 * controlled by changing the voltage fed to them. The third fan may
42 * only be completely off or on. The third fan is meant to only be
43 * disabled/enabled when entering/exiting the lowest power-saving
44 * mode of the machine.
46 * An environmental control kernel thread periodically monitors all
47 * temperature sensors. Based upon the samples it will adjust the
48 * fan speeds to try and keep the system within a certain temperature
49 * range (the goal being to make the fans as quiet as possible without
50 * allowing the system to get too hot).
52 * If the temperature begins to rise/fall outside of the acceptable
53 * operating range, a periodic warning will be sent to the kernel log.
54 * The fans will be put on full blast to attempt to deal with this
55 * situation. After exceeding the acceptable operating range by a
56 * certain threshold, the kernel thread will shut down the system.
57 * Here, the thread is attempting to shut the machine down cleanly
58 * before the hardware based power-off event is triggered.
61 /* These settings are in Celsius. We use these defaults only
62 * if we cannot interrogate the cpu-fru SEEPROM.
65 s8 high_pwroff, high_shutdown, high_warn;
66 s8 low_warn, low_shutdown, low_pwroff;
69 static struct temp_limits cpu_temp_limits[2] = {
70 { 100, 85, 80, 5, -5, -10 },
71 { 100, 85, 80, 5, -5, -10 },
74 static struct temp_limits amb_temp_limits[2] = {
75 { 65, 55, 40, 5, -5, -10 },
76 { 65, 55, 40, 5, -5, -10 },
79 enum fan_action { FAN_SLOWER, FAN_SAME, FAN_FASTER, FAN_FULLBLAST, FAN_STATE_MAX };
81 struct bbc_cpu_temperature {
82 struct bbc_cpu_temperature *next;
84 struct bbc_i2c_client *client;
87 /* Current readings, and history. */
97 enum fan_action fan_todo[2];
102 struct bbc_cpu_temperature *all_bbc_temps;
104 struct bbc_fan_control {
105 struct bbc_fan_control *next;
107 struct bbc_i2c_client *client;
112 int system_fan_speed;
115 struct bbc_fan_control *all_bbc_fans;
117 #define CPU_FAN_REG 0xf0
118 #define SYS_FAN_REG 0xf2
119 #define PSUPPLY_FAN_REG 0xf4
121 #define FAN_SPEED_MIN 0x0c
122 #define FAN_SPEED_MAX 0x3f
124 #define PSUPPLY_FAN_ON 0x1f
125 #define PSUPPLY_FAN_OFF 0x00
127 static void set_fan_speeds(struct bbc_fan_control *fp)
129 /* Put temperatures into range so we don't mis-program
132 if (fp->cpu_fan_speed < FAN_SPEED_MIN)
133 fp->cpu_fan_speed = FAN_SPEED_MIN;
134 if (fp->cpu_fan_speed > FAN_SPEED_MAX)
135 fp->cpu_fan_speed = FAN_SPEED_MAX;
136 if (fp->system_fan_speed < FAN_SPEED_MIN)
137 fp->system_fan_speed = FAN_SPEED_MIN;
138 if (fp->system_fan_speed > FAN_SPEED_MAX)
139 fp->system_fan_speed = FAN_SPEED_MAX;
141 printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
143 fp->cpu_fan_speed, fp->system_fan_speed);
146 bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG);
147 bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG);
148 bbc_i2c_writeb(fp->client,
149 (fp->psupply_fan_on ?
150 PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF),
154 static void get_current_temps(struct bbc_cpu_temperature *tp)
156 tp->prev_amb_temp = tp->curr_amb_temp;
157 bbc_i2c_readb(tp->client,
158 (unsigned char *) &tp->curr_amb_temp,
160 tp->prev_cpu_temp = tp->curr_cpu_temp;
161 bbc_i2c_readb(tp->client,
162 (unsigned char *) &tp->curr_cpu_temp,
165 printk("temp%d: cpu(%d C) amb(%d C)\n",
167 (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp);
172 static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp)
174 static int shutting_down = 0;
175 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
176 char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
180 if (shutting_down != 0)
183 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
184 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
186 val = tp->curr_amb_temp;
187 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
188 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
190 val = tp->curr_cpu_temp;
193 printk(KERN_CRIT "temp%d: Outside of safe %s "
194 "operating temperature, %d C.\n",
195 tp->index, type, val);
197 printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n");
200 if (kernel_execve("/sbin/shutdown", argv, envp) < 0)
201 printk(KERN_CRIT "envctrl: shutdown execution failed\n");
204 #define WARN_INTERVAL (30 * HZ)
206 static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
210 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
211 if (tp->curr_amb_temp >=
212 amb_temp_limits[tp->index].high_warn) {
213 printk(KERN_WARNING "temp%d: "
214 "Above safe ambient operating temperature, %d C.\n",
215 tp->index, (int) tp->curr_amb_temp);
217 } else if (tp->curr_amb_temp <
218 amb_temp_limits[tp->index].low_warn) {
219 printk(KERN_WARNING "temp%d: "
220 "Below safe ambient operating temperature, %d C.\n",
221 tp->index, (int) tp->curr_amb_temp);
225 *last_warn = jiffies;
226 } else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn ||
227 tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn)
230 /* Now check the shutdown limits. */
231 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
232 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
233 do_envctrl_shutdown(tp);
238 tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST;
239 } else if ((tick & (8 - 1)) == 0) {
240 s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10;
243 amb_goal_lo = amb_goal_hi - 3;
245 /* We do not try to avoid 'too cold' events. Basically we
246 * only try to deal with over-heating and fan noise reduction.
248 if (tp->avg_amb_temp < amb_goal_hi) {
249 if (tp->avg_amb_temp >= amb_goal_lo)
250 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
252 tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER;
254 tp->fan_todo[FAN_AMBIENT] = FAN_FASTER;
257 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
261 static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
265 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
266 if (tp->curr_cpu_temp >=
267 cpu_temp_limits[tp->index].high_warn) {
268 printk(KERN_WARNING "temp%d: "
269 "Above safe CPU operating temperature, %d C.\n",
270 tp->index, (int) tp->curr_cpu_temp);
272 } else if (tp->curr_cpu_temp <
273 cpu_temp_limits[tp->index].low_warn) {
274 printk(KERN_WARNING "temp%d: "
275 "Below safe CPU operating temperature, %d C.\n",
276 tp->index, (int) tp->curr_cpu_temp);
280 *last_warn = jiffies;
281 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn ||
282 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn)
285 /* Now check the shutdown limits. */
286 if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
287 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
288 do_envctrl_shutdown(tp);
293 tp->fan_todo[FAN_CPU] = FAN_FULLBLAST;
294 } else if ((tick & (8 - 1)) == 0) {
295 s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10;
298 cpu_goal_lo = cpu_goal_hi - 3;
300 /* We do not try to avoid 'too cold' events. Basically we
301 * only try to deal with over-heating and fan noise reduction.
303 if (tp->avg_cpu_temp < cpu_goal_hi) {
304 if (tp->avg_cpu_temp >= cpu_goal_lo)
305 tp->fan_todo[FAN_CPU] = FAN_SAME;
307 tp->fan_todo[FAN_CPU] = FAN_SLOWER;
309 tp->fan_todo[FAN_CPU] = FAN_FASTER;
312 tp->fan_todo[FAN_CPU] = FAN_SAME;
316 static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn)
318 tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2);
319 tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2);
321 analyze_ambient_temp(tp, last_warn, tp->sample_tick);
322 analyze_cpu_temp(tp, last_warn, tp->sample_tick);
327 static enum fan_action prioritize_fan_action(int which_fan)
329 struct bbc_cpu_temperature *tp;
330 enum fan_action decision = FAN_STATE_MAX;
332 /* Basically, prioritize what the temperature sensors
333 * recommend we do, and perform that action on all the
336 for (tp = all_bbc_temps; tp; tp = tp->next) {
337 if (tp->fan_todo[which_fan] == FAN_FULLBLAST) {
338 decision = FAN_FULLBLAST;
341 if (tp->fan_todo[which_fan] == FAN_SAME &&
342 decision != FAN_FASTER)
344 else if (tp->fan_todo[which_fan] == FAN_FASTER)
345 decision = FAN_FASTER;
346 else if (decision != FAN_FASTER &&
347 decision != FAN_SAME &&
348 tp->fan_todo[which_fan] == FAN_SLOWER)
349 decision = FAN_SLOWER;
351 if (decision == FAN_STATE_MAX)
357 static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp)
359 enum fan_action decision = prioritize_fan_action(FAN_AMBIENT);
362 if (decision == FAN_SAME)
366 if (decision == FAN_FULLBLAST) {
367 if (fp->system_fan_speed >= FAN_SPEED_MAX)
370 fp->system_fan_speed = FAN_SPEED_MAX;
372 if (decision == FAN_FASTER) {
373 if (fp->system_fan_speed >= FAN_SPEED_MAX)
376 fp->system_fan_speed += 2;
378 int orig_speed = fp->system_fan_speed;
380 if (orig_speed <= FAN_SPEED_MIN ||
381 orig_speed <= (fp->cpu_fan_speed - 3))
384 fp->system_fan_speed -= 1;
391 static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp)
393 enum fan_action decision = prioritize_fan_action(FAN_CPU);
396 if (decision == FAN_SAME)
400 if (decision == FAN_FULLBLAST) {
401 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
404 fp->cpu_fan_speed = FAN_SPEED_MAX;
406 if (decision == FAN_FASTER) {
407 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
410 fp->cpu_fan_speed += 2;
411 if (fp->system_fan_speed <
412 (fp->cpu_fan_speed - 3))
413 fp->system_fan_speed =
414 fp->cpu_fan_speed - 3;
417 if (fp->cpu_fan_speed <= FAN_SPEED_MIN)
420 fp->cpu_fan_speed -= 1;
427 static void maybe_new_fan_speeds(struct bbc_fan_control *fp)
431 new = maybe_new_ambient_fan_speed(fp);
432 new |= maybe_new_cpu_fan_speed(fp);
438 static void fans_full_blast(void)
440 struct bbc_fan_control *fp;
442 /* Since we will not be monitoring things anymore, put
443 * the fans on full blast.
445 for (fp = all_bbc_fans; fp; fp = fp->next) {
446 fp->cpu_fan_speed = FAN_SPEED_MAX;
447 fp->system_fan_speed = FAN_SPEED_MAX;
448 fp->psupply_fan_on = 1;
453 #define POLL_INTERVAL (5 * 1000)
454 static unsigned long last_warning_jiffies;
455 static struct task_struct *kenvctrld_task;
457 static int kenvctrld(void *__unused)
459 printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n");
460 last_warning_jiffies = jiffies - WARN_INTERVAL;
462 struct bbc_cpu_temperature *tp;
463 struct bbc_fan_control *fp;
465 msleep_interruptible(POLL_INTERVAL);
466 if (kthread_should_stop())
469 for (tp = all_bbc_temps; tp; tp = tp->next) {
470 get_current_temps(tp);
471 analyze_temps(tp, &last_warning_jiffies);
473 for (fp = all_bbc_fans; fp; fp = fp->next)
474 maybe_new_fan_speeds(fp);
476 printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n");
483 static void attach_one_temp(struct linux_ebus_child *echild, int temp_idx)
485 struct bbc_cpu_temperature *tp = kmalloc(sizeof(*tp), GFP_KERNEL);
489 memset(tp, 0, sizeof(*tp));
490 tp->client = bbc_i2c_attach(echild);
496 tp->index = temp_idx;
498 struct bbc_cpu_temperature **tpp = &all_bbc_temps;
500 tpp = &((*tpp)->next);
505 /* Tell it to convert once every 5 seconds, clear all cfg
508 bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE);
509 bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE);
511 /* Program the hard temperature limits into the chip. */
512 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff,
513 MAX1617_WR_AMB_HIGHLIM);
514 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff,
515 MAX1617_WR_AMB_LOWLIM);
516 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff,
517 MAX1617_WR_CPU_HIGHLIM);
518 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff,
519 MAX1617_WR_CPU_LOWLIM);
521 get_current_temps(tp);
522 tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp;
523 tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp;
525 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
526 tp->fan_todo[FAN_CPU] = FAN_SAME;
529 static void attach_one_fan(struct linux_ebus_child *echild, int fan_idx)
531 struct bbc_fan_control *fp = kmalloc(sizeof(*fp), GFP_KERNEL);
535 memset(fp, 0, sizeof(*fp));
536 fp->client = bbc_i2c_attach(echild);
545 struct bbc_fan_control **fpp = &all_bbc_fans;
547 fpp = &((*fpp)->next);
552 /* The i2c device controlling the fans is write-only.
553 * So the only way to keep track of the current power
554 * level fed to the fans is via software. Choose half
555 * power for cpu/system and 'on' fo the powersupply fan
558 fp->psupply_fan_on = 1;
559 fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
560 fp->cpu_fan_speed += FAN_SPEED_MIN;
561 fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
562 fp->system_fan_speed += FAN_SPEED_MIN;
567 int bbc_envctrl_init(void)
569 struct linux_ebus_child *echild;
574 while ((echild = bbc_i2c_getdev(devidx++)) != NULL) {
575 if (!strcmp(echild->prom_node->name, "temperature"))
576 attach_one_temp(echild, temp_index++);
577 if (!strcmp(echild->prom_node->name, "fan-control"))
578 attach_one_fan(echild, fan_index++);
580 if (temp_index != 0 && fan_index != 0) {
581 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
582 if (IS_ERR(kenvctrld_task))
583 return PTR_ERR(kenvctrld_task);
589 static void destroy_one_temp(struct bbc_cpu_temperature *tp)
591 bbc_i2c_detach(tp->client);
595 static void destroy_one_fan(struct bbc_fan_control *fp)
597 bbc_i2c_detach(fp->client);
601 void bbc_envctrl_cleanup(void)
603 struct bbc_cpu_temperature *tp;
604 struct bbc_fan_control *fp;
606 kthread_stop(kenvctrld_task);
610 struct bbc_cpu_temperature *next = tp->next;
611 destroy_one_temp(tp);
614 all_bbc_temps = NULL;
618 struct bbc_fan_control *next = fp->next;