ia64/linux-2.6.18-xen.hg

annotate drivers/macintosh/therm_pm72.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 04 10:32:57 2009 +0100 (2009-06-04)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * Device driver for the thermostats & fan controller of the
ian@0 3 * Apple G5 "PowerMac7,2" desktop machines.
ian@0 4 *
ian@0 5 * (c) Copyright IBM Corp. 2003-2004
ian@0 6 *
ian@0 7 * Maintained by: Benjamin Herrenschmidt
ian@0 8 * <benh@kernel.crashing.org>
ian@0 9 *
ian@0 10 *
ian@0 11 * The algorithm used is the PID control algorithm, used the same
ian@0 12 * way the published Darwin code does, using the same values that
ian@0 13 * are present in the Darwin 7.0 snapshot property lists.
ian@0 14 *
ian@0 15 * As far as the CPUs control loops are concerned, I use the
ian@0 16 * calibration & PID constants provided by the EEPROM,
ian@0 17 * I do _not_ embed any value from the property lists, as the ones
ian@0 18 * provided by Darwin 7.0 seem to always have an older version that
ian@0 19 * what I've seen on the actual computers.
ian@0 20 * It would be interesting to verify that though. Darwin has a
ian@0 21 * version code of 1.0.0d11 for all control loops it seems, while
ian@0 22 * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
ian@0 23 *
ian@0 24 * Darwin doesn't provide source to all parts, some missing
ian@0 25 * bits like the AppleFCU driver or the actual scale of some
ian@0 26 * of the values returned by sensors had to be "guessed" some
ian@0 27 * way... or based on what Open Firmware does.
ian@0 28 *
ian@0 29 * I didn't yet figure out how to get the slots power consumption
ian@0 30 * out of the FCU, so that part has not been implemented yet and
ian@0 31 * the slots fan is set to a fixed 50% PWM, hoping this value is
ian@0 32 * safe enough ...
ian@0 33 *
ian@0 34 * Note: I have observed strange oscillations of the CPU control
ian@0 35 * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
ian@0 36 * oscillates slowly (over several minutes) between the minimum
ian@0 37 * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
ian@0 38 * this, it could be some incorrect constant or an error in the
ian@0 39 * way I ported the algorithm, or it could be just normal. I
ian@0 40 * don't have full understanding on the way Apple tweaked the PID
ian@0 41 * algorithm for the CPU control, it is definitely not a standard
ian@0 42 * implementation...
ian@0 43 *
ian@0 44 * TODO: - Check MPU structure version/signature
ian@0 45 * - Add things like /sbin/overtemp for non-critical
ian@0 46 * overtemp conditions so userland can take some policy
ian@0 47 * decisions, like slewing down CPUs
ian@0 48 * - Deal with fan and i2c failures in a better way
ian@0 49 * - Maybe do a generic PID based on params used for
ian@0 50 * U3 and Drives ? Definitely need to factor code a bit
ian@0 51 * bettter... also make sensor detection more robust using
ian@0 52 * the device-tree to probe for them
ian@0 53 * - Figure out how to get the slots consumption and set the
ian@0 54 * slots fan accordingly
ian@0 55 *
ian@0 56 * History:
ian@0 57 *
ian@0 58 * Nov. 13, 2003 : 0.5
ian@0 59 * - First release
ian@0 60 *
ian@0 61 * Nov. 14, 2003 : 0.6
ian@0 62 * - Read fan speed from FCU, low level fan routines now deal
ian@0 63 * with errors & check fan status, though higher level don't
ian@0 64 * do much.
ian@0 65 * - Move a bunch of definitions to .h file
ian@0 66 *
ian@0 67 * Nov. 18, 2003 : 0.7
ian@0 68 * - Fix build on ppc64 kernel
ian@0 69 * - Move back statics definitions to .c file
ian@0 70 * - Avoid calling schedule_timeout with a negative number
ian@0 71 *
ian@0 72 * Dec. 18, 2003 : 0.8
ian@0 73 * - Fix typo when reading back fan speed on 2 CPU machines
ian@0 74 *
ian@0 75 * Mar. 11, 2004 : 0.9
ian@0 76 * - Rework code accessing the ADC chips, make it more robust and
ian@0 77 * closer to the chip spec. Also make sure it is configured properly,
ian@0 78 * I've seen yet unexplained cases where on startup, I would have stale
ian@0 79 * values in the configuration register
ian@0 80 * - Switch back to use of target fan speed for PID, thus lowering
ian@0 81 * pressure on i2c
ian@0 82 *
ian@0 83 * Oct. 20, 2004 : 1.1
ian@0 84 * - Add device-tree lookup for fan IDs, should detect liquid cooling
ian@0 85 * pumps when present
ian@0 86 * - Enable driver for PowerMac7,3 machines
ian@0 87 * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
ian@0 88 * - Add new CPU cooling algorithm for machines with liquid cooling
ian@0 89 * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
ian@0 90 * - Fix a signed/unsigned compare issue in some PID loops
ian@0 91 *
ian@0 92 * Mar. 10, 2005 : 1.2
ian@0 93 * - Add basic support for Xserve G5
ian@0 94 * - Retreive pumps min/max from EEPROM image in device-tree (broken)
ian@0 95 * - Use min/max macros here or there
ian@0 96 * - Latest darwin updated U3H min fan speed to 20% PWM
ian@0 97 *
ian@0 98 * July. 06, 2006 : 1.3
ian@0 99 * - Fix setting of RPM fans on Xserve G5 (they were going too fast)
ian@0 100 * - Add missing slots fan control loop for Xserve G5
ian@0 101 * - Lower fixed slots fan speed from 50% to 40% on desktop G5s. We
ian@0 102 * still can't properly implement the control loop for these, so let's
ian@0 103 * reduce the noise a little bit, it appears that 40% still gives us
ian@0 104 * a pretty good air flow
ian@0 105 * - Add code to "tickle" the FCU regulary so it doesn't think that
ian@0 106 * we are gone while in fact, the machine just didn't need any fan
ian@0 107 * speed change lately
ian@0 108 *
ian@0 109 */
ian@0 110
ian@0 111 #include <linux/types.h>
ian@0 112 #include <linux/module.h>
ian@0 113 #include <linux/errno.h>
ian@0 114 #include <linux/kernel.h>
ian@0 115 #include <linux/delay.h>
ian@0 116 #include <linux/sched.h>
ian@0 117 #include <linux/slab.h>
ian@0 118 #include <linux/init.h>
ian@0 119 #include <linux/spinlock.h>
ian@0 120 #include <linux/smp_lock.h>
ian@0 121 #include <linux/wait.h>
ian@0 122 #include <linux/reboot.h>
ian@0 123 #include <linux/kmod.h>
ian@0 124 #include <linux/i2c.h>
ian@0 125 #include <asm/prom.h>
ian@0 126 #include <asm/machdep.h>
ian@0 127 #include <asm/io.h>
ian@0 128 #include <asm/system.h>
ian@0 129 #include <asm/sections.h>
ian@0 130 #include <asm/of_device.h>
ian@0 131 #include <asm/macio.h>
ian@0 132
ian@0 133 #include "therm_pm72.h"
ian@0 134
ian@0 135 #define VERSION "1.3"
ian@0 136
ian@0 137 #undef DEBUG
ian@0 138
ian@0 139 #ifdef DEBUG
ian@0 140 #define DBG(args...) printk(args)
ian@0 141 #else
ian@0 142 #define DBG(args...) do { } while(0)
ian@0 143 #endif
ian@0 144
ian@0 145
ian@0 146 /*
ian@0 147 * Driver statics
ian@0 148 */
ian@0 149
ian@0 150 static struct of_device * of_dev;
ian@0 151 static struct i2c_adapter * u3_0;
ian@0 152 static struct i2c_adapter * u3_1;
ian@0 153 static struct i2c_adapter * k2;
ian@0 154 static struct i2c_client * fcu;
ian@0 155 static struct cpu_pid_state cpu_state[2];
ian@0 156 static struct basckside_pid_params backside_params;
ian@0 157 static struct backside_pid_state backside_state;
ian@0 158 static struct drives_pid_state drives_state;
ian@0 159 static struct dimm_pid_state dimms_state;
ian@0 160 static struct slots_pid_state slots_state;
ian@0 161 static int state;
ian@0 162 static int cpu_count;
ian@0 163 static int cpu_pid_type;
ian@0 164 static pid_t ctrl_task;
ian@0 165 static struct completion ctrl_complete;
ian@0 166 static int critical_state;
ian@0 167 static int rackmac;
ian@0 168 static s32 dimm_output_clamp;
ian@0 169 static int fcu_rpm_shift;
ian@0 170 static int fcu_tickle_ticks;
ian@0 171 static DECLARE_MUTEX(driver_lock);
ian@0 172
ian@0 173 /*
ian@0 174 * We have 3 types of CPU PID control. One is "split" old style control
ian@0 175 * for intake & exhaust fans, the other is "combined" control for both
ian@0 176 * CPUs that also deals with the pumps when present. To be "compatible"
ian@0 177 * with OS X at this point, we only use "COMBINED" on the machines that
ian@0 178 * are identified as having the pumps (though that identification is at
ian@0 179 * least dodgy). Ultimately, we could probably switch completely to this
ian@0 180 * algorithm provided we hack it to deal with the UP case
ian@0 181 */
ian@0 182 #define CPU_PID_TYPE_SPLIT 0
ian@0 183 #define CPU_PID_TYPE_COMBINED 1
ian@0 184 #define CPU_PID_TYPE_RACKMAC 2
ian@0 185
ian@0 186 /*
ian@0 187 * This table describes all fans in the FCU. The "id" and "type" values
ian@0 188 * are defaults valid for all earlier machines. Newer machines will
ian@0 189 * eventually override the table content based on the device-tree
ian@0 190 */
ian@0 191 struct fcu_fan_table
ian@0 192 {
ian@0 193 char* loc; /* location code */
ian@0 194 int type; /* 0 = rpm, 1 = pwm, 2 = pump */
ian@0 195 int id; /* id or -1 */
ian@0 196 };
ian@0 197
ian@0 198 #define FCU_FAN_RPM 0
ian@0 199 #define FCU_FAN_PWM 1
ian@0 200
ian@0 201 #define FCU_FAN_ABSENT_ID -1
ian@0 202
ian@0 203 #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
ian@0 204
ian@0 205 struct fcu_fan_table fcu_fans[] = {
ian@0 206 [BACKSIDE_FAN_PWM_INDEX] = {
ian@0 207 .loc = "BACKSIDE,SYS CTRLR FAN",
ian@0 208 .type = FCU_FAN_PWM,
ian@0 209 .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
ian@0 210 },
ian@0 211 [DRIVES_FAN_RPM_INDEX] = {
ian@0 212 .loc = "DRIVE BAY",
ian@0 213 .type = FCU_FAN_RPM,
ian@0 214 .id = DRIVES_FAN_RPM_DEFAULT_ID,
ian@0 215 },
ian@0 216 [SLOTS_FAN_PWM_INDEX] = {
ian@0 217 .loc = "SLOT,PCI FAN",
ian@0 218 .type = FCU_FAN_PWM,
ian@0 219 .id = SLOTS_FAN_PWM_DEFAULT_ID,
ian@0 220 },
ian@0 221 [CPUA_INTAKE_FAN_RPM_INDEX] = {
ian@0 222 .loc = "CPU A INTAKE",
ian@0 223 .type = FCU_FAN_RPM,
ian@0 224 .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
ian@0 225 },
ian@0 226 [CPUA_EXHAUST_FAN_RPM_INDEX] = {
ian@0 227 .loc = "CPU A EXHAUST",
ian@0 228 .type = FCU_FAN_RPM,
ian@0 229 .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
ian@0 230 },
ian@0 231 [CPUB_INTAKE_FAN_RPM_INDEX] = {
ian@0 232 .loc = "CPU B INTAKE",
ian@0 233 .type = FCU_FAN_RPM,
ian@0 234 .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
ian@0 235 },
ian@0 236 [CPUB_EXHAUST_FAN_RPM_INDEX] = {
ian@0 237 .loc = "CPU B EXHAUST",
ian@0 238 .type = FCU_FAN_RPM,
ian@0 239 .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
ian@0 240 },
ian@0 241 /* pumps aren't present by default, have to be looked up in the
ian@0 242 * device-tree
ian@0 243 */
ian@0 244 [CPUA_PUMP_RPM_INDEX] = {
ian@0 245 .loc = "CPU A PUMP",
ian@0 246 .type = FCU_FAN_RPM,
ian@0 247 .id = FCU_FAN_ABSENT_ID,
ian@0 248 },
ian@0 249 [CPUB_PUMP_RPM_INDEX] = {
ian@0 250 .loc = "CPU B PUMP",
ian@0 251 .type = FCU_FAN_RPM,
ian@0 252 .id = FCU_FAN_ABSENT_ID,
ian@0 253 },
ian@0 254 /* Xserve fans */
ian@0 255 [CPU_A1_FAN_RPM_INDEX] = {
ian@0 256 .loc = "CPU A 1",
ian@0 257 .type = FCU_FAN_RPM,
ian@0 258 .id = FCU_FAN_ABSENT_ID,
ian@0 259 },
ian@0 260 [CPU_A2_FAN_RPM_INDEX] = {
ian@0 261 .loc = "CPU A 2",
ian@0 262 .type = FCU_FAN_RPM,
ian@0 263 .id = FCU_FAN_ABSENT_ID,
ian@0 264 },
ian@0 265 [CPU_A3_FAN_RPM_INDEX] = {
ian@0 266 .loc = "CPU A 3",
ian@0 267 .type = FCU_FAN_RPM,
ian@0 268 .id = FCU_FAN_ABSENT_ID,
ian@0 269 },
ian@0 270 [CPU_B1_FAN_RPM_INDEX] = {
ian@0 271 .loc = "CPU B 1",
ian@0 272 .type = FCU_FAN_RPM,
ian@0 273 .id = FCU_FAN_ABSENT_ID,
ian@0 274 },
ian@0 275 [CPU_B2_FAN_RPM_INDEX] = {
ian@0 276 .loc = "CPU B 2",
ian@0 277 .type = FCU_FAN_RPM,
ian@0 278 .id = FCU_FAN_ABSENT_ID,
ian@0 279 },
ian@0 280 [CPU_B3_FAN_RPM_INDEX] = {
ian@0 281 .loc = "CPU B 3",
ian@0 282 .type = FCU_FAN_RPM,
ian@0 283 .id = FCU_FAN_ABSENT_ID,
ian@0 284 },
ian@0 285 };
ian@0 286
ian@0 287 /*
ian@0 288 * i2c_driver structure to attach to the host i2c controller
ian@0 289 */
ian@0 290
ian@0 291 static int therm_pm72_attach(struct i2c_adapter *adapter);
ian@0 292 static int therm_pm72_detach(struct i2c_adapter *adapter);
ian@0 293
ian@0 294 static struct i2c_driver therm_pm72_driver =
ian@0 295 {
ian@0 296 .driver = {
ian@0 297 .name = "therm_pm72",
ian@0 298 },
ian@0 299 .attach_adapter = therm_pm72_attach,
ian@0 300 .detach_adapter = therm_pm72_detach,
ian@0 301 };
ian@0 302
ian@0 303 /*
ian@0 304 * Utility function to create an i2c_client structure and
ian@0 305 * attach it to one of u3 adapters
ian@0 306 */
ian@0 307 static struct i2c_client *attach_i2c_chip(int id, const char *name)
ian@0 308 {
ian@0 309 struct i2c_client *clt;
ian@0 310 struct i2c_adapter *adap;
ian@0 311
ian@0 312 if (id & 0x200)
ian@0 313 adap = k2;
ian@0 314 else if (id & 0x100)
ian@0 315 adap = u3_1;
ian@0 316 else
ian@0 317 adap = u3_0;
ian@0 318 if (adap == NULL)
ian@0 319 return NULL;
ian@0 320
ian@0 321 clt = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
ian@0 322 if (clt == NULL)
ian@0 323 return NULL;
ian@0 324 memset(clt, 0, sizeof(struct i2c_client));
ian@0 325
ian@0 326 clt->addr = (id >> 1) & 0x7f;
ian@0 327 clt->adapter = adap;
ian@0 328 clt->driver = &therm_pm72_driver;
ian@0 329 strncpy(clt->name, name, I2C_NAME_SIZE-1);
ian@0 330
ian@0 331 if (i2c_attach_client(clt)) {
ian@0 332 printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
ian@0 333 kfree(clt);
ian@0 334 return NULL;
ian@0 335 }
ian@0 336 return clt;
ian@0 337 }
ian@0 338
ian@0 339 /*
ian@0 340 * Utility function to get rid of the i2c_client structure
ian@0 341 * (will also detach from the adapter hopepfully)
ian@0 342 */
ian@0 343 static void detach_i2c_chip(struct i2c_client *clt)
ian@0 344 {
ian@0 345 i2c_detach_client(clt);
ian@0 346 kfree(clt);
ian@0 347 }
ian@0 348
ian@0 349 /*
ian@0 350 * Here are the i2c chip access wrappers
ian@0 351 */
ian@0 352
ian@0 353 static void initialize_adc(struct cpu_pid_state *state)
ian@0 354 {
ian@0 355 int rc;
ian@0 356 u8 buf[2];
ian@0 357
ian@0 358 /* Read ADC the configuration register and cache it. We
ian@0 359 * also make sure Config2 contains proper values, I've seen
ian@0 360 * cases where we got stale grabage in there, thus preventing
ian@0 361 * proper reading of conv. values
ian@0 362 */
ian@0 363
ian@0 364 /* Clear Config2 */
ian@0 365 buf[0] = 5;
ian@0 366 buf[1] = 0;
ian@0 367 i2c_master_send(state->monitor, buf, 2);
ian@0 368
ian@0 369 /* Read & cache Config1 */
ian@0 370 buf[0] = 1;
ian@0 371 rc = i2c_master_send(state->monitor, buf, 1);
ian@0 372 if (rc > 0) {
ian@0 373 rc = i2c_master_recv(state->monitor, buf, 1);
ian@0 374 if (rc > 0) {
ian@0 375 state->adc_config = buf[0];
ian@0 376 DBG("ADC config reg: %02x\n", state->adc_config);
ian@0 377 /* Disable shutdown mode */
ian@0 378 state->adc_config &= 0xfe;
ian@0 379 buf[0] = 1;
ian@0 380 buf[1] = state->adc_config;
ian@0 381 rc = i2c_master_send(state->monitor, buf, 2);
ian@0 382 }
ian@0 383 }
ian@0 384 if (rc <= 0)
ian@0 385 printk(KERN_ERR "therm_pm72: Error reading ADC config"
ian@0 386 " register !\n");
ian@0 387 }
ian@0 388
ian@0 389 static int read_smon_adc(struct cpu_pid_state *state, int chan)
ian@0 390 {
ian@0 391 int rc, data, tries = 0;
ian@0 392 u8 buf[2];
ian@0 393
ian@0 394 for (;;) {
ian@0 395 /* Set channel */
ian@0 396 buf[0] = 1;
ian@0 397 buf[1] = (state->adc_config & 0x1f) | (chan << 5);
ian@0 398 rc = i2c_master_send(state->monitor, buf, 2);
ian@0 399 if (rc <= 0)
ian@0 400 goto error;
ian@0 401 /* Wait for convertion */
ian@0 402 msleep(1);
ian@0 403 /* Switch to data register */
ian@0 404 buf[0] = 4;
ian@0 405 rc = i2c_master_send(state->monitor, buf, 1);
ian@0 406 if (rc <= 0)
ian@0 407 goto error;
ian@0 408 /* Read result */
ian@0 409 rc = i2c_master_recv(state->monitor, buf, 2);
ian@0 410 if (rc < 0)
ian@0 411 goto error;
ian@0 412 data = ((u16)buf[0]) << 8 | (u16)buf[1];
ian@0 413 return data >> 6;
ian@0 414 error:
ian@0 415 DBG("Error reading ADC, retrying...\n");
ian@0 416 if (++tries > 10) {
ian@0 417 printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
ian@0 418 return -1;
ian@0 419 }
ian@0 420 msleep(10);
ian@0 421 }
ian@0 422 }
ian@0 423
ian@0 424 static int read_lm87_reg(struct i2c_client * chip, int reg)
ian@0 425 {
ian@0 426 int rc, tries = 0;
ian@0 427 u8 buf;
ian@0 428
ian@0 429 for (;;) {
ian@0 430 /* Set address */
ian@0 431 buf = (u8)reg;
ian@0 432 rc = i2c_master_send(chip, &buf, 1);
ian@0 433 if (rc <= 0)
ian@0 434 goto error;
ian@0 435 rc = i2c_master_recv(chip, &buf, 1);
ian@0 436 if (rc <= 0)
ian@0 437 goto error;
ian@0 438 return (int)buf;
ian@0 439 error:
ian@0 440 DBG("Error reading LM87, retrying...\n");
ian@0 441 if (++tries > 10) {
ian@0 442 printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
ian@0 443 return -1;
ian@0 444 }
ian@0 445 msleep(10);
ian@0 446 }
ian@0 447 }
ian@0 448
ian@0 449 static int fan_read_reg(int reg, unsigned char *buf, int nb)
ian@0 450 {
ian@0 451 int tries, nr, nw;
ian@0 452
ian@0 453 buf[0] = reg;
ian@0 454 tries = 0;
ian@0 455 for (;;) {
ian@0 456 nw = i2c_master_send(fcu, buf, 1);
ian@0 457 if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
ian@0 458 break;
ian@0 459 msleep(10);
ian@0 460 ++tries;
ian@0 461 }
ian@0 462 if (nw <= 0) {
ian@0 463 printk(KERN_ERR "Failure writing address to FCU: %d", nw);
ian@0 464 return -EIO;
ian@0 465 }
ian@0 466 tries = 0;
ian@0 467 for (;;) {
ian@0 468 nr = i2c_master_recv(fcu, buf, nb);
ian@0 469 if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
ian@0 470 break;
ian@0 471 msleep(10);
ian@0 472 ++tries;
ian@0 473 }
ian@0 474 if (nr <= 0)
ian@0 475 printk(KERN_ERR "Failure reading data from FCU: %d", nw);
ian@0 476 return nr;
ian@0 477 }
ian@0 478
ian@0 479 static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
ian@0 480 {
ian@0 481 int tries, nw;
ian@0 482 unsigned char buf[16];
ian@0 483
ian@0 484 buf[0] = reg;
ian@0 485 memcpy(buf+1, ptr, nb);
ian@0 486 ++nb;
ian@0 487 tries = 0;
ian@0 488 for (;;) {
ian@0 489 nw = i2c_master_send(fcu, buf, nb);
ian@0 490 if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
ian@0 491 break;
ian@0 492 msleep(10);
ian@0 493 ++tries;
ian@0 494 }
ian@0 495 if (nw < 0)
ian@0 496 printk(KERN_ERR "Failure writing to FCU: %d", nw);
ian@0 497 return nw;
ian@0 498 }
ian@0 499
ian@0 500 static int start_fcu(void)
ian@0 501 {
ian@0 502 unsigned char buf = 0xff;
ian@0 503 int rc;
ian@0 504
ian@0 505 rc = fan_write_reg(0xe, &buf, 1);
ian@0 506 if (rc < 0)
ian@0 507 return -EIO;
ian@0 508 rc = fan_write_reg(0x2e, &buf, 1);
ian@0 509 if (rc < 0)
ian@0 510 return -EIO;
ian@0 511 rc = fan_read_reg(0, &buf, 1);
ian@0 512 if (rc < 0)
ian@0 513 return -EIO;
ian@0 514 fcu_rpm_shift = (buf == 1) ? 2 : 3;
ian@0 515 printk(KERN_DEBUG "FCU Initialized, RPM fan shift is %d\n",
ian@0 516 fcu_rpm_shift);
ian@0 517
ian@0 518 return 0;
ian@0 519 }
ian@0 520
ian@0 521 static int set_rpm_fan(int fan_index, int rpm)
ian@0 522 {
ian@0 523 unsigned char buf[2];
ian@0 524 int rc, id, min, max;
ian@0 525
ian@0 526 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
ian@0 527 return -EINVAL;
ian@0 528 id = fcu_fans[fan_index].id;
ian@0 529 if (id == FCU_FAN_ABSENT_ID)
ian@0 530 return -EINVAL;
ian@0 531
ian@0 532 min = 2400 >> fcu_rpm_shift;
ian@0 533 max = 56000 >> fcu_rpm_shift;
ian@0 534
ian@0 535 if (rpm < min)
ian@0 536 rpm = min;
ian@0 537 else if (rpm > max)
ian@0 538 rpm = max;
ian@0 539 buf[0] = rpm >> (8 - fcu_rpm_shift);
ian@0 540 buf[1] = rpm << fcu_rpm_shift;
ian@0 541 rc = fan_write_reg(0x10 + (id * 2), buf, 2);
ian@0 542 if (rc < 0)
ian@0 543 return -EIO;
ian@0 544 return 0;
ian@0 545 }
ian@0 546
ian@0 547 static int get_rpm_fan(int fan_index, int programmed)
ian@0 548 {
ian@0 549 unsigned char failure;
ian@0 550 unsigned char active;
ian@0 551 unsigned char buf[2];
ian@0 552 int rc, id, reg_base;
ian@0 553
ian@0 554 if (fcu_fans[fan_index].type != FCU_FAN_RPM)
ian@0 555 return -EINVAL;
ian@0 556 id = fcu_fans[fan_index].id;
ian@0 557 if (id == FCU_FAN_ABSENT_ID)
ian@0 558 return -EINVAL;
ian@0 559
ian@0 560 rc = fan_read_reg(0xb, &failure, 1);
ian@0 561 if (rc != 1)
ian@0 562 return -EIO;
ian@0 563 if ((failure & (1 << id)) != 0)
ian@0 564 return -EFAULT;
ian@0 565 rc = fan_read_reg(0xd, &active, 1);
ian@0 566 if (rc != 1)
ian@0 567 return -EIO;
ian@0 568 if ((active & (1 << id)) == 0)
ian@0 569 return -ENXIO;
ian@0 570
ian@0 571 /* Programmed value or real current speed */
ian@0 572 reg_base = programmed ? 0x10 : 0x11;
ian@0 573 rc = fan_read_reg(reg_base + (id * 2), buf, 2);
ian@0 574 if (rc != 2)
ian@0 575 return -EIO;
ian@0 576
ian@0 577 return (buf[0] << (8 - fcu_rpm_shift)) | buf[1] >> fcu_rpm_shift;
ian@0 578 }
ian@0 579
ian@0 580 static int set_pwm_fan(int fan_index, int pwm)
ian@0 581 {
ian@0 582 unsigned char buf[2];
ian@0 583 int rc, id;
ian@0 584
ian@0 585 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
ian@0 586 return -EINVAL;
ian@0 587 id = fcu_fans[fan_index].id;
ian@0 588 if (id == FCU_FAN_ABSENT_ID)
ian@0 589 return -EINVAL;
ian@0 590
ian@0 591 if (pwm < 10)
ian@0 592 pwm = 10;
ian@0 593 else if (pwm > 100)
ian@0 594 pwm = 100;
ian@0 595 pwm = (pwm * 2559) / 1000;
ian@0 596 buf[0] = pwm;
ian@0 597 rc = fan_write_reg(0x30 + (id * 2), buf, 1);
ian@0 598 if (rc < 0)
ian@0 599 return rc;
ian@0 600 return 0;
ian@0 601 }
ian@0 602
ian@0 603 static int get_pwm_fan(int fan_index)
ian@0 604 {
ian@0 605 unsigned char failure;
ian@0 606 unsigned char active;
ian@0 607 unsigned char buf[2];
ian@0 608 int rc, id;
ian@0 609
ian@0 610 if (fcu_fans[fan_index].type != FCU_FAN_PWM)
ian@0 611 return -EINVAL;
ian@0 612 id = fcu_fans[fan_index].id;
ian@0 613 if (id == FCU_FAN_ABSENT_ID)
ian@0 614 return -EINVAL;
ian@0 615
ian@0 616 rc = fan_read_reg(0x2b, &failure, 1);
ian@0 617 if (rc != 1)
ian@0 618 return -EIO;
ian@0 619 if ((failure & (1 << id)) != 0)
ian@0 620 return -EFAULT;
ian@0 621 rc = fan_read_reg(0x2d, &active, 1);
ian@0 622 if (rc != 1)
ian@0 623 return -EIO;
ian@0 624 if ((active & (1 << id)) == 0)
ian@0 625 return -ENXIO;
ian@0 626
ian@0 627 /* Programmed value or real current speed */
ian@0 628 rc = fan_read_reg(0x30 + (id * 2), buf, 1);
ian@0 629 if (rc != 1)
ian@0 630 return -EIO;
ian@0 631
ian@0 632 return (buf[0] * 1000) / 2559;
ian@0 633 }
ian@0 634
ian@0 635 static void tickle_fcu(void)
ian@0 636 {
ian@0 637 int pwm;
ian@0 638
ian@0 639 pwm = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
ian@0 640
ian@0 641 DBG("FCU Tickle, slots fan is: %d\n", pwm);
ian@0 642 if (pwm < 0)
ian@0 643 pwm = 100;
ian@0 644
ian@0 645 if (!rackmac) {
ian@0 646 pwm = SLOTS_FAN_DEFAULT_PWM;
ian@0 647 } else if (pwm < SLOTS_PID_OUTPUT_MIN)
ian@0 648 pwm = SLOTS_PID_OUTPUT_MIN;
ian@0 649
ian@0 650 /* That is hopefully enough to make the FCU happy */
ian@0 651 set_pwm_fan(SLOTS_FAN_PWM_INDEX, pwm);
ian@0 652 }
ian@0 653
ian@0 654
ian@0 655 /*
ian@0 656 * Utility routine to read the CPU calibration EEPROM data
ian@0 657 * from the device-tree
ian@0 658 */
ian@0 659 static int read_eeprom(int cpu, struct mpu_data *out)
ian@0 660 {
ian@0 661 struct device_node *np;
ian@0 662 char nodename[64];
ian@0 663 u8 *data;
ian@0 664 int len;
ian@0 665
ian@0 666 /* prom.c routine for finding a node by path is a bit brain dead
ian@0 667 * and requires exact @xxx unit numbers. This is a bit ugly but
ian@0 668 * will work for these machines
ian@0 669 */
ian@0 670 sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
ian@0 671 np = of_find_node_by_path(nodename);
ian@0 672 if (np == NULL) {
ian@0 673 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid node from device-tree\n");
ian@0 674 return -ENODEV;
ian@0 675 }
ian@0 676 data = (u8 *)get_property(np, "cpuid", &len);
ian@0 677 if (data == NULL) {
ian@0 678 printk(KERN_ERR "therm_pm72: Failed to retrieve cpuid property from device-tree\n");
ian@0 679 of_node_put(np);
ian@0 680 return -ENODEV;
ian@0 681 }
ian@0 682 memcpy(out, data, sizeof(struct mpu_data));
ian@0 683 of_node_put(np);
ian@0 684
ian@0 685 return 0;
ian@0 686 }
ian@0 687
ian@0 688 static void fetch_cpu_pumps_minmax(void)
ian@0 689 {
ian@0 690 struct cpu_pid_state *state0 = &cpu_state[0];
ian@0 691 struct cpu_pid_state *state1 = &cpu_state[1];
ian@0 692 u16 pump_min = 0, pump_max = 0xffff;
ian@0 693 u16 tmp[4];
ian@0 694
ian@0 695 /* Try to fetch pumps min/max infos from eeprom */
ian@0 696
ian@0 697 memcpy(&tmp, &state0->mpu.processor_part_num, 8);
ian@0 698 if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
ian@0 699 pump_min = max(pump_min, tmp[0]);
ian@0 700 pump_max = min(pump_max, tmp[1]);
ian@0 701 }
ian@0 702 if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
ian@0 703 pump_min = max(pump_min, tmp[2]);
ian@0 704 pump_max = min(pump_max, tmp[3]);
ian@0 705 }
ian@0 706
ian@0 707 /* Double check the values, this _IS_ needed as the EEPROM on
ian@0 708 * some dual 2.5Ghz G5s seem, at least, to have both min & max
ian@0 709 * same to the same value ... (grrrr)
ian@0 710 */
ian@0 711 if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
ian@0 712 pump_min = CPU_PUMP_OUTPUT_MIN;
ian@0 713 pump_max = CPU_PUMP_OUTPUT_MAX;
ian@0 714 }
ian@0 715
ian@0 716 state0->pump_min = state1->pump_min = pump_min;
ian@0 717 state0->pump_max = state1->pump_max = pump_max;
ian@0 718 }
ian@0 719
ian@0 720 /*
ian@0 721 * Now, unfortunately, sysfs doesn't give us a nice void * we could
ian@0 722 * pass around to the attribute functions, so we don't really have
ian@0 723 * choice but implement a bunch of them...
ian@0 724 *
ian@0 725 * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
ian@0 726 * the input twice... I accept patches :)
ian@0 727 */
ian@0 728 #define BUILD_SHOW_FUNC_FIX(name, data) \
ian@0 729 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
ian@0 730 { \
ian@0 731 ssize_t r; \
ian@0 732 down(&driver_lock); \
ian@0 733 r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
ian@0 734 up(&driver_lock); \
ian@0 735 return r; \
ian@0 736 }
ian@0 737 #define BUILD_SHOW_FUNC_INT(name, data) \
ian@0 738 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
ian@0 739 { \
ian@0 740 return sprintf(buf, "%d", data); \
ian@0 741 }
ian@0 742
ian@0 743 BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
ian@0 744 BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
ian@0 745 BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
ian@0 746 BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
ian@0 747 BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
ian@0 748
ian@0 749 BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
ian@0 750 BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
ian@0 751 BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
ian@0 752 BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
ian@0 753 BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
ian@0 754
ian@0 755 BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
ian@0 756 BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
ian@0 757
ian@0 758 BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
ian@0 759 BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
ian@0 760
ian@0 761 BUILD_SHOW_FUNC_FIX(slots_temperature, slots_state.last_temp)
ian@0 762 BUILD_SHOW_FUNC_INT(slots_fan_pwm, slots_state.pwm)
ian@0 763
ian@0 764 BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
ian@0 765
ian@0 766 static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
ian@0 767 static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
ian@0 768 static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
ian@0 769 static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
ian@0 770 static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
ian@0 771
ian@0 772 static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
ian@0 773 static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
ian@0 774 static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
ian@0 775 static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
ian@0 776 static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
ian@0 777
ian@0 778 static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
ian@0 779 static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
ian@0 780
ian@0 781 static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
ian@0 782 static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
ian@0 783
ian@0 784 static DEVICE_ATTR(slots_temperature,S_IRUGO,show_slots_temperature,NULL);
ian@0 785 static DEVICE_ATTR(slots_fan_pwm,S_IRUGO,show_slots_fan_pwm,NULL);
ian@0 786
ian@0 787 static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
ian@0 788
ian@0 789 /*
ian@0 790 * CPUs fans control loop
ian@0 791 */
ian@0 792
ian@0 793 static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
ian@0 794 {
ian@0 795 s32 ltemp, volts, amps;
ian@0 796 int index, rc = 0;
ian@0 797
ian@0 798 /* Default (in case of error) */
ian@0 799 *temp = state->cur_temp;
ian@0 800 *power = state->cur_power;
ian@0 801
ian@0 802 if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
ian@0 803 index = (state->index == 0) ?
ian@0 804 CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
ian@0 805 else
ian@0 806 index = (state->index == 0) ?
ian@0 807 CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
ian@0 808
ian@0 809 /* Read current fan status */
ian@0 810 rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
ian@0 811 if (rc < 0) {
ian@0 812 /* XXX What do we do now ? Nothing for now, keep old value, but
ian@0 813 * return error upstream
ian@0 814 */
ian@0 815 DBG(" cpu %d, fan reading error !\n", state->index);
ian@0 816 } else {
ian@0 817 state->rpm = rc;
ian@0 818 DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
ian@0 819 }
ian@0 820
ian@0 821 /* Get some sensor readings and scale it */
ian@0 822 ltemp = read_smon_adc(state, 1);
ian@0 823 if (ltemp == -1) {
ian@0 824 /* XXX What do we do now ? */
ian@0 825 state->overtemp++;
ian@0 826 if (rc == 0)
ian@0 827 rc = -EIO;
ian@0 828 DBG(" cpu %d, temp reading error !\n", state->index);
ian@0 829 } else {
ian@0 830 /* Fixup temperature according to diode calibration
ian@0 831 */
ian@0 832 DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
ian@0 833 state->index,
ian@0 834 ltemp, state->mpu.mdiode, state->mpu.bdiode);
ian@0 835 *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
ian@0 836 state->last_temp = *temp;
ian@0 837 DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
ian@0 838 }
ian@0 839
ian@0 840 /*
ian@0 841 * Read voltage & current and calculate power
ian@0 842 */
ian@0 843 volts = read_smon_adc(state, 3);
ian@0 844 amps = read_smon_adc(state, 4);
ian@0 845
ian@0 846 /* Scale voltage and current raw sensor values according to fixed scales
ian@0 847 * obtained in Darwin and calculate power from I and V
ian@0 848 */
ian@0 849 volts *= ADC_CPU_VOLTAGE_SCALE;
ian@0 850 amps *= ADC_CPU_CURRENT_SCALE;
ian@0 851 *power = (((u64)volts) * ((u64)amps)) >> 16;
ian@0 852 state->voltage = volts;
ian@0 853 state->current_a = amps;
ian@0 854 state->last_power = *power;
ian@0 855
ian@0 856 DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
ian@0 857 state->index, FIX32TOPRINT(state->current_a),
ian@0 858 FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
ian@0 859
ian@0 860 return 0;
ian@0 861 }
ian@0 862
ian@0 863 static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
ian@0 864 {
ian@0 865 s32 power_target, integral, derivative, proportional, adj_in_target, sval;
ian@0 866 s64 integ_p, deriv_p, prop_p, sum;
ian@0 867 int i;
ian@0 868
ian@0 869 /* Calculate power target value (could be done once for all)
ian@0 870 * and convert to a 16.16 fp number
ian@0 871 */
ian@0 872 power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
ian@0 873 DBG(" power target: %d.%03d, error: %d.%03d\n",
ian@0 874 FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
ian@0 875
ian@0 876 /* Store temperature and power in history array */
ian@0 877 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
ian@0 878 state->temp_history[state->cur_temp] = temp;
ian@0 879 state->cur_power = (state->cur_power + 1) % state->count_power;
ian@0 880 state->power_history[state->cur_power] = power;
ian@0 881 state->error_history[state->cur_power] = power_target - power;
ian@0 882
ian@0 883 /* If first loop, fill the history table */
ian@0 884 if (state->first) {
ian@0 885 for (i = 0; i < (state->count_power - 1); i++) {
ian@0 886 state->cur_power = (state->cur_power + 1) % state->count_power;
ian@0 887 state->power_history[state->cur_power] = power;
ian@0 888 state->error_history[state->cur_power] = power_target - power;
ian@0 889 }
ian@0 890 for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
ian@0 891 state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
ian@0 892 state->temp_history[state->cur_temp] = temp;
ian@0 893 }
ian@0 894 state->first = 0;
ian@0 895 }
ian@0 896
ian@0 897 /* Calculate the integral term normally based on the "power" values */
ian@0 898 sum = 0;
ian@0 899 integral = 0;
ian@0 900 for (i = 0; i < state->count_power; i++)
ian@0 901 integral += state->error_history[i];
ian@0 902 integral *= CPU_PID_INTERVAL;
ian@0 903 DBG(" integral: %08x\n", integral);
ian@0 904
ian@0 905 /* Calculate the adjusted input (sense value).
ian@0 906 * G_r is 12.20
ian@0 907 * integ is 16.16
ian@0 908 * so the result is 28.36
ian@0 909 *
ian@0 910 * input target is mpu.ttarget, input max is mpu.tmax
ian@0 911 */
ian@0 912 integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
ian@0 913 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
ian@0 914 sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
ian@0 915 adj_in_target = (state->mpu.ttarget << 16);
ian@0 916 if (adj_in_target > sval)
ian@0 917 adj_in_target = sval;
ian@0 918 DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
ian@0 919 state->mpu.ttarget);
ian@0 920
ian@0 921 /* Calculate the derivative term */
ian@0 922 derivative = state->temp_history[state->cur_temp] -
ian@0 923 state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
ian@0 924 % CPU_TEMP_HISTORY_SIZE];
ian@0 925 derivative /= CPU_PID_INTERVAL;
ian@0 926 deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
ian@0 927 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
ian@0 928 sum += deriv_p;
ian@0 929
ian@0 930 /* Calculate the proportional term */
ian@0 931 proportional = temp - adj_in_target;
ian@0 932 prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
ian@0 933 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
ian@0 934 sum += prop_p;
ian@0 935
ian@0 936 /* Scale sum */
ian@0 937 sum >>= 36;
ian@0 938
ian@0 939 DBG(" sum: %d\n", (int)sum);
ian@0 940 state->rpm += (s32)sum;
ian@0 941 }
ian@0 942
ian@0 943 static void do_monitor_cpu_combined(void)
ian@0 944 {
ian@0 945 struct cpu_pid_state *state0 = &cpu_state[0];
ian@0 946 struct cpu_pid_state *state1 = &cpu_state[1];
ian@0 947 s32 temp0, power0, temp1, power1;
ian@0 948 s32 temp_combi, power_combi;
ian@0 949 int rc, intake, pump;
ian@0 950
ian@0 951 rc = do_read_one_cpu_values(state0, &temp0, &power0);
ian@0 952 if (rc < 0) {
ian@0 953 /* XXX What do we do now ? */
ian@0 954 }
ian@0 955 state1->overtemp = 0;
ian@0 956 rc = do_read_one_cpu_values(state1, &temp1, &power1);
ian@0 957 if (rc < 0) {
ian@0 958 /* XXX What do we do now ? */
ian@0 959 }
ian@0 960 if (state1->overtemp)
ian@0 961 state0->overtemp++;
ian@0 962
ian@0 963 temp_combi = max(temp0, temp1);
ian@0 964 power_combi = max(power0, power1);
ian@0 965
ian@0 966 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
ian@0 967 * full blown immediately and try to trigger a shutdown
ian@0 968 */
ian@0 969 if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
ian@0 970 printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
ian@0 971 temp_combi >> 16);
ian@0 972 state0->overtemp += CPU_MAX_OVERTEMP / 4;
ian@0 973 } else if (temp_combi > (state0->mpu.tmax << 16))
ian@0 974 state0->overtemp++;
ian@0 975 else
ian@0 976 state0->overtemp = 0;
ian@0 977 if (state0->overtemp >= CPU_MAX_OVERTEMP)
ian@0 978 critical_state = 1;
ian@0 979 if (state0->overtemp > 0) {
ian@0 980 state0->rpm = state0->mpu.rmaxn_exhaust_fan;
ian@0 981 state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
ian@0 982 pump = state0->pump_max;
ian@0 983 goto do_set_fans;
ian@0 984 }
ian@0 985
ian@0 986 /* Do the PID */
ian@0 987 do_cpu_pid(state0, temp_combi, power_combi);
ian@0 988
ian@0 989 /* Range check */
ian@0 990 state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
ian@0 991 state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
ian@0 992
ian@0 993 /* Calculate intake fan speed */
ian@0 994 intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
ian@0 995 intake = max(intake, (int)state0->mpu.rminn_intake_fan);
ian@0 996 intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
ian@0 997 state0->intake_rpm = intake;
ian@0 998
ian@0 999 /* Calculate pump speed */
ian@0 1000 pump = (state0->rpm * state0->pump_max) /
ian@0 1001 state0->mpu.rmaxn_exhaust_fan;
ian@0 1002 pump = min(pump, state0->pump_max);
ian@0 1003 pump = max(pump, state0->pump_min);
ian@0 1004
ian@0 1005 do_set_fans:
ian@0 1006 /* We copy values from state 0 to state 1 for /sysfs */
ian@0 1007 state1->rpm = state0->rpm;
ian@0 1008 state1->intake_rpm = state0->intake_rpm;
ian@0 1009
ian@0 1010 DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
ian@0 1011 state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
ian@0 1012
ian@0 1013 /* We should check for errors, shouldn't we ? But then, what
ian@0 1014 * do we do once the error occurs ? For FCU notified fan
ian@0 1015 * failures (-EFAULT) we probably want to notify userland
ian@0 1016 * some way...
ian@0 1017 */
ian@0 1018 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
ian@0 1019 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
ian@0 1020 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
ian@0 1021 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
ian@0 1022
ian@0 1023 if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
ian@0 1024 set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
ian@0 1025 if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
ian@0 1026 set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
ian@0 1027 }
ian@0 1028
ian@0 1029 static void do_monitor_cpu_split(struct cpu_pid_state *state)
ian@0 1030 {
ian@0 1031 s32 temp, power;
ian@0 1032 int rc, intake;
ian@0 1033
ian@0 1034 /* Read current fan status */
ian@0 1035 rc = do_read_one_cpu_values(state, &temp, &power);
ian@0 1036 if (rc < 0) {
ian@0 1037 /* XXX What do we do now ? */
ian@0 1038 }
ian@0 1039
ian@0 1040 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
ian@0 1041 * full blown immediately and try to trigger a shutdown
ian@0 1042 */
ian@0 1043 if (temp >= ((state->mpu.tmax + 8) << 16)) {
ian@0 1044 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
ian@0 1045 " (%d) !\n",
ian@0 1046 state->index, temp >> 16);
ian@0 1047 state->overtemp += CPU_MAX_OVERTEMP / 4;
ian@0 1048 } else if (temp > (state->mpu.tmax << 16))
ian@0 1049 state->overtemp++;
ian@0 1050 else
ian@0 1051 state->overtemp = 0;
ian@0 1052 if (state->overtemp >= CPU_MAX_OVERTEMP)
ian@0 1053 critical_state = 1;
ian@0 1054 if (state->overtemp > 0) {
ian@0 1055 state->rpm = state->mpu.rmaxn_exhaust_fan;
ian@0 1056 state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
ian@0 1057 goto do_set_fans;
ian@0 1058 }
ian@0 1059
ian@0 1060 /* Do the PID */
ian@0 1061 do_cpu_pid(state, temp, power);
ian@0 1062
ian@0 1063 /* Range check */
ian@0 1064 state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
ian@0 1065 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
ian@0 1066
ian@0 1067 /* Calculate intake fan */
ian@0 1068 intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
ian@0 1069 intake = max(intake, (int)state->mpu.rminn_intake_fan);
ian@0 1070 intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
ian@0 1071 state->intake_rpm = intake;
ian@0 1072
ian@0 1073 do_set_fans:
ian@0 1074 DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
ian@0 1075 state->index, (int)state->rpm, intake, state->overtemp);
ian@0 1076
ian@0 1077 /* We should check for errors, shouldn't we ? But then, what
ian@0 1078 * do we do once the error occurs ? For FCU notified fan
ian@0 1079 * failures (-EFAULT) we probably want to notify userland
ian@0 1080 * some way...
ian@0 1081 */
ian@0 1082 if (state->index == 0) {
ian@0 1083 set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
ian@0 1084 set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
ian@0 1085 } else {
ian@0 1086 set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
ian@0 1087 set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
ian@0 1088 }
ian@0 1089 }
ian@0 1090
ian@0 1091 static void do_monitor_cpu_rack(struct cpu_pid_state *state)
ian@0 1092 {
ian@0 1093 s32 temp, power, fan_min;
ian@0 1094 int rc;
ian@0 1095
ian@0 1096 /* Read current fan status */
ian@0 1097 rc = do_read_one_cpu_values(state, &temp, &power);
ian@0 1098 if (rc < 0) {
ian@0 1099 /* XXX What do we do now ? */
ian@0 1100 }
ian@0 1101
ian@0 1102 /* Check tmax, increment overtemp if we are there. At tmax+8, we go
ian@0 1103 * full blown immediately and try to trigger a shutdown
ian@0 1104 */
ian@0 1105 if (temp >= ((state->mpu.tmax + 8) << 16)) {
ian@0 1106 printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
ian@0 1107 " (%d) !\n",
ian@0 1108 state->index, temp >> 16);
ian@0 1109 state->overtemp = CPU_MAX_OVERTEMP / 4;
ian@0 1110 } else if (temp > (state->mpu.tmax << 16))
ian@0 1111 state->overtemp++;
ian@0 1112 else
ian@0 1113 state->overtemp = 0;
ian@0 1114 if (state->overtemp >= CPU_MAX_OVERTEMP)
ian@0 1115 critical_state = 1;
ian@0 1116 if (state->overtemp > 0) {
ian@0 1117 state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
ian@0 1118 goto do_set_fans;
ian@0 1119 }
ian@0 1120
ian@0 1121 /* Do the PID */
ian@0 1122 do_cpu_pid(state, temp, power);
ian@0 1123
ian@0 1124 /* Check clamp from dimms */
ian@0 1125 fan_min = dimm_output_clamp;
ian@0 1126 fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
ian@0 1127
ian@0 1128 DBG(" CPU min mpu = %d, min dimm = %d\n",
ian@0 1129 state->mpu.rminn_intake_fan, dimm_output_clamp);
ian@0 1130
ian@0 1131 state->rpm = max(state->rpm, (int)fan_min);
ian@0 1132 state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
ian@0 1133 state->intake_rpm = state->rpm;
ian@0 1134
ian@0 1135 do_set_fans:
ian@0 1136 DBG("** CPU %d RPM: %d overtemp: %d\n",
ian@0 1137 state->index, (int)state->rpm, state->overtemp);
ian@0 1138
ian@0 1139 /* We should check for errors, shouldn't we ? But then, what
ian@0 1140 * do we do once the error occurs ? For FCU notified fan
ian@0 1141 * failures (-EFAULT) we probably want to notify userland
ian@0 1142 * some way...
ian@0 1143 */
ian@0 1144 if (state->index == 0) {
ian@0 1145 set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
ian@0 1146 set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
ian@0 1147 set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
ian@0 1148 } else {
ian@0 1149 set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
ian@0 1150 set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
ian@0 1151 set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
ian@0 1152 }
ian@0 1153 }
ian@0 1154
ian@0 1155 /*
ian@0 1156 * Initialize the state structure for one CPU control loop
ian@0 1157 */
ian@0 1158 static int init_cpu_state(struct cpu_pid_state *state, int index)
ian@0 1159 {
ian@0 1160 state->index = index;
ian@0 1161 state->first = 1;
ian@0 1162 state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
ian@0 1163 state->overtemp = 0;
ian@0 1164 state->adc_config = 0x00;
ian@0 1165
ian@0 1166
ian@0 1167 if (index == 0)
ian@0 1168 state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
ian@0 1169 else if (index == 1)
ian@0 1170 state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
ian@0 1171 if (state->monitor == NULL)
ian@0 1172 goto fail;
ian@0 1173
ian@0 1174 if (read_eeprom(index, &state->mpu))
ian@0 1175 goto fail;
ian@0 1176
ian@0 1177 state->count_power = state->mpu.tguardband;
ian@0 1178 if (state->count_power > CPU_POWER_HISTORY_SIZE) {
ian@0 1179 printk(KERN_WARNING "Warning ! too many power history slots\n");
ian@0 1180 state->count_power = CPU_POWER_HISTORY_SIZE;
ian@0 1181 }
ian@0 1182 DBG("CPU %d Using %d power history entries\n", index, state->count_power);
ian@0 1183
ian@0 1184 if (index == 0) {
ian@0 1185 device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
ian@0 1186 device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
ian@0 1187 device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
ian@0 1188 device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
ian@0 1189 device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
ian@0 1190 } else {
ian@0 1191 device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
ian@0 1192 device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
ian@0 1193 device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
ian@0 1194 device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
ian@0 1195 device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
ian@0 1196 }
ian@0 1197
ian@0 1198 return 0;
ian@0 1199 fail:
ian@0 1200 if (state->monitor)
ian@0 1201 detach_i2c_chip(state->monitor);
ian@0 1202 state->monitor = NULL;
ian@0 1203
ian@0 1204 return -ENODEV;
ian@0 1205 }
ian@0 1206
ian@0 1207 /*
ian@0 1208 * Dispose of the state data for one CPU control loop
ian@0 1209 */
ian@0 1210 static void dispose_cpu_state(struct cpu_pid_state *state)
ian@0 1211 {
ian@0 1212 if (state->monitor == NULL)
ian@0 1213 return;
ian@0 1214
ian@0 1215 if (state->index == 0) {
ian@0 1216 device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
ian@0 1217 device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
ian@0 1218 device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
ian@0 1219 device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
ian@0 1220 device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
ian@0 1221 } else {
ian@0 1222 device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
ian@0 1223 device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
ian@0 1224 device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
ian@0 1225 device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
ian@0 1226 device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
ian@0 1227 }
ian@0 1228
ian@0 1229 detach_i2c_chip(state->monitor);
ian@0 1230 state->monitor = NULL;
ian@0 1231 }
ian@0 1232
ian@0 1233 /*
ian@0 1234 * Motherboard backside & U3 heatsink fan control loop
ian@0 1235 */
ian@0 1236 static void do_monitor_backside(struct backside_pid_state *state)
ian@0 1237 {
ian@0 1238 s32 temp, integral, derivative, fan_min;
ian@0 1239 s64 integ_p, deriv_p, prop_p, sum;
ian@0 1240 int i, rc;
ian@0 1241
ian@0 1242 if (--state->ticks != 0)
ian@0 1243 return;
ian@0 1244 state->ticks = backside_params.interval;
ian@0 1245
ian@0 1246 DBG("backside:\n");
ian@0 1247
ian@0 1248 /* Check fan status */
ian@0 1249 rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
ian@0 1250 if (rc < 0) {
ian@0 1251 printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
ian@0 1252 /* XXX What do we do now ? */
ian@0 1253 } else
ian@0 1254 state->pwm = rc;
ian@0 1255 DBG(" current pwm: %d\n", state->pwm);
ian@0 1256
ian@0 1257 /* Get some sensor readings */
ian@0 1258 temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
ian@0 1259 state->last_temp = temp;
ian@0 1260 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
ian@0 1261 FIX32TOPRINT(backside_params.input_target));
ian@0 1262
ian@0 1263 /* Store temperature and error in history array */
ian@0 1264 state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
ian@0 1265 state->sample_history[state->cur_sample] = temp;
ian@0 1266 state->error_history[state->cur_sample] = temp - backside_params.input_target;
ian@0 1267
ian@0 1268 /* If first loop, fill the history table */
ian@0 1269 if (state->first) {
ian@0 1270 for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
ian@0 1271 state->cur_sample = (state->cur_sample + 1) %
ian@0 1272 BACKSIDE_PID_HISTORY_SIZE;
ian@0 1273 state->sample_history[state->cur_sample] = temp;
ian@0 1274 state->error_history[state->cur_sample] =
ian@0 1275 temp - backside_params.input_target;
ian@0 1276 }
ian@0 1277 state->first = 0;
ian@0 1278 }
ian@0 1279
ian@0 1280 /* Calculate the integral term */
ian@0 1281 sum = 0;
ian@0 1282 integral = 0;
ian@0 1283 for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
ian@0 1284 integral += state->error_history[i];
ian@0 1285 integral *= backside_params.interval;
ian@0 1286 DBG(" integral: %08x\n", integral);
ian@0 1287 integ_p = ((s64)backside_params.G_r) * (s64)integral;
ian@0 1288 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
ian@0 1289 sum += integ_p;
ian@0 1290
ian@0 1291 /* Calculate the derivative term */
ian@0 1292 derivative = state->error_history[state->cur_sample] -
ian@0 1293 state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
ian@0 1294 % BACKSIDE_PID_HISTORY_SIZE];
ian@0 1295 derivative /= backside_params.interval;
ian@0 1296 deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
ian@0 1297 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
ian@0 1298 sum += deriv_p;
ian@0 1299
ian@0 1300 /* Calculate the proportional term */
ian@0 1301 prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
ian@0 1302 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
ian@0 1303 sum += prop_p;
ian@0 1304
ian@0 1305 /* Scale sum */
ian@0 1306 sum >>= 36;
ian@0 1307
ian@0 1308 DBG(" sum: %d\n", (int)sum);
ian@0 1309 if (backside_params.additive)
ian@0 1310 state->pwm += (s32)sum;
ian@0 1311 else
ian@0 1312 state->pwm = sum;
ian@0 1313
ian@0 1314 /* Check for clamp */
ian@0 1315 fan_min = (dimm_output_clamp * 100) / 14000;
ian@0 1316 fan_min = max(fan_min, backside_params.output_min);
ian@0 1317
ian@0 1318 state->pwm = max(state->pwm, fan_min);
ian@0 1319 state->pwm = min(state->pwm, backside_params.output_max);
ian@0 1320
ian@0 1321 DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
ian@0 1322 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
ian@0 1323 }
ian@0 1324
ian@0 1325 /*
ian@0 1326 * Initialize the state structure for the backside fan control loop
ian@0 1327 */
ian@0 1328 static int init_backside_state(struct backside_pid_state *state)
ian@0 1329 {
ian@0 1330 struct device_node *u3;
ian@0 1331 int u3h = 1; /* conservative by default */
ian@0 1332
ian@0 1333 /*
ian@0 1334 * There are different PID params for machines with U3 and machines
ian@0 1335 * with U3H, pick the right ones now
ian@0 1336 */
ian@0 1337 u3 = of_find_node_by_path("/u3@0,f8000000");
ian@0 1338 if (u3 != NULL) {
ian@0 1339 u32 *vers = (u32 *)get_property(u3, "device-rev", NULL);
ian@0 1340 if (vers)
ian@0 1341 if (((*vers) & 0x3f) < 0x34)
ian@0 1342 u3h = 0;
ian@0 1343 of_node_put(u3);
ian@0 1344 }
ian@0 1345
ian@0 1346 if (rackmac) {
ian@0 1347 backside_params.G_d = BACKSIDE_PID_RACK_G_d;
ian@0 1348 backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
ian@0 1349 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
ian@0 1350 backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
ian@0 1351 backside_params.G_p = BACKSIDE_PID_RACK_G_p;
ian@0 1352 backside_params.G_r = BACKSIDE_PID_G_r;
ian@0 1353 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
ian@0 1354 backside_params.additive = 0;
ian@0 1355 } else if (u3h) {
ian@0 1356 backside_params.G_d = BACKSIDE_PID_U3H_G_d;
ian@0 1357 backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
ian@0 1358 backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
ian@0 1359 backside_params.interval = BACKSIDE_PID_INTERVAL;
ian@0 1360 backside_params.G_p = BACKSIDE_PID_G_p;
ian@0 1361 backside_params.G_r = BACKSIDE_PID_G_r;
ian@0 1362 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
ian@0 1363 backside_params.additive = 1;
ian@0 1364 } else {
ian@0 1365 backside_params.G_d = BACKSIDE_PID_U3_G_d;
ian@0 1366 backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
ian@0 1367 backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
ian@0 1368 backside_params.interval = BACKSIDE_PID_INTERVAL;
ian@0 1369 backside_params.G_p = BACKSIDE_PID_G_p;
ian@0 1370 backside_params.G_r = BACKSIDE_PID_G_r;
ian@0 1371 backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
ian@0 1372 backside_params.additive = 1;
ian@0 1373 }
ian@0 1374
ian@0 1375 state->ticks = 1;
ian@0 1376 state->first = 1;
ian@0 1377 state->pwm = 50;
ian@0 1378
ian@0 1379 state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
ian@0 1380 if (state->monitor == NULL)
ian@0 1381 return -ENODEV;
ian@0 1382
ian@0 1383 device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
ian@0 1384 device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
ian@0 1385
ian@0 1386 return 0;
ian@0 1387 }
ian@0 1388
ian@0 1389 /*
ian@0 1390 * Dispose of the state data for the backside control loop
ian@0 1391 */
ian@0 1392 static void dispose_backside_state(struct backside_pid_state *state)
ian@0 1393 {
ian@0 1394 if (state->monitor == NULL)
ian@0 1395 return;
ian@0 1396
ian@0 1397 device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
ian@0 1398 device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
ian@0 1399
ian@0 1400 detach_i2c_chip(state->monitor);
ian@0 1401 state->monitor = NULL;
ian@0 1402 }
ian@0 1403
ian@0 1404 /*
ian@0 1405 * Drives bay fan control loop
ian@0 1406 */
ian@0 1407 static void do_monitor_drives(struct drives_pid_state *state)
ian@0 1408 {
ian@0 1409 s32 temp, integral, derivative;
ian@0 1410 s64 integ_p, deriv_p, prop_p, sum;
ian@0 1411 int i, rc;
ian@0 1412
ian@0 1413 if (--state->ticks != 0)
ian@0 1414 return;
ian@0 1415 state->ticks = DRIVES_PID_INTERVAL;
ian@0 1416
ian@0 1417 DBG("drives:\n");
ian@0 1418
ian@0 1419 /* Check fan status */
ian@0 1420 rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
ian@0 1421 if (rc < 0) {
ian@0 1422 printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
ian@0 1423 /* XXX What do we do now ? */
ian@0 1424 } else
ian@0 1425 state->rpm = rc;
ian@0 1426 DBG(" current rpm: %d\n", state->rpm);
ian@0 1427
ian@0 1428 /* Get some sensor readings */
ian@0 1429 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
ian@0 1430 DS1775_TEMP)) << 8;
ian@0 1431 state->last_temp = temp;
ian@0 1432 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
ian@0 1433 FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
ian@0 1434
ian@0 1435 /* Store temperature and error in history array */
ian@0 1436 state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
ian@0 1437 state->sample_history[state->cur_sample] = temp;
ian@0 1438 state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
ian@0 1439
ian@0 1440 /* If first loop, fill the history table */
ian@0 1441 if (state->first) {
ian@0 1442 for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
ian@0 1443 state->cur_sample = (state->cur_sample + 1) %
ian@0 1444 DRIVES_PID_HISTORY_SIZE;
ian@0 1445 state->sample_history[state->cur_sample] = temp;
ian@0 1446 state->error_history[state->cur_sample] =
ian@0 1447 temp - DRIVES_PID_INPUT_TARGET;
ian@0 1448 }
ian@0 1449 state->first = 0;
ian@0 1450 }
ian@0 1451
ian@0 1452 /* Calculate the integral term */
ian@0 1453 sum = 0;
ian@0 1454 integral = 0;
ian@0 1455 for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
ian@0 1456 integral += state->error_history[i];
ian@0 1457 integral *= DRIVES_PID_INTERVAL;
ian@0 1458 DBG(" integral: %08x\n", integral);
ian@0 1459 integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
ian@0 1460 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
ian@0 1461 sum += integ_p;
ian@0 1462
ian@0 1463 /* Calculate the derivative term */
ian@0 1464 derivative = state->error_history[state->cur_sample] -
ian@0 1465 state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
ian@0 1466 % DRIVES_PID_HISTORY_SIZE];
ian@0 1467 derivative /= DRIVES_PID_INTERVAL;
ian@0 1468 deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
ian@0 1469 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
ian@0 1470 sum += deriv_p;
ian@0 1471
ian@0 1472 /* Calculate the proportional term */
ian@0 1473 prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
ian@0 1474 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
ian@0 1475 sum += prop_p;
ian@0 1476
ian@0 1477 /* Scale sum */
ian@0 1478 sum >>= 36;
ian@0 1479
ian@0 1480 DBG(" sum: %d\n", (int)sum);
ian@0 1481 state->rpm += (s32)sum;
ian@0 1482
ian@0 1483 state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
ian@0 1484 state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
ian@0 1485
ian@0 1486 DBG("** DRIVES RPM: %d\n", (int)state->rpm);
ian@0 1487 set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
ian@0 1488 }
ian@0 1489
ian@0 1490 /*
ian@0 1491 * Initialize the state structure for the drives bay fan control loop
ian@0 1492 */
ian@0 1493 static int init_drives_state(struct drives_pid_state *state)
ian@0 1494 {
ian@0 1495 state->ticks = 1;
ian@0 1496 state->first = 1;
ian@0 1497 state->rpm = 1000;
ian@0 1498
ian@0 1499 state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
ian@0 1500 if (state->monitor == NULL)
ian@0 1501 return -ENODEV;
ian@0 1502
ian@0 1503 device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
ian@0 1504 device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
ian@0 1505
ian@0 1506 return 0;
ian@0 1507 }
ian@0 1508
ian@0 1509 /*
ian@0 1510 * Dispose of the state data for the drives control loop
ian@0 1511 */
ian@0 1512 static void dispose_drives_state(struct drives_pid_state *state)
ian@0 1513 {
ian@0 1514 if (state->monitor == NULL)
ian@0 1515 return;
ian@0 1516
ian@0 1517 device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
ian@0 1518 device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
ian@0 1519
ian@0 1520 detach_i2c_chip(state->monitor);
ian@0 1521 state->monitor = NULL;
ian@0 1522 }
ian@0 1523
ian@0 1524 /*
ian@0 1525 * DIMMs temp control loop
ian@0 1526 */
ian@0 1527 static void do_monitor_dimms(struct dimm_pid_state *state)
ian@0 1528 {
ian@0 1529 s32 temp, integral, derivative, fan_min;
ian@0 1530 s64 integ_p, deriv_p, prop_p, sum;
ian@0 1531 int i;
ian@0 1532
ian@0 1533 if (--state->ticks != 0)
ian@0 1534 return;
ian@0 1535 state->ticks = DIMM_PID_INTERVAL;
ian@0 1536
ian@0 1537 DBG("DIMM:\n");
ian@0 1538
ian@0 1539 DBG(" current value: %d\n", state->output);
ian@0 1540
ian@0 1541 temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
ian@0 1542 if (temp < 0)
ian@0 1543 return;
ian@0 1544 temp <<= 16;
ian@0 1545 state->last_temp = temp;
ian@0 1546 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
ian@0 1547 FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
ian@0 1548
ian@0 1549 /* Store temperature and error in history array */
ian@0 1550 state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
ian@0 1551 state->sample_history[state->cur_sample] = temp;
ian@0 1552 state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
ian@0 1553
ian@0 1554 /* If first loop, fill the history table */
ian@0 1555 if (state->first) {
ian@0 1556 for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
ian@0 1557 state->cur_sample = (state->cur_sample + 1) %
ian@0 1558 DIMM_PID_HISTORY_SIZE;
ian@0 1559 state->sample_history[state->cur_sample] = temp;
ian@0 1560 state->error_history[state->cur_sample] =
ian@0 1561 temp - DIMM_PID_INPUT_TARGET;
ian@0 1562 }
ian@0 1563 state->first = 0;
ian@0 1564 }
ian@0 1565
ian@0 1566 /* Calculate the integral term */
ian@0 1567 sum = 0;
ian@0 1568 integral = 0;
ian@0 1569 for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
ian@0 1570 integral += state->error_history[i];
ian@0 1571 integral *= DIMM_PID_INTERVAL;
ian@0 1572 DBG(" integral: %08x\n", integral);
ian@0 1573 integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
ian@0 1574 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
ian@0 1575 sum += integ_p;
ian@0 1576
ian@0 1577 /* Calculate the derivative term */
ian@0 1578 derivative = state->error_history[state->cur_sample] -
ian@0 1579 state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
ian@0 1580 % DIMM_PID_HISTORY_SIZE];
ian@0 1581 derivative /= DIMM_PID_INTERVAL;
ian@0 1582 deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
ian@0 1583 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
ian@0 1584 sum += deriv_p;
ian@0 1585
ian@0 1586 /* Calculate the proportional term */
ian@0 1587 prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
ian@0 1588 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
ian@0 1589 sum += prop_p;
ian@0 1590
ian@0 1591 /* Scale sum */
ian@0 1592 sum >>= 36;
ian@0 1593
ian@0 1594 DBG(" sum: %d\n", (int)sum);
ian@0 1595 state->output = (s32)sum;
ian@0 1596 state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
ian@0 1597 state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
ian@0 1598 dimm_output_clamp = state->output;
ian@0 1599
ian@0 1600 DBG("** DIMM clamp value: %d\n", (int)state->output);
ian@0 1601
ian@0 1602 /* Backside PID is only every 5 seconds, force backside fan clamping now */
ian@0 1603 fan_min = (dimm_output_clamp * 100) / 14000;
ian@0 1604 fan_min = max(fan_min, backside_params.output_min);
ian@0 1605 if (backside_state.pwm < fan_min) {
ian@0 1606 backside_state.pwm = fan_min;
ian@0 1607 DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
ian@0 1608 set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
ian@0 1609 }
ian@0 1610 }
ian@0 1611
ian@0 1612 /*
ian@0 1613 * Initialize the state structure for the DIMM temp control loop
ian@0 1614 */
ian@0 1615 static int init_dimms_state(struct dimm_pid_state *state)
ian@0 1616 {
ian@0 1617 state->ticks = 1;
ian@0 1618 state->first = 1;
ian@0 1619 state->output = 4000;
ian@0 1620
ian@0 1621 state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
ian@0 1622 if (state->monitor == NULL)
ian@0 1623 return -ENODEV;
ian@0 1624
ian@0 1625 device_create_file(&of_dev->dev, &dev_attr_dimms_temperature);
ian@0 1626
ian@0 1627 return 0;
ian@0 1628 }
ian@0 1629
ian@0 1630 /*
ian@0 1631 * Dispose of the state data for the DIMM control loop
ian@0 1632 */
ian@0 1633 static void dispose_dimms_state(struct dimm_pid_state *state)
ian@0 1634 {
ian@0 1635 if (state->monitor == NULL)
ian@0 1636 return;
ian@0 1637
ian@0 1638 device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
ian@0 1639
ian@0 1640 detach_i2c_chip(state->monitor);
ian@0 1641 state->monitor = NULL;
ian@0 1642 }
ian@0 1643
ian@0 1644 /*
ian@0 1645 * Slots fan control loop
ian@0 1646 */
ian@0 1647 static void do_monitor_slots(struct slots_pid_state *state)
ian@0 1648 {
ian@0 1649 s32 temp, integral, derivative;
ian@0 1650 s64 integ_p, deriv_p, prop_p, sum;
ian@0 1651 int i, rc;
ian@0 1652
ian@0 1653 if (--state->ticks != 0)
ian@0 1654 return;
ian@0 1655 state->ticks = SLOTS_PID_INTERVAL;
ian@0 1656
ian@0 1657 DBG("slots:\n");
ian@0 1658
ian@0 1659 /* Check fan status */
ian@0 1660 rc = get_pwm_fan(SLOTS_FAN_PWM_INDEX);
ian@0 1661 if (rc < 0) {
ian@0 1662 printk(KERN_WARNING "Error %d reading slots fan !\n", rc);
ian@0 1663 /* XXX What do we do now ? */
ian@0 1664 } else
ian@0 1665 state->pwm = rc;
ian@0 1666 DBG(" current pwm: %d\n", state->pwm);
ian@0 1667
ian@0 1668 /* Get some sensor readings */
ian@0 1669 temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor,
ian@0 1670 DS1775_TEMP)) << 8;
ian@0 1671 state->last_temp = temp;
ian@0 1672 DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
ian@0 1673 FIX32TOPRINT(SLOTS_PID_INPUT_TARGET));
ian@0 1674
ian@0 1675 /* Store temperature and error in history array */
ian@0 1676 state->cur_sample = (state->cur_sample + 1) % SLOTS_PID_HISTORY_SIZE;
ian@0 1677 state->sample_history[state->cur_sample] = temp;
ian@0 1678 state->error_history[state->cur_sample] = temp - SLOTS_PID_INPUT_TARGET;
ian@0 1679
ian@0 1680 /* If first loop, fill the history table */
ian@0 1681 if (state->first) {
ian@0 1682 for (i = 0; i < (SLOTS_PID_HISTORY_SIZE - 1); i++) {
ian@0 1683 state->cur_sample = (state->cur_sample + 1) %
ian@0 1684 SLOTS_PID_HISTORY_SIZE;
ian@0 1685 state->sample_history[state->cur_sample] = temp;
ian@0 1686 state->error_history[state->cur_sample] =
ian@0 1687 temp - SLOTS_PID_INPUT_TARGET;
ian@0 1688 }
ian@0 1689 state->first = 0;
ian@0 1690 }
ian@0 1691
ian@0 1692 /* Calculate the integral term */
ian@0 1693 sum = 0;
ian@0 1694 integral = 0;
ian@0 1695 for (i = 0; i < SLOTS_PID_HISTORY_SIZE; i++)
ian@0 1696 integral += state->error_history[i];
ian@0 1697 integral *= SLOTS_PID_INTERVAL;
ian@0 1698 DBG(" integral: %08x\n", integral);
ian@0 1699 integ_p = ((s64)SLOTS_PID_G_r) * (s64)integral;
ian@0 1700 DBG(" integ_p: %d\n", (int)(integ_p >> 36));
ian@0 1701 sum += integ_p;
ian@0 1702
ian@0 1703 /* Calculate the derivative term */
ian@0 1704 derivative = state->error_history[state->cur_sample] -
ian@0 1705 state->error_history[(state->cur_sample + SLOTS_PID_HISTORY_SIZE - 1)
ian@0 1706 % SLOTS_PID_HISTORY_SIZE];
ian@0 1707 derivative /= SLOTS_PID_INTERVAL;
ian@0 1708 deriv_p = ((s64)SLOTS_PID_G_d) * (s64)derivative;
ian@0 1709 DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
ian@0 1710 sum += deriv_p;
ian@0 1711
ian@0 1712 /* Calculate the proportional term */
ian@0 1713 prop_p = ((s64)SLOTS_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
ian@0 1714 DBG(" prop_p: %d\n", (int)(prop_p >> 36));
ian@0 1715 sum += prop_p;
ian@0 1716
ian@0 1717 /* Scale sum */
ian@0 1718 sum >>= 36;
ian@0 1719
ian@0 1720 DBG(" sum: %d\n", (int)sum);
ian@0 1721 state->pwm = (s32)sum;
ian@0 1722
ian@0 1723 state->pwm = max(state->pwm, SLOTS_PID_OUTPUT_MIN);
ian@0 1724 state->pwm = min(state->pwm, SLOTS_PID_OUTPUT_MAX);
ian@0 1725
ian@0 1726 DBG("** DRIVES PWM: %d\n", (int)state->pwm);
ian@0 1727 set_pwm_fan(SLOTS_FAN_PWM_INDEX, state->pwm);
ian@0 1728 }
ian@0 1729
ian@0 1730 /*
ian@0 1731 * Initialize the state structure for the slots bay fan control loop
ian@0 1732 */
ian@0 1733 static int init_slots_state(struct slots_pid_state *state)
ian@0 1734 {
ian@0 1735 state->ticks = 1;
ian@0 1736 state->first = 1;
ian@0 1737 state->pwm = 50;
ian@0 1738
ian@0 1739 state->monitor = attach_i2c_chip(XSERVE_SLOTS_LM75, "slots_temp");
ian@0 1740 if (state->monitor == NULL)
ian@0 1741 return -ENODEV;
ian@0 1742
ian@0 1743 device_create_file(&of_dev->dev, &dev_attr_slots_temperature);
ian@0 1744 device_create_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
ian@0 1745
ian@0 1746 return 0;
ian@0 1747 }
ian@0 1748
ian@0 1749 /*
ian@0 1750 * Dispose of the state data for the slots control loop
ian@0 1751 */
ian@0 1752 static void dispose_slots_state(struct slots_pid_state *state)
ian@0 1753 {
ian@0 1754 if (state->monitor == NULL)
ian@0 1755 return;
ian@0 1756
ian@0 1757 device_remove_file(&of_dev->dev, &dev_attr_slots_temperature);
ian@0 1758 device_remove_file(&of_dev->dev, &dev_attr_slots_fan_pwm);
ian@0 1759
ian@0 1760 detach_i2c_chip(state->monitor);
ian@0 1761 state->monitor = NULL;
ian@0 1762 }
ian@0 1763
ian@0 1764
ian@0 1765 static int call_critical_overtemp(void)
ian@0 1766 {
ian@0 1767 char *argv[] = { critical_overtemp_path, NULL };
ian@0 1768 static char *envp[] = { "HOME=/",
ian@0 1769 "TERM=linux",
ian@0 1770 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
ian@0 1771 NULL };
ian@0 1772
ian@0 1773 return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
ian@0 1774 }
ian@0 1775
ian@0 1776
ian@0 1777 /*
ian@0 1778 * Here's the kernel thread that calls the various control loops
ian@0 1779 */
ian@0 1780 static int main_control_loop(void *x)
ian@0 1781 {
ian@0 1782 daemonize("kfand");
ian@0 1783
ian@0 1784 DBG("main_control_loop started\n");
ian@0 1785
ian@0 1786 down(&driver_lock);
ian@0 1787
ian@0 1788 if (start_fcu() < 0) {
ian@0 1789 printk(KERN_ERR "kfand: failed to start FCU\n");
ian@0 1790 up(&driver_lock);
ian@0 1791 goto out;
ian@0 1792 }
ian@0 1793
ian@0 1794 /* Set the PCI fan once for now on non-RackMac */
ian@0 1795 if (!rackmac)
ian@0 1796 set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
ian@0 1797
ian@0 1798 /* Initialize ADCs */
ian@0 1799 initialize_adc(&cpu_state[0]);
ian@0 1800 if (cpu_state[1].monitor != NULL)
ian@0 1801 initialize_adc(&cpu_state[1]);
ian@0 1802
ian@0 1803 fcu_tickle_ticks = FCU_TICKLE_TICKS;
ian@0 1804
ian@0 1805 up(&driver_lock);
ian@0 1806
ian@0 1807 while (state == state_attached) {
ian@0 1808 unsigned long elapsed, start;
ian@0 1809
ian@0 1810 start = jiffies;
ian@0 1811
ian@0 1812 down(&driver_lock);
ian@0 1813
ian@0 1814 /* Tickle the FCU just in case */
ian@0 1815 if (--fcu_tickle_ticks < 0) {
ian@0 1816 fcu_tickle_ticks = FCU_TICKLE_TICKS;
ian@0 1817 tickle_fcu();
ian@0 1818 }
ian@0 1819
ian@0 1820 /* First, we always calculate the new DIMMs state on an Xserve */
ian@0 1821 if (rackmac)
ian@0 1822 do_monitor_dimms(&dimms_state);
ian@0 1823
ian@0 1824 /* Then, the CPUs */
ian@0 1825 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
ian@0 1826 do_monitor_cpu_combined();
ian@0 1827 else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
ian@0 1828 do_monitor_cpu_rack(&cpu_state[0]);
ian@0 1829 if (cpu_state[1].monitor != NULL)
ian@0 1830 do_monitor_cpu_rack(&cpu_state[1]);
ian@0 1831 // better deal with UP
ian@0 1832 } else {
ian@0 1833 do_monitor_cpu_split(&cpu_state[0]);
ian@0 1834 if (cpu_state[1].monitor != NULL)
ian@0 1835 do_monitor_cpu_split(&cpu_state[1]);
ian@0 1836 // better deal with UP
ian@0 1837 }
ian@0 1838 /* Then, the rest */
ian@0 1839 do_monitor_backside(&backside_state);
ian@0 1840 if (rackmac)
ian@0 1841 do_monitor_slots(&slots_state);
ian@0 1842 else
ian@0 1843 do_monitor_drives(&drives_state);
ian@0 1844 up(&driver_lock);
ian@0 1845
ian@0 1846 if (critical_state == 1) {
ian@0 1847 printk(KERN_WARNING "Temperature control detected a critical condition\n");
ian@0 1848 printk(KERN_WARNING "Attempting to shut down...\n");
ian@0 1849 if (call_critical_overtemp()) {
ian@0 1850 printk(KERN_WARNING "Can't call %s, power off now!\n",
ian@0 1851 critical_overtemp_path);
ian@0 1852 machine_power_off();
ian@0 1853 }
ian@0 1854 }
ian@0 1855 if (critical_state > 0)
ian@0 1856 critical_state++;
ian@0 1857 if (critical_state > MAX_CRITICAL_STATE) {
ian@0 1858 printk(KERN_WARNING "Shutdown timed out, power off now !\n");
ian@0 1859 machine_power_off();
ian@0 1860 }
ian@0 1861
ian@0 1862 // FIXME: Deal with signals
ian@0 1863 elapsed = jiffies - start;
ian@0 1864 if (elapsed < HZ)
ian@0 1865 schedule_timeout_interruptible(HZ - elapsed);
ian@0 1866 }
ian@0 1867
ian@0 1868 out:
ian@0 1869 DBG("main_control_loop ended\n");
ian@0 1870
ian@0 1871 ctrl_task = 0;
ian@0 1872 complete_and_exit(&ctrl_complete, 0);
ian@0 1873 }
ian@0 1874
ian@0 1875 /*
ian@0 1876 * Dispose the control loops when tearing down
ian@0 1877 */
ian@0 1878 static void dispose_control_loops(void)
ian@0 1879 {
ian@0 1880 dispose_cpu_state(&cpu_state[0]);
ian@0 1881 dispose_cpu_state(&cpu_state[1]);
ian@0 1882 dispose_backside_state(&backside_state);
ian@0 1883 dispose_drives_state(&drives_state);
ian@0 1884 dispose_slots_state(&slots_state);
ian@0 1885 dispose_dimms_state(&dimms_state);
ian@0 1886 }
ian@0 1887
ian@0 1888 /*
ian@0 1889 * Create the control loops. U3-0 i2c bus is up, so we can now
ian@0 1890 * get to the various sensors
ian@0 1891 */
ian@0 1892 static int create_control_loops(void)
ian@0 1893 {
ian@0 1894 struct device_node *np;
ian@0 1895
ian@0 1896 /* Count CPUs from the device-tree, we don't care how many are
ian@0 1897 * actually used by Linux
ian@0 1898 */
ian@0 1899 cpu_count = 0;
ian@0 1900 for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
ian@0 1901 cpu_count++;
ian@0 1902
ian@0 1903 DBG("counted %d CPUs in the device-tree\n", cpu_count);
ian@0 1904
ian@0 1905 /* Decide the type of PID algorithm to use based on the presence of
ian@0 1906 * the pumps, though that may not be the best way, that is good enough
ian@0 1907 * for now
ian@0 1908 */
ian@0 1909 if (rackmac)
ian@0 1910 cpu_pid_type = CPU_PID_TYPE_RACKMAC;
ian@0 1911 else if (machine_is_compatible("PowerMac7,3")
ian@0 1912 && (cpu_count > 1)
ian@0 1913 && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
ian@0 1914 && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
ian@0 1915 printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
ian@0 1916 cpu_pid_type = CPU_PID_TYPE_COMBINED;
ian@0 1917 } else
ian@0 1918 cpu_pid_type = CPU_PID_TYPE_SPLIT;
ian@0 1919
ian@0 1920 /* Create control loops for everything. If any fail, everything
ian@0 1921 * fails
ian@0 1922 */
ian@0 1923 if (init_cpu_state(&cpu_state[0], 0))
ian@0 1924 goto fail;
ian@0 1925 if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
ian@0 1926 fetch_cpu_pumps_minmax();
ian@0 1927
ian@0 1928 if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
ian@0 1929 goto fail;
ian@0 1930 if (init_backside_state(&backside_state))
ian@0 1931 goto fail;
ian@0 1932 if (rackmac && init_dimms_state(&dimms_state))
ian@0 1933 goto fail;
ian@0 1934 if (rackmac && init_slots_state(&slots_state))
ian@0 1935 goto fail;
ian@0 1936 if (!rackmac && init_drives_state(&drives_state))
ian@0 1937 goto fail;
ian@0 1938
ian@0 1939 DBG("all control loops up !\n");
ian@0 1940
ian@0 1941 return 0;
ian@0 1942
ian@0 1943 fail:
ian@0 1944 DBG("failure creating control loops, disposing\n");
ian@0 1945
ian@0 1946 dispose_control_loops();
ian@0 1947
ian@0 1948 return -ENODEV;
ian@0 1949 }
ian@0 1950
ian@0 1951 /*
ian@0 1952 * Start the control loops after everything is up, that is create
ian@0 1953 * the thread that will make them run
ian@0 1954 */
ian@0 1955 static void start_control_loops(void)
ian@0 1956 {
ian@0 1957 init_completion(&ctrl_complete);
ian@0 1958
ian@0 1959 ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL);
ian@0 1960 }
ian@0 1961
ian@0 1962 /*
ian@0 1963 * Stop the control loops when tearing down
ian@0 1964 */
ian@0 1965 static void stop_control_loops(void)
ian@0 1966 {
ian@0 1967 if (ctrl_task != 0)
ian@0 1968 wait_for_completion(&ctrl_complete);
ian@0 1969 }
ian@0 1970
ian@0 1971 /*
ian@0 1972 * Attach to the i2c FCU after detecting U3-1 bus
ian@0 1973 */
ian@0 1974 static int attach_fcu(void)
ian@0 1975 {
ian@0 1976 fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
ian@0 1977 if (fcu == NULL)
ian@0 1978 return -ENODEV;
ian@0 1979
ian@0 1980 DBG("FCU attached\n");
ian@0 1981
ian@0 1982 return 0;
ian@0 1983 }
ian@0 1984
ian@0 1985 /*
ian@0 1986 * Detach from the i2c FCU when tearing down
ian@0 1987 */
ian@0 1988 static void detach_fcu(void)
ian@0 1989 {
ian@0 1990 if (fcu)
ian@0 1991 detach_i2c_chip(fcu);
ian@0 1992 fcu = NULL;
ian@0 1993 }
ian@0 1994
ian@0 1995 /*
ian@0 1996 * Attach to the i2c controller. We probe the various chips based
ian@0 1997 * on the device-tree nodes and build everything for the driver to
ian@0 1998 * run, we then kick the driver monitoring thread
ian@0 1999 */
ian@0 2000 static int therm_pm72_attach(struct i2c_adapter *adapter)
ian@0 2001 {
ian@0 2002 down(&driver_lock);
ian@0 2003
ian@0 2004 /* Check state */
ian@0 2005 if (state == state_detached)
ian@0 2006 state = state_attaching;
ian@0 2007 if (state != state_attaching) {
ian@0 2008 up(&driver_lock);
ian@0 2009 return 0;
ian@0 2010 }
ian@0 2011
ian@0 2012 /* Check if we are looking for one of these */
ian@0 2013 if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
ian@0 2014 u3_0 = adapter;
ian@0 2015 DBG("found U3-0\n");
ian@0 2016 if (k2 || !rackmac)
ian@0 2017 if (create_control_loops())
ian@0 2018 u3_0 = NULL;
ian@0 2019 } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
ian@0 2020 u3_1 = adapter;
ian@0 2021 DBG("found U3-1, attaching FCU\n");
ian@0 2022 if (attach_fcu())
ian@0 2023 u3_1 = NULL;
ian@0 2024 } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
ian@0 2025 k2 = adapter;
ian@0 2026 DBG("Found K2\n");
ian@0 2027 if (u3_0 && rackmac)
ian@0 2028 if (create_control_loops())
ian@0 2029 k2 = NULL;
ian@0 2030 }
ian@0 2031 /* We got all we need, start control loops */
ian@0 2032 if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
ian@0 2033 DBG("everything up, starting control loops\n");
ian@0 2034 state = state_attached;
ian@0 2035 start_control_loops();
ian@0 2036 }
ian@0 2037 up(&driver_lock);
ian@0 2038
ian@0 2039 return 0;
ian@0 2040 }
ian@0 2041
ian@0 2042 /*
ian@0 2043 * Called on every adapter when the driver or the i2c controller
ian@0 2044 * is going away.
ian@0 2045 */
ian@0 2046 static int therm_pm72_detach(struct i2c_adapter *adapter)
ian@0 2047 {
ian@0 2048 down(&driver_lock);
ian@0 2049
ian@0 2050 if (state != state_detached)
ian@0 2051 state = state_detaching;
ian@0 2052
ian@0 2053 /* Stop control loops if any */
ian@0 2054 DBG("stopping control loops\n");
ian@0 2055 up(&driver_lock);
ian@0 2056 stop_control_loops();
ian@0 2057 down(&driver_lock);
ian@0 2058
ian@0 2059 if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
ian@0 2060 DBG("lost U3-0, disposing control loops\n");
ian@0 2061 dispose_control_loops();
ian@0 2062 u3_0 = NULL;
ian@0 2063 }
ian@0 2064
ian@0 2065 if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
ian@0 2066 DBG("lost U3-1, detaching FCU\n");
ian@0 2067 detach_fcu();
ian@0 2068 u3_1 = NULL;
ian@0 2069 }
ian@0 2070 if (u3_0 == NULL && u3_1 == NULL)
ian@0 2071 state = state_detached;
ian@0 2072
ian@0 2073 up(&driver_lock);
ian@0 2074
ian@0 2075 return 0;
ian@0 2076 }
ian@0 2077
ian@0 2078 static int fan_check_loc_match(const char *loc, int fan)
ian@0 2079 {
ian@0 2080 char tmp[64];
ian@0 2081 char *c, *e;
ian@0 2082
ian@0 2083 strlcpy(tmp, fcu_fans[fan].loc, 64);
ian@0 2084
ian@0 2085 c = tmp;
ian@0 2086 for (;;) {
ian@0 2087 e = strchr(c, ',');
ian@0 2088 if (e)
ian@0 2089 *e = 0;
ian@0 2090 if (strcmp(loc, c) == 0)
ian@0 2091 return 1;
ian@0 2092 if (e == NULL)
ian@0 2093 break;
ian@0 2094 c = e + 1;
ian@0 2095 }
ian@0 2096 return 0;
ian@0 2097 }
ian@0 2098
ian@0 2099 static void fcu_lookup_fans(struct device_node *fcu_node)
ian@0 2100 {
ian@0 2101 struct device_node *np = NULL;
ian@0 2102 int i;
ian@0 2103
ian@0 2104 /* The table is filled by default with values that are suitable
ian@0 2105 * for the old machines without device-tree informations. We scan
ian@0 2106 * the device-tree and override those values with whatever is
ian@0 2107 * there
ian@0 2108 */
ian@0 2109
ian@0 2110 DBG("Looking up FCU controls in device-tree...\n");
ian@0 2111
ian@0 2112 while ((np = of_get_next_child(fcu_node, np)) != NULL) {
ian@0 2113 int type = -1;
ian@0 2114 char *loc;
ian@0 2115 u32 *reg;
ian@0 2116
ian@0 2117 DBG(" control: %s, type: %s\n", np->name, np->type);
ian@0 2118
ian@0 2119 /* Detect control type */
ian@0 2120 if (!strcmp(np->type, "fan-rpm-control") ||
ian@0 2121 !strcmp(np->type, "fan-rpm"))
ian@0 2122 type = FCU_FAN_RPM;
ian@0 2123 if (!strcmp(np->type, "fan-pwm-control") ||
ian@0 2124 !strcmp(np->type, "fan-pwm"))
ian@0 2125 type = FCU_FAN_PWM;
ian@0 2126 /* Only care about fans for now */
ian@0 2127 if (type == -1)
ian@0 2128 continue;
ian@0 2129
ian@0 2130 /* Lookup for a matching location */
ian@0 2131 loc = (char *)get_property(np, "location", NULL);
ian@0 2132 reg = (u32 *)get_property(np, "reg", NULL);
ian@0 2133 if (loc == NULL || reg == NULL)
ian@0 2134 continue;
ian@0 2135 DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
ian@0 2136
ian@0 2137 for (i = 0; i < FCU_FAN_COUNT; i++) {
ian@0 2138 int fan_id;
ian@0 2139
ian@0 2140 if (!fan_check_loc_match(loc, i))
ian@0 2141 continue;
ian@0 2142 DBG(" location match, index: %d\n", i);
ian@0 2143 fcu_fans[i].id = FCU_FAN_ABSENT_ID;
ian@0 2144 if (type != fcu_fans[i].type) {
ian@0 2145 printk(KERN_WARNING "therm_pm72: Fan type mismatch "
ian@0 2146 "in device-tree for %s\n", np->full_name);
ian@0 2147 break;
ian@0 2148 }
ian@0 2149 if (type == FCU_FAN_RPM)
ian@0 2150 fan_id = ((*reg) - 0x10) / 2;
ian@0 2151 else
ian@0 2152 fan_id = ((*reg) - 0x30) / 2;
ian@0 2153 if (fan_id > 7) {
ian@0 2154 printk(KERN_WARNING "therm_pm72: Can't parse "
ian@0 2155 "fan ID in device-tree for %s\n", np->full_name);
ian@0 2156 break;
ian@0 2157 }
ian@0 2158 DBG(" fan id -> %d, type -> %d\n", fan_id, type);
ian@0 2159 fcu_fans[i].id = fan_id;
ian@0 2160 }
ian@0 2161 }
ian@0 2162
ian@0 2163 /* Now dump the array */
ian@0 2164 printk(KERN_INFO "Detected fan controls:\n");
ian@0 2165 for (i = 0; i < FCU_FAN_COUNT; i++) {
ian@0 2166 if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
ian@0 2167 continue;
ian@0 2168 printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
ian@0 2169 fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
ian@0 2170 fcu_fans[i].id, fcu_fans[i].loc);
ian@0 2171 }
ian@0 2172 }
ian@0 2173
ian@0 2174 static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
ian@0 2175 {
ian@0 2176 state = state_detached;
ian@0 2177
ian@0 2178 /* Lookup the fans in the device tree */
ian@0 2179 fcu_lookup_fans(dev->node);
ian@0 2180
ian@0 2181 /* Add the driver */
ian@0 2182 return i2c_add_driver(&therm_pm72_driver);
ian@0 2183 }
ian@0 2184
ian@0 2185 static int fcu_of_remove(struct of_device* dev)
ian@0 2186 {
ian@0 2187 i2c_del_driver(&therm_pm72_driver);
ian@0 2188
ian@0 2189 return 0;
ian@0 2190 }
ian@0 2191
ian@0 2192 static struct of_device_id fcu_match[] =
ian@0 2193 {
ian@0 2194 {
ian@0 2195 .type = "fcu",
ian@0 2196 },
ian@0 2197 {},
ian@0 2198 };
ian@0 2199
ian@0 2200 static struct of_platform_driver fcu_of_platform_driver =
ian@0 2201 {
ian@0 2202 .name = "temperature",
ian@0 2203 .match_table = fcu_match,
ian@0 2204 .probe = fcu_of_probe,
ian@0 2205 .remove = fcu_of_remove
ian@0 2206 };
ian@0 2207
ian@0 2208 /*
ian@0 2209 * Check machine type, attach to i2c controller
ian@0 2210 */
ian@0 2211 static int __init therm_pm72_init(void)
ian@0 2212 {
ian@0 2213 struct device_node *np;
ian@0 2214
ian@0 2215 rackmac = machine_is_compatible("RackMac3,1");
ian@0 2216
ian@0 2217 if (!machine_is_compatible("PowerMac7,2") &&
ian@0 2218 !machine_is_compatible("PowerMac7,3") &&
ian@0 2219 !rackmac)
ian@0 2220 return -ENODEV;
ian@0 2221
ian@0 2222 printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
ian@0 2223
ian@0 2224 np = of_find_node_by_type(NULL, "fcu");
ian@0 2225 if (np == NULL) {
ian@0 2226 /* Some machines have strangely broken device-tree */
ian@0 2227 np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
ian@0 2228 if (np == NULL) {
ian@0 2229 printk(KERN_ERR "Can't find FCU in device-tree !\n");
ian@0 2230 return -ENODEV;
ian@0 2231 }
ian@0 2232 }
ian@0 2233 of_dev = of_platform_device_create(np, "temperature", NULL);
ian@0 2234 if (of_dev == NULL) {
ian@0 2235 printk(KERN_ERR "Can't register FCU platform device !\n");
ian@0 2236 return -ENODEV;
ian@0 2237 }
ian@0 2238
ian@0 2239 of_register_driver(&fcu_of_platform_driver);
ian@0 2240
ian@0 2241 return 0;
ian@0 2242 }
ian@0 2243
ian@0 2244 static void __exit therm_pm72_exit(void)
ian@0 2245 {
ian@0 2246 of_unregister_driver(&fcu_of_platform_driver);
ian@0 2247
ian@0 2248 if (of_dev)
ian@0 2249 of_device_unregister(of_dev);
ian@0 2250 }
ian@0 2251
ian@0 2252 module_init(therm_pm72_init);
ian@0 2253 module_exit(therm_pm72_exit);
ian@0 2254
ian@0 2255 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
ian@0 2256 MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
ian@0 2257 MODULE_LICENSE("GPL");
ian@0 2258