blob: a58f6733381ae6c8a94635b2aa544564d9c356fa [file] [log] [blame]
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +11001/*
2 * Windfarm PowerMac thermal control. SMU based sensors
3 *
4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
6 *
7 * Released under the term of the GNU GPL v2.
8 */
9
10#include <linux/types.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/delay.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/wait.h>
Tim Schmielaude259682006-01-08 01:02:05 -080017#include <linux/completion.h>
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110018#include <asm/prom.h>
19#include <asm/machdep.h>
20#include <asm/io.h>
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110021#include <asm/sections.h>
22#include <asm/smu.h>
23
24#include "windfarm.h"
25
26#define VERSION "0.2"
27
28#undef DEBUG
29
30#ifdef DEBUG
31#define DBG(args...) printk(args)
32#else
33#define DBG(args...) do { } while(0)
34#endif
35
36/*
37 * Various SMU "partitions" calibration objects for which we
38 * keep pointers here for use by bits & pieces of the driver
39 */
40static struct smu_sdbp_cpuvcp *cpuvcp;
41static int cpuvcp_version;
42static struct smu_sdbp_cpudiode *cpudiode;
43static struct smu_sdbp_slotspow *slotspow;
44static u8 *debugswitches;
45
46/*
47 * SMU basic sensors objects
48 */
49
50static LIST_HEAD(smu_ads);
51
52struct smu_ad_sensor {
53 struct list_head link;
54 u32 reg; /* index in SMU */
55 struct wf_sensor sens;
56};
57#define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
58
59static void smu_ads_release(struct wf_sensor *sr)
60{
61 struct smu_ad_sensor *ads = to_smu_ads(sr);
62
63 kfree(ads);
64}
65
66static int smu_read_adc(u8 id, s32 *value)
67{
68 struct smu_simple_cmd cmd;
Peter Zijlstra6e9a4732006-09-30 23:28:10 -070069 DECLARE_COMPLETION_ONSTACK(comp);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +110070 int rc;
71
72 rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
73 smu_done_complete, &comp, id);
74 if (rc)
75 return rc;
76 wait_for_completion(&comp);
77 if (cmd.cmd.status != 0)
78 return cmd.cmd.status;
79 if (cmd.cmd.reply_len != 2) {
80 printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
81 id, cmd.cmd.reply_len);
82 return -EIO;
83 }
84 *value = *((u16 *)cmd.buffer);
85 return 0;
86}
87
88static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
89{
90 struct smu_ad_sensor *ads = to_smu_ads(sr);
91 int rc;
92 s32 val;
93 s64 scaled;
94
95 rc = smu_read_adc(ads->reg, &val);
96 if (rc) {
97 printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
98 rc);
99 return rc;
100 }
101
102 /* Ok, we have to scale & adjust, taking units into account */
103 scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
104 scaled >>= 3;
105 scaled += ((s64)cpudiode->b_value) << 9;
106 *value = (s32)(scaled << 1);
107
108 return 0;
109}
110
111static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
112{
113 struct smu_ad_sensor *ads = to_smu_ads(sr);
114 s32 val, scaled;
115 int rc;
116
117 rc = smu_read_adc(ads->reg, &val);
118 if (rc) {
119 printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
120 rc);
121 return rc;
122 }
123
124 /* Ok, we have to scale & adjust, taking units into account */
125 scaled = (s32)(val * (u32)cpuvcp->curr_scale);
126 scaled += (s32)cpuvcp->curr_offset;
127 *value = scaled << 4;
128
129 return 0;
130}
131
132static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
133{
134 struct smu_ad_sensor *ads = to_smu_ads(sr);
135 s32 val, scaled;
136 int rc;
137
138 rc = smu_read_adc(ads->reg, &val);
139 if (rc) {
140 printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
141 rc);
142 return rc;
143 }
144
145 /* Ok, we have to scale & adjust, taking units into account */
146 scaled = (s32)(val * (u32)cpuvcp->volt_scale);
147 scaled += (s32)cpuvcp->volt_offset;
148 *value = scaled << 4;
149
150 return 0;
151}
152
153static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
154{
155 struct smu_ad_sensor *ads = to_smu_ads(sr);
156 s32 val, scaled;
157 int rc;
158
159 rc = smu_read_adc(ads->reg, &val);
160 if (rc) {
161 printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
162 rc);
163 return rc;
164 }
165
166 /* Ok, we have to scale & adjust, taking units into account */
167 scaled = (s32)(val * (u32)slotspow->pow_scale);
168 scaled += (s32)slotspow->pow_offset;
169 *value = scaled << 4;
170
171 return 0;
172}
173
174
Julia Lawallde854e52017-08-02 23:01:45 +0200175static const struct wf_sensor_ops smu_cputemp_ops = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100176 .get_value = smu_cputemp_get,
177 .release = smu_ads_release,
178 .owner = THIS_MODULE,
179};
Julia Lawallde854e52017-08-02 23:01:45 +0200180static const struct wf_sensor_ops smu_cpuamp_ops = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100181 .get_value = smu_cpuamp_get,
182 .release = smu_ads_release,
183 .owner = THIS_MODULE,
184};
Julia Lawallde854e52017-08-02 23:01:45 +0200185static const struct wf_sensor_ops smu_cpuvolt_ops = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100186 .get_value = smu_cpuvolt_get,
187 .release = smu_ads_release,
188 .owner = THIS_MODULE,
189};
Julia Lawallde854e52017-08-02 23:01:45 +0200190static const struct wf_sensor_ops smu_slotspow_ops = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100191 .get_value = smu_slotspow_get,
192 .release = smu_ads_release,
193 .owner = THIS_MODULE,
194};
195
196
197static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
198{
199 struct smu_ad_sensor *ads;
Rob Herringbf82d372018-11-16 16:11:01 -0600200 const char *l;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000201 const u32 *v;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100202
203 ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
204 if (ads == NULL)
205 return NULL;
Stephen Rothwell01b27262007-04-27 13:41:15 +1000206 l = of_get_property(node, "location", NULL);
Rob Herringbf82d372018-11-16 16:11:01 -0600207 if (l == NULL)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100208 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 */
Rob Herringbf82d372018-11-16 16:11:01 -0600217 if (of_node_is_type(node, "temp-sensor") &&
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100218 !strcmp(l, "CPU T-Diode")) {
219 ads->sens.ops = &smu_cputemp_ops;
220 ads->sens.name = "cpu-temp";
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100221 if (cpudiode == NULL) {
222 DBG("wf: cpudiode partition (%02x) not found\n",
223 SMU_SDB_CPUDIODE_ID);
224 goto fail;
225 }
Rob Herringbf82d372018-11-16 16:11:01 -0600226 } else if (of_node_is_type(node, "current-sensor") &&
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100227 !strcmp(l, "CPU Current")) {
228 ads->sens.ops = &smu_cpuamp_ops;
229 ads->sens.name = "cpu-current";
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100230 if (cpuvcp == NULL) {
231 DBG("wf: cpuvcp partition (%02x) not found\n",
232 SMU_SDB_CPUVCP_ID);
233 goto fail;
234 }
Rob Herringbf82d372018-11-16 16:11:01 -0600235 } else if (of_node_is_type(node, "voltage-sensor") &&
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100236 !strcmp(l, "CPU Voltage")) {
237 ads->sens.ops = &smu_cpuvolt_ops;
238 ads->sens.name = "cpu-voltage";
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100239 if (cpuvcp == NULL) {
240 DBG("wf: cpuvcp partition (%02x) not found\n",
241 SMU_SDB_CPUVCP_ID);
242 goto fail;
243 }
Rob Herringbf82d372018-11-16 16:11:01 -0600244 } else if (of_node_is_type(node, "power-sensor") &&
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100245 !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
Stephen Rothwell01b27262007-04-27 13:41:15 +1000256 v = of_get_property(node, "reg", NULL);
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100257 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 int fake_volts : 1;
278 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, &amps);
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
Julia Lawallde854e52017-08-02 23:01:45 +0200329static const struct wf_sensor_ops smu_cpu_power_ops = {
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100330 .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 */
Grant Likely71a157e2010-02-01 21:34:14 -0700364 if ((of_machine_is_compatible("PowerMac8,1") ||
365 of_machine_is_compatible("PowerMac8,2") ||
366 of_machine_is_compatible("PowerMac9,1")) &&
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100367 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
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100381static void smu_fetch_param_partitions(void)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100382{
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000383 const struct smu_sdbp_header *hdr;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100384
385 /* Get CPU voltage/current/power calibration data */
386 hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100387 if (hdr != NULL) {
388 cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
389 /* Keep version around */
390 cpuvcp_version = hdr->version;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100391 }
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100392
393 /* Get CPU diode calibration data */
394 hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100395 if (hdr != NULL)
396 cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100397
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];
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100407}
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;
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100413
414 if (!smu_present())
415 return -ENODEV;
416
417 /* Get parameters partitions */
Benjamin Herrenschmidtac171c42006-02-08 16:42:51 +1100418 smu_fetch_param_partitions();
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100419
420 smu = of_find_node_by_type(NULL, "smu");
421 if (smu == NULL)
422 return -ENODEV;
423
424 /* Look for sensors subdir */
425 for (sensors = NULL;
426 (sensors = of_get_next_child(smu, sensors)) != NULL;)
Rob Herringf1e0add2018-12-05 13:50:28 -0600427 if (of_node_name_eq(sensors, "sensors"))
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100428 break;
429
430 of_node_put(smu);
431
432 /* Create basic sensors */
433 for (s = NULL;
434 sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
435 struct smu_ad_sensor *ads;
436
437 ads = smu_ads_create(s);
438 if (ads == NULL)
439 continue;
440 list_add(&ads->link, &smu_ads);
441 /* keep track of cpu voltage & current */
442 if (!strcmp(ads->sens.name, "cpu-voltage"))
443 volt_sensor = ads;
444 else if (!strcmp(ads->sens.name, "cpu-current"))
445 curr_sensor = ads;
446 }
447
448 of_node_put(sensors);
449
450 /* Create CPU power sensor if possible */
451 if (volt_sensor && curr_sensor)
452 smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
453 &curr_sensor->sens);
454
455 return 0;
456}
457
458static void __exit smu_sensors_exit(void)
459{
460 struct smu_ad_sensor *ads;
461
462 /* dispose of power sensor */
463 if (smu_cpu_power)
464 wf_unregister_sensor(&smu_cpu_power->sens);
465
466 /* dispose of basic sensors */
467 while (!list_empty(&smu_ads)) {
468 ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
469 list_del(&ads->link);
470 wf_unregister_sensor(&ads->sens);
471 }
472}
473
474
475module_init(smu_sensors_init);
476module_exit(smu_sensors_exit);
477
478MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
479MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
480MODULE_LICENSE("GPL");
481