windfarm_smu_sensors.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Windfarm PowerMac thermal control. SMU based sensors
  4. *
  5. * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
  6. * <benh@kernel.crashing.org>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/wait.h>
  15. #include <linux/completion.h>
  16. #include <asm/prom.h>
  17. #include <asm/machdep.h>
  18. #include <asm/io.h>
  19. #include <asm/sections.h>
  20. #include <asm/smu.h>
  21. #include "windfarm.h"
  22. #define VERSION "0.2"
  23. #undef DEBUG
  24. #ifdef DEBUG
  25. #define DBG(args...) printk(args)
  26. #else
  27. #define DBG(args...) do { } while(0)
  28. #endif
  29. /*
  30. * Various SMU "partitions" calibration objects for which we
  31. * keep pointers here for use by bits & pieces of the driver
  32. */
  33. static struct smu_sdbp_cpuvcp *cpuvcp;
  34. static int cpuvcp_version;
  35. static struct smu_sdbp_cpudiode *cpudiode;
  36. static struct smu_sdbp_slotspow *slotspow;
  37. static u8 *debugswitches;
  38. /*
  39. * SMU basic sensors objects
  40. */
  41. static LIST_HEAD(smu_ads);
  42. struct smu_ad_sensor {
  43. struct list_head link;
  44. u32 reg; /* index in SMU */
  45. struct wf_sensor sens;
  46. };
  47. #define to_smu_ads(c) container_of(c, struct smu_ad_sensor, sens)
  48. static void smu_ads_release(struct wf_sensor *sr)
  49. {
  50. struct smu_ad_sensor *ads = to_smu_ads(sr);
  51. kfree(ads);
  52. }
  53. static int smu_read_adc(u8 id, s32 *value)
  54. {
  55. struct smu_simple_cmd cmd;
  56. DECLARE_COMPLETION_ONSTACK(comp);
  57. int rc;
  58. rc = smu_queue_simple(&cmd, SMU_CMD_READ_ADC, 1,
  59. smu_done_complete, &comp, id);
  60. if (rc)
  61. return rc;
  62. wait_for_completion(&comp);
  63. if (cmd.cmd.status != 0)
  64. return cmd.cmd.status;
  65. if (cmd.cmd.reply_len != 2) {
  66. printk(KERN_ERR "winfarm: read ADC 0x%x returned %d bytes !\n",
  67. id, cmd.cmd.reply_len);
  68. return -EIO;
  69. }
  70. *value = *((u16 *)cmd.buffer);
  71. return 0;
  72. }
  73. static int smu_cputemp_get(struct wf_sensor *sr, s32 *value)
  74. {
  75. struct smu_ad_sensor *ads = to_smu_ads(sr);
  76. int rc;
  77. s32 val;
  78. s64 scaled;
  79. rc = smu_read_adc(ads->reg, &val);
  80. if (rc) {
  81. printk(KERN_ERR "windfarm: read CPU temp failed, err %d\n",
  82. rc);
  83. return rc;
  84. }
  85. /* Ok, we have to scale & adjust, taking units into account */
  86. scaled = (s64)(((u64)val) * (u64)cpudiode->m_value);
  87. scaled >>= 3;
  88. scaled += ((s64)cpudiode->b_value) << 9;
  89. *value = (s32)(scaled << 1);
  90. return 0;
  91. }
  92. static int smu_cpuamp_get(struct wf_sensor *sr, s32 *value)
  93. {
  94. struct smu_ad_sensor *ads = to_smu_ads(sr);
  95. s32 val, scaled;
  96. int rc;
  97. rc = smu_read_adc(ads->reg, &val);
  98. if (rc) {
  99. printk(KERN_ERR "windfarm: read CPU current failed, err %d\n",
  100. rc);
  101. return rc;
  102. }
  103. /* Ok, we have to scale & adjust, taking units into account */
  104. scaled = (s32)(val * (u32)cpuvcp->curr_scale);
  105. scaled += (s32)cpuvcp->curr_offset;
  106. *value = scaled << 4;
  107. return 0;
  108. }
  109. static int smu_cpuvolt_get(struct wf_sensor *sr, s32 *value)
  110. {
  111. struct smu_ad_sensor *ads = to_smu_ads(sr);
  112. s32 val, scaled;
  113. int rc;
  114. rc = smu_read_adc(ads->reg, &val);
  115. if (rc) {
  116. printk(KERN_ERR "windfarm: read CPU voltage failed, err %d\n",
  117. rc);
  118. return rc;
  119. }
  120. /* Ok, we have to scale & adjust, taking units into account */
  121. scaled = (s32)(val * (u32)cpuvcp->volt_scale);
  122. scaled += (s32)cpuvcp->volt_offset;
  123. *value = scaled << 4;
  124. return 0;
  125. }
  126. static int smu_slotspow_get(struct wf_sensor *sr, s32 *value)
  127. {
  128. struct smu_ad_sensor *ads = to_smu_ads(sr);
  129. s32 val, scaled;
  130. int rc;
  131. rc = smu_read_adc(ads->reg, &val);
  132. if (rc) {
  133. printk(KERN_ERR "windfarm: read slots power failed, err %d\n",
  134. rc);
  135. return rc;
  136. }
  137. /* Ok, we have to scale & adjust, taking units into account */
  138. scaled = (s32)(val * (u32)slotspow->pow_scale);
  139. scaled += (s32)slotspow->pow_offset;
  140. *value = scaled << 4;
  141. return 0;
  142. }
  143. static const struct wf_sensor_ops smu_cputemp_ops = {
  144. .get_value = smu_cputemp_get,
  145. .release = smu_ads_release,
  146. .owner = THIS_MODULE,
  147. };
  148. static const struct wf_sensor_ops smu_cpuamp_ops = {
  149. .get_value = smu_cpuamp_get,
  150. .release = smu_ads_release,
  151. .owner = THIS_MODULE,
  152. };
  153. static const struct wf_sensor_ops smu_cpuvolt_ops = {
  154. .get_value = smu_cpuvolt_get,
  155. .release = smu_ads_release,
  156. .owner = THIS_MODULE,
  157. };
  158. static const struct wf_sensor_ops smu_slotspow_ops = {
  159. .get_value = smu_slotspow_get,
  160. .release = smu_ads_release,
  161. .owner = THIS_MODULE,
  162. };
  163. static struct smu_ad_sensor *smu_ads_create(struct device_node *node)
  164. {
  165. struct smu_ad_sensor *ads;
  166. const char *l;
  167. const u32 *v;
  168. ads = kmalloc(sizeof(struct smu_ad_sensor), GFP_KERNEL);
  169. if (ads == NULL)
  170. return NULL;
  171. l = of_get_property(node, "location", NULL);
  172. if (l == NULL)
  173. goto fail;
  174. /* We currently pick the sensors based on the OF name and location
  175. * properties, while Darwin uses the sensor-id's.
  176. * The problem with the IDs is that they are model specific while it
  177. * looks like apple has been doing a reasonably good job at keeping
  178. * the names and locations consistents so I'll stick with the names
  179. * and locations for now.
  180. */
  181. if (of_node_is_type(node, "temp-sensor") &&
  182. !strcmp(l, "CPU T-Diode")) {
  183. ads->sens.ops = &smu_cputemp_ops;
  184. ads->sens.name = "cpu-temp";
  185. if (cpudiode == NULL) {
  186. DBG("wf: cpudiode partition (%02x) not found\n",
  187. SMU_SDB_CPUDIODE_ID);
  188. goto fail;
  189. }
  190. } else if (of_node_is_type(node, "current-sensor") &&
  191. !strcmp(l, "CPU Current")) {
  192. ads->sens.ops = &smu_cpuamp_ops;
  193. ads->sens.name = "cpu-current";
  194. if (cpuvcp == NULL) {
  195. DBG("wf: cpuvcp partition (%02x) not found\n",
  196. SMU_SDB_CPUVCP_ID);
  197. goto fail;
  198. }
  199. } else if (of_node_is_type(node, "voltage-sensor") &&
  200. !strcmp(l, "CPU Voltage")) {
  201. ads->sens.ops = &smu_cpuvolt_ops;
  202. ads->sens.name = "cpu-voltage";
  203. if (cpuvcp == NULL) {
  204. DBG("wf: cpuvcp partition (%02x) not found\n",
  205. SMU_SDB_CPUVCP_ID);
  206. goto fail;
  207. }
  208. } else if (of_node_is_type(node, "power-sensor") &&
  209. !strcmp(l, "Slots Power")) {
  210. ads->sens.ops = &smu_slotspow_ops;
  211. ads->sens.name = "slots-power";
  212. if (slotspow == NULL) {
  213. DBG("wf: slotspow partition (%02x) not found\n",
  214. SMU_SDB_SLOTSPOW_ID);
  215. goto fail;
  216. }
  217. } else
  218. goto fail;
  219. v = of_get_property(node, "reg", NULL);
  220. if (v == NULL)
  221. goto fail;
  222. ads->reg = *v;
  223. if (wf_register_sensor(&ads->sens))
  224. goto fail;
  225. return ads;
  226. fail:
  227. kfree(ads);
  228. return NULL;
  229. }
  230. /*
  231. * SMU Power combo sensor object
  232. */
  233. struct smu_cpu_power_sensor {
  234. struct list_head link;
  235. struct wf_sensor *volts;
  236. struct wf_sensor *amps;
  237. int fake_volts : 1;
  238. int quadratic : 1;
  239. struct wf_sensor sens;
  240. };
  241. #define to_smu_cpu_power(c) container_of(c, struct smu_cpu_power_sensor, sens)
  242. static struct smu_cpu_power_sensor *smu_cpu_power;
  243. static void smu_cpu_power_release(struct wf_sensor *sr)
  244. {
  245. struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
  246. if (pow->volts)
  247. wf_put_sensor(pow->volts);
  248. if (pow->amps)
  249. wf_put_sensor(pow->amps);
  250. kfree(pow);
  251. }
  252. static int smu_cpu_power_get(struct wf_sensor *sr, s32 *value)
  253. {
  254. struct smu_cpu_power_sensor *pow = to_smu_cpu_power(sr);
  255. s32 volts, amps, power;
  256. u64 tmps, tmpa, tmpb;
  257. int rc;
  258. rc = pow->amps->ops->get_value(pow->amps, &amps);
  259. if (rc)
  260. return rc;
  261. if (pow->fake_volts) {
  262. *value = amps * 12 - 0x30000;
  263. return 0;
  264. }
  265. rc = pow->volts->ops->get_value(pow->volts, &volts);
  266. if (rc)
  267. return rc;
  268. power = (s32)((((u64)volts) * ((u64)amps)) >> 16);
  269. if (!pow->quadratic) {
  270. *value = power;
  271. return 0;
  272. }
  273. tmps = (((u64)power) * ((u64)power)) >> 16;
  274. tmpa = ((u64)cpuvcp->power_quads[0]) * tmps;
  275. tmpb = ((u64)cpuvcp->power_quads[1]) * ((u64)power);
  276. *value = (tmpa >> 28) + (tmpb >> 28) + (cpuvcp->power_quads[2] >> 12);
  277. return 0;
  278. }
  279. static const struct wf_sensor_ops smu_cpu_power_ops = {
  280. .get_value = smu_cpu_power_get,
  281. .release = smu_cpu_power_release,
  282. .owner = THIS_MODULE,
  283. };
  284. static struct smu_cpu_power_sensor *
  285. smu_cpu_power_create(struct wf_sensor *volts, struct wf_sensor *amps)
  286. {
  287. struct smu_cpu_power_sensor *pow;
  288. pow = kmalloc(sizeof(struct smu_cpu_power_sensor), GFP_KERNEL);
  289. if (pow == NULL)
  290. return NULL;
  291. pow->sens.ops = &smu_cpu_power_ops;
  292. pow->sens.name = "cpu-power";
  293. wf_get_sensor(volts);
  294. pow->volts = volts;
  295. wf_get_sensor(amps);
  296. pow->amps = amps;
  297. /* Some early machines need a faked voltage */
  298. if (debugswitches && ((*debugswitches) & 0x80)) {
  299. printk(KERN_INFO "windfarm: CPU Power sensor using faked"
  300. " voltage !\n");
  301. pow->fake_volts = 1;
  302. } else
  303. pow->fake_volts = 0;
  304. /* Try to use quadratic transforms on PowerMac8,1 and 9,1 for now,
  305. * I yet have to figure out what's up with 8,2 and will have to
  306. * adjust for later, unless we can 100% trust the SDB partition...
  307. */
  308. if ((of_machine_is_compatible("PowerMac8,1") ||
  309. of_machine_is_compatible("PowerMac8,2") ||
  310. of_machine_is_compatible("PowerMac9,1")) &&
  311. cpuvcp_version >= 2) {
  312. pow->quadratic = 1;
  313. DBG("windfarm: CPU Power using quadratic transform\n");
  314. } else
  315. pow->quadratic = 0;
  316. if (wf_register_sensor(&pow->sens))
  317. goto fail;
  318. return pow;
  319. fail:
  320. kfree(pow);
  321. return NULL;
  322. }
  323. static void smu_fetch_param_partitions(void)
  324. {
  325. const struct smu_sdbp_header *hdr;
  326. /* Get CPU voltage/current/power calibration data */
  327. hdr = smu_get_sdb_partition(SMU_SDB_CPUVCP_ID, NULL);
  328. if (hdr != NULL) {
  329. cpuvcp = (struct smu_sdbp_cpuvcp *)&hdr[1];
  330. /* Keep version around */
  331. cpuvcp_version = hdr->version;
  332. }
  333. /* Get CPU diode calibration data */
  334. hdr = smu_get_sdb_partition(SMU_SDB_CPUDIODE_ID, NULL);
  335. if (hdr != NULL)
  336. cpudiode = (struct smu_sdbp_cpudiode *)&hdr[1];
  337. /* Get slots power calibration data if any */
  338. hdr = smu_get_sdb_partition(SMU_SDB_SLOTSPOW_ID, NULL);
  339. if (hdr != NULL)
  340. slotspow = (struct smu_sdbp_slotspow *)&hdr[1];
  341. /* Get debug switches if any */
  342. hdr = smu_get_sdb_partition(SMU_SDB_DEBUG_SWITCHES_ID, NULL);
  343. if (hdr != NULL)
  344. debugswitches = (u8 *)&hdr[1];
  345. }
  346. static int __init smu_sensors_init(void)
  347. {
  348. struct device_node *smu, *sensors, *s;
  349. struct smu_ad_sensor *volt_sensor = NULL, *curr_sensor = NULL;
  350. if (!smu_present())
  351. return -ENODEV;
  352. /* Get parameters partitions */
  353. smu_fetch_param_partitions();
  354. smu = of_find_node_by_type(NULL, "smu");
  355. if (smu == NULL)
  356. return -ENODEV;
  357. /* Look for sensors subdir */
  358. for_each_child_of_node(smu, sensors)
  359. if (of_node_name_eq(sensors, "sensors"))
  360. break;
  361. of_node_put(smu);
  362. /* Create basic sensors */
  363. for (s = NULL;
  364. sensors && (s = of_get_next_child(sensors, s)) != NULL;) {
  365. struct smu_ad_sensor *ads;
  366. ads = smu_ads_create(s);
  367. if (ads == NULL)
  368. continue;
  369. list_add(&ads->link, &smu_ads);
  370. /* keep track of cpu voltage & current */
  371. if (!strcmp(ads->sens.name, "cpu-voltage"))
  372. volt_sensor = ads;
  373. else if (!strcmp(ads->sens.name, "cpu-current"))
  374. curr_sensor = ads;
  375. }
  376. of_node_put(sensors);
  377. /* Create CPU power sensor if possible */
  378. if (volt_sensor && curr_sensor)
  379. smu_cpu_power = smu_cpu_power_create(&volt_sensor->sens,
  380. &curr_sensor->sens);
  381. return 0;
  382. }
  383. static void __exit smu_sensors_exit(void)
  384. {
  385. struct smu_ad_sensor *ads;
  386. /* dispose of power sensor */
  387. if (smu_cpu_power)
  388. wf_unregister_sensor(&smu_cpu_power->sens);
  389. /* dispose of basic sensors */
  390. while (!list_empty(&smu_ads)) {
  391. ads = list_entry(smu_ads.next, struct smu_ad_sensor, link);
  392. list_del(&ads->link);
  393. wf_unregister_sensor(&ads->sens);
  394. }
  395. }
  396. module_init(smu_sensors_init);
  397. module_exit(smu_sensors_exit);
  398. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  399. MODULE_DESCRIPTION("SMU sensor objects for PowerMacs thermal control");
  400. MODULE_LICENSE("GPL");