cros_ec.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Chromium OS cros_ec driver
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. */
  7. /*
  8. * This is the interface to the Chrome OS EC. It provides keyboard functions,
  9. * power control and battery management. Quite a few other functions are
  10. * provided to enable the EC software to be updated, talk to the EC's I2C bus
  11. * and store a small amount of data in a memory which persists while the EC
  12. * is not reset.
  13. */
  14. #define LOG_CATEGORY UCLASS_CROS_EC
  15. #include <common.h>
  16. #include <command.h>
  17. #include <dm.h>
  18. #include <i2c.h>
  19. #include <cros_ec.h>
  20. #include <fdtdec.h>
  21. #include <malloc.h>
  22. #include <spi.h>
  23. #include <linux/errno.h>
  24. #include <asm/io.h>
  25. #include <asm-generic/gpio.h>
  26. #include <dm/device-internal.h>
  27. #include <dm/of_extra.h>
  28. #include <dm/uclass-internal.h>
  29. #ifdef DEBUG_TRACE
  30. #define debug_trace(fmt, b...) debug(fmt, #b)
  31. #else
  32. #define debug_trace(fmt, b...)
  33. #endif
  34. enum {
  35. /* Timeout waiting for a flash erase command to complete */
  36. CROS_EC_CMD_TIMEOUT_MS = 5000,
  37. /* Timeout waiting for a synchronous hash to be recomputed */
  38. CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
  39. };
  40. #define INVALID_HCMD 0xFF
  41. /*
  42. * Map UHEPI masks to non UHEPI commands in order to support old EC FW
  43. * which does not support UHEPI command.
  44. */
  45. static const struct {
  46. u8 set_cmd;
  47. u8 clear_cmd;
  48. u8 get_cmd;
  49. } event_map[] = {
  50. [EC_HOST_EVENT_MAIN] = {
  51. INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
  52. INVALID_HCMD,
  53. },
  54. [EC_HOST_EVENT_B] = {
  55. INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
  56. EC_CMD_HOST_EVENT_GET_B,
  57. },
  58. [EC_HOST_EVENT_SCI_MASK] = {
  59. EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
  60. EC_CMD_HOST_EVENT_GET_SCI_MASK,
  61. },
  62. [EC_HOST_EVENT_SMI_MASK] = {
  63. EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
  64. EC_CMD_HOST_EVENT_GET_SMI_MASK,
  65. },
  66. [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
  67. INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
  68. },
  69. [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
  70. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  71. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  72. },
  73. [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
  74. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  75. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  76. },
  77. [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
  78. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  79. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  80. },
  81. [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
  82. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  83. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  84. },
  85. };
  86. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
  87. {
  88. #ifdef DEBUG
  89. int i;
  90. printf("%s: ", name);
  91. if (cmd != -1)
  92. printf("cmd=%#x: ", cmd);
  93. for (i = 0; i < len; i++)
  94. printf("%02x ", data[i]);
  95. printf("\n");
  96. #endif
  97. }
  98. /*
  99. * Calculate a simple 8-bit checksum of a data block
  100. *
  101. * @param data Data block to checksum
  102. * @param size Size of data block in bytes
  103. * @return checksum value (0 to 255)
  104. */
  105. int cros_ec_calc_checksum(const uint8_t *data, int size)
  106. {
  107. int csum, i;
  108. for (i = csum = 0; i < size; i++)
  109. csum += data[i];
  110. return csum & 0xff;
  111. }
  112. /**
  113. * Create a request packet for protocol version 3.
  114. *
  115. * The packet is stored in the device's internal output buffer.
  116. *
  117. * @param dev CROS-EC device
  118. * @param cmd Command to send (EC_CMD_...)
  119. * @param cmd_version Version of command to send (EC_VER_...)
  120. * @param dout Output data (may be NULL If dout_len=0)
  121. * @param dout_len Size of output data in bytes
  122. * @return packet size in bytes, or <0 if error.
  123. */
  124. static int create_proto3_request(struct cros_ec_dev *cdev,
  125. int cmd, int cmd_version,
  126. const void *dout, int dout_len)
  127. {
  128. struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
  129. int out_bytes = dout_len + sizeof(*rq);
  130. /* Fail if output size is too big */
  131. if (out_bytes > (int)sizeof(cdev->dout)) {
  132. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  133. return -EC_RES_REQUEST_TRUNCATED;
  134. }
  135. /* Fill in request packet */
  136. rq->struct_version = EC_HOST_REQUEST_VERSION;
  137. rq->checksum = 0;
  138. rq->command = cmd;
  139. rq->command_version = cmd_version;
  140. rq->reserved = 0;
  141. rq->data_len = dout_len;
  142. /* Copy data after header */
  143. memcpy(rq + 1, dout, dout_len);
  144. /* Write checksum field so the entire packet sums to 0 */
  145. rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
  146. cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
  147. /* Return size of request packet */
  148. return out_bytes;
  149. }
  150. /**
  151. * Prepare the device to receive a protocol version 3 response.
  152. *
  153. * @param dev CROS-EC device
  154. * @param din_len Maximum size of response in bytes
  155. * @return maximum expected number of bytes in response, or <0 if error.
  156. */
  157. static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
  158. {
  159. int in_bytes = din_len + sizeof(struct ec_host_response);
  160. /* Fail if input size is too big */
  161. if (in_bytes > (int)sizeof(cdev->din)) {
  162. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  163. return -EC_RES_RESPONSE_TOO_BIG;
  164. }
  165. /* Return expected size of response packet */
  166. return in_bytes;
  167. }
  168. /**
  169. * Handle a protocol version 3 response packet.
  170. *
  171. * The packet must already be stored in the device's internal input buffer.
  172. *
  173. * @param dev CROS-EC device
  174. * @param dinp Returns pointer to response data
  175. * @param din_len Maximum size of response in bytes
  176. * @return number of bytes of response data, or <0 if error. Note that error
  177. * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
  178. * overlap!)
  179. */
  180. static int handle_proto3_response(struct cros_ec_dev *dev,
  181. uint8_t **dinp, int din_len)
  182. {
  183. struct ec_host_response *rs = (struct ec_host_response *)dev->din;
  184. int in_bytes;
  185. int csum;
  186. cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
  187. /* Check input data */
  188. if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
  189. debug("%s: EC response version mismatch\n", __func__);
  190. return -EC_RES_INVALID_RESPONSE;
  191. }
  192. if (rs->reserved) {
  193. debug("%s: EC response reserved != 0\n", __func__);
  194. return -EC_RES_INVALID_RESPONSE;
  195. }
  196. if (rs->data_len > din_len) {
  197. debug("%s: EC returned too much data\n", __func__);
  198. return -EC_RES_RESPONSE_TOO_BIG;
  199. }
  200. cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
  201. /* Update in_bytes to actual data size */
  202. in_bytes = sizeof(*rs) + rs->data_len;
  203. /* Verify checksum */
  204. csum = cros_ec_calc_checksum(dev->din, in_bytes);
  205. if (csum) {
  206. debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
  207. csum);
  208. return -EC_RES_INVALID_CHECKSUM;
  209. }
  210. /* Return error result, if any */
  211. if (rs->result)
  212. return -(int)rs->result;
  213. /* If we're still here, set response data pointer and return length */
  214. *dinp = (uint8_t *)(rs + 1);
  215. return rs->data_len;
  216. }
  217. static int send_command_proto3(struct cros_ec_dev *cdev,
  218. int cmd, int cmd_version,
  219. const void *dout, int dout_len,
  220. uint8_t **dinp, int din_len)
  221. {
  222. struct dm_cros_ec_ops *ops;
  223. int out_bytes, in_bytes;
  224. int rv;
  225. /* Create request packet */
  226. out_bytes = create_proto3_request(cdev, cmd, cmd_version,
  227. dout, dout_len);
  228. if (out_bytes < 0)
  229. return out_bytes;
  230. /* Prepare response buffer */
  231. in_bytes = prepare_proto3_response_buffer(cdev, din_len);
  232. if (in_bytes < 0)
  233. return in_bytes;
  234. ops = dm_cros_ec_get_ops(cdev->dev);
  235. rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
  236. -ENOSYS;
  237. if (rv < 0)
  238. return rv;
  239. /* Process the response */
  240. return handle_proto3_response(cdev, dinp, din_len);
  241. }
  242. static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
  243. const void *dout, int dout_len,
  244. uint8_t **dinp, int din_len)
  245. {
  246. struct dm_cros_ec_ops *ops;
  247. int ret = -1;
  248. /* Handle protocol version 3 support */
  249. if (dev->protocol_version == 3) {
  250. return send_command_proto3(dev, cmd, cmd_version,
  251. dout, dout_len, dinp, din_len);
  252. }
  253. ops = dm_cros_ec_get_ops(dev->dev);
  254. ret = ops->command(dev->dev, cmd, cmd_version,
  255. (const uint8_t *)dout, dout_len, dinp, din_len);
  256. return ret;
  257. }
  258. /**
  259. * Send a command to the CROS-EC device and return the reply.
  260. *
  261. * The device's internal input/output buffers are used.
  262. *
  263. * @param dev CROS-EC device
  264. * @param cmd Command to send (EC_CMD_...)
  265. * @param cmd_version Version of command to send (EC_VER_...)
  266. * @param dout Output data (may be NULL If dout_len=0)
  267. * @param dout_len Size of output data in bytes
  268. * @param dinp Response data (may be NULL If din_len=0).
  269. * If not NULL, it will be updated to point to the data
  270. * and will always be double word aligned (64-bits)
  271. * @param din_len Maximum size of response in bytes
  272. * @return number of bytes in response, or -ve on error
  273. */
  274. static int ec_command_inptr(struct udevice *dev, uint8_t cmd,
  275. int cmd_version, const void *dout, int dout_len,
  276. uint8_t **dinp, int din_len)
  277. {
  278. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  279. uint8_t *din = NULL;
  280. int len;
  281. len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
  282. din_len);
  283. /* If the command doesn't complete, wait a while */
  284. if (len == -EC_RES_IN_PROGRESS) {
  285. struct ec_response_get_comms_status *resp = NULL;
  286. ulong start;
  287. /* Wait for command to complete */
  288. start = get_timer(0);
  289. do {
  290. int ret;
  291. mdelay(50); /* Insert some reasonable delay */
  292. ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
  293. NULL, 0,
  294. (uint8_t **)&resp, sizeof(*resp));
  295. if (ret < 0)
  296. return ret;
  297. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  298. debug("%s: Command %#02x timeout\n",
  299. __func__, cmd);
  300. return -EC_RES_TIMEOUT;
  301. }
  302. } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
  303. /* OK it completed, so read the status response */
  304. /* not sure why it was 0 for the last argument */
  305. len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
  306. &din, din_len);
  307. }
  308. debug("%s: len=%d, din=%p\n", __func__, len, din);
  309. if (dinp) {
  310. /* If we have any data to return, it must be 64bit-aligned */
  311. assert(len <= 0 || !((uintptr_t)din & 7));
  312. *dinp = din;
  313. }
  314. return len;
  315. }
  316. /**
  317. * Send a command to the CROS-EC device and return the reply.
  318. *
  319. * The device's internal input/output buffers are used.
  320. *
  321. * @param dev CROS-EC device
  322. * @param cmd Command to send (EC_CMD_...)
  323. * @param cmd_version Version of command to send (EC_VER_...)
  324. * @param dout Output data (may be NULL If dout_len=0)
  325. * @param dout_len Size of output data in bytes
  326. * @param din Response data (may be NULL If din_len=0).
  327. * It not NULL, it is a place for ec_command() to copy the
  328. * data to.
  329. * @param din_len Maximum size of response in bytes
  330. * @return number of bytes in response, or -ve on error
  331. */
  332. static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
  333. const void *dout, int dout_len,
  334. void *din, int din_len)
  335. {
  336. uint8_t *in_buffer;
  337. int len;
  338. assert((din_len == 0) || din);
  339. len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
  340. &in_buffer, din_len);
  341. if (len > 0) {
  342. /*
  343. * If we were asked to put it somewhere, do so, otherwise just
  344. * disregard the result.
  345. */
  346. if (din && in_buffer) {
  347. assert(len <= din_len);
  348. memmove(din, in_buffer, len);
  349. }
  350. }
  351. return len;
  352. }
  353. int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
  354. {
  355. if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
  356. sizeof(scan->data)) != sizeof(scan->data))
  357. return -1;
  358. return 0;
  359. }
  360. int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
  361. {
  362. struct ec_response_get_version *r;
  363. int ret;
  364. ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  365. (uint8_t **)&r, sizeof(*r));
  366. if (ret != sizeof(*r)) {
  367. log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
  368. return -1;
  369. }
  370. if (maxlen > (int)sizeof(r->version_string_ro))
  371. maxlen = sizeof(r->version_string_ro);
  372. switch (r->current_image) {
  373. case EC_IMAGE_RO:
  374. memcpy(id, r->version_string_ro, maxlen);
  375. break;
  376. case EC_IMAGE_RW:
  377. memcpy(id, r->version_string_rw, maxlen);
  378. break;
  379. default:
  380. log_err("Invalid EC image %d\n", r->current_image);
  381. return -1;
  382. }
  383. id[maxlen - 1] = '\0';
  384. return 0;
  385. }
  386. int cros_ec_read_version(struct udevice *dev,
  387. struct ec_response_get_version **versionp)
  388. {
  389. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  390. (uint8_t **)versionp, sizeof(**versionp))
  391. != sizeof(**versionp))
  392. return -1;
  393. return 0;
  394. }
  395. int cros_ec_read_build_info(struct udevice *dev, char **strp)
  396. {
  397. if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
  398. (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
  399. return -1;
  400. return 0;
  401. }
  402. int cros_ec_read_current_image(struct udevice *dev,
  403. enum ec_current_image *image)
  404. {
  405. struct ec_response_get_version *r;
  406. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  407. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  408. return -1;
  409. *image = r->current_image;
  410. return 0;
  411. }
  412. static int cros_ec_wait_on_hash_done(struct udevice *dev,
  413. struct ec_response_vboot_hash *hash)
  414. {
  415. struct ec_params_vboot_hash p;
  416. ulong start;
  417. start = get_timer(0);
  418. while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
  419. mdelay(50); /* Insert some reasonable delay */
  420. p.cmd = EC_VBOOT_HASH_GET;
  421. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  422. hash, sizeof(*hash)) < 0)
  423. return -1;
  424. if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
  425. debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
  426. return -EC_RES_TIMEOUT;
  427. }
  428. }
  429. return 0;
  430. }
  431. int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
  432. struct ec_response_vboot_hash *hash)
  433. {
  434. struct ec_params_vboot_hash p;
  435. int rv;
  436. p.cmd = EC_VBOOT_HASH_GET;
  437. p.offset = hash_offset;
  438. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  439. hash, sizeof(*hash)) < 0)
  440. return -1;
  441. /* If the EC is busy calculating the hash, fidget until it's done. */
  442. rv = cros_ec_wait_on_hash_done(dev, hash);
  443. if (rv)
  444. return rv;
  445. /* If the hash is valid, we're done. Otherwise, we have to kick it off
  446. * again and wait for it to complete. Note that we explicitly assume
  447. * that hashing zero bytes is always wrong, even though that would
  448. * produce a valid hash value. */
  449. if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
  450. return 0;
  451. debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
  452. __func__, hash->status, hash->size);
  453. p.cmd = EC_VBOOT_HASH_START;
  454. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  455. p.nonce_size = 0;
  456. p.offset = hash_offset;
  457. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  458. hash, sizeof(*hash)) < 0)
  459. return -1;
  460. rv = cros_ec_wait_on_hash_done(dev, hash);
  461. if (rv)
  462. return rv;
  463. debug("%s: hash done\n", __func__);
  464. return 0;
  465. }
  466. static int cros_ec_invalidate_hash(struct udevice *dev)
  467. {
  468. struct ec_params_vboot_hash p;
  469. struct ec_response_vboot_hash *hash;
  470. /* We don't have an explict command for the EC to discard its current
  471. * hash value, so we'll just tell it to calculate one that we know is
  472. * wrong (we claim that hashing zero bytes is always invalid).
  473. */
  474. p.cmd = EC_VBOOT_HASH_RECALC;
  475. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  476. p.nonce_size = 0;
  477. p.offset = 0;
  478. p.size = 0;
  479. debug("%s:\n", __func__);
  480. if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  481. (uint8_t **)&hash, sizeof(*hash)) < 0)
  482. return -1;
  483. /* No need to wait for it to finish */
  484. return 0;
  485. }
  486. int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
  487. {
  488. struct ec_params_reboot_ec p;
  489. p.cmd = cmd;
  490. p.flags = flags;
  491. if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
  492. < 0)
  493. return -1;
  494. if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
  495. /*
  496. * EC reboot will take place immediately so delay to allow it
  497. * to complete. Note that some reboot types (EC_REBOOT_COLD)
  498. * will reboot the AP as well, in which case we won't actually
  499. * get to this point.
  500. */
  501. /*
  502. * TODO(rspangler@chromium.org): Would be nice if we had a
  503. * better way to determine when the reboot is complete. Could
  504. * we poll a memory-mapped LPC value?
  505. */
  506. udelay(50000);
  507. }
  508. return 0;
  509. }
  510. int cros_ec_interrupt_pending(struct udevice *dev)
  511. {
  512. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  513. /* no interrupt support : always poll */
  514. if (!dm_gpio_is_valid(&cdev->ec_int))
  515. return -ENOENT;
  516. return dm_gpio_get_value(&cdev->ec_int);
  517. }
  518. int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
  519. {
  520. if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
  521. sizeof(*info)) != sizeof(*info))
  522. return -1;
  523. return 0;
  524. }
  525. int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
  526. {
  527. struct ec_response_host_event_mask rsp;
  528. int ret;
  529. ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
  530. if (ret < 0)
  531. return ret;
  532. else if (ret != sizeof(rsp))
  533. return -EINVAL;
  534. *mask = rsp.mask;
  535. return 0;
  536. }
  537. int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
  538. {
  539. struct ec_params_host_event_mask req;
  540. int ret;
  541. req.mask = mask;
  542. ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
  543. if (ret < 0)
  544. return ret;
  545. return 0;
  546. }
  547. int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
  548. {
  549. struct ec_response_host_event_mask *resp;
  550. /*
  551. * Use the B copy of the event flags, because the main copy is already
  552. * used by ACPI/SMI.
  553. */
  554. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
  555. (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
  556. return -1;
  557. if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
  558. return -1;
  559. *events_ptr = resp->mask;
  560. return 0;
  561. }
  562. int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
  563. {
  564. struct ec_params_host_event_mask params;
  565. params.mask = events;
  566. /*
  567. * Use the B copy of the event flags, so it affects the data returned
  568. * by cros_ec_get_host_events().
  569. */
  570. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
  571. &params, sizeof(params), NULL, 0) < 0)
  572. return -1;
  573. return 0;
  574. }
  575. int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
  576. uint32_t set_flags,
  577. struct ec_response_flash_protect *resp)
  578. {
  579. struct ec_params_flash_protect params;
  580. params.mask = set_mask;
  581. params.flags = set_flags;
  582. if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
  583. &params, sizeof(params),
  584. resp, sizeof(*resp)) != sizeof(*resp))
  585. return -1;
  586. return 0;
  587. }
  588. int cros_ec_entering_mode(struct udevice *dev, int mode)
  589. {
  590. int rc;
  591. rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
  592. NULL, 0);
  593. if (rc)
  594. return -1;
  595. return 0;
  596. }
  597. static int cros_ec_check_version(struct udevice *dev)
  598. {
  599. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  600. struct ec_params_hello req;
  601. struct ec_response_hello *resp;
  602. struct dm_cros_ec_ops *ops;
  603. int ret;
  604. ops = dm_cros_ec_get_ops(dev);
  605. if (ops->check_version) {
  606. ret = ops->check_version(dev);
  607. if (ret)
  608. return ret;
  609. }
  610. /*
  611. * TODO(sjg@chromium.org).
  612. * There is a strange oddity here with the EC. We could just ignore
  613. * the response, i.e. pass the last two parameters as NULL and 0.
  614. * In this case we won't read back very many bytes from the EC.
  615. * On the I2C bus the EC gets upset about this and will try to send
  616. * the bytes anyway. This means that we will have to wait for that
  617. * to complete before continuing with a new EC command.
  618. *
  619. * This problem is probably unique to the I2C bus.
  620. *
  621. * So for now, just read all the data anyway.
  622. */
  623. /* Try sending a version 3 packet */
  624. cdev->protocol_version = 3;
  625. req.in_data = 0;
  626. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  627. (uint8_t **)&resp, sizeof(*resp)) > 0)
  628. return 0;
  629. /* Try sending a version 2 packet */
  630. cdev->protocol_version = 2;
  631. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  632. (uint8_t **)&resp, sizeof(*resp)) > 0)
  633. return 0;
  634. /*
  635. * Fail if we're still here, since the EC doesn't understand any
  636. * protcol version we speak. Version 1 interface without command
  637. * version is no longer supported, and we don't know about any new
  638. * protocol versions.
  639. */
  640. cdev->protocol_version = 0;
  641. printf("%s: ERROR: old EC interface not supported\n", __func__);
  642. return -1;
  643. }
  644. int cros_ec_test(struct udevice *dev)
  645. {
  646. struct ec_params_hello req;
  647. struct ec_response_hello *resp;
  648. req.in_data = 0x12345678;
  649. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  650. (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
  651. printf("ec_command_inptr() returned error\n");
  652. return -1;
  653. }
  654. if (resp->out_data != req.in_data + 0x01020304) {
  655. printf("Received invalid handshake %x\n", resp->out_data);
  656. return -1;
  657. }
  658. return 0;
  659. }
  660. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  661. uint32_t *offset, uint32_t *size)
  662. {
  663. struct ec_params_flash_region_info p;
  664. struct ec_response_flash_region_info *r;
  665. int ret;
  666. p.region = region;
  667. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  668. EC_VER_FLASH_REGION_INFO,
  669. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  670. if (ret != sizeof(*r))
  671. return -1;
  672. if (offset)
  673. *offset = r->offset;
  674. if (size)
  675. *size = r->size;
  676. return 0;
  677. }
  678. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
  679. {
  680. struct ec_params_flash_erase p;
  681. p.offset = offset;
  682. p.size = size;
  683. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  684. NULL, 0);
  685. }
  686. /**
  687. * Write a single block to the flash
  688. *
  689. * Write a block of data to the EC flash. The size must not exceed the flash
  690. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  691. *
  692. * The offset starts at 0. You can obtain the region information from
  693. * cros_ec_flash_offset() to find out where to write for a particular region.
  694. *
  695. * Attempting to write to the region where the EC is currently running from
  696. * will result in an error.
  697. *
  698. * @param dev CROS-EC device
  699. * @param data Pointer to data buffer to write
  700. * @param offset Offset within flash to write to.
  701. * @param size Number of bytes to write
  702. * @return 0 if ok, -1 on error
  703. */
  704. static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
  705. uint32_t offset, uint32_t size)
  706. {
  707. struct ec_params_flash_write *p;
  708. int ret;
  709. p = malloc(sizeof(*p) + size);
  710. if (!p)
  711. return -ENOMEM;
  712. p->offset = offset;
  713. p->size = size;
  714. assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
  715. memcpy(p + 1, data, p->size);
  716. ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  717. p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
  718. free(p);
  719. return ret;
  720. }
  721. /**
  722. * Return optimal flash write burst size
  723. */
  724. static int cros_ec_flash_write_burst_size(struct udevice *dev)
  725. {
  726. return EC_FLASH_WRITE_VER0_SIZE;
  727. }
  728. /**
  729. * Check if a block of data is erased (all 0xff)
  730. *
  731. * This function is useful when dealing with flash, for checking whether a
  732. * data block is erased and thus does not need to be programmed.
  733. *
  734. * @param data Pointer to data to check (must be word-aligned)
  735. * @param size Number of bytes to check (must be word-aligned)
  736. * @return 0 if erased, non-zero if any word is not erased
  737. */
  738. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  739. {
  740. assert(!(size & 3));
  741. size /= sizeof(uint32_t);
  742. for (; size > 0; size -= 4, data++)
  743. if (*data != -1U)
  744. return 0;
  745. return 1;
  746. }
  747. /**
  748. * Read back flash parameters
  749. *
  750. * This function reads back parameters of the flash as reported by the EC
  751. *
  752. * @param dev Pointer to device
  753. * @param info Pointer to output flash info struct
  754. */
  755. int cros_ec_read_flashinfo(struct udevice *dev,
  756. struct ec_response_flash_info *info)
  757. {
  758. int ret;
  759. ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
  760. NULL, 0, info, sizeof(*info));
  761. if (ret < 0)
  762. return ret;
  763. return ret < sizeof(*info) ? -1 : 0;
  764. }
  765. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  766. uint32_t offset, uint32_t size)
  767. {
  768. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  769. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  770. uint32_t end, off;
  771. int ret;
  772. if (!burst)
  773. return -EINVAL;
  774. /*
  775. * TODO: round up to the nearest multiple of write size. Can get away
  776. * without that on link right now because its write size is 4 bytes.
  777. */
  778. end = offset + size;
  779. for (off = offset; off < end; off += burst, data += burst) {
  780. uint32_t todo;
  781. /* If the data is empty, there is no point in programming it */
  782. todo = min(end - off, burst);
  783. if (cdev->optimise_flash_write &&
  784. cros_ec_data_is_erased((uint32_t *)data, todo))
  785. continue;
  786. ret = cros_ec_flash_write_block(dev, data, off, todo);
  787. if (ret)
  788. return ret;
  789. }
  790. return 0;
  791. }
  792. /**
  793. * Run verification on a slot
  794. *
  795. * @param me CrosEc instance
  796. * @param region Region to run verification on
  797. * @return 0 if success or not applicable. Non-zero if verification failed.
  798. */
  799. int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
  800. {
  801. struct ec_params_efs_verify p;
  802. int rv;
  803. log_info("EFS: EC is verifying updated image...\n");
  804. p.region = region;
  805. rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
  806. if (rv >= 0) {
  807. log_info("EFS: Verification success\n");
  808. return 0;
  809. }
  810. if (rv == -EC_RES_INVALID_COMMAND) {
  811. log_info("EFS: EC doesn't support EFS_VERIFY command\n");
  812. return 0;
  813. }
  814. log_info("EFS: Verification failed\n");
  815. return rv;
  816. }
  817. /**
  818. * Read a single block from the flash
  819. *
  820. * Read a block of data from the EC flash. The size must not exceed the flash
  821. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  822. *
  823. * The offset starts at 0. You can obtain the region information from
  824. * cros_ec_flash_offset() to find out where to read for a particular region.
  825. *
  826. * @param dev CROS-EC device
  827. * @param data Pointer to data buffer to read into
  828. * @param offset Offset within flash to read from
  829. * @param size Number of bytes to read
  830. * @return 0 if ok, -1 on error
  831. */
  832. static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
  833. uint32_t offset, uint32_t size)
  834. {
  835. struct ec_params_flash_read p;
  836. p.offset = offset;
  837. p.size = size;
  838. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  839. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  840. }
  841. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  842. uint32_t size)
  843. {
  844. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  845. uint32_t end, off;
  846. int ret;
  847. end = offset + size;
  848. for (off = offset; off < end; off += burst, data += burst) {
  849. ret = cros_ec_flash_read_block(dev, data, off,
  850. min(end - off, burst));
  851. if (ret)
  852. return ret;
  853. }
  854. return 0;
  855. }
  856. int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
  857. int image_size)
  858. {
  859. uint32_t rw_offset, rw_size;
  860. int ret;
  861. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
  862. &rw_size))
  863. return -1;
  864. if (image_size > (int)rw_size)
  865. return -1;
  866. /* Invalidate the existing hash, just in case the AP reboots
  867. * unexpectedly during the update. If that happened, the EC RW firmware
  868. * would be invalid, but the EC would still have the original hash.
  869. */
  870. ret = cros_ec_invalidate_hash(dev);
  871. if (ret)
  872. return ret;
  873. /*
  874. * Erase the entire RW section, so that the EC doesn't see any garbage
  875. * past the new image if it's smaller than the current image.
  876. *
  877. * TODO: could optimize this to erase just the current image, since
  878. * presumably everything past that is 0xff's. But would still need to
  879. * round up to the nearest multiple of erase size.
  880. */
  881. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  882. if (ret)
  883. return ret;
  884. /* Write the image */
  885. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  886. if (ret)
  887. return ret;
  888. return 0;
  889. }
  890. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
  891. {
  892. struct ec_params_vbnvcontext p;
  893. int len;
  894. if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
  895. return -EINVAL;
  896. p.op = EC_VBNV_CONTEXT_OP_READ;
  897. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  898. &p, sizeof(uint32_t) + size, block, size);
  899. if (len != size) {
  900. log_err("Expected %d bytes, got %d\n", size, len);
  901. return -EIO;
  902. }
  903. return 0;
  904. }
  905. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
  906. {
  907. struct ec_params_vbnvcontext p;
  908. int len;
  909. if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
  910. return -EINVAL;
  911. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  912. memcpy(p.block, block, size);
  913. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  914. &p, sizeof(uint32_t) + size, NULL, 0);
  915. if (len < 0)
  916. return -1;
  917. return 0;
  918. }
  919. int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
  920. {
  921. struct ec_params_battery_cutoff p;
  922. int len;
  923. p.flags = flags;
  924. len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
  925. NULL, 0);
  926. if (len < 0)
  927. return -1;
  928. return 0;
  929. }
  930. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
  931. {
  932. struct ec_params_ldo_set params;
  933. params.index = index;
  934. params.state = state;
  935. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
  936. NULL, 0))
  937. return -1;
  938. return 0;
  939. }
  940. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
  941. {
  942. struct ec_params_ldo_get params;
  943. struct ec_response_ldo_get *resp;
  944. params.index = index;
  945. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
  946. (uint8_t **)&resp, sizeof(*resp)) !=
  947. sizeof(*resp))
  948. return -1;
  949. *state = resp->state;
  950. return 0;
  951. }
  952. int cros_ec_register(struct udevice *dev)
  953. {
  954. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  955. char id[MSG_BYTES];
  956. cdev->dev = dev;
  957. gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
  958. GPIOD_IS_IN);
  959. cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
  960. if (cros_ec_check_version(dev)) {
  961. debug("%s: Could not detect CROS-EC version\n", __func__);
  962. return -CROS_EC_ERR_CHECK_VERSION;
  963. }
  964. if (cros_ec_read_id(dev, id, sizeof(id))) {
  965. debug("%s: Could not read KBC ID\n", __func__);
  966. return -CROS_EC_ERR_READ_ID;
  967. }
  968. /* Remember this device for use by the cros_ec command */
  969. debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
  970. cdev->protocol_version, id);
  971. return 0;
  972. }
  973. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
  974. {
  975. ofnode flash_node, node;
  976. flash_node = dev_read_subnode(dev, "flash");
  977. if (!ofnode_valid(flash_node)) {
  978. debug("Failed to find flash node\n");
  979. return -1;
  980. }
  981. if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
  982. debug("Failed to decode flash node in chrome-ec\n");
  983. return -1;
  984. }
  985. config->flash_erase_value = ofnode_read_s32_default(flash_node,
  986. "erase-value", -1);
  987. ofnode_for_each_subnode(node, flash_node) {
  988. const char *name = ofnode_get_name(node);
  989. enum ec_flash_region region;
  990. if (0 == strcmp(name, "ro")) {
  991. region = EC_FLASH_REGION_RO;
  992. } else if (0 == strcmp(name, "rw")) {
  993. region = EC_FLASH_REGION_ACTIVE;
  994. } else if (0 == strcmp(name, "wp-ro")) {
  995. region = EC_FLASH_REGION_WP_RO;
  996. } else {
  997. debug("Unknown EC flash region name '%s'\n", name);
  998. return -1;
  999. }
  1000. if (ofnode_read_fmap_entry(node, &config->region[region])) {
  1001. debug("Failed to decode flash region in chrome-ec'\n");
  1002. return -1;
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
  1008. int nmsgs)
  1009. {
  1010. union {
  1011. struct ec_params_i2c_passthru p;
  1012. uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1013. } params;
  1014. union {
  1015. struct ec_response_i2c_passthru r;
  1016. uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1017. } response;
  1018. struct ec_params_i2c_passthru *p = &params.p;
  1019. struct ec_response_i2c_passthru *r = &response.r;
  1020. struct ec_params_i2c_passthru_msg *msg;
  1021. uint8_t *pdata, *read_ptr = NULL;
  1022. int read_len;
  1023. int size;
  1024. int rv;
  1025. int i;
  1026. p->port = port;
  1027. p->num_msgs = nmsgs;
  1028. size = sizeof(*p) + p->num_msgs * sizeof(*msg);
  1029. /* Create a message to write the register address and optional data */
  1030. pdata = (uint8_t *)p + size;
  1031. read_len = 0;
  1032. for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
  1033. bool is_read = in->flags & I2C_M_RD;
  1034. msg->addr_flags = in->addr;
  1035. msg->len = in->len;
  1036. if (is_read) {
  1037. msg->addr_flags |= EC_I2C_FLAG_READ;
  1038. read_len += in->len;
  1039. read_ptr = in->buf;
  1040. if (sizeof(*r) + read_len > sizeof(response)) {
  1041. puts("Read length too big for buffer\n");
  1042. return -1;
  1043. }
  1044. } else {
  1045. if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
  1046. puts("Params too large for buffer\n");
  1047. return -1;
  1048. }
  1049. memcpy(pdata, in->buf, in->len);
  1050. pdata += in->len;
  1051. }
  1052. }
  1053. rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
  1054. r, sizeof(*r) + read_len);
  1055. if (rv < 0)
  1056. return rv;
  1057. /* Parse response */
  1058. if (r->i2c_status & EC_I2C_STATUS_ERROR) {
  1059. printf("Transfer failed with status=0x%x\n", r->i2c_status);
  1060. return -1;
  1061. }
  1062. if (rv < sizeof(*r) + read_len) {
  1063. puts("Truncated read response\n");
  1064. return -1;
  1065. }
  1066. /* We only support a single read message for each transfer */
  1067. if (read_len)
  1068. memcpy(read_ptr, r->data, read_len);
  1069. return 0;
  1070. }
  1071. int cros_ec_check_feature(struct udevice *dev, int feature)
  1072. {
  1073. struct ec_response_get_features r;
  1074. int rv;
  1075. rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
  1076. if (rv)
  1077. return rv;
  1078. if (feature >= 8 * sizeof(r.flags))
  1079. return -1;
  1080. return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
  1081. }
  1082. /*
  1083. * Query the EC for specified mask indicating enabled events.
  1084. * The EC maintains separate event masks for SMI, SCI and WAKE.
  1085. */
  1086. static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
  1087. uint64_t *value)
  1088. {
  1089. int ret;
  1090. struct ec_params_host_event req;
  1091. struct ec_response_host_event rsp;
  1092. req.action = action;
  1093. req.mask_type = mask;
  1094. if (action != EC_HOST_EVENT_GET)
  1095. req.value = *value;
  1096. else
  1097. *value = 0;
  1098. ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
  1099. sizeof(rsp));
  1100. if (action != EC_HOST_EVENT_GET)
  1101. return ret;
  1102. if (ret == 0)
  1103. *value = rsp.value;
  1104. return ret;
  1105. }
  1106. static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
  1107. uint action, uint64_t *value)
  1108. {
  1109. int ret = -1;
  1110. struct ec_params_host_event_mask req;
  1111. struct ec_response_host_event_mask rsp;
  1112. if (hcmd == INVALID_HCMD)
  1113. return ret;
  1114. if (action != EC_HOST_EVENT_GET)
  1115. req.mask = (uint32_t)*value;
  1116. else
  1117. *value = 0;
  1118. ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
  1119. if (action != EC_HOST_EVENT_GET)
  1120. return ret;
  1121. if (ret == 0)
  1122. *value = rsp.mask;
  1123. return ret;
  1124. }
  1125. bool cros_ec_is_uhepi_supported(struct udevice *dev)
  1126. {
  1127. #define UHEPI_SUPPORTED 1
  1128. #define UHEPI_NOT_SUPPORTED 2
  1129. static int uhepi_support;
  1130. if (!uhepi_support) {
  1131. uhepi_support = cros_ec_check_feature(dev,
  1132. EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
  1133. UHEPI_NOT_SUPPORTED;
  1134. log_debug("Chrome EC: UHEPI %s\n",
  1135. uhepi_support == UHEPI_SUPPORTED ? "supported" :
  1136. "not supported");
  1137. }
  1138. return uhepi_support == UHEPI_SUPPORTED;
  1139. }
  1140. static int cros_ec_get_mask(struct udevice *dev, uint type)
  1141. {
  1142. u64 value = 0;
  1143. if (cros_ec_is_uhepi_supported(dev)) {
  1144. cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
  1145. } else {
  1146. assert(type < ARRAY_SIZE(event_map));
  1147. cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
  1148. EC_HOST_EVENT_GET, &value);
  1149. }
  1150. return value;
  1151. }
  1152. static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
  1153. {
  1154. if (cros_ec_is_uhepi_supported(dev))
  1155. return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
  1156. assert(type < ARRAY_SIZE(event_map));
  1157. return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
  1158. EC_HOST_EVENT_CLEAR, &mask);
  1159. }
  1160. uint64_t cros_ec_get_events_b(struct udevice *dev)
  1161. {
  1162. return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
  1163. }
  1164. int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
  1165. {
  1166. log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
  1167. return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
  1168. }
  1169. int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
  1170. {
  1171. struct ec_params_charge_state p;
  1172. struct ec_response_charge_state r;
  1173. int ret;
  1174. p.cmd = CHARGE_STATE_CMD_GET_PARAM;
  1175. p.get_param.param = CS_PARAM_LIMIT_POWER;
  1176. ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
  1177. &r, sizeof(r));
  1178. /*
  1179. * If our EC doesn't support the LIMIT_POWER parameter, assume that
  1180. * LIMIT_POWER is not requested.
  1181. */
  1182. if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
  1183. log_warning("PARAM_LIMIT_POWER not supported by EC\n");
  1184. return -ENOSYS;
  1185. }
  1186. if (ret != sizeof(r.get_param))
  1187. return -EINVAL;
  1188. *limit_powerp = r.get_param.value;
  1189. return 0;
  1190. }
  1191. int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
  1192. {
  1193. struct ec_params_config_power_button params;
  1194. int ret;
  1195. params.flags = flags;
  1196. ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
  1197. &params, sizeof(params), NULL, 0);
  1198. if (ret < 0)
  1199. return ret;
  1200. return 0;
  1201. }
  1202. int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
  1203. {
  1204. u32 mask;
  1205. int ret;
  1206. ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
  1207. &mask);
  1208. if (ret < 0)
  1209. return ret;
  1210. return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
  1211. }
  1212. int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
  1213. {
  1214. u32 mask;
  1215. int ret;
  1216. ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
  1217. &mask);
  1218. if (ret < 0)
  1219. return ret;
  1220. /* Set lid close event state in the EC SMI event mask */
  1221. if (enable)
  1222. mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
  1223. else
  1224. mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
  1225. ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
  1226. if (ret < 0)
  1227. return ret;
  1228. printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
  1229. return 0;
  1230. }
  1231. UCLASS_DRIVER(cros_ec) = {
  1232. .id = UCLASS_CROS_EC,
  1233. .name = "cros-ec",
  1234. .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
  1235. .post_bind = dm_scan_fdt_dev,
  1236. .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
  1237. };