tpec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* ThinkPad embedded-controller sensors and power sources (see tpec.h).
  2. *
  3. * The X201 (i5 M 520, Arrandale) has no IT87xx Super I/O like the
  4. * AT3IONT-I. Its board temperatures, fan tachometer, battery and AC state
  5. * live behind the ACPI embedded controller, which Linux's thinkpad_acpi
  6. * reads through the same ports: command/status 0x66, data 0x62. The EC
  7. * protocol is simple enough to drive directly:
  8. *
  9. * write 0x80 (read) to 0x66, then the register index to 0x62,
  10. * wait for OBF (0x01) and read the byte back from 0x62.
  11. *
  12. * The SMBIOS type 1 entry gates the probe to Lenovo/IBM machines, so the
  13. * EC ports are never touched on the ASUS board, QEMU or other hardware.
  14. *
  15. * Two details matter on real hardware:
  16. * - There is no SCI handler, so hotkey events queue up in the output buffer.
  17. * A pending byte is drained before every command; otherwise the EC would
  18. * never replace it with the register we asked for.
  19. * - Firmware keeps running in legacy/SMM mode (we have no ACPI interpreter),
  20. * so an SMI can access the EC between our port accesses. The status
  21. * handshake plus bounded polling keeps that to an occasional bad sample.
  22. * Temperature/fan reads are read-only; the battery reader writes only the
  23. * H8 information-page selector at 0x81 and restores it afterwards.
  24. *
  25. * EC register map (thinkpad_acpi/H8): 0x78..0x7F and 0xC0..0xC7 temperatures
  26. * (0x80 = sensor not present, 0x00 = register not implemented), 0x84/0x85
  27. * fan tachometer LSB/MSB and 0x2F fan control (bit 7 automatic, bit 6
  28. * disengaged/full speed, bits 2:0 level). EC 0xEF is the functional
  29. * revision: revision 3 and later only implement the 0x78..0x7F sensors.
  30. *
  31. * Battery/AC: 0x38/0x39 are the battery 0/1 status bytes (bit 7 present,
  32. * bit 6 discharging, bit 5 charging) and 0x46 bit 4 is the AC-adapter
  33. * state. Battery data is a 16-byte window at 0xA0..0xAF selected by 0x81:
  34. * the high nibble is the battery (0x00/0x10), the low nibble the page
  35. * (0 dynamic, 1 mode, 2 static design data, 5 manufacturer, 6 model).
  36. */
  37. #include "tpec.h"
  38. #include "ec.h"
  39. #include "paging.h"
  40. #include "serial.h"
  41. #define TP_FAN_LEVEL 0x2F
  42. #define TP_FAN_LSB 0x84
  43. #define TP_FAN_MSB 0x85
  44. #define TP_TEMP_LO 0x78 /* temp_c[0..7] */
  45. #define TP_TEMP_HI 0xC0 /* temp_c[8..15] */
  46. #define TP_EC_REV 0xEF /* EC functional revision */
  47. #define TP_BAT0_STATUS 0x38 /* bit 7 present, 6 discharging, 5 charging */
  48. #define TP_BAT1_STATUS 0x39
  49. #define TP_AC_STATUS 0x46 /* bit 4: AC adapter online */
  50. #define TP_BAT_PAGE 0x81 /* information-page selector */
  51. #define TP_BAT_DATA 0xA0 /* 16-byte selected page */
  52. #define TP_BAT_PAGE_DYNAMIC 0
  53. #define TP_BAT_PAGE_MODE 1
  54. #define TP_BAT_PAGE_STATIC 2
  55. #define TP_BAT_PAGE_MFR 5
  56. #define TP_BAT_PAGE_MODEL 6
  57. #define TP_BAT_REMAINING 0x00
  58. #define TP_BAT_FULL 0x02
  59. #define TP_BAT_RSOC 0x06
  60. #define TP_BAT_RATE 0x08
  61. #define TP_BAT_VOLTAGE 0x0A
  62. static int tpec_state = -1; /* -1 unprobed, 0 absent, 1 present */
  63. static unsigned int tpec_ec_rev;
  64. static char tpec_vendor[16];
  65. static char tpec_product[32];
  66. static char tpec_version[32];
  67. static unsigned int rd32(const unsigned char *p)
  68. {
  69. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  70. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  71. }
  72. static void copy_trim(char *dst, unsigned int size, const unsigned char *src)
  73. {
  74. unsigned int i = 0;
  75. while (i + 1 < size && src && src[i]) {
  76. dst[i] = (char)src[i];
  77. i++;
  78. }
  79. while (i && dst[i - 1] == ' ')
  80. i--;
  81. dst[i] = '\0';
  82. }
  83. /* The n-th (1-based) string that follows a formatted SMBIOS structure. */
  84. static const unsigned char *smbios_string(const unsigned char *p,
  85. const unsigned char *end,
  86. unsigned int index)
  87. {
  88. unsigned int n = 1;
  89. if (!index)
  90. return 0;
  91. while (p < end && *p) {
  92. const unsigned char *s = p;
  93. if (n == index)
  94. return s;
  95. while (p < end && *p)
  96. p++;
  97. p++;
  98. n++;
  99. }
  100. return 0;
  101. }
  102. static int smbios_type1(char vendor[16], char product[32], char version[32])
  103. {
  104. const unsigned char *ep = 0;
  105. const unsigned char *table, *end, *p;
  106. unsigned int addr, table_addr, table_len, i;
  107. for (addr = 0xF0000; addr < 0x100000; addr += 16) {
  108. const unsigned char *q = (const unsigned char *)addr;
  109. unsigned char sum = 0;
  110. unsigned int len;
  111. if (q[0] != '_' || q[1] != 'S' || q[2] != 'M' || q[3] != '_')
  112. continue;
  113. len = q[5];
  114. if (len < 0x1F || len > 0x40)
  115. continue;
  116. for (i = 0; i < len; i++)
  117. sum = (unsigned char)(sum + q[i]);
  118. if (sum)
  119. continue;
  120. if (q[0x10] != '_' || q[0x11] != 'D' || q[0x12] != 'M' ||
  121. q[0x13] != 'I' || q[0x14] != '_')
  122. continue;
  123. ep = q;
  124. break;
  125. }
  126. if (!ep)
  127. return -1;
  128. table_len = (unsigned int)ep[0x16] | ((unsigned int)ep[0x17] << 8);
  129. table_addr = rd32(ep + 0x18);
  130. if (table_addr < 0x1000 || table_addr >= 0xFF000000u ||
  131. table_len < 4 || table_len > 0x10000u ||
  132. table_addr + table_len < table_addr)
  133. return -1;
  134. /* SMBIOS tables can sit above lowmem on larger machines. */
  135. table = ioremap(table_addr, table_len, IOREMAP_RAM);
  136. if (!table)
  137. return -1;
  138. end = table + table_len;
  139. for (p = table; p + 4 <= end; ) {
  140. unsigned char type = p[0];
  141. unsigned int len = p[1];
  142. const unsigned char *strs, *next;
  143. if (len < 4)
  144. break;
  145. if (type == 127) /* end-of-table */
  146. break;
  147. strs = p + len;
  148. if (strs > end)
  149. break;
  150. if (type == 1) {
  151. if (len >= 8) {
  152. copy_trim(vendor, 16,
  153. smbios_string(strs, end, p[4]));
  154. copy_trim(product, 32,
  155. smbios_string(strs, end, p[5]));
  156. copy_trim(version, 32,
  157. smbios_string(strs, end, p[6]));
  158. return 0;
  159. }
  160. break;
  161. }
  162. next = strs;
  163. while (next + 1 < end && !(next[0] == 0 && next[1] == 0))
  164. next++;
  165. next += 2;
  166. if (next > end)
  167. break;
  168. p = next;
  169. }
  170. return -1;
  171. }
  172. /* Select an H8 battery information page. Only writes 0x81 when the page
  173. * already differs; *dirty remembers that the original page must be restored. */
  174. static int ec_battery_page(unsigned int battery, unsigned int page, int *dirty)
  175. {
  176. unsigned char cur;
  177. unsigned char want = (unsigned char)((battery << 4) | page);
  178. if (ec_read(TP_BAT_PAGE, &cur) == 0 && cur == want)
  179. return 0;
  180. if (ec_write(TP_BAT_PAGE, want) != 0)
  181. return -1;
  182. *dirty = 1;
  183. return 0;
  184. }
  185. static void ec_read_string(char *dst, unsigned int size, unsigned char reg)
  186. {
  187. unsigned int i = 0;
  188. while (i + 1 < size) {
  189. unsigned char b;
  190. if (ec_read((unsigned char)(reg + i), &b) != 0)
  191. break;
  192. if (b == 0x00 || b == 0xFF)
  193. break;
  194. dst[i++] = (char)b;
  195. }
  196. while (i && dst[i - 1] == ' ')
  197. i--;
  198. dst[i] = '\0';
  199. }
  200. static int str_eq(const char *a, const char *b)
  201. {
  202. while (*a && *a == *b) {
  203. a++;
  204. b++;
  205. }
  206. return *a == *b;
  207. }
  208. static void log_info(void)
  209. {
  210. serial_puts(SERIAL_COM1, "tpec: ");
  211. if (tpec_vendor[0]) {
  212. serial_puts(SERIAL_COM1, tpec_vendor);
  213. serial_puts(SERIAL_COM1, " ");
  214. }
  215. if (tpec_product[0]) {
  216. serial_puts(SERIAL_COM1, tpec_product);
  217. serial_puts(SERIAL_COM1, " ");
  218. }
  219. if (tpec_version[0])
  220. serial_puts(SERIAL_COM1, tpec_version);
  221. }
  222. void tpec_init(void)
  223. {
  224. char vendor[16], product[32], version[32];
  225. unsigned char probe, rev;
  226. tpec_state = 0;
  227. tpec_vendor[0] = '\0';
  228. tpec_product[0] = '\0';
  229. tpec_version[0] = '\0';
  230. if (smbios_type1(vendor, product, version) != 0) {
  231. serial_puts(SERIAL_COM1, "tpec: no SMBIOS type 1, disabled\n");
  232. return;
  233. }
  234. copy_trim(tpec_vendor, sizeof(tpec_vendor), (const unsigned char *)vendor);
  235. copy_trim(tpec_product, sizeof(tpec_product),
  236. (const unsigned char *)product);
  237. copy_trim(tpec_version, sizeof(tpec_version),
  238. (const unsigned char *)version);
  239. if (!str_eq(vendor, "LENOVO") && !str_eq(vendor, "IBM")) {
  240. log_info();
  241. serial_puts(SERIAL_COM1, ": not a ThinkPad vendor, no EC probe\n");
  242. return;
  243. }
  244. if (ec_read(TP_FAN_LEVEL, &probe) != 0) {
  245. log_info();
  246. serial_puts(SERIAL_COM1, ": EC not responding\n");
  247. return;
  248. }
  249. if (ec_read(TP_EC_REV, &rev) == 0)
  250. tpec_ec_rev = rev;
  251. tpec_state = 1;
  252. log_info();
  253. serial_puts(SERIAL_COM1, ": EC ok, fw rev ");
  254. serial_putdec(SERIAL_COM1, tpec_ec_rev);
  255. serial_puts(SERIAL_COM1, ", fan level ");
  256. serial_puthex(SERIAL_COM1, probe);
  257. serial_puts(SERIAL_COM1, "\n");
  258. }
  259. int tpec_present(void)
  260. {
  261. if (tpec_state < 0)
  262. tpec_init();
  263. return tpec_state > 0;
  264. }
  265. void tpec_read(struct tpec_info *out)
  266. {
  267. unsigned char b;
  268. unsigned int i;
  269. out->present = 0;
  270. out->vendor[0] = '\0';
  271. out->product[0] = '\0';
  272. out->version[0] = '\0';
  273. for (i = 0; i < TPEC_TEMP_COUNT; i++)
  274. out->temp_c[i] = -128;
  275. out->fan_rpm = 0;
  276. out->fan_level = 0;
  277. if (tpec_state < 0)
  278. tpec_init();
  279. if (!tpec_state)
  280. return;
  281. out->present = 1;
  282. for (i = 0; i < sizeof(out->vendor) - 1 && tpec_vendor[i]; i++)
  283. out->vendor[i] = tpec_vendor[i];
  284. out->vendor[i] = '\0';
  285. for (i = 0; i < sizeof(out->product) - 1 && tpec_product[i]; i++)
  286. out->product[i] = tpec_product[i];
  287. out->product[i] = '\0';
  288. for (i = 0; i < sizeof(out->version) - 1 && tpec_version[i]; i++)
  289. out->version[i] = tpec_version[i];
  290. out->version[i] = '\0';
  291. for (i = 0; i < 8; i++) {
  292. if (ec_read((unsigned char)(TP_TEMP_LO + i), &b) != 0)
  293. continue;
  294. /* 0x80 = sensor present but unavailable, 0x00 = register not
  295. * implemented (thinkpad_acpi treats a block of zeros as an EC
  296. * that does not expose the register map at all). */
  297. if (b != 0x80 && b != 0x00)
  298. out->temp_c[i] = (signed char)b;
  299. }
  300. /* EC functional revision 3 and later only uses 0x78..0x7F; the 0xC0
  301. * block may hold unrelated values (e.g. a power supply ID) there. */
  302. if (tpec_ec_rev < 3) {
  303. for (i = 0; i < 8; i++) {
  304. if (ec_read((unsigned char)(TP_TEMP_HI + i), &b) != 0)
  305. continue;
  306. if (b != 0x80 && b != 0x00)
  307. out->temp_c[8 + i] = (signed char)b;
  308. }
  309. }
  310. {
  311. unsigned char lo = 0, hi = 0;
  312. if (ec_read(TP_FAN_LSB, &lo) == 0 &&
  313. ec_read(TP_FAN_MSB, &hi) == 0) {
  314. if (!(lo == 0xFF && hi == 0xFF))
  315. out->fan_rpm = ((unsigned int)hi << 8) | lo;
  316. }
  317. }
  318. if (ec_read(TP_FAN_LEVEL, &b) == 0)
  319. out->fan_level = b;
  320. }
  321. static void tpec_copy_ident(char *dst, unsigned int size, const char *src)
  322. {
  323. unsigned int i;
  324. for (i = 0; i + 1 < size && src[i]; i++)
  325. dst[i] = src[i];
  326. dst[i] = '\0';
  327. }
  328. static void tpec_read_battery(struct power_battery *b, unsigned int index,
  329. int detail, int *page_dirty)
  330. {
  331. unsigned char st;
  332. unsigned int v;
  333. unsigned int remaining = 0, full = 0, rsoc = 0xFFFF;
  334. int have_remaining = 0, have_full = 0, have_rsoc = 0;
  335. int have_voltage = 0;
  336. if (ec_read((unsigned char)(TP_BAT0_STATUS + index), &st) != 0) {
  337. b->error = 1;
  338. return;
  339. }
  340. b->status = st;
  341. b->present = (st & 0x80) ? 1 : 0;
  342. b->charging = (st & 0x20) ? 1 : 0;
  343. b->discharging = (st & 0x40) ? 1 : 0;
  344. if (!b->present)
  345. return;
  346. if (ec_battery_page(index, TP_BAT_PAGE_DYNAMIC, page_dirty) != 0) {
  347. b->error = 1;
  348. return;
  349. }
  350. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_REMAINING), &v) == 0) {
  351. remaining = v;
  352. have_remaining = (v != 0xFFFF);
  353. }
  354. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_FULL), &v) == 0) {
  355. full = v;
  356. have_full = (v != 0xFFFF);
  357. }
  358. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_RSOC), &v) == 0) {
  359. rsoc = v;
  360. have_rsoc = (v != 0xFFFF);
  361. }
  362. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_RATE), &v) == 0) {
  363. if (v != 0xFFFF)
  364. b->rate = (int)(short)v;
  365. } else {
  366. b->error = 1;
  367. }
  368. if (ec_read16((unsigned char)(TP_BAT_DATA + TP_BAT_VOLTAGE), &v) == 0 &&
  369. v != 0xFFFF) {
  370. b->voltage_mv = v;
  371. have_voltage = 1;
  372. } else {
  373. b->error = 1;
  374. }
  375. b->remaining = have_remaining ? remaining : 0;
  376. b->full = have_full ? full : 0;
  377. if (!have_remaining || !have_full || !have_voltage)
  378. b->error = 1;
  379. /* Percentage is unit-independent (both capacities use the same unit).
  380. * Reject the 0xFFFF "unknown" marker and obviously inconsistent reads;
  381. * fall back to the SBS relative state of charge if that is sane. */
  382. if (have_full && have_remaining && full > 0 && full != 0xFFFF &&
  383. remaining != 0xFFFF && remaining <= full + full / 4) {
  384. b->percent = (int)((remaining * 100u + full / 2u) / full);
  385. if (b->percent > 100)
  386. b->percent = 100;
  387. b->percent_valid = 1;
  388. } else if (have_rsoc && rsoc <= 100) {
  389. b->percent = (int)rsoc;
  390. b->percent_valid = 1;
  391. }
  392. if (!detail)
  393. return;
  394. if (ec_battery_page(index, TP_BAT_PAGE_MODE, page_dirty) == 0) {
  395. if (ec_read16(TP_BAT_DATA, &v) == 0 && v != 0xFFFF) {
  396. b->unit_known = 1;
  397. b->unit_mwh = (int)((v >> 15) & 1);
  398. }
  399. } else {
  400. b->error = 1;
  401. }
  402. if (ec_battery_page(index, TP_BAT_PAGE_STATIC, page_dirty) == 0) {
  403. if (ec_read16(TP_BAT_DATA, &v) == 0 && v != 0xFFFF)
  404. b->design = v;
  405. } else {
  406. b->error = 1;
  407. }
  408. /* The vendor DSDT's GBIF/GBST report mWh/mW when the mode bit is set:
  409. * capacities are scaled by 10 (the 10.8 V pack) and the rate by the
  410. * measured voltage. Match that so powerinfo shows the same units as
  411. * the firmware's ACPI battery interface. The percentage above was
  412. * already computed from the raw values, so the scale does not matter. */
  413. if (b->unit_mwh) {
  414. b->remaining *= 10;
  415. b->full *= 10;
  416. b->design *= 10;
  417. if (b->voltage_mv)
  418. b->rate = (b->rate * (int)b->voltage_mv) / 1000;
  419. }
  420. if (ec_battery_page(index, TP_BAT_PAGE_MFR, page_dirty) == 0)
  421. ec_read_string(b->manufacturer, sizeof(b->manufacturer),
  422. TP_BAT_DATA);
  423. else
  424. b->error = 1;
  425. if (ec_battery_page(index, TP_BAT_PAGE_MODEL, page_dirty) == 0)
  426. ec_read_string(b->model, sizeof(b->model), TP_BAT_DATA);
  427. else
  428. b->error = 1;
  429. }
  430. void tpec_power_read(struct power_info *out, int detail)
  431. {
  432. unsigned int i;
  433. unsigned char saved_page = 0, ac;
  434. int have_saved = 0, page_dirty = 0;
  435. for (i = 0; i < sizeof(*out); i++)
  436. ((unsigned char *)out)[i] = 0;
  437. out->ac_online = -1;
  438. if (tpec_state < 0)
  439. tpec_init();
  440. if (!tpec_state)
  441. return;
  442. out->present = 1;
  443. out->source = SYS_POWER_SRC_TPEC;
  444. out->ec_rev = tpec_ec_rev;
  445. tpec_copy_ident(out->vendor, sizeof(out->vendor), tpec_vendor);
  446. tpec_copy_ident(out->product, sizeof(out->product), tpec_product);
  447. tpec_copy_ident(out->version, sizeof(out->version), tpec_version);
  448. if (ec_read(TP_AC_STATUS, &ac) == 0)
  449. out->ac_online = (ac & 0x10) ? 1 : 0;
  450. if (ec_read(TP_BAT_PAGE, &saved_page) == 0)
  451. have_saved = 1;
  452. for (i = 0; i < POWER_BATT_MAX; i++) {
  453. tpec_read_battery(&out->battery[i], i, detail, &page_dirty);
  454. if (out->battery[i].present)
  455. out->battery_count++;
  456. }
  457. if (page_dirty)
  458. (void)ec_write(TP_BAT_PAGE, have_saved ? saved_page : 0);
  459. }