cros_ec.c 38 KB

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