cros_ec.c 38 KB

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