10Sduke// SPDX-License-Identifier: GPL-2.0-only 21879Sstefank/* 30Sduke * Windfarm PowerMac thermal control. SMU based sensors 40Sduke * 50Sduke * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp. 60Sduke * <benh@kernel.crashing.org> 70Sduke */ 80Sduke 90Sduke#include <linux/types.h> 100Sduke#include <linux/errno.h> 110Sduke#include <linux/kernel.h> 120Sduke#include <linux/delay.h> 130Sduke#include <linux/slab.h> 140Sduke#include <linux/init.h> 150Sduke#include <linux/wait.h> 160Sduke#include <linux/completion.h> 170Sduke#include <linux/of.h> 180Sduke 191472Strims#include <asm/machdep.h> 201472Strims#include <asm/io.h> 211472Strims#include <asm/sections.h> 220Sduke#include <asm/smu.h> 230Sduke 240Sduke#include "windfarm.h" 251879Sstefank 261879Sstefank#define VERSION "0.2" 271879Sstefank 280Sduke#undef DEBUG 29304Snever 300Sduke#ifdef DEBUG 310Sduke#define DBG(args...) printk(args) 320Sduke#else 330Sduke#define DBG(args...) do { } while(0) 34304Snever#endif 351909Siveresov 361909Siveresov/* 371909Siveresov * Various SMU "partitions" calibration objects for which we 38304Snever * keep pointers here for use by bits & pieces of the driver 39304Snever */ 40304Sneverstatic struct smu_sdbp_cpuvcp *cpuvcp; 41304Sneverstatic int cpuvcp_version; 42304Sneverstatic struct smu_sdbp_cpudiode *cpudiode; 43304Sneverstatic struct smu_sdbp_slotspow *slotspow; 441909Siveresovstatic u8 *debugswitches; 450Sduke 460Sduke/* 470Sduke * SMU basic sensors objects 480Sduke */ 490Sduke 50304Sneverstatic LIST_HEAD(smu_ads); 510Sduke 520Sdukestruct smu_ad_sensor { 530Sduke struct list_head link; 540Sduke u32 reg; /* index in SMU */ 550Sduke struct wf_sensor sens; 560Sduke}; 570Sduke#define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens) 580Sduke 590Sdukestatic void smu_ads_release(struct wf_sensor *sr) 600Sduke{ 610Sduke struct smu_ad_sensor *ads = to_smu_ads(sr); 620Sduke 630Sduke kfree(ads); 640Sduke} 650Sduke 660Sdukestatic int smu_read_adc(u8 id, s32 *value) 670Sduke{ 680Sduke struct smu_simple_cmd cmd; 690Sduke DECLARE_COMPLETION_ONSTACK(comp); 709995Svdeshpande int rc; 710Sduke 720Sduke rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1, 730Sduke smu_done_complete, &comp, id); 740Sduke if (rc) 750Sduke return rc; 760Sduke wait_for_completion(&comp); 770Sduke if (cmd.cmd.status != 0) 780Sduke return cmd.cmd.status; 790Sduke if (cmd.cmd.reply_len != 2) { 800Sduke printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n", 810Sduke id, cmd.cmd.reply_len); 820Sduke return -EIO; 830Sduke } 840Sduke *value = *((u16 *)cmd.buffer); 850Sduke return 0; 868379Skvn} 870Sduke 888379Skvnstatic int smu_cputemp_get(struct wf_sensor *sr, s32 *value) 890Sduke{ 900Sduke struct smu_ad_sensor *ads = to_smu_ads(sr); 910Sduke int rc; 920Sduke s32 val; 930Sduke s64 scaled; 940Sduke 950Sduke rc = smu_read_adc(ads->reg, &val); 960Sduke if (rc) { 970Sduke printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n", 980Sduke rc); 990Sduke return rc; 1000Sduke } 1010Sduke 1028379Skvn /* Ok, we have to scale & adjust, taking units into account */ 10312084Smcberg scaled = (s64)(((u64)val) * (u64)cpudiode->m_value); 1048379Skvn scaled >>= 3; 1058379Skvn scaled += ((s64)cpudiode->b_value) << 9; 1068379Skvn *value = (s32)(scaled << 1); 10712084Smcberg 1080Sduke return 0; 1090Sduke} 1100Sduke 1111909Siveresovstatic int smu_cpuamp_get(struct wf_sensor *sr, s32 *value) 1120Sduke{ 1130Sduke struct smu_ad_sensor *ads = to_smu_ads(sr); 1140Sduke s32 val, scaled; 1158379Skvn int rc; 1160Sduke 1170Sduke rc = smu_read_adc(ads->reg, &val); 1180Sduke if (rc) { 1190Sduke printk(KERN_ERR "windfarm: read CPU current failed, err %d\n", 1200Sduke rc); 1210Sduke return rc; 1220Sduke } 1230Sduke 1240Sduke /* Ok, we have to scale & adjust, taking units into account */ 1250Sduke scaled = (s32)(val * (u32)cpuvcp->curr_scale); 1260Sduke scaled += (s32)cpuvcp->curr_offset; 1270Sduke *value = scaled << 4; 1280Sduke 1290Sduke return 0; 1300Sduke} 1310Sduke 1320Sdukestatic int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value) 1330Sduke{ 1340Sduke struct smu_ad_sensor *ads = to_smu_ads(sr); 1350Sduke s32 val, scaled; 1360Sduke int rc; 1370Sduke 1380Sduke rc = smu_read_adc(ads->reg, &val); 1390Sduke if (rc) { 1400Sduke printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n", 1410Sduke rc); 1420Sduke return rc; 1430Sduke } 1440Sduke 1450Sduke /* Ok, we have to scale & adjust, taking units into account */ 1460Sduke scaled = (s32)(val * (u32)cpuvcp->volt_scale); 1470Sduke scaled += (s32)cpuvcp->volt_offset; 1480Sduke *value = scaled << 4; 1490Sduke 1500Sduke return 0; 1510Sduke} 1520Sduke 1530Sdukestatic int smu_slotspow_get(struct wf_sensor *sr, s32 *value) 1540Sduke{ 1550Sduke struct smu_ad_sensor *ads = to_smu_ads(sr); 1560Sduke s32 val, scaled; 1570Sduke int rc; 1580Sduke 1590Sduke rc = smu_read_adc(ads->reg, &val); 1600Sduke if (rc) { 1610Sduke printk(KERN_ERR "windfarm: read slots power failed, err %d\n", 1620Sduke rc); 1630Sduke return rc; 1640Sduke } 1650Sduke 1660Sduke /* Ok, we have to scale & adjust, taking units into account */ 1670Sduke scaled = (s32)(val * (u32)slotspow->pow_scale); 1680Sduke scaled += (s32)slotspow->pow_offset; 1690Sduke *value = scaled << 4; 1700Sduke 1710Sduke return 0; 1720Sduke} 1730Sduke 1740Sduke 1750Sdukestatic const struct wf_sensor_ops smu_cputemp_ops = { 1760Sduke .get_value = smu_cputemp_get, 1770Sduke .release = smu_ads_release, 1780Sduke .owner = THIS_MODULE, 1790Sduke}; 1800Sdukestatic const struct wf_sensor_ops smu_cpuamp_ops = { 1810Sduke .get_value = smu_cpuamp_get, 1820Sduke .release = smu_ads_release, 1830Sduke .owner = THIS_MODULE, 1840Sduke}; 1850Sdukestatic const struct wf_sensor_ops smu_cpuvolt_ops = { 1860Sduke .get_value = smu_cpuvolt_get, 1870Sduke .release = smu_ads_release, 1880Sduke .owner = THIS_MODULE, 1890Sduke}; 1900Sdukestatic const struct wf_sensor_ops smu_slotspow_ops = { 1910Sduke .get_value = smu_slotspow_get, 1920Sduke .release = smu_ads_release, 1930Sduke .owner = THIS_MODULE, 1940Sduke}; 1951879Sstefank 1961879Sstefank 197static struct smu_ad_sensor *smu_ads_create(struct device_node *node) 198{ 199 struct smu_ad_sensor *ads; 200 const char *l; 201 const u32 *v; 202 203 ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL); 204 if (ads == NULL) 205 return NULL; 206 l = of_get_property(node, "location", NULL); 207 if (l == NULL) 208 goto fail; 209 210 /* We currently pick the sensors based on the OF name and location 211 * properties, while Darwin uses the sensor-id's. 212 * The problem with the IDs is that they are model specific while it 213 * looks like apple has been doing a reasonably good job at keeping 214 * the names and locations consistents so I'll stick with the names 215 * and locations for now. 216 */ 217 if (of_node_is_type(node, "temp-sensor") && 218 !strcmp(l, "CPU T-Diode")) { 219 ads->sens.ops = &smu_cputemp_ops; 220 ads->sens.name = "cpu-temp"; 221 if (cpudiode == NULL) { 222 DBG("wf: cpudiode partition (%02x) not found\n", 223 SMU_SDB_CPUDIODE_ID); 224 goto fail; 225 } 226 } else if (of_node_is_type(node, "current-sensor") && 227 !strcmp(l, "CPU Current")) { 228 ads->sens.ops = &smu_cpuamp_ops; 229 ads->sens.name = "cpu-current"; 230 if (cpuvcp == NULL) { 231 DBG("wf: cpuvcp partition (%02x) not found\n", 232 SMU_SDB_CPUVCP_ID); 233 goto fail; 234 } 235 } else if (of_node_is_type(node, "voltage-sensor") && 236 !strcmp(l, "CPU Voltage")) { 237 ads->sens.ops = &smu_cpuvolt_ops; 238 ads->sens.name = "cpu-voltage"; 239 if (cpuvcp == NULL) { 240 DBG("wf: cpuvcp partition (%02x) not found\n", 241 SMU_SDB_CPUVCP_ID); 242 goto fail; 243 } 244 } else if (of_node_is_type(node, "power-sensor") && 245 !strcmp(l, "Slots Power")) { 246 ads->sens.ops = &smu_slotspow_ops; 247 ads->sens.name = "slots-power"; 248 if (slotspow == NULL) { 249 DBG("wf: slotspow partition (%02x) not found\n", 250 SMU_SDB_SLOTSPOW_ID); 251 goto fail; 252 } 253 } else 254 goto fail; 255 256 v = of_get_property(node, "reg", NULL); 257 if (v == NULL) 258 goto fail; 259 ads->reg = *v; 260 261 if (wf_register_sensor(&ads->sens)) 262 goto fail; 263 return ads; 264 fail: 265 kfree(ads); 266 return NULL; 267} 268 269/* 270 * SMU Power combo sensor object 271 */ 272 273struct smu_cpu_power_sensor { 274 struct list_head link; 275 struct wf_sensor *volts; 276 struct wf_sensor *amps; 277 unsigned int fake_volts : 1; 278 unsigned int quadratic : 1; 279 struct wf_sensor sens; 280}; 281#define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens) 282 283static struct smu_cpu_power_sensor *smu_cpu_power; 284 285static void smu_cpu_power_release(struct wf_sensor *sr) 286{ 287 struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr); 288 289 if (pow->volts) 290 wf_put_sensor(pow->volts); 291 if (pow->amps) 292 wf_put_sensor(pow->amps); 293 kfree(pow); 294} 295 296static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value) 297{ 298 struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr); 299 s32 volts, amps, power; 300 u64 tmps, tmpa, tmpb; 301 int rc; 302 303 rc = pow->amps->ops->get_value(pow->amps, &s); 304 if (rc) 305 return rc; 306 307 if (pow->fake_volts) { 308 *value = amps * 12 - 0x30000; 309 return 0; 310 } 311 312 rc = pow->volts->ops->get_value(pow->volts, &volts); 313 if (rc) 314 return rc; 315 316 power = (s32)((((u64)volts) * ((u64)amps)) >> 16); 317 if (!pow->quadratic) { 318 *value = power; 319 return 0; 320 } 321 tmps = (((u64)power) * ((u64)power)) >> 16; 322 tmpa = ((u64)cpuvcp->power_quads[0]) * tmps; 323 tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power); 324 *value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12); 325 326 return 0; 327} 328 329static const struct wf_sensor_ops smu_cpu_power_ops = { 330 .get_value = smu_cpu_power_get, 331 .release = smu_cpu_power_release, 332 .owner = THIS_MODULE, 333}; 334 335 336static struct smu_cpu_power_sensor * 337smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps) 338{ 339 struct smu_cpu_power_sensor *pow; 340 341 pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL); 342 if (pow == NULL) 343 return NULL; 344 pow->sens.ops = &smu_cpu_power_ops; 345 pow->sens.name = "cpu-power"; 346 347 wf_get_sensor(volts); 348 pow->volts = volts; 349 wf_get_sensor(amps); 350 pow->amps = amps; 351 352 /* Some early machines need a faked voltage */ 353 if (debugswitches && ((*debugswitches) & 0x80)) { 354 printk(KERN_INFO "windfarm: CPU Power sensor using faked" 355 " voltage !\n"); 356 pow->fake_volts = 1; 357 } else 358 pow->fake_volts = 0; 359 360 /* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now, 361 * I yet have to figure out what's up with 8,2 and will have to 362 * adjust for later, unless we can 100% trust the SDB partition... 363 */ 364 if ((of_machine_is_compatible("PowerMac8,1") || 365 of_machine_is_compatible("PowerMac8,2") || 366 of_machine_is_compatible("PowerMac9,1")) && 367 cpuvcp_version >= 2) { 368 pow->quadratic = 1; 369 DBG("windfarm: CPU Power using quadratic transform\n"); 370 } else 371 pow->quadratic = 0; 372 373 if (wf_register_sensor(&pow->sens)) 374 goto fail; 375 return pow; 376 fail: 377 kfree(pow); 378 return NULL; 379} 380 381static void smu_fetch_param_partitions(void) 382{ 383 const struct smu_sdbp_header *hdr; 384 385 /* Get CPU voltage/current/power calibration data */ 386 hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL); 387 if (hdr != NULL) { 388 cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1]; 389 /* Keep version around */ 390 cpuvcp_version = hdr->version; 391 } 392 393 /* Get CPU diode calibration data */ 394 hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL); 395 if (hdr != NULL) 396 cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1]; 397 398 /* Get slots power calibration data if any */ 399 hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL); 400 if (hdr != NULL) 401 slotspow = (struct smu_sdbp_slotspow *)&hdr[1]; 402 403 /* Get debug switches if any */ 404 hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL); 405 if (hdr != NULL) 406 debugswitches = (u8 *)&hdr[1]; 407} 408 409static int __init smu_sensors_init(void) 410{ 411 struct device_node *smu, *sensors, *s; 412 struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL; 413 414 if (!smu_present()) 415 return -ENODEV; 416 417 /* Get parameters partitions */ 418 smu_fetch_param_partitions(); 419 420 smu = of_find_node_by_type(NULL, "smu"); 421 if (smu == NULL) 422 return -ENODEV; 423 424 /* Look for sensors subdir */ 425 for_each_child_of_node(smu, sensors) 426 if (of_node_name_eq(sensors, "sensors")) 427 break; 428 429 of_node_put(smu); 430 431 /* Create basic sensors */ 432 for (s = NULL; 433 sensors && (s = of_get_next_child(sensors, s)) != NULL;) { 434 struct smu_ad_sensor *ads; 435 436 ads = smu_ads_create(s); 437 if (ads == NULL) 438 continue; 439 list_add(&ads->link, &smu_ads); 440 /* keep track of cpu voltage & current */ 441 if (!strcmp(ads->sens.name, "cpu-voltage")) 442 volt_sensor = ads; 443 else if (!strcmp(ads->sens.name, "cpu-current")) 444 curr_sensor = ads; 445 } 446 447 of_node_put(sensors); 448 449 /* Create CPU power sensor if possible */ 450 if (volt_sensor && curr_sensor) 451 smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens, 452 &curr_sensor->sens); 453 454 return 0; 455} 456 457static void __exit smu_sensors_exit(void) 458{ 459 struct smu_ad_sensor *ads; 460 461 /* dispose of power sensor */ 462 if (smu_cpu_power) 463 wf_unregister_sensor(&smu_cpu_power->sens); 464 465 /* dispose of basic sensors */ 466 while (!list_empty(&smu_ads)) { 467 ads = list_entry(smu_ads.next, struct smu_ad_sensor, link); 468 list_del(&ads->link); 469 wf_unregister_sensor(&ads->sens); 470 } 471} 472 473 474module_init(smu_sensors_init); 475module_exit(smu_sensors_exit); 476 477MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); 478MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control"); 479MODULE_LICENSE("GPL"); 480 481