cros_ec.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 *cdev,
  78. int cmd, int cmd_version,
  79. const void *dout, int dout_len)
  80. {
  81. struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
  82. int out_bytes = dout_len + sizeof(*rq);
  83. /* Fail if output size is too big */
  84. if (out_bytes > (int)sizeof(cdev->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(cdev->dout, out_bytes));
  99. cros_ec_dump_data("out", cmd, cdev->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 *cdev, 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(cdev->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 *cdev,
  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(cdev, 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(cdev, din_len);
  185. if (in_bytes < 0)
  186. return in_bytes;
  187. ops = dm_cros_ec_get_ops(cdev->dev);
  188. rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
  189. -ENOSYS;
  190. if (rv < 0)
  191. return rv;
  192. /* Process the response */
  193. return handle_proto3_response(cdev, 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 udevice *dev, uint8_t cmd,
  228. int cmd_version, const void *dout, int dout_len,
  229. uint8_t **dinp, int din_len)
  230. {
  231. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  232. uint8_t *din = NULL;
  233. int len;
  234. len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
  235. din_len);
  236. /* If the command doesn't complete, wait a while */
  237. if (len == -EC_RES_IN_PROGRESS) {
  238. struct ec_response_get_comms_status *resp = NULL;
  239. ulong start;
  240. /* Wait for command to complete */
  241. start = get_timer(0);
  242. do {
  243. int ret;
  244. mdelay(50); /* Insert some reasonable delay */
  245. ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
  246. NULL, 0,
  247. (uint8_t **)&resp, sizeof(*resp));
  248. if (ret < 0)
  249. return ret;
  250. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  251. debug("%s: Command %#02x timeout\n",
  252. __func__, cmd);
  253. return -EC_RES_TIMEOUT;
  254. }
  255. } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
  256. /* OK it completed, so read the status response */
  257. /* not sure why it was 0 for the last argument */
  258. len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
  259. &din, din_len);
  260. }
  261. debug("%s: len=%d, din=%p\n", __func__, len, din);
  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 udevice *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. if (ec_command(dev, 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 udevice *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 udevice *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 udevice *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 udevice *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 udevice *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 udevice *dev, uint hash_offset,
  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. p.offset = hash_offset;
  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 = hash_offset;
  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 udevice *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 udevice *dev, enum ec_reboot_cmd cmd, 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 udevice *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 udevice *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 udevice *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 udevice *dev, uint32_t set_mask,
  502. 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 udevice *dev)
  515. {
  516. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  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);
  522. if (ops->check_version) {
  523. ret = ops->check_version(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. cdev->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. cdev->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. cdev->protocol_version = 0;
  560. printf("%s: ERROR: old EC interface not supported\n", __func__);
  561. return -1;
  562. }
  563. int cros_ec_test(struct udevice *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 udevice *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 udevice *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 udevice *dev, const uint8_t *data,
  624. 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 udevice *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 udevice *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 udevice *dev, const uint8_t *data,
  685. uint32_t offset, uint32_t size)
  686. {
  687. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  688. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  689. uint32_t end, off;
  690. int ret;
  691. /*
  692. * TODO: round up to the nearest multiple of write size. Can get away
  693. * without that on link right now because its write size is 4 bytes.
  694. */
  695. end = offset + size;
  696. for (off = offset; off < end; off += burst, data += burst) {
  697. uint32_t todo;
  698. /* If the data is empty, there is no point in programming it */
  699. todo = min(end - off, burst);
  700. if (cdev->optimise_flash_write &&
  701. cros_ec_data_is_erased((uint32_t *)data, todo))
  702. continue;
  703. ret = cros_ec_flash_write_block(dev, data, off, todo);
  704. if (ret)
  705. return ret;
  706. }
  707. return 0;
  708. }
  709. /**
  710. * Read a single block from the flash
  711. *
  712. * Read a block of data from the EC flash. The size must not exceed the flash
  713. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  714. *
  715. * The offset starts at 0. You can obtain the region information from
  716. * cros_ec_flash_offset() to find out where to read for a particular region.
  717. *
  718. * @param dev CROS-EC device
  719. * @param data Pointer to data buffer to read into
  720. * @param offset Offset within flash to read from
  721. * @param size Number of bytes to read
  722. * @return 0 if ok, -1 on error
  723. */
  724. static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
  725. uint32_t offset, uint32_t size)
  726. {
  727. struct ec_params_flash_read p;
  728. p.offset = offset;
  729. p.size = size;
  730. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  731. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  732. }
  733. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  734. uint32_t size)
  735. {
  736. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  737. uint32_t end, off;
  738. int ret;
  739. end = offset + size;
  740. for (off = offset; off < end; off += burst, data += burst) {
  741. ret = cros_ec_flash_read_block(dev, data, off,
  742. min(end - off, burst));
  743. if (ret)
  744. return ret;
  745. }
  746. return 0;
  747. }
  748. int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
  749. int image_size)
  750. {
  751. uint32_t rw_offset, rw_size;
  752. int ret;
  753. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
  754. &rw_size))
  755. return -1;
  756. if (image_size > (int)rw_size)
  757. return -1;
  758. /* Invalidate the existing hash, just in case the AP reboots
  759. * unexpectedly during the update. If that happened, the EC RW firmware
  760. * would be invalid, but the EC would still have the original hash.
  761. */
  762. ret = cros_ec_invalidate_hash(dev);
  763. if (ret)
  764. return ret;
  765. /*
  766. * Erase the entire RW section, so that the EC doesn't see any garbage
  767. * past the new image if it's smaller than the current image.
  768. *
  769. * TODO: could optimize this to erase just the current image, since
  770. * presumably everything past that is 0xff's. But would still need to
  771. * round up to the nearest multiple of erase size.
  772. */
  773. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  774. if (ret)
  775. return ret;
  776. /* Write the image */
  777. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  778. if (ret)
  779. return ret;
  780. return 0;
  781. }
  782. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
  783. {
  784. struct ec_params_vbnvcontext p;
  785. int len;
  786. if (size != EC_VBNV_BLOCK_SIZE)
  787. return -EINVAL;
  788. p.op = EC_VBNV_CONTEXT_OP_READ;
  789. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  790. &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
  791. if (len < EC_VBNV_BLOCK_SIZE)
  792. return -EIO;
  793. return 0;
  794. }
  795. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
  796. {
  797. struct ec_params_vbnvcontext p;
  798. int len;
  799. if (size != EC_VBNV_BLOCK_SIZE)
  800. return -EINVAL;
  801. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  802. memcpy(p.block, block, sizeof(p.block));
  803. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  804. &p, sizeof(p), NULL, 0);
  805. if (len < 0)
  806. return -1;
  807. return 0;
  808. }
  809. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
  810. {
  811. struct ec_params_ldo_set params;
  812. params.index = index;
  813. params.state = state;
  814. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
  815. NULL, 0))
  816. return -1;
  817. return 0;
  818. }
  819. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
  820. {
  821. struct ec_params_ldo_get params;
  822. struct ec_response_ldo_get *resp;
  823. params.index = index;
  824. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
  825. (uint8_t **)&resp, sizeof(*resp)) !=
  826. sizeof(*resp))
  827. return -1;
  828. *state = resp->state;
  829. return 0;
  830. }
  831. int cros_ec_register(struct udevice *dev)
  832. {
  833. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  834. char id[MSG_BYTES];
  835. cdev->dev = dev;
  836. gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
  837. GPIOD_IS_IN);
  838. cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
  839. if (cros_ec_check_version(dev)) {
  840. debug("%s: Could not detect CROS-EC version\n", __func__);
  841. return -CROS_EC_ERR_CHECK_VERSION;
  842. }
  843. if (cros_ec_read_id(dev, id, sizeof(id))) {
  844. debug("%s: Could not read KBC ID\n", __func__);
  845. return -CROS_EC_ERR_READ_ID;
  846. }
  847. /* Remember this device for use by the cros_ec command */
  848. debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
  849. cdev->protocol_version, id);
  850. return 0;
  851. }
  852. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
  853. {
  854. ofnode flash_node, node;
  855. flash_node = dev_read_subnode(dev, "flash");
  856. if (!ofnode_valid(flash_node)) {
  857. debug("Failed to find flash node\n");
  858. return -1;
  859. }
  860. if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
  861. debug("Failed to decode flash node in chrome-ec\n");
  862. return -1;
  863. }
  864. config->flash_erase_value = ofnode_read_s32_default(flash_node,
  865. "erase-value", -1);
  866. ofnode_for_each_subnode(node, flash_node) {
  867. const char *name = ofnode_get_name(node);
  868. enum ec_flash_region region;
  869. if (0 == strcmp(name, "ro")) {
  870. region = EC_FLASH_REGION_RO;
  871. } else if (0 == strcmp(name, "rw")) {
  872. region = EC_FLASH_REGION_ACTIVE;
  873. } else if (0 == strcmp(name, "wp-ro")) {
  874. region = EC_FLASH_REGION_WP_RO;
  875. } else {
  876. debug("Unknown EC flash region name '%s'\n", name);
  877. return -1;
  878. }
  879. if (ofnode_read_fmap_entry(node, &config->region[region])) {
  880. debug("Failed to decode flash region in chrome-ec'\n");
  881. return -1;
  882. }
  883. }
  884. return 0;
  885. }
  886. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
  887. int nmsgs)
  888. {
  889. union {
  890. struct ec_params_i2c_passthru p;
  891. uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
  892. } params;
  893. union {
  894. struct ec_response_i2c_passthru r;
  895. uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
  896. } response;
  897. struct ec_params_i2c_passthru *p = &params.p;
  898. struct ec_response_i2c_passthru *r = &response.r;
  899. struct ec_params_i2c_passthru_msg *msg;
  900. uint8_t *pdata, *read_ptr = NULL;
  901. int read_len;
  902. int size;
  903. int rv;
  904. int i;
  905. p->port = port;
  906. p->num_msgs = nmsgs;
  907. size = sizeof(*p) + p->num_msgs * sizeof(*msg);
  908. /* Create a message to write the register address and optional data */
  909. pdata = (uint8_t *)p + size;
  910. read_len = 0;
  911. for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
  912. bool is_read = in->flags & I2C_M_RD;
  913. msg->addr_flags = in->addr;
  914. msg->len = in->len;
  915. if (is_read) {
  916. msg->addr_flags |= EC_I2C_FLAG_READ;
  917. read_len += in->len;
  918. read_ptr = in->buf;
  919. if (sizeof(*r) + read_len > sizeof(response)) {
  920. puts("Read length too big for buffer\n");
  921. return -1;
  922. }
  923. } else {
  924. if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
  925. puts("Params too large for buffer\n");
  926. return -1;
  927. }
  928. memcpy(pdata, in->buf, in->len);
  929. pdata += in->len;
  930. }
  931. }
  932. rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
  933. r, sizeof(*r) + read_len);
  934. if (rv < 0)
  935. return rv;
  936. /* Parse response */
  937. if (r->i2c_status & EC_I2C_STATUS_ERROR) {
  938. printf("Transfer failed with status=0x%x\n", r->i2c_status);
  939. return -1;
  940. }
  941. if (rv < sizeof(*r) + read_len) {
  942. puts("Truncated read response\n");
  943. return -1;
  944. }
  945. /* We only support a single read message for each transfer */
  946. if (read_len)
  947. memcpy(read_ptr, r->data, read_len);
  948. return 0;
  949. }
  950. UCLASS_DRIVER(cros_ec) = {
  951. .id = UCLASS_CROS_EC,
  952. .name = "cros_ec",
  953. .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
  954. .post_bind = dm_scan_fdt_dev,
  955. };