windfarm_smu_sensors.c 11 KB

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