cros_ec.c 29 KB

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