cros_ec.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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, struct ec_response_vboot_hash *hash)
  380. {
  381. struct ec_params_vboot_hash p;
  382. int rv;
  383. p.cmd = EC_VBOOT_HASH_GET;
  384. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  385. hash, sizeof(*hash)) < 0)
  386. return -1;
  387. /* If the EC is busy calculating the hash, fidget until it's done. */
  388. rv = cros_ec_wait_on_hash_done(dev, hash);
  389. if (rv)
  390. return rv;
  391. /* If the hash is valid, we're done. Otherwise, we have to kick it off
  392. * again and wait for it to complete. Note that we explicitly assume
  393. * that hashing zero bytes is always wrong, even though that would
  394. * produce a valid hash value. */
  395. if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
  396. return 0;
  397. debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
  398. __func__, hash->status, hash->size);
  399. p.cmd = EC_VBOOT_HASH_START;
  400. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  401. p.nonce_size = 0;
  402. p.offset = EC_VBOOT_HASH_OFFSET_RW;
  403. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  404. hash, sizeof(*hash)) < 0)
  405. return -1;
  406. rv = cros_ec_wait_on_hash_done(dev, hash);
  407. if (rv)
  408. return rv;
  409. debug("%s: hash done\n", __func__);
  410. return 0;
  411. }
  412. static int cros_ec_invalidate_hash(struct udevice *dev)
  413. {
  414. struct ec_params_vboot_hash p;
  415. struct ec_response_vboot_hash *hash;
  416. /* We don't have an explict command for the EC to discard its current
  417. * hash value, so we'll just tell it to calculate one that we know is
  418. * wrong (we claim that hashing zero bytes is always invalid).
  419. */
  420. p.cmd = EC_VBOOT_HASH_RECALC;
  421. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  422. p.nonce_size = 0;
  423. p.offset = 0;
  424. p.size = 0;
  425. debug("%s:\n", __func__);
  426. if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  427. (uint8_t **)&hash, sizeof(*hash)) < 0)
  428. return -1;
  429. /* No need to wait for it to finish */
  430. return 0;
  431. }
  432. int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
  433. {
  434. struct ec_params_reboot_ec p;
  435. p.cmd = cmd;
  436. p.flags = flags;
  437. if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
  438. < 0)
  439. return -1;
  440. if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
  441. /*
  442. * EC reboot will take place immediately so delay to allow it
  443. * to complete. Note that some reboot types (EC_REBOOT_COLD)
  444. * will reboot the AP as well, in which case we won't actually
  445. * get to this point.
  446. */
  447. /*
  448. * TODO(rspangler@chromium.org): Would be nice if we had a
  449. * better way to determine when the reboot is complete. Could
  450. * we poll a memory-mapped LPC value?
  451. */
  452. udelay(50000);
  453. }
  454. return 0;
  455. }
  456. int cros_ec_interrupt_pending(struct udevice *dev)
  457. {
  458. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  459. /* no interrupt support : always poll */
  460. if (!dm_gpio_is_valid(&cdev->ec_int))
  461. return -ENOENT;
  462. return dm_gpio_get_value(&cdev->ec_int);
  463. }
  464. int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
  465. {
  466. if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
  467. sizeof(*info)) != sizeof(*info))
  468. return -1;
  469. return 0;
  470. }
  471. int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
  472. {
  473. struct ec_response_host_event_mask *resp;
  474. /*
  475. * Use the B copy of the event flags, because the main copy is already
  476. * used by ACPI/SMI.
  477. */
  478. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
  479. (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
  480. return -1;
  481. if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
  482. return -1;
  483. *events_ptr = resp->mask;
  484. return 0;
  485. }
  486. int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
  487. {
  488. struct ec_params_host_event_mask params;
  489. params.mask = events;
  490. /*
  491. * Use the B copy of the event flags, so it affects the data returned
  492. * by cros_ec_get_host_events().
  493. */
  494. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
  495. &params, sizeof(params), NULL, 0) < 0)
  496. return -1;
  497. return 0;
  498. }
  499. int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
  500. uint32_t set_flags,
  501. struct ec_response_flash_protect *resp)
  502. {
  503. struct ec_params_flash_protect params;
  504. params.mask = set_mask;
  505. params.flags = set_flags;
  506. if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
  507. &params, sizeof(params),
  508. resp, sizeof(*resp)) != sizeof(*resp))
  509. return -1;
  510. return 0;
  511. }
  512. static int cros_ec_check_version(struct udevice *dev)
  513. {
  514. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  515. struct ec_params_hello req;
  516. struct ec_response_hello *resp;
  517. struct dm_cros_ec_ops *ops;
  518. int ret;
  519. ops = dm_cros_ec_get_ops(dev);
  520. if (ops->check_version) {
  521. ret = ops->check_version(dev);
  522. if (ret)
  523. return ret;
  524. }
  525. /*
  526. * TODO(sjg@chromium.org).
  527. * There is a strange oddity here with the EC. We could just ignore
  528. * the response, i.e. pass the last two parameters as NULL and 0.
  529. * In this case we won't read back very many bytes from the EC.
  530. * On the I2C bus the EC gets upset about this and will try to send
  531. * the bytes anyway. This means that we will have to wait for that
  532. * to complete before continuing with a new EC command.
  533. *
  534. * This problem is probably unique to the I2C bus.
  535. *
  536. * So for now, just read all the data anyway.
  537. */
  538. /* Try sending a version 3 packet */
  539. cdev->protocol_version = 3;
  540. req.in_data = 0;
  541. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  542. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  543. return 0;
  544. }
  545. /* Try sending a version 2 packet */
  546. cdev->protocol_version = 2;
  547. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  548. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  549. return 0;
  550. }
  551. /*
  552. * Fail if we're still here, since the EC doesn't understand any
  553. * protcol version we speak. Version 1 interface without command
  554. * version is no longer supported, and we don't know about any new
  555. * protocol versions.
  556. */
  557. cdev->protocol_version = 0;
  558. printf("%s: ERROR: old EC interface not supported\n", __func__);
  559. return -1;
  560. }
  561. int cros_ec_test(struct udevice *dev)
  562. {
  563. struct ec_params_hello req;
  564. struct ec_response_hello *resp;
  565. req.in_data = 0x12345678;
  566. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  567. (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
  568. printf("ec_command_inptr() returned error\n");
  569. return -1;
  570. }
  571. if (resp->out_data != req.in_data + 0x01020304) {
  572. printf("Received invalid handshake %x\n", resp->out_data);
  573. return -1;
  574. }
  575. return 0;
  576. }
  577. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  578. uint32_t *offset, uint32_t *size)
  579. {
  580. struct ec_params_flash_region_info p;
  581. struct ec_response_flash_region_info *r;
  582. int ret;
  583. p.region = region;
  584. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  585. EC_VER_FLASH_REGION_INFO,
  586. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  587. if (ret != sizeof(*r))
  588. return -1;
  589. if (offset)
  590. *offset = r->offset;
  591. if (size)
  592. *size = r->size;
  593. return 0;
  594. }
  595. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
  596. {
  597. struct ec_params_flash_erase p;
  598. p.offset = offset;
  599. p.size = size;
  600. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  601. NULL, 0);
  602. }
  603. /**
  604. * Write a single block to the flash
  605. *
  606. * Write a block of data to the EC flash. The size must not exceed the flash
  607. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  608. *
  609. * The offset starts at 0. You can obtain the region information from
  610. * cros_ec_flash_offset() to find out where to write for a particular region.
  611. *
  612. * Attempting to write to the region where the EC is currently running from
  613. * will result in an error.
  614. *
  615. * @param dev CROS-EC device
  616. * @param data Pointer to data buffer to write
  617. * @param offset Offset within flash to write to.
  618. * @param size Number of bytes to write
  619. * @return 0 if ok, -1 on error
  620. */
  621. static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
  622. uint32_t offset, uint32_t size)
  623. {
  624. struct ec_params_flash_write *p;
  625. int ret;
  626. p = malloc(sizeof(*p) + size);
  627. if (!p)
  628. return -ENOMEM;
  629. p->offset = offset;
  630. p->size = size;
  631. assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
  632. memcpy(p + 1, data, p->size);
  633. ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  634. p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
  635. free(p);
  636. return ret;
  637. }
  638. /**
  639. * Return optimal flash write burst size
  640. */
  641. static int cros_ec_flash_write_burst_size(struct udevice *dev)
  642. {
  643. return EC_FLASH_WRITE_VER0_SIZE;
  644. }
  645. /**
  646. * Check if a block of data is erased (all 0xff)
  647. *
  648. * This function is useful when dealing with flash, for checking whether a
  649. * data block is erased and thus does not need to be programmed.
  650. *
  651. * @param data Pointer to data to check (must be word-aligned)
  652. * @param size Number of bytes to check (must be word-aligned)
  653. * @return 0 if erased, non-zero if any word is not erased
  654. */
  655. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  656. {
  657. assert(!(size & 3));
  658. size /= sizeof(uint32_t);
  659. for (; size > 0; size -= 4, data++)
  660. if (*data != -1U)
  661. return 0;
  662. return 1;
  663. }
  664. /**
  665. * Read back flash parameters
  666. *
  667. * This function reads back parameters of the flash as reported by the EC
  668. *
  669. * @param dev Pointer to device
  670. * @param info Pointer to output flash info struct
  671. */
  672. int cros_ec_read_flashinfo(struct udevice *dev,
  673. struct ec_response_flash_info *info)
  674. {
  675. int ret;
  676. ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
  677. NULL, 0, info, sizeof(*info));
  678. if (ret < 0)
  679. return ret;
  680. return ret < sizeof(*info) ? -1 : 0;
  681. }
  682. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  683. uint32_t offset, uint32_t size)
  684. {
  685. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  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 (cdev->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 udevice *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 udevice *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 udevice *dev, const uint8_t *image,
  747. 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_nvdata(struct udevice *dev, uint8_t *block, int size)
  780. {
  781. struct ec_params_vbnvcontext p;
  782. int len;
  783. if (size != EC_VBNV_BLOCK_SIZE)
  784. return -EINVAL;
  785. p.op = EC_VBNV_CONTEXT_OP_READ;
  786. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  787. &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
  788. if (len < EC_VBNV_BLOCK_SIZE)
  789. return -EIO;
  790. return 0;
  791. }
  792. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
  793. {
  794. struct ec_params_vbnvcontext p;
  795. int len;
  796. if (size != EC_VBNV_BLOCK_SIZE)
  797. return -EINVAL;
  798. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  799. memcpy(p.block, block, sizeof(p.block));
  800. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  801. &p, sizeof(p), NULL, 0);
  802. if (len < 0)
  803. return -1;
  804. return 0;
  805. }
  806. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
  807. {
  808. struct ec_params_ldo_set params;
  809. params.index = index;
  810. params.state = state;
  811. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
  812. NULL, 0))
  813. return -1;
  814. return 0;
  815. }
  816. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
  817. {
  818. struct ec_params_ldo_get params;
  819. struct ec_response_ldo_get *resp;
  820. params.index = index;
  821. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
  822. (uint8_t **)&resp, sizeof(*resp)) !=
  823. sizeof(*resp))
  824. return -1;
  825. *state = resp->state;
  826. return 0;
  827. }
  828. int cros_ec_register(struct udevice *dev)
  829. {
  830. struct cros_ec_dev *cdev = dev_get_uclass_priv(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 = dev_read_bool(dev, "optimise-flash-write");
  836. if (cros_ec_check_version(dev)) {
  837. debug("%s: Could not detect CROS-EC version\n", __func__);
  838. return -CROS_EC_ERR_CHECK_VERSION;
  839. }
  840. if (cros_ec_read_id(dev, id, sizeof(id))) {
  841. debug("%s: Could not read KBC ID\n", __func__);
  842. return -CROS_EC_ERR_READ_ID;
  843. }
  844. /* Remember this device for use by the cros_ec command */
  845. debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
  846. cdev->protocol_version, id);
  847. return 0;
  848. }
  849. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
  850. {
  851. ofnode flash_node, node;
  852. flash_node = dev_read_subnode(dev, "flash");
  853. if (!ofnode_valid(flash_node)) {
  854. debug("Failed to find flash node\n");
  855. return -1;
  856. }
  857. if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
  858. debug("Failed to decode flash node in chrome-ec\n");
  859. return -1;
  860. }
  861. config->flash_erase_value = ofnode_read_s32_default(flash_node,
  862. "erase-value", -1);
  863. ofnode_for_each_subnode(node, flash_node) {
  864. const char *name = ofnode_get_name(node);
  865. enum ec_flash_region region;
  866. if (0 == strcmp(name, "ro")) {
  867. region = EC_FLASH_REGION_RO;
  868. } else if (0 == strcmp(name, "rw")) {
  869. region = EC_FLASH_REGION_RW;
  870. } else if (0 == strcmp(name, "wp-ro")) {
  871. region = EC_FLASH_REGION_WP_RO;
  872. } else {
  873. debug("Unknown EC flash region name '%s'\n", name);
  874. return -1;
  875. }
  876. if (ofnode_read_fmap_entry(node, &config->region[region])) {
  877. debug("Failed to decode flash region in chrome-ec'\n");
  878. return -1;
  879. }
  880. }
  881. return 0;
  882. }
  883. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
  884. int nmsgs)
  885. {
  886. union {
  887. struct ec_params_i2c_passthru p;
  888. uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
  889. } params;
  890. union {
  891. struct ec_response_i2c_passthru r;
  892. uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
  893. } response;
  894. struct ec_params_i2c_passthru *p = &params.p;
  895. struct ec_response_i2c_passthru *r = &response.r;
  896. struct ec_params_i2c_passthru_msg *msg;
  897. uint8_t *pdata, *read_ptr = NULL;
  898. int read_len;
  899. int size;
  900. int rv;
  901. int i;
  902. p->port = port;
  903. p->num_msgs = nmsgs;
  904. size = sizeof(*p) + p->num_msgs * sizeof(*msg);
  905. /* Create a message to write the register address and optional data */
  906. pdata = (uint8_t *)p + size;
  907. read_len = 0;
  908. for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
  909. bool is_read = in->flags & I2C_M_RD;
  910. msg->addr_flags = in->addr;
  911. msg->len = in->len;
  912. if (is_read) {
  913. msg->addr_flags |= EC_I2C_FLAG_READ;
  914. read_len += in->len;
  915. read_ptr = in->buf;
  916. if (sizeof(*r) + read_len > sizeof(response)) {
  917. puts("Read length too big for buffer\n");
  918. return -1;
  919. }
  920. } else {
  921. if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
  922. puts("Params too large for buffer\n");
  923. return -1;
  924. }
  925. memcpy(pdata, in->buf, in->len);
  926. pdata += in->len;
  927. }
  928. }
  929. rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
  930. r, sizeof(*r) + read_len);
  931. if (rv < 0)
  932. return rv;
  933. /* Parse response */
  934. if (r->i2c_status & EC_I2C_STATUS_ERROR) {
  935. printf("Transfer failed with status=0x%x\n", r->i2c_status);
  936. return -1;
  937. }
  938. if (rv < sizeof(*r) + read_len) {
  939. puts("Truncated read response\n");
  940. return -1;
  941. }
  942. /* We only support a single read message for each transfer */
  943. if (read_len)
  944. memcpy(read_ptr, r->data, read_len);
  945. return 0;
  946. }
  947. UCLASS_DRIVER(cros_ec) = {
  948. .id = UCLASS_CROS_EC,
  949. .name = "cros_ec",
  950. .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
  951. .post_bind = dm_scan_fdt_dev,
  952. };