cros_ec.c 29 KB

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