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/kthread.h>
8 #include <linux/delay.h>
9 #include <linux/kmod.h>
10 #include <linux/reboot.h>
11 #include <asm/oplib.h>
19 /* WARNING: Making changes to this driver is very dangerous.
20 * If you misprogram the sensor chips they can
21 * cut the power on you instantly.
24 /* Two temperature sensors exist in the SunBLADE-1000 enclosure.
25 * Both are implemented using max1617 i2c devices. Each max1617
26 * monitors 2 temperatures, one for one of the cpu dies and the other
27 * for the ambient temperature.
29 * The max1617 is capable of being programmed with power-off
30 * temperature values, one low limit and one high limit. These
31 * can be controlled independently for the cpu or ambient temperature.
32 * If a limit is violated, the power is simply shut off. The frequency
33 * with which the max1617 does temperature sampling can be controlled
36 * Three fans exist inside the machine, all three are controlled with
37 * an i2c digital to analog converter. There is a fan directed at the
38 * two processor slots, another for the rest of the enclosure, and the
39 * third is for the power supply. The first two fans may be speed
40 * controlled by changing the voltage fed to them. The third fan may
41 * only be completely off or on. The third fan is meant to only be
42 * disabled/enabled when entering/exiting the lowest power-saving
43 * mode of the machine.
45 * An environmental control kernel thread periodically monitors all
46 * temperature sensors. Based upon the samples it will adjust the
47 * fan speeds to try and keep the system within a certain temperature
48 * range (the goal being to make the fans as quiet as possible without
49 * allowing the system to get too hot).
51 * If the temperature begins to rise/fall outside of the acceptable
52 * operating range, a periodic warning will be sent to the kernel log.
53 * The fans will be put on full blast to attempt to deal with this
54 * situation. After exceeding the acceptable operating range by a
55 * certain threshold, the kernel thread will shut down the system.
56 * Here, the thread is attempting to shut the machine down cleanly
57 * before the hardware based power-off event is triggered.
60 /* These settings are in Celsius. We use these defaults only
61 * if we cannot interrogate the cpu-fru SEEPROM.
64 s8 high_pwroff, high_shutdown, high_warn;
65 s8 low_warn, low_shutdown, low_pwroff;
68 static struct temp_limits cpu_temp_limits[2] = {
69 { 100, 85, 80, 5, -5, -10 },
70 { 100, 85, 80, 5, -5, -10 },
73 static struct temp_limits amb_temp_limits[2] = {
74 { 65, 55, 40, 5, -5, -10 },
75 { 65, 55, 40, 5, -5, -10 },
78 enum fan_action { FAN_SLOWER, FAN_SAME, FAN_FASTER, FAN_FULLBLAST, FAN_STATE_MAX };
80 struct bbc_cpu_temperature {
81 struct bbc_cpu_temperature *next;
83 struct bbc_i2c_client *client;
86 /* Current readings, and history. */
96 enum fan_action fan_todo[2];
101 struct bbc_cpu_temperature *all_bbc_temps;
103 struct bbc_fan_control {
104 struct bbc_fan_control *next;
106 struct bbc_i2c_client *client;
111 int system_fan_speed;
114 struct bbc_fan_control *all_bbc_fans;
116 #define CPU_FAN_REG 0xf0
117 #define SYS_FAN_REG 0xf2
118 #define PSUPPLY_FAN_REG 0xf4
120 #define FAN_SPEED_MIN 0x0c
121 #define FAN_SPEED_MAX 0x3f
123 #define PSUPPLY_FAN_ON 0x1f
124 #define PSUPPLY_FAN_OFF 0x00
126 static void set_fan_speeds(struct bbc_fan_control *fp)
128 /* Put temperatures into range so we don't mis-program
131 if (fp->cpu_fan_speed < FAN_SPEED_MIN)
132 fp->cpu_fan_speed = FAN_SPEED_MIN;
133 if (fp->cpu_fan_speed > FAN_SPEED_MAX)
134 fp->cpu_fan_speed = FAN_SPEED_MAX;
135 if (fp->system_fan_speed < FAN_SPEED_MIN)
136 fp->system_fan_speed = FAN_SPEED_MIN;
137 if (fp->system_fan_speed > FAN_SPEED_MAX)
138 fp->system_fan_speed = FAN_SPEED_MAX;
140 printk("fan%d: Changed fan speed to cpu(%02x) sys(%02x)\n",
142 fp->cpu_fan_speed, fp->system_fan_speed);
145 bbc_i2c_writeb(fp->client, fp->cpu_fan_speed, CPU_FAN_REG);
146 bbc_i2c_writeb(fp->client, fp->system_fan_speed, SYS_FAN_REG);
147 bbc_i2c_writeb(fp->client,
148 (fp->psupply_fan_on ?
149 PSUPPLY_FAN_ON : PSUPPLY_FAN_OFF),
153 static void get_current_temps(struct bbc_cpu_temperature *tp)
155 tp->prev_amb_temp = tp->curr_amb_temp;
156 bbc_i2c_readb(tp->client,
157 (unsigned char *) &tp->curr_amb_temp,
159 tp->prev_cpu_temp = tp->curr_cpu_temp;
160 bbc_i2c_readb(tp->client,
161 (unsigned char *) &tp->curr_cpu_temp,
164 printk("temp%d: cpu(%d C) amb(%d C)\n",
166 (int) tp->curr_cpu_temp, (int) tp->curr_amb_temp);
171 static void do_envctrl_shutdown(struct bbc_cpu_temperature *tp)
173 static int shutting_down = 0;
177 if (shutting_down != 0)
180 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
181 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
183 val = tp->curr_amb_temp;
184 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
185 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
187 val = tp->curr_cpu_temp;
190 printk(KERN_CRIT "temp%d: Outside of safe %s "
191 "operating temperature, %d C.\n",
192 tp->index, type, val);
194 printk(KERN_CRIT "kenvctrld: Shutting down the system now.\n");
197 if (orderly_poweroff(true) < 0)
198 printk(KERN_CRIT "envctrl: shutdown execution failed\n");
201 #define WARN_INTERVAL (30 * HZ)
203 static void analyze_ambient_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
207 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
208 if (tp->curr_amb_temp >=
209 amb_temp_limits[tp->index].high_warn) {
210 printk(KERN_WARNING "temp%d: "
211 "Above safe ambient operating temperature, %d C.\n",
212 tp->index, (int) tp->curr_amb_temp);
214 } else if (tp->curr_amb_temp <
215 amb_temp_limits[tp->index].low_warn) {
216 printk(KERN_WARNING "temp%d: "
217 "Below safe ambient operating temperature, %d C.\n",
218 tp->index, (int) tp->curr_amb_temp);
222 *last_warn = jiffies;
223 } else if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_warn ||
224 tp->curr_amb_temp < amb_temp_limits[tp->index].low_warn)
227 /* Now check the shutdown limits. */
228 if (tp->curr_amb_temp >= amb_temp_limits[tp->index].high_shutdown ||
229 tp->curr_amb_temp < amb_temp_limits[tp->index].low_shutdown) {
230 do_envctrl_shutdown(tp);
235 tp->fan_todo[FAN_AMBIENT] = FAN_FULLBLAST;
236 } else if ((tick & (8 - 1)) == 0) {
237 s8 amb_goal_hi = amb_temp_limits[tp->index].high_warn - 10;
240 amb_goal_lo = amb_goal_hi - 3;
242 /* We do not try to avoid 'too cold' events. Basically we
243 * only try to deal with over-heating and fan noise reduction.
245 if (tp->avg_amb_temp < amb_goal_hi) {
246 if (tp->avg_amb_temp >= amb_goal_lo)
247 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
249 tp->fan_todo[FAN_AMBIENT] = FAN_SLOWER;
251 tp->fan_todo[FAN_AMBIENT] = FAN_FASTER;
254 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
258 static void analyze_cpu_temp(struct bbc_cpu_temperature *tp, unsigned long *last_warn, int tick)
262 if (time_after(jiffies, (*last_warn + WARN_INTERVAL))) {
263 if (tp->curr_cpu_temp >=
264 cpu_temp_limits[tp->index].high_warn) {
265 printk(KERN_WARNING "temp%d: "
266 "Above safe CPU operating temperature, %d C.\n",
267 tp->index, (int) tp->curr_cpu_temp);
269 } else if (tp->curr_cpu_temp <
270 cpu_temp_limits[tp->index].low_warn) {
271 printk(KERN_WARNING "temp%d: "
272 "Below safe CPU operating temperature, %d C.\n",
273 tp->index, (int) tp->curr_cpu_temp);
277 *last_warn = jiffies;
278 } else if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_warn ||
279 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_warn)
282 /* Now check the shutdown limits. */
283 if (tp->curr_cpu_temp >= cpu_temp_limits[tp->index].high_shutdown ||
284 tp->curr_cpu_temp < cpu_temp_limits[tp->index].low_shutdown) {
285 do_envctrl_shutdown(tp);
290 tp->fan_todo[FAN_CPU] = FAN_FULLBLAST;
291 } else if ((tick & (8 - 1)) == 0) {
292 s8 cpu_goal_hi = cpu_temp_limits[tp->index].high_warn - 10;
295 cpu_goal_lo = cpu_goal_hi - 3;
297 /* We do not try to avoid 'too cold' events. Basically we
298 * only try to deal with over-heating and fan noise reduction.
300 if (tp->avg_cpu_temp < cpu_goal_hi) {
301 if (tp->avg_cpu_temp >= cpu_goal_lo)
302 tp->fan_todo[FAN_CPU] = FAN_SAME;
304 tp->fan_todo[FAN_CPU] = FAN_SLOWER;
306 tp->fan_todo[FAN_CPU] = FAN_FASTER;
309 tp->fan_todo[FAN_CPU] = FAN_SAME;
313 static void analyze_temps(struct bbc_cpu_temperature *tp, unsigned long *last_warn)
315 tp->avg_amb_temp = (s8)((int)((int)tp->avg_amb_temp + (int)tp->curr_amb_temp) / 2);
316 tp->avg_cpu_temp = (s8)((int)((int)tp->avg_cpu_temp + (int)tp->curr_cpu_temp) / 2);
318 analyze_ambient_temp(tp, last_warn, tp->sample_tick);
319 analyze_cpu_temp(tp, last_warn, tp->sample_tick);
324 static enum fan_action prioritize_fan_action(int which_fan)
326 struct bbc_cpu_temperature *tp;
327 enum fan_action decision = FAN_STATE_MAX;
329 /* Basically, prioritize what the temperature sensors
330 * recommend we do, and perform that action on all the
333 for (tp = all_bbc_temps; tp; tp = tp->next) {
334 if (tp->fan_todo[which_fan] == FAN_FULLBLAST) {
335 decision = FAN_FULLBLAST;
338 if (tp->fan_todo[which_fan] == FAN_SAME &&
339 decision != FAN_FASTER)
341 else if (tp->fan_todo[which_fan] == FAN_FASTER)
342 decision = FAN_FASTER;
343 else if (decision != FAN_FASTER &&
344 decision != FAN_SAME &&
345 tp->fan_todo[which_fan] == FAN_SLOWER)
346 decision = FAN_SLOWER;
348 if (decision == FAN_STATE_MAX)
354 static int maybe_new_ambient_fan_speed(struct bbc_fan_control *fp)
356 enum fan_action decision = prioritize_fan_action(FAN_AMBIENT);
359 if (decision == FAN_SAME)
363 if (decision == FAN_FULLBLAST) {
364 if (fp->system_fan_speed >= FAN_SPEED_MAX)
367 fp->system_fan_speed = FAN_SPEED_MAX;
369 if (decision == FAN_FASTER) {
370 if (fp->system_fan_speed >= FAN_SPEED_MAX)
373 fp->system_fan_speed += 2;
375 int orig_speed = fp->system_fan_speed;
377 if (orig_speed <= FAN_SPEED_MIN ||
378 orig_speed <= (fp->cpu_fan_speed - 3))
381 fp->system_fan_speed -= 1;
388 static int maybe_new_cpu_fan_speed(struct bbc_fan_control *fp)
390 enum fan_action decision = prioritize_fan_action(FAN_CPU);
393 if (decision == FAN_SAME)
397 if (decision == FAN_FULLBLAST) {
398 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
401 fp->cpu_fan_speed = FAN_SPEED_MAX;
403 if (decision == FAN_FASTER) {
404 if (fp->cpu_fan_speed >= FAN_SPEED_MAX)
407 fp->cpu_fan_speed += 2;
408 if (fp->system_fan_speed <
409 (fp->cpu_fan_speed - 3))
410 fp->system_fan_speed =
411 fp->cpu_fan_speed - 3;
414 if (fp->cpu_fan_speed <= FAN_SPEED_MIN)
417 fp->cpu_fan_speed -= 1;
424 static void maybe_new_fan_speeds(struct bbc_fan_control *fp)
428 new = maybe_new_ambient_fan_speed(fp);
429 new |= maybe_new_cpu_fan_speed(fp);
435 static void fans_full_blast(void)
437 struct bbc_fan_control *fp;
439 /* Since we will not be monitoring things anymore, put
440 * the fans on full blast.
442 for (fp = all_bbc_fans; fp; fp = fp->next) {
443 fp->cpu_fan_speed = FAN_SPEED_MAX;
444 fp->system_fan_speed = FAN_SPEED_MAX;
445 fp->psupply_fan_on = 1;
450 #define POLL_INTERVAL (5 * 1000)
451 static unsigned long last_warning_jiffies;
452 static struct task_struct *kenvctrld_task;
454 static int kenvctrld(void *__unused)
456 printk(KERN_INFO "bbc_envctrl: kenvctrld starting...\n");
457 last_warning_jiffies = jiffies - WARN_INTERVAL;
459 struct bbc_cpu_temperature *tp;
460 struct bbc_fan_control *fp;
462 msleep_interruptible(POLL_INTERVAL);
463 if (kthread_should_stop())
466 for (tp = all_bbc_temps; tp; tp = tp->next) {
467 get_current_temps(tp);
468 analyze_temps(tp, &last_warning_jiffies);
470 for (fp = all_bbc_fans; fp; fp = fp->next)
471 maybe_new_fan_speeds(fp);
473 printk(KERN_INFO "bbc_envctrl: kenvctrld exiting...\n");
480 static void attach_one_temp(struct linux_ebus_child *echild, int temp_idx)
482 struct bbc_cpu_temperature *tp = kmalloc(sizeof(*tp), GFP_KERNEL);
486 memset(tp, 0, sizeof(*tp));
487 tp->client = bbc_i2c_attach(echild);
493 tp->index = temp_idx;
495 struct bbc_cpu_temperature **tpp = &all_bbc_temps;
497 tpp = &((*tpp)->next);
502 /* Tell it to convert once every 5 seconds, clear all cfg
505 bbc_i2c_writeb(tp->client, 0x00, MAX1617_WR_CFG_BYTE);
506 bbc_i2c_writeb(tp->client, 0x02, MAX1617_WR_CVRATE_BYTE);
508 /* Program the hard temperature limits into the chip. */
509 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].high_pwroff,
510 MAX1617_WR_AMB_HIGHLIM);
511 bbc_i2c_writeb(tp->client, amb_temp_limits[tp->index].low_pwroff,
512 MAX1617_WR_AMB_LOWLIM);
513 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].high_pwroff,
514 MAX1617_WR_CPU_HIGHLIM);
515 bbc_i2c_writeb(tp->client, cpu_temp_limits[tp->index].low_pwroff,
516 MAX1617_WR_CPU_LOWLIM);
518 get_current_temps(tp);
519 tp->prev_cpu_temp = tp->avg_cpu_temp = tp->curr_cpu_temp;
520 tp->prev_amb_temp = tp->avg_amb_temp = tp->curr_amb_temp;
522 tp->fan_todo[FAN_AMBIENT] = FAN_SAME;
523 tp->fan_todo[FAN_CPU] = FAN_SAME;
526 static void attach_one_fan(struct linux_ebus_child *echild, int fan_idx)
528 struct bbc_fan_control *fp = kmalloc(sizeof(*fp), GFP_KERNEL);
532 memset(fp, 0, sizeof(*fp));
533 fp->client = bbc_i2c_attach(echild);
542 struct bbc_fan_control **fpp = &all_bbc_fans;
544 fpp = &((*fpp)->next);
549 /* The i2c device controlling the fans is write-only.
550 * So the only way to keep track of the current power
551 * level fed to the fans is via software. Choose half
552 * power for cpu/system and 'on' fo the powersupply fan
555 fp->psupply_fan_on = 1;
556 fp->cpu_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
557 fp->cpu_fan_speed += FAN_SPEED_MIN;
558 fp->system_fan_speed = (FAN_SPEED_MAX - FAN_SPEED_MIN) / 2;
559 fp->system_fan_speed += FAN_SPEED_MIN;
564 int bbc_envctrl_init(void)
566 struct linux_ebus_child *echild;
571 while ((echild = bbc_i2c_getdev(devidx++)) != NULL) {
572 if (!strcmp(echild->prom_node->name, "temperature"))
573 attach_one_temp(echild, temp_index++);
574 if (!strcmp(echild->prom_node->name, "fan-control"))
575 attach_one_fan(echild, fan_index++);
577 if (temp_index != 0 && fan_index != 0) {
578 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
579 if (IS_ERR(kenvctrld_task))
580 return PTR_ERR(kenvctrld_task);
586 static void destroy_one_temp(struct bbc_cpu_temperature *tp)
588 bbc_i2c_detach(tp->client);
592 static void destroy_one_fan(struct bbc_fan_control *fp)
594 bbc_i2c_detach(fp->client);
598 void bbc_envctrl_cleanup(void)
600 struct bbc_cpu_temperature *tp;
601 struct bbc_fan_control *fp;
603 kthread_stop(kenvctrld_task);
607 struct bbc_cpu_temperature *next = tp->next;
608 destroy_one_temp(tp);
611 all_bbc_temps = NULL;
615 struct bbc_fan_control *next = fp->next;