cros_ec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* Chrome OS embedded controller: battery, AC adapter and board sensors.
  2. *
  3. * Chromebooks do not expose ACPI _BIF/_BST through an interpreter we could
  4. * run; instead the Chrome EC implements the standard ACPI EC command
  5. * interface at ports 0x62/0x66, and its ACPI memory window 0x20..0xFF
  6. * mirrors the EC memmap 0x00..0xDF. That gives us the same data the
  7. * firmware's DSDT reads:
  8. *
  9. * EC_MEMMAP_TEMP_SENSOR 0x00..0x0f temperatures (value + 200 = Kelvin)
  10. * EC_MEMMAP_FAN 0x10..0x17 fan tachometers (16-bit, 0xffff none)
  11. * EC_MEMMAP_TEMP_SENSOR_B 0x18..0x1f more temperatures (thermal version 2)
  12. * EC_MEMMAP_SWITCHES 0x30 lid open / power button
  13. * EC_MEMMAP_BATT_VOLT 0x40 present voltage (mV)
  14. * EC_MEMMAP_BATT_RATE 0x44 present rate (mA, ACPI power unit)
  15. * EC_MEMMAP_BATT_CAP 0x48 remaining capacity (mAh)
  16. * EC_MEMMAP_BATT_FLAG 0x4c AC/battery/charging state
  17. * EC_MEMMAP_BATT_COUNT 0x4d battery count
  18. * EC_MEMMAP_BATT_INDEX 0x4e selected battery
  19. * EC_MEMMAP_BATT_DCAP 0x50 design capacity (mAh)
  20. * EC_MEMMAP_BATT_DVLT 0x54 design voltage (mV)
  21. * EC_MEMMAP_BATT_LFCC 0x58 last full charge capacity (mAh)
  22. * EC_MEMMAP_BATT_CCNT 0x5c cycle count
  23. * EC_MEMMAP_BATT_MFGR/MODEL/SERIAL/TYPE 0x60..0x7f (8-byte strings)
  24. *
  25. * The battery index is selected through EC_ACPI_MEM_BATTERY_INDEX (0x12);
  26. * longer strings come from EC_ACPI_MEM_STRINGS_FIFO (0x16). The memmap is
  27. * read-only from the AP, so the only writes are the documented selector and
  28. * FIFO commands.
  29. *
  30. * The probe is gated on the EC ACPI version and the 'E','C' memmap id; the
  31. * ThinkPad backend is tried first and skips this one on Lenovo machines.
  32. */
  33. #include "cros_ec.h"
  34. #include "ec.h"
  35. #include "tpec.h"
  36. #include "serial.h"
  37. #define CROS_ACPI_VERSION 0x00
  38. #define CROS_ACPI_BATTERY_INDEX 0x12
  39. #define CROS_ACPI_STRINGS_FIFO 0x16
  40. #define CROS_ACPI_FAN_DUTY 0x04
  41. #define CROS_MEM_ACPI_BASE 0x20
  42. #define CROS_MEM_TEMP_SENSOR 0x00
  43. #define CROS_MEM_TEMP_SENSOR_B 0x18
  44. #define CROS_MEM_FAN 0x10
  45. #define CROS_MEM_ID 0x20
  46. #define CROS_MEM_ID_VERSION 0x22
  47. #define CROS_MEM_THERMAL_VERSION 0x23
  48. #define CROS_MEM_BATTERY_VERSION 0x24
  49. #define CROS_MEM_SWITCHES 0x30
  50. #define CROS_MEM_BATT_VOLT 0x40
  51. #define CROS_MEM_BATT_RATE 0x44
  52. #define CROS_MEM_BATT_CAP 0x48
  53. #define CROS_MEM_BATT_FLAG 0x4c
  54. #define CROS_MEM_BATT_COUNT 0x4d
  55. #define CROS_MEM_BATT_INDEX 0x4e
  56. #define CROS_MEM_BATT_DCAP 0x50
  57. #define CROS_MEM_BATT_DVLT 0x54
  58. #define CROS_MEM_BATT_LFCC 0x58
  59. #define CROS_MEM_BATT_CCNT 0x5c
  60. #define CROS_MEM_BATT_MFGR 0x60
  61. #define CROS_MEM_BATT_MODEL 0x68
  62. #define CROS_MEM_BATT_SERIAL 0x70
  63. /* EC_MEMMAP_BATT_FLAG bits. */
  64. #define CROS_BATT_AC_PRESENT 0x01
  65. #define CROS_BATT_PRESENT 0x02
  66. #define CROS_BATT_DISCHARGING 0x04
  67. #define CROS_BATT_CHARGING 0x08
  68. #define CROS_BATT_INVALID_DATA 0x20
  69. #define CROS_BATT_CUT_OFF 0x40
  70. /* EC_MEMMAP_SWITCHES bits. */
  71. #define CROS_SWITCH_LID_OPEN 0x01
  72. #define CROS_SWITCH_POWER_BUTTON 0x02
  73. /* Device feature bits (EC_ACPI_MEM_DEVICE_FEATURES, 8 bytes). */
  74. #define CROS_FEATURE_BATT (1u << 17)
  75. #define CROS_FEATURE_DFUD (1u << 0)
  76. #define CROS_UNKNOWN32 0xFFFFFFFFu
  77. #define CROS_TEMP_OFFSET 73 /* raw + 200 = Kelvin, so raw - 73 = C */
  78. static int cros_state = -1; /* -1 unprobed, 0 absent, 1 present */
  79. static unsigned int cros_acpi_version;
  80. static unsigned int cros_id_version;
  81. static unsigned int cros_battery_version;
  82. static unsigned int cros_thermal_version;
  83. static unsigned int cros_features;
  84. static int cros_fifo = -1; /* -1 unprobed, 0 absent, 1 present */
  85. static int mem_read(unsigned int off, unsigned char *out)
  86. {
  87. return ec_read((unsigned char)(CROS_MEM_ACPI_BASE + off), out);
  88. }
  89. static int mem_read16(unsigned int off, unsigned int *out)
  90. {
  91. unsigned char lo, hi;
  92. if (mem_read(off, &lo) != 0 || mem_read(off + 1, &hi) != 0)
  93. return -1;
  94. *out = (unsigned int)lo | ((unsigned int)hi << 8);
  95. return 0;
  96. }
  97. static int mem_read32(unsigned int off, unsigned int *out)
  98. {
  99. unsigned char b0, b1, b2, b3;
  100. if (mem_read(off, &b0) != 0 || mem_read(off + 1, &b1) != 0 ||
  101. mem_read(off + 2, &b2) != 0 || mem_read(off + 3, &b3) != 0)
  102. return -1;
  103. *out = (unsigned int)b0 | ((unsigned int)b1 << 8) |
  104. ((unsigned int)b2 << 16) | ((unsigned int)b3 << 24);
  105. return 0;
  106. }
  107. /* Read one of the 8-byte fixed strings from the memmap. */
  108. static void read_short_string(unsigned int off, char *dst, unsigned int size)
  109. {
  110. unsigned int i, n = 8;
  111. if (n > size - 1)
  112. n = size - 1;
  113. for (i = 0; i < n; i++) {
  114. unsigned char b;
  115. if (mem_read(off + i, &b) != 0)
  116. break;
  117. if (b == 0 || b == 0xFF)
  118. break;
  119. dst[i] = (char)b;
  120. }
  121. while (i && dst[i - 1] == ' ')
  122. i--;
  123. dst[i] = '\0';
  124. }
  125. /* Long strings from EC_ACPI_MEM_STRINGS_FIFO, when the EC supports them. */
  126. static void read_fifo_string(unsigned int id, char *dst, unsigned int size)
  127. {
  128. unsigned int i;
  129. if (ec_write(CROS_ACPI_STRINGS_FIFO, (unsigned char)id) != 0) {
  130. dst[0] = '\0';
  131. return;
  132. }
  133. for (i = 0; i + 1 < size; i++) {
  134. unsigned char b;
  135. if (ec_read(CROS_ACPI_STRINGS_FIFO, &b) != 0)
  136. break;
  137. if (b == 0)
  138. break;
  139. dst[i] = (char)b;
  140. }
  141. while (i && dst[i - 1] == ' ')
  142. i--;
  143. dst[i] = '\0';
  144. }
  145. static void read_string(unsigned int off, unsigned int fifo_id, char *dst,
  146. unsigned int size)
  147. {
  148. if (cros_fifo > 0) {
  149. read_fifo_string(fifo_id, dst, size);
  150. if (dst[0])
  151. return;
  152. }
  153. read_short_string(off, dst, size);
  154. }
  155. static int battery_select(unsigned int index)
  156. {
  157. unsigned char cur;
  158. unsigned int i;
  159. if (mem_read(CROS_MEM_BATT_INDEX, &cur) == 0 &&
  160. cur == (unsigned char)index)
  161. return 0;
  162. if (ec_write(CROS_ACPI_BATTERY_INDEX, (unsigned char)index) != 0)
  163. return -1;
  164. /* The DSDT's BTSW waits 5 ms for BTIX to follow BTID. Each read is
  165. * already several port handshakes, so a bounded retry is enough. */
  166. for (i = 0; i < 50; i++) {
  167. if (mem_read(CROS_MEM_BATT_INDEX, &cur) == 0 &&
  168. cur == (unsigned char)index)
  169. return 0;
  170. }
  171. return -1;
  172. }
  173. static void read_temp(unsigned int off, int *out)
  174. {
  175. unsigned char b;
  176. if (mem_read(off, &b) != 0 || b >= 0xFC) {
  177. *out = -128;
  178. return;
  179. }
  180. *out = (int)b - CROS_TEMP_OFFSET;
  181. }
  182. void cros_ec_init(void)
  183. {
  184. unsigned char ver = 0, id0 = 0, id1 = 0, b;
  185. unsigned int i, feature = 0;
  186. cros_state = 0;
  187. cros_acpi_version = 0;
  188. cros_id_version = 0;
  189. cros_battery_version = 0;
  190. cros_thermal_version = 0;
  191. cros_features = 0;
  192. cros_fifo = -1;
  193. /* The ThinkPad H8 has its own driver and the first claim on 0x62/0x66. */
  194. if (tpec_present()) {
  195. serial_puts(SERIAL_COM1, "cros_ec: ThinkPad EC present, skipped\n");
  196. return;
  197. }
  198. if (ec_read(CROS_ACPI_VERSION, &ver) != 0) {
  199. serial_puts(SERIAL_COM1, "cros_ec: no ACPI EC at 0x62/0x66\n");
  200. return;
  201. }
  202. if (mem_read(CROS_MEM_ID, &id0) != 0 ||
  203. mem_read(CROS_MEM_ID + 1, &id1) != 0 ||
  204. id0 != 'E' || id1 != 'C') {
  205. serial_puts(SERIAL_COM1, "cros_ec: no Chrome EC signature\n");
  206. return;
  207. }
  208. for (i = 0; i < 4; i++) {
  209. unsigned char f;
  210. if (mem_read(0x0A + i, &f) != 0) {
  211. feature = 0;
  212. break;
  213. }
  214. feature |= (unsigned int)f << (8 * i);
  215. }
  216. if (feature & CROS_FEATURE_DFUD)
  217. feature = 0;
  218. if (mem_read(CROS_MEM_ID_VERSION, &b) == 0)
  219. cros_id_version = b;
  220. if (mem_read(CROS_MEM_THERMAL_VERSION, &b) == 0)
  221. cros_thermal_version = b;
  222. if (mem_read(CROS_MEM_BATTERY_VERSION, &b) == 0)
  223. cros_battery_version = b;
  224. cros_acpi_version = ver;
  225. cros_features = feature;
  226. cros_state = 1;
  227. /* Probe the extended-string FIFO once (write 0, read the version). */
  228. {
  229. unsigned char fifo = 0xFF;
  230. if (ec_write(CROS_ACPI_STRINGS_FIFO, 0) == 0)
  231. (void)ec_read(CROS_ACPI_STRINGS_FIFO, &fifo);
  232. cros_fifo = (fifo >= 1 && fifo < 0xFF) ? 1 : 0;
  233. }
  234. serial_puts(SERIAL_COM1, "cros_ec: Chrome EC, ACPI v");
  235. serial_putdec(SERIAL_COM1, cros_acpi_version);
  236. serial_puts(SERIAL_COM1, ", id v");
  237. serial_putdec(SERIAL_COM1, cros_id_version);
  238. serial_puts(SERIAL_COM1, ", battery v");
  239. serial_putdec(SERIAL_COM1, cros_battery_version);
  240. serial_puts(SERIAL_COM1, ", thermal v");
  241. serial_putdec(SERIAL_COM1, cros_thermal_version);
  242. serial_puts(SERIAL_COM1, ", features ");
  243. serial_puthex(SERIAL_COM1, cros_features);
  244. serial_puts(SERIAL_COM1, ", fifo ");
  245. serial_puts(SERIAL_COM1, cros_fifo ? "yes" : "no");
  246. serial_puts(SERIAL_COM1, "\n");
  247. if (mem_read(CROS_MEM_BATT_COUNT, &b) == 0) {
  248. serial_puts(SERIAL_COM1, "cros_ec: batteries ");
  249. serial_putdec(SERIAL_COM1, b);
  250. serial_puts(SERIAL_COM1, ", feature batt ");
  251. serial_puts(SERIAL_COM1,
  252. (cros_features & CROS_FEATURE_BATT) ? "yes" : "no");
  253. serial_puts(SERIAL_COM1, "\n");
  254. }
  255. }
  256. int cros_ec_present(void)
  257. {
  258. if (cros_state < 0)
  259. cros_ec_init();
  260. return cros_state > 0;
  261. }
  262. void cros_ec_power_read(struct power_info *out, int detail)
  263. {
  264. unsigned int i, count;
  265. unsigned char flags = 0;
  266. unsigned char count_byte = 0;
  267. int ac = -1;
  268. for (i = 0; i < sizeof(*out); i++)
  269. ((unsigned char *)out)[i] = 0;
  270. out->ac_online = -1;
  271. if (cros_state < 0)
  272. cros_ec_init();
  273. if (!cros_state)
  274. return;
  275. out->present = 1;
  276. out->source = SYS_POWER_SRC_CROS;
  277. out->ec_rev = cros_acpi_version;
  278. if (mem_read(CROS_MEM_BATT_FLAG, &flags) == 0)
  279. ac = (flags & CROS_BATT_AC_PRESENT) ? 1 : 0;
  280. out->ac_online = ac;
  281. if (mem_read(CROS_MEM_BATT_COUNT, &count_byte) != 0 ||
  282. count_byte == 0 || count_byte > POWER_BATT_MAX)
  283. count = 1;
  284. else
  285. count = count_byte;
  286. for (i = 0; i < count; i++) {
  287. struct power_battery *b = &out->battery[i];
  288. unsigned int volt = 0, rate = 0, cap = 0, lfcc = 0;
  289. unsigned int design = 0, dvlt = 0, cycles = 0;
  290. unsigned int full;
  291. unsigned char f = 0;
  292. if (battery_select(i) != 0) {
  293. b->error = 1;
  294. continue;
  295. }
  296. if (mem_read(CROS_MEM_BATT_FLAG, &f) != 0) {
  297. b->error = 1;
  298. continue;
  299. }
  300. b->status = f;
  301. b->present = (f & CROS_BATT_PRESENT) ? 1 : 0;
  302. b->charging = (f & CROS_BATT_CHARGING) ? 1 : 0;
  303. b->discharging = (f & CROS_BATT_DISCHARGING) ? 1 : 0;
  304. if (f & (CROS_BATT_INVALID_DATA | CROS_BATT_CUT_OFF))
  305. b->error = 1;
  306. if (!b->present)
  307. continue;
  308. if (mem_read32(CROS_MEM_BATT_VOLT, &volt) != 0 ||
  309. mem_read32(CROS_MEM_BATT_RATE, &rate) != 0 ||
  310. mem_read32(CROS_MEM_BATT_CAP, &cap) != 0 ||
  311. mem_read32(CROS_MEM_BATT_LFCC, &lfcc) != 0)
  312. b->error = 1;
  313. if (volt != CROS_UNKNOWN32)
  314. b->voltage_mv = volt;
  315. if (rate != CROS_UNKNOWN32)
  316. b->rate = (int)rate;
  317. if (cap != CROS_UNKNOWN32)
  318. b->remaining = cap;
  319. if (lfcc != CROS_UNKNOWN32)
  320. b->full = lfcc;
  321. if (detail) {
  322. if (mem_read32(CROS_MEM_BATT_DCAP, &design) != 0 ||
  323. mem_read32(CROS_MEM_BATT_DVLT, &dvlt) != 0 ||
  324. mem_read32(CROS_MEM_BATT_CCNT, &cycles) != 0)
  325. b->error = 1;
  326. if (design != CROS_UNKNOWN32)
  327. b->design = design;
  328. if (cycles != CROS_UNKNOWN32)
  329. b->cycle_count = cycles;
  330. (void)dvlt; /* design voltage is informational */
  331. }
  332. /* _BIF declares mAh/mA on Chromebooks. */
  333. b->unit_known = 1;
  334. b->unit_mwh = 0;
  335. /* The Cyan DSDT compensates a full pack that the EC reports as
  336. * neither charging nor discharging while AC is present. */
  337. full = b->full ? b->full : b->design;
  338. if (ac > 0 && !(b->discharging && b->charging) && full &&
  339. b->remaining > full - full / 16 &&
  340. b->remaining < full + full / 16)
  341. b->remaining = full;
  342. if (full && b->remaining &&
  343. b->remaining <= full + full / 4) {
  344. unsigned int pct =
  345. (b->remaining * 100u + full / 2u) / full;
  346. if (pct > 100)
  347. pct = 100;
  348. b->percent = (int)pct;
  349. b->percent_valid = 1;
  350. }
  351. if (detail) {
  352. read_string(CROS_MEM_BATT_MFGR, 3, b->manufacturer,
  353. sizeof(b->manufacturer));
  354. read_string(CROS_MEM_BATT_MODEL, 1, b->model,
  355. sizeof(b->model));
  356. read_string(CROS_MEM_BATT_SERIAL, 2, b->serial,
  357. sizeof(b->serial));
  358. }
  359. out->battery_count++;
  360. }
  361. }
  362. void cros_ec_sensors_read(struct cros_ec_sensors *out)
  363. {
  364. unsigned int i;
  365. unsigned char b;
  366. for (i = 0; i < sizeof(*out); i++)
  367. ((unsigned char *)out)[i] = 0;
  368. for (i = 0; i < CROS_EC_TEMP_MAX; i++)
  369. out->temp_c[i] = -128;
  370. if (cros_state < 0)
  371. cros_ec_init();
  372. if (!cros_state)
  373. return;
  374. out->present = 1;
  375. out->acpi_version = cros_acpi_version;
  376. out->battery_version = cros_battery_version;
  377. out->thermal_version = cros_thermal_version;
  378. for (i = 0; i < 16; i++)
  379. read_temp(CROS_MEM_TEMP_SENSOR + i, &out->temp_c[i]);
  380. for (i = 0; i < 8; i++)
  381. read_temp(CROS_MEM_TEMP_SENSOR_B + i, &out->temp_c[16 + i]);
  382. for (i = 0; i < CROS_EC_FAN_MAX; i++) {
  383. unsigned int v;
  384. if (mem_read16(CROS_MEM_FAN + 2 * i, &v) == 0 &&
  385. v != 0xFFFF) {
  386. out->fan_rpm[i] = v;
  387. out->fan_present |= 1u << i;
  388. }
  389. }
  390. if (ec_read(CROS_ACPI_FAN_DUTY, &b) == 0)
  391. out->fan_duty = b;
  392. if (mem_read(CROS_MEM_SWITCHES, &b) == 0)
  393. out->switches = b;
  394. }