cros_ec.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Chromium OS cros_ec driver
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. */
  7. /*
  8. * This is the interface to the Chrome OS EC. It provides keyboard functions,
  9. * power control and battery management. Quite a few other functions are
  10. * provided to enable the EC software to be updated, talk to the EC's I2C bus
  11. * and store a small amount of data in a memory which persists while the EC
  12. * is not reset.
  13. */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <dm.h>
  17. #include <i2c.h>
  18. #include <cros_ec.h>
  19. #include <fdtdec.h>
  20. #include <malloc.h>
  21. #include <spi.h>
  22. #include <linux/errno.h>
  23. #include <asm/io.h>
  24. #include <asm-generic/gpio.h>
  25. #include <dm/device-internal.h>
  26. #include <dm/of_extra.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. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
  40. {
  41. #ifdef DEBUG
  42. int i;
  43. printf("%s: ", name);
  44. if (cmd != -1)
  45. printf("cmd=%#x: ", cmd);
  46. for (i = 0; i < len; i++)
  47. printf("%02x ", data[i]);
  48. printf("\n");
  49. #endif
  50. }
  51. /*
  52. * Calculate a simple 8-bit checksum of a data block
  53. *
  54. * @param data Data block to checksum
  55. * @param size Size of data block in bytes
  56. * @return checksum value (0 to 255)
  57. */
  58. int cros_ec_calc_checksum(const uint8_t *data, int size)
  59. {
  60. int csum, i;
  61. for (i = csum = 0; i < size; i++)
  62. csum += data[i];
  63. return csum & 0xff;
  64. }
  65. /**
  66. * Create a request packet for protocol version 3.
  67. *
  68. * The packet is stored in the device's internal output buffer.
  69. *
  70. * @param dev CROS-EC device
  71. * @param cmd Command to send (EC_CMD_...)
  72. * @param cmd_version Version of command to send (EC_VER_...)
  73. * @param dout Output data (may be NULL If dout_len=0)
  74. * @param dout_len Size of output data in bytes
  75. * @return packet size in bytes, or <0 if error.
  76. */
  77. static int create_proto3_request(struct cros_ec_dev *dev,
  78. int cmd, int cmd_version,
  79. const void *dout, int dout_len)
  80. {
  81. struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
  82. int out_bytes = dout_len + sizeof(*rq);
  83. /* Fail if output size is too big */
  84. if (out_bytes > (int)sizeof(dev->dout)) {
  85. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  86. return -EC_RES_REQUEST_TRUNCATED;
  87. }
  88. /* Fill in request packet */
  89. rq->struct_version = EC_HOST_REQUEST_VERSION;
  90. rq->checksum = 0;
  91. rq->command = cmd;
  92. rq->command_version = cmd_version;
  93. rq->reserved = 0;
  94. rq->data_len = dout_len;
  95. /* Copy data after header */
  96. memcpy(rq + 1, dout, dout_len);
  97. /* Write checksum field so the entire packet sums to 0 */
  98. rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
  99. cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
  100. /* Return size of request packet */
  101. return out_bytes;
  102. }
  103. /**
  104. * Prepare the device to receive a protocol version 3 response.
  105. *
  106. * @param dev CROS-EC device
  107. * @param din_len Maximum size of response in bytes
  108. * @return maximum expected number of bytes in response, or <0 if error.
  109. */
  110. static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
  111. {
  112. int in_bytes = din_len + sizeof(struct ec_host_response);
  113. /* Fail if input size is too big */
  114. if (in_bytes > (int)sizeof(dev->din)) {
  115. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  116. return -EC_RES_RESPONSE_TOO_BIG;
  117. }
  118. /* Return expected size of response packet */
  119. return in_bytes;
  120. }
  121. /**
  122. * Handle a protocol version 3 response packet.
  123. *
  124. * The packet must already be stored in the device's internal input buffer.
  125. *
  126. * @param dev CROS-EC device
  127. * @param dinp Returns pointer to response data
  128. * @param din_len Maximum size of response in bytes
  129. * @return number of bytes of response data, or <0 if error. Note that error
  130. * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
  131. * overlap!)
  132. */
  133. static int handle_proto3_response(struct cros_ec_dev *dev,
  134. uint8_t **dinp, int din_len)
  135. {
  136. struct ec_host_response *rs = (struct ec_host_response *)dev->din;
  137. int in_bytes;
  138. int csum;
  139. cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
  140. /* Check input data */
  141. if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
  142. debug("%s: EC response version mismatch\n", __func__);
  143. return -EC_RES_INVALID_RESPONSE;
  144. }
  145. if (rs->reserved) {
  146. debug("%s: EC response reserved != 0\n", __func__);
  147. return -EC_RES_INVALID_RESPONSE;
  148. }
  149. if (rs->data_len > din_len) {
  150. debug("%s: EC returned too much data\n", __func__);
  151. return -EC_RES_RESPONSE_TOO_BIG;
  152. }
  153. cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
  154. /* Update in_bytes to actual data size */
  155. in_bytes = sizeof(*rs) + rs->data_len;
  156. /* Verify checksum */
  157. csum = cros_ec_calc_checksum(dev->din, in_bytes);
  158. if (csum) {
  159. debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
  160. csum);
  161. return -EC_RES_INVALID_CHECKSUM;
  162. }
  163. /* Return error result, if any */
  164. if (rs->result)
  165. return -(int)rs->result;
  166. /* If we're still here, set response data pointer and return length */
  167. *dinp = (uint8_t *)(rs + 1);
  168. return rs->data_len;
  169. }
  170. static int send_command_proto3(struct cros_ec_dev *dev,
  171. int cmd, int cmd_version,
  172. const void *dout, int dout_len,
  173. uint8_t **dinp, int din_len)
  174. {
  175. struct dm_cros_ec_ops *ops;
  176. int out_bytes, in_bytes;
  177. int rv;
  178. /* Create request packet */
  179. out_bytes = create_proto3_request(dev, cmd, cmd_version,
  180. dout, dout_len);
  181. if (out_bytes < 0)
  182. return out_bytes;
  183. /* Prepare response buffer */
  184. in_bytes = prepare_proto3_response_buffer(dev, din_len);
  185. if (in_bytes < 0)
  186. return in_bytes;
  187. ops = dm_cros_ec_get_ops(dev->dev);
  188. rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
  189. if (rv < 0)
  190. return rv;
  191. /* Process the response */
  192. return handle_proto3_response(dev, dinp, din_len);
  193. }
  194. static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  195. const void *dout, int dout_len,
  196. uint8_t **dinp, int din_len)
  197. {
  198. struct dm_cros_ec_ops *ops;
  199. int ret = -1;
  200. /* Handle protocol version 3 support */
  201. if (dev->protocol_version == 3) {
  202. return send_command_proto3(dev, cmd, cmd_version,
  203. dout, dout_len, dinp, din_len);
  204. }
  205. ops = dm_cros_ec_get_ops(dev->dev);
  206. ret = ops->command(dev->dev, cmd, cmd_version,
  207. (const uint8_t *)dout, dout_len, dinp, din_len);
  208. return ret;
  209. }
  210. /**
  211. * Send a command to the CROS-EC device and return the reply.
  212. *
  213. * The device's internal input/output buffers are used.
  214. *
  215. * @param dev CROS-EC device
  216. * @param cmd Command to send (EC_CMD_...)
  217. * @param cmd_version Version of command to send (EC_VER_...)
  218. * @param dout Output data (may be NULL If dout_len=0)
  219. * @param dout_len Size of output data in bytes
  220. * @param dinp Response data (may be NULL If din_len=0).
  221. * If not NULL, it will be updated to point to the data
  222. * and will always be double word aligned (64-bits)
  223. * @param din_len Maximum size of response in bytes
  224. * @return number of bytes in response, or -ve on error
  225. */
  226. static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
  227. int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
  228. int din_len)
  229. {
  230. uint8_t *din = NULL;
  231. int len;
  232. len = send_command(dev, cmd, cmd_version, dout, dout_len,
  233. &din, din_len);
  234. /* If the command doesn't complete, wait a while */
  235. if (len == -EC_RES_IN_PROGRESS) {
  236. struct ec_response_get_comms_status *resp = NULL;
  237. ulong start;
  238. /* Wait for command to complete */
  239. start = get_timer(0);
  240. do {
  241. int ret;
  242. mdelay(50); /* Insert some reasonable delay */
  243. ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
  244. NULL, 0,
  245. (uint8_t **)&resp, sizeof(*resp));
  246. if (ret < 0)
  247. return ret;
  248. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  249. debug("%s: Command %#02x timeout\n",
  250. __func__, cmd);
  251. return -EC_RES_TIMEOUT;
  252. }
  253. } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
  254. /* OK it completed, so read the status response */
  255. /* not sure why it was 0 for the last argument */
  256. len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
  257. NULL, 0, &din, din_len);
  258. }
  259. debug("%s: len=%d, din=%p\n", __func__, len, din);
  260. if (dinp) {
  261. /* If we have any data to return, it must be 64bit-aligned */
  262. assert(len <= 0 || !((uintptr_t)din & 7));
  263. *dinp = din;
  264. }
  265. return len;
  266. }
  267. /**
  268. * Send a command to the CROS-EC device and return the reply.
  269. *
  270. * The device's internal input/output buffers are used.
  271. *
  272. * @param dev CROS-EC device
  273. * @param cmd Command to send (EC_CMD_...)
  274. * @param cmd_version Version of command to send (EC_VER_...)
  275. * @param dout Output data (may be NULL If dout_len=0)
  276. * @param dout_len Size of output data in bytes
  277. * @param din Response data (may be NULL If din_len=0).
  278. * It not NULL, it is a place for ec_command() to copy the
  279. * data to.
  280. * @param din_len Maximum size of response in bytes
  281. * @return number of bytes in response, or -ve on error
  282. */
  283. static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  284. const void *dout, int dout_len,
  285. void *din, int din_len)
  286. {
  287. uint8_t *in_buffer;
  288. int len;
  289. assert((din_len == 0) || din);
  290. len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
  291. &in_buffer, din_len);
  292. if (len > 0) {
  293. /*
  294. * If we were asked to put it somewhere, do so, otherwise just
  295. * disregard the result.
  296. */
  297. if (din && in_buffer) {
  298. assert(len <= din_len);
  299. memmove(din, in_buffer, len);
  300. }
  301. }
  302. return len;
  303. }
  304. int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
  305. {
  306. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  307. if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
  308. sizeof(scan->data)) != sizeof(scan->data))
  309. return -1;
  310. return 0;
  311. }
  312. int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
  313. {
  314. struct ec_response_get_version *r;
  315. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  316. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  317. return -1;
  318. if (maxlen > (int)sizeof(r->version_string_ro))
  319. maxlen = sizeof(r->version_string_ro);
  320. switch (r->current_image) {
  321. case EC_IMAGE_RO:
  322. memcpy(id, r->version_string_ro, maxlen);
  323. break;
  324. case EC_IMAGE_RW:
  325. memcpy(id, r->version_string_rw, maxlen);
  326. break;
  327. default:
  328. return -1;
  329. }
  330. id[maxlen - 1] = '\0';
  331. return 0;
  332. }
  333. int cros_ec_read_version(struct cros_ec_dev *dev,
  334. struct ec_response_get_version **versionp)
  335. {
  336. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  337. (uint8_t **)versionp, sizeof(**versionp))
  338. != sizeof(**versionp))
  339. return -1;
  340. return 0;
  341. }
  342. int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
  343. {
  344. if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
  345. (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
  346. return -1;
  347. return 0;
  348. }
  349. int cros_ec_read_current_image(struct cros_ec_dev *dev,
  350. enum ec_current_image *image)
  351. {
  352. struct ec_response_get_version *r;
  353. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  354. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  355. return -1;
  356. *image = r->current_image;
  357. return 0;
  358. }
  359. static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
  360. struct ec_response_vboot_hash *hash)
  361. {
  362. struct ec_params_vboot_hash p;
  363. ulong start;
  364. start = get_timer(0);
  365. while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
  366. mdelay(50); /* Insert some reasonable delay */
  367. p.cmd = EC_VBOOT_HASH_GET;
  368. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  369. hash, sizeof(*hash)) < 0)
  370. return -1;
  371. if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
  372. debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
  373. return -EC_RES_TIMEOUT;
  374. }
  375. }
  376. return 0;
  377. }
  378. int cros_ec_read_hash(struct cros_ec_dev *dev,
  379. struct ec_response_vboot_hash *hash)
  380. {
  381. struct ec_params_vboot_hash p;
  382. int rv;
  383. p.cmd = EC_VBOOT_HASH_GET;
  384. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  385. hash, sizeof(*hash)) < 0)
  386. return -1;
  387. /* If the EC is busy calculating the hash, fidget until it's done. */
  388. rv = cros_ec_wait_on_hash_done(dev, hash);
  389. if (rv)
  390. return rv;
  391. /* If the hash is valid, we're done. Otherwise, we have to kick it off
  392. * again and wait for it to complete. Note that we explicitly assume
  393. * that hashing zero bytes is always wrong, even though that would
  394. * produce a valid hash value. */
  395. if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
  396. return 0;
  397. debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
  398. __func__, hash->status, hash->size);
  399. p.cmd = EC_VBOOT_HASH_START;
  400. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  401. p.nonce_size = 0;
  402. p.offset = EC_VBOOT_HASH_OFFSET_RW;
  403. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  404. hash, sizeof(*hash)) < 0)
  405. return -1;
  406. rv = cros_ec_wait_on_hash_done(dev, hash);
  407. if (rv)
  408. return rv;
  409. debug("%s: hash done\n", __func__);
  410. return 0;
  411. }
  412. static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
  413. {
  414. struct ec_params_vboot_hash p;
  415. struct ec_response_vboot_hash *hash;
  416. /* We don't have an explict command for the EC to discard its current
  417. * hash value, so we'll just tell it to calculate one that we know is
  418. * wrong (we claim that hashing zero bytes is always invalid).
  419. */
  420. p.cmd = EC_VBOOT_HASH_RECALC;
  421. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  422. p.nonce_size = 0;
  423. p.offset = 0;
  424. p.size = 0;
  425. debug("%s:\n", __func__);
  426. if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  427. (uint8_t **)&hash, sizeof(*hash)) < 0)
  428. return -1;
  429. /* No need to wait for it to finish */
  430. return 0;
  431. }
  432. int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
  433. uint8_t flags)
  434. {
  435. struct ec_params_reboot_ec p;
  436. p.cmd = cmd;
  437. p.flags = flags;
  438. if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
  439. < 0)
  440. return -1;
  441. if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
  442. /*
  443. * EC reboot will take place immediately so delay to allow it
  444. * to complete. Note that some reboot types (EC_REBOOT_COLD)
  445. * will reboot the AP as well, in which case we won't actually
  446. * get to this point.
  447. */
  448. /*
  449. * TODO(rspangler@chromium.org): Would be nice if we had a
  450. * better way to determine when the reboot is complete. Could
  451. * we poll a memory-mapped LPC value?
  452. */
  453. udelay(50000);
  454. }
  455. return 0;
  456. }
  457. int cros_ec_interrupt_pending(struct udevice *dev)
  458. {
  459. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  460. /* no interrupt support : always poll */
  461. if (!dm_gpio_is_valid(&cdev->ec_int))
  462. return -ENOENT;
  463. return dm_gpio_get_value(&cdev->ec_int);
  464. }
  465. int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
  466. {
  467. if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
  468. sizeof(*info)) != sizeof(*info))
  469. return -1;
  470. return 0;
  471. }
  472. int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
  473. {
  474. struct ec_response_host_event_mask *resp;
  475. /*
  476. * Use the B copy of the event flags, because the main copy is already
  477. * used by ACPI/SMI.
  478. */
  479. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
  480. (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
  481. return -1;
  482. if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
  483. return -1;
  484. *events_ptr = resp->mask;
  485. return 0;
  486. }
  487. int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
  488. {
  489. struct ec_params_host_event_mask params;
  490. params.mask = events;
  491. /*
  492. * Use the B copy of the event flags, so it affects the data returned
  493. * by cros_ec_get_host_events().
  494. */
  495. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
  496. &params, sizeof(params), NULL, 0) < 0)
  497. return -1;
  498. return 0;
  499. }
  500. int cros_ec_flash_protect(struct cros_ec_dev *dev,
  501. uint32_t set_mask, uint32_t set_flags,
  502. struct ec_response_flash_protect *resp)
  503. {
  504. struct ec_params_flash_protect params;
  505. params.mask = set_mask;
  506. params.flags = set_flags;
  507. if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
  508. &params, sizeof(params),
  509. resp, sizeof(*resp)) != sizeof(*resp))
  510. return -1;
  511. return 0;
  512. }
  513. static int cros_ec_check_version(struct cros_ec_dev *dev)
  514. {
  515. struct ec_params_hello req;
  516. struct ec_response_hello *resp;
  517. struct dm_cros_ec_ops *ops;
  518. int ret;
  519. ops = dm_cros_ec_get_ops(dev->dev);
  520. if (ops->check_version) {
  521. ret = ops->check_version(dev->dev);
  522. if (ret)
  523. return ret;
  524. }
  525. /*
  526. * TODO(sjg@chromium.org).
  527. * There is a strange oddity here with the EC. We could just ignore
  528. * the response, i.e. pass the last two parameters as NULL and 0.
  529. * In this case we won't read back very many bytes from the EC.
  530. * On the I2C bus the EC gets upset about this and will try to send
  531. * the bytes anyway. This means that we will have to wait for that
  532. * to complete before continuing with a new EC command.
  533. *
  534. * This problem is probably unique to the I2C bus.
  535. *
  536. * So for now, just read all the data anyway.
  537. */
  538. /* Try sending a version 3 packet */
  539. dev->protocol_version = 3;
  540. req.in_data = 0;
  541. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  542. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  543. return 0;
  544. }
  545. /* Try sending a version 2 packet */
  546. dev->protocol_version = 2;
  547. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  548. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  549. return 0;
  550. }
  551. /*
  552. * Fail if we're still here, since the EC doesn't understand any
  553. * protcol version we speak. Version 1 interface without command
  554. * version is no longer supported, and we don't know about any new
  555. * protocol versions.
  556. */
  557. dev->protocol_version = 0;
  558. printf("%s: ERROR: old EC interface not supported\n", __func__);
  559. return -1;
  560. }
  561. int cros_ec_test(struct cros_ec_dev *dev)
  562. {
  563. struct ec_params_hello req;
  564. struct ec_response_hello *resp;
  565. req.in_data = 0x12345678;
  566. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  567. (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
  568. printf("ec_command_inptr() returned error\n");
  569. return -1;
  570. }
  571. if (resp->out_data != req.in_data + 0x01020304) {
  572. printf("Received invalid handshake %x\n", resp->out_data);
  573. return -1;
  574. }
  575. return 0;
  576. }
  577. int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
  578. uint32_t *offset, uint32_t *size)
  579. {
  580. struct ec_params_flash_region_info p;
  581. struct ec_response_flash_region_info *r;
  582. int ret;
  583. p.region = region;
  584. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  585. EC_VER_FLASH_REGION_INFO,
  586. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  587. if (ret != sizeof(*r))
  588. return -1;
  589. if (offset)
  590. *offset = r->offset;
  591. if (size)
  592. *size = r->size;
  593. return 0;
  594. }
  595. int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
  596. {
  597. struct ec_params_flash_erase p;
  598. p.offset = offset;
  599. p.size = size;
  600. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  601. NULL, 0);
  602. }
  603. /**
  604. * Write a single block to the flash
  605. *
  606. * Write a block of data to the EC flash. The size must not exceed the flash
  607. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  608. *
  609. * The offset starts at 0. You can obtain the region information from
  610. * cros_ec_flash_offset() to find out where to write for a particular region.
  611. *
  612. * Attempting to write to the region where the EC is currently running from
  613. * will result in an error.
  614. *
  615. * @param dev CROS-EC device
  616. * @param data Pointer to data buffer to write
  617. * @param offset Offset within flash to write to.
  618. * @param size Number of bytes to write
  619. * @return 0 if ok, -1 on error
  620. */
  621. static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
  622. const uint8_t *data, uint32_t offset, uint32_t size)
  623. {
  624. struct ec_params_flash_write *p;
  625. int ret;
  626. p = malloc(sizeof(*p) + size);
  627. if (!p)
  628. return -ENOMEM;
  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. ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  634. p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
  635. free(p);
  636. return ret;
  637. }
  638. /**
  639. * Return optimal flash write burst size
  640. */
  641. static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
  642. {
  643. return EC_FLASH_WRITE_VER0_SIZE;
  644. }
  645. /**
  646. * Check if a block of data is erased (all 0xff)
  647. *
  648. * This function is useful when dealing with flash, for checking whether a
  649. * data block is erased and thus does not need to be programmed.
  650. *
  651. * @param data Pointer to data to check (must be word-aligned)
  652. * @param size Number of bytes to check (must be word-aligned)
  653. * @return 0 if erased, non-zero if any word is not erased
  654. */
  655. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  656. {
  657. assert(!(size & 3));
  658. size /= sizeof(uint32_t);
  659. for (; size > 0; size -= 4, data++)
  660. if (*data != -1U)
  661. return 0;
  662. return 1;
  663. }
  664. /**
  665. * Read back flash parameters
  666. *
  667. * This function reads back parameters of the flash as reported by the EC
  668. *
  669. * @param dev Pointer to device
  670. * @param info Pointer to output flash info struct
  671. */
  672. int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
  673. struct ec_response_flash_info *info)
  674. {
  675. int ret;
  676. ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
  677. NULL, 0, info, sizeof(*info));
  678. if (ret < 0)
  679. return ret;
  680. return ret < sizeof(*info) ? -1 : 0;
  681. }
  682. int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  683. uint32_t offset, uint32_t size)
  684. {
  685. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  686. uint32_t end, off;
  687. int ret;
  688. /*
  689. * TODO: round up to the nearest multiple of write size. Can get away
  690. * without that on link right now because its write size is 4 bytes.
  691. */
  692. end = offset + size;
  693. for (off = offset; off < end; off += burst, data += burst) {
  694. uint32_t todo;
  695. /* If the data is empty, there is no point in programming it */
  696. todo = min(end - off, burst);
  697. if (dev->optimise_flash_write &&
  698. cros_ec_data_is_erased((uint32_t *)data, todo))
  699. continue;
  700. ret = cros_ec_flash_write_block(dev, data, off, todo);
  701. if (ret)
  702. return ret;
  703. }
  704. return 0;
  705. }
  706. /**
  707. * Read a single block from the flash
  708. *
  709. * Read a block of data from the EC flash. The size must not exceed the flash
  710. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  711. *
  712. * The offset starts at 0. You can obtain the region information from
  713. * cros_ec_flash_offset() to find out where to read for a particular region.
  714. *
  715. * @param dev CROS-EC device
  716. * @param data Pointer to data buffer to read into
  717. * @param offset Offset within flash to read from
  718. * @param size Number of bytes to read
  719. * @return 0 if ok, -1 on error
  720. */
  721. static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
  722. uint32_t offset, uint32_t size)
  723. {
  724. struct ec_params_flash_read p;
  725. p.offset = offset;
  726. p.size = size;
  727. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  728. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  729. }
  730. int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
  731. uint32_t size)
  732. {
  733. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  734. uint32_t end, off;
  735. int ret;
  736. end = offset + size;
  737. for (off = offset; off < end; off += burst, data += burst) {
  738. ret = cros_ec_flash_read_block(dev, data, off,
  739. min(end - off, burst));
  740. if (ret)
  741. return ret;
  742. }
  743. return 0;
  744. }
  745. int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  746. const uint8_t *image, int image_size)
  747. {
  748. uint32_t rw_offset, rw_size;
  749. int ret;
  750. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
  751. return -1;
  752. if (image_size > (int)rw_size)
  753. return -1;
  754. /* Invalidate the existing hash, just in case the AP reboots
  755. * unexpectedly during the update. If that happened, the EC RW firmware
  756. * would be invalid, but the EC would still have the original hash.
  757. */
  758. ret = cros_ec_invalidate_hash(dev);
  759. if (ret)
  760. return ret;
  761. /*
  762. * Erase the entire RW section, so that the EC doesn't see any garbage
  763. * past the new image if it's smaller than the current image.
  764. *
  765. * TODO: could optimize this to erase just the current image, since
  766. * presumably everything past that is 0xff's. But would still need to
  767. * round up to the nearest multiple of erase size.
  768. */
  769. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  770. if (ret)
  771. return ret;
  772. /* Write the image */
  773. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  774. if (ret)
  775. return ret;
  776. return 0;
  777. }
  778. int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
  779. {
  780. struct ec_params_vbnvcontext p;
  781. int len;
  782. p.op = EC_VBNV_CONTEXT_OP_READ;
  783. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  784. &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
  785. if (len < EC_VBNV_BLOCK_SIZE)
  786. return -1;
  787. return 0;
  788. }
  789. int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
  790. {
  791. struct ec_params_vbnvcontext p;
  792. int len;
  793. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  794. memcpy(p.block, block, sizeof(p.block));
  795. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  796. &p, sizeof(p), NULL, 0);
  797. if (len < 0)
  798. return -1;
  799. return 0;
  800. }
  801. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
  802. {
  803. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  804. struct ec_params_ldo_set params;
  805. params.index = index;
  806. params.state = state;
  807. if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
  808. NULL, 0))
  809. return -1;
  810. return 0;
  811. }
  812. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
  813. {
  814. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  815. struct ec_params_ldo_get params;
  816. struct ec_response_ldo_get *resp;
  817. params.index = index;
  818. if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
  819. (uint8_t **)&resp, sizeof(*resp)) !=
  820. sizeof(*resp))
  821. return -1;
  822. *state = resp->state;
  823. return 0;
  824. }
  825. int cros_ec_register(struct udevice *dev)
  826. {
  827. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  828. char id[MSG_BYTES];
  829. cdev->dev = dev;
  830. gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
  831. GPIOD_IS_IN);
  832. cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
  833. if (cros_ec_check_version(cdev)) {
  834. debug("%s: Could not detect CROS-EC version\n", __func__);
  835. return -CROS_EC_ERR_CHECK_VERSION;
  836. }
  837. if (cros_ec_read_id(cdev, id, sizeof(id))) {
  838. debug("%s: Could not read KBC ID\n", __func__);
  839. return -CROS_EC_ERR_READ_ID;
  840. }
  841. /* Remember this device for use by the cros_ec command */
  842. debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
  843. cdev->protocol_version, id);
  844. return 0;
  845. }
  846. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
  847. {
  848. ofnode flash_node, node;
  849. flash_node = dev_read_subnode(dev, "flash");
  850. if (!ofnode_valid(flash_node)) {
  851. debug("Failed to find flash node\n");
  852. return -1;
  853. }
  854. if (of_read_fmap_entry(flash_node, "flash", &config->flash)) {
  855. debug("Failed to decode flash node in chrome-ec\n");
  856. return -1;
  857. }
  858. config->flash_erase_value = ofnode_read_s32_default(flash_node,
  859. "erase-value", -1);
  860. ofnode_for_each_subnode(node, flash_node) {
  861. const char *name = ofnode_get_name(node);
  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 (of_read_fmap_entry(node, "reg", &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, int port, struct i2c_msg *in,
  881. 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 = port;
  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. UCLASS_DRIVER(cros_ec) = {
  946. .id = UCLASS_CROS_EC,
  947. .name = "cros_ec",
  948. .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
  949. .post_bind = dm_scan_fdt_dev,
  950. };