cros_ec.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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. #define LOG_CATEGORY UCLASS_CROS_EC
  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 <log.h>
  22. #include <malloc.h>
  23. #include <spi.h>
  24. #include <linux/delay.h>
  25. #include <linux/errno.h>
  26. #include <asm/io.h>
  27. #include <asm-generic/gpio.h>
  28. #include <dm/device-internal.h>
  29. #include <dm/of_extra.h>
  30. #include <dm/uclass-internal.h>
  31. #ifdef DEBUG_TRACE
  32. #define debug_trace(fmt, b...) debug(fmt, #b)
  33. #else
  34. #define debug_trace(fmt, b...)
  35. #endif
  36. enum {
  37. /* Timeout waiting for a flash erase command to complete */
  38. CROS_EC_CMD_TIMEOUT_MS = 5000,
  39. /* Timeout waiting for a synchronous hash to be recomputed */
  40. CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
  41. /* Wait 10 ms between attempts to check if EC's hash is ready */
  42. CROS_EC_HASH_CHECK_DELAY_MS = 10,
  43. };
  44. #define INVALID_HCMD 0xFF
  45. /*
  46. * Map UHEPI masks to non UHEPI commands in order to support old EC FW
  47. * which does not support UHEPI command.
  48. */
  49. static const struct {
  50. u8 set_cmd;
  51. u8 clear_cmd;
  52. u8 get_cmd;
  53. } event_map[] = {
  54. [EC_HOST_EVENT_MAIN] = {
  55. INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
  56. INVALID_HCMD,
  57. },
  58. [EC_HOST_EVENT_B] = {
  59. INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
  60. EC_CMD_HOST_EVENT_GET_B,
  61. },
  62. [EC_HOST_EVENT_SCI_MASK] = {
  63. EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
  64. EC_CMD_HOST_EVENT_GET_SCI_MASK,
  65. },
  66. [EC_HOST_EVENT_SMI_MASK] = {
  67. EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
  68. EC_CMD_HOST_EVENT_GET_SMI_MASK,
  69. },
  70. [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
  71. INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
  72. },
  73. [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
  74. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  75. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  76. },
  77. [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
  78. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  79. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  80. },
  81. [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
  82. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  83. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  84. },
  85. [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
  86. EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
  87. EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  88. },
  89. };
  90. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
  91. {
  92. #ifdef DEBUG
  93. int i;
  94. printf("%s: ", name);
  95. if (cmd != -1)
  96. printf("cmd=%#x: ", cmd);
  97. for (i = 0; i < len; i++)
  98. printf("%02x ", data[i]);
  99. printf("\n");
  100. #endif
  101. }
  102. /*
  103. * Calculate a simple 8-bit checksum of a data block
  104. *
  105. * @param data Data block to checksum
  106. * @param size Size of data block in bytes
  107. * Return: checksum value (0 to 255)
  108. */
  109. int cros_ec_calc_checksum(const uint8_t *data, int size)
  110. {
  111. int csum, i;
  112. for (i = csum = 0; i < size; i++)
  113. csum += data[i];
  114. return csum & 0xff;
  115. }
  116. /**
  117. * Create a request packet for protocol version 3.
  118. *
  119. * The packet is stored in the device's internal output buffer.
  120. *
  121. * @param dev CROS-EC device
  122. * @param cmd Command to send (EC_CMD_...)
  123. * @param cmd_version Version of command to send (EC_VER_...)
  124. * @param dout Output data (may be NULL If dout_len=0)
  125. * @param dout_len Size of output data in bytes
  126. * Return: packet size in bytes, or <0 if error.
  127. */
  128. static int create_proto3_request(struct cros_ec_dev *cdev,
  129. int cmd, int cmd_version,
  130. const void *dout, int dout_len)
  131. {
  132. struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
  133. int out_bytes = dout_len + sizeof(*rq);
  134. /* Fail if output size is too big */
  135. if (out_bytes > (int)sizeof(cdev->dout)) {
  136. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  137. return -EC_RES_REQUEST_TRUNCATED;
  138. }
  139. /* Fill in request packet */
  140. rq->struct_version = EC_HOST_REQUEST_VERSION;
  141. rq->checksum = 0;
  142. rq->command = cmd;
  143. rq->command_version = cmd_version;
  144. rq->reserved = 0;
  145. rq->data_len = dout_len;
  146. /* Copy data after header */
  147. memcpy(rq + 1, dout, dout_len);
  148. /* Write checksum field so the entire packet sums to 0 */
  149. rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
  150. cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
  151. /* Return size of request packet */
  152. return out_bytes;
  153. }
  154. /**
  155. * Prepare the device to receive a protocol version 3 response.
  156. *
  157. * @param dev CROS-EC device
  158. * @param din_len Maximum size of response in bytes
  159. * Return: maximum expected number of bytes in response, or <0 if error.
  160. */
  161. static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
  162. {
  163. int in_bytes = din_len + sizeof(struct ec_host_response);
  164. /* Fail if input size is too big */
  165. if (in_bytes > (int)sizeof(cdev->din)) {
  166. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  167. return -EC_RES_RESPONSE_TOO_BIG;
  168. }
  169. /* Return expected size of response packet */
  170. return in_bytes;
  171. }
  172. /**
  173. * Handle a protocol version 3 response packet.
  174. *
  175. * The packet must already be stored in the device's internal input buffer.
  176. *
  177. * @param dev CROS-EC device
  178. * @param dinp Returns pointer to response data
  179. * @param din_len Maximum size of response in bytes
  180. * Return: number of bytes of response data, or <0 if error. Note that error
  181. * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
  182. * overlap!)
  183. */
  184. static int handle_proto3_response(struct cros_ec_dev *dev,
  185. uint8_t **dinp, int din_len)
  186. {
  187. struct ec_host_response *rs = (struct ec_host_response *)dev->din;
  188. int in_bytes;
  189. int csum;
  190. cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
  191. /* Check input data */
  192. if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
  193. debug("%s: EC response version mismatch\n", __func__);
  194. return -EC_RES_INVALID_RESPONSE;
  195. }
  196. if (rs->reserved) {
  197. debug("%s: EC response reserved != 0\n", __func__);
  198. return -EC_RES_INVALID_RESPONSE;
  199. }
  200. if (rs->data_len > din_len) {
  201. debug("%s: EC returned too much data\n", __func__);
  202. return -EC_RES_RESPONSE_TOO_BIG;
  203. }
  204. cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
  205. /* Update in_bytes to actual data size */
  206. in_bytes = sizeof(*rs) + rs->data_len;
  207. /* Verify checksum */
  208. csum = cros_ec_calc_checksum(dev->din, in_bytes);
  209. if (csum) {
  210. debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
  211. csum);
  212. return -EC_RES_INVALID_CHECKSUM;
  213. }
  214. /* Return error result, if any */
  215. if (rs->result)
  216. return -(int)rs->result;
  217. /* If we're still here, set response data pointer and return length */
  218. *dinp = (uint8_t *)(rs + 1);
  219. return rs->data_len;
  220. }
  221. static int send_command_proto3(struct cros_ec_dev *cdev,
  222. int cmd, int cmd_version,
  223. const void *dout, int dout_len,
  224. uint8_t **dinp, int din_len)
  225. {
  226. struct dm_cros_ec_ops *ops;
  227. int out_bytes, in_bytes;
  228. int rv;
  229. /* Create request packet */
  230. out_bytes = create_proto3_request(cdev, cmd, cmd_version,
  231. dout, dout_len);
  232. if (out_bytes < 0)
  233. return out_bytes;
  234. /* Prepare response buffer */
  235. in_bytes = prepare_proto3_response_buffer(cdev, din_len);
  236. if (in_bytes < 0)
  237. return in_bytes;
  238. ops = dm_cros_ec_get_ops(cdev->dev);
  239. rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
  240. -ENOSYS;
  241. if (rv < 0)
  242. return rv;
  243. /* Process the response */
  244. return handle_proto3_response(cdev, dinp, din_len);
  245. }
  246. static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
  247. const void *dout, int dout_len,
  248. uint8_t **dinp, int din_len)
  249. {
  250. struct dm_cros_ec_ops *ops;
  251. int ret = -1;
  252. /* Handle protocol version 3 support */
  253. if (dev->protocol_version == 3) {
  254. return send_command_proto3(dev, cmd, cmd_version,
  255. dout, dout_len, dinp, din_len);
  256. }
  257. ops = dm_cros_ec_get_ops(dev->dev);
  258. ret = ops->command(dev->dev, cmd, cmd_version,
  259. (const uint8_t *)dout, dout_len, dinp, din_len);
  260. return ret;
  261. }
  262. /**
  263. * Send a command to the CROS-EC device and return the reply.
  264. *
  265. * The device's internal input/output buffers are used.
  266. *
  267. * @param dev CROS-EC device
  268. * @param cmd Command to send (EC_CMD_...)
  269. * @param cmd_version Version of command to send (EC_VER_...)
  270. * @param dout Output data (may be NULL If dout_len=0)
  271. * @param dout_len Size of output data in bytes
  272. * @param dinp Response data (may be NULL If din_len=0).
  273. * If not NULL, it will be updated to point to the data
  274. * and will always be double word aligned (64-bits)
  275. * @param din_len Maximum size of response in bytes
  276. * Return: number of bytes in response, or -ve on error
  277. */
  278. static int ec_command_inptr(struct udevice *dev, uint cmd,
  279. int cmd_version, const void *dout, int dout_len,
  280. uint8_t **dinp, int din_len)
  281. {
  282. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  283. uint8_t *din = NULL;
  284. int len;
  285. len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
  286. din_len);
  287. /* If the command doesn't complete, wait a while */
  288. if (len == -EC_RES_IN_PROGRESS) {
  289. struct ec_response_get_comms_status *resp = NULL;
  290. ulong start;
  291. /* Wait for command to complete */
  292. start = get_timer(0);
  293. do {
  294. int ret;
  295. mdelay(50); /* Insert some reasonable delay */
  296. ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
  297. NULL, 0,
  298. (uint8_t **)&resp, sizeof(*resp));
  299. if (ret < 0)
  300. return ret;
  301. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  302. debug("%s: Command %#02x timeout\n",
  303. __func__, cmd);
  304. return -EC_RES_TIMEOUT;
  305. }
  306. } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
  307. /* OK it completed, so read the status response */
  308. /* not sure why it was 0 for the last argument */
  309. len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
  310. &din, din_len);
  311. }
  312. debug("%s: len=%d, din=%p\n", __func__, len, din);
  313. if (dinp) {
  314. /* If we have any data to return, it must be 64bit-aligned */
  315. assert(len <= 0 || !((uintptr_t)din & 7));
  316. *dinp = din;
  317. }
  318. return len;
  319. }
  320. /**
  321. * Send a command to the CROS-EC device and return the reply.
  322. *
  323. * The device's internal input/output buffers are used.
  324. *
  325. * @param dev CROS-EC device
  326. * @param cmd Command to send (EC_CMD_...)
  327. * @param cmd_version Version of command to send (EC_VER_...)
  328. * @param dout Output data (may be NULL If dout_len=0)
  329. * @param dout_len Size of output data in bytes
  330. * @param din Response data (may be NULL If din_len=0).
  331. * It not NULL, it is a place for ec_command() to copy the
  332. * data to.
  333. * @param din_len Maximum size of response in bytes
  334. * Return: number of bytes in response, or -ve on error
  335. */
  336. static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
  337. const void *dout, int dout_len,
  338. void *din, int din_len)
  339. {
  340. uint8_t *in_buffer;
  341. int len;
  342. assert((din_len == 0) || din);
  343. len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
  344. &in_buffer, din_len);
  345. if (len > 0) {
  346. /*
  347. * If we were asked to put it somewhere, do so, otherwise just
  348. * disregard the result.
  349. */
  350. if (din && in_buffer) {
  351. assert(len <= din_len);
  352. if (len > din_len)
  353. return -ENOSPC;
  354. memmove(din, in_buffer, len);
  355. }
  356. }
  357. return len;
  358. }
  359. int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
  360. {
  361. if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
  362. sizeof(scan->data)) != sizeof(scan->data))
  363. return -1;
  364. return 0;
  365. }
  366. int cros_ec_get_next_event(struct udevice *dev,
  367. struct ec_response_get_next_event *event)
  368. {
  369. int ret;
  370. ret = ec_command(dev, EC_CMD_GET_NEXT_EVENT, 0, NULL, 0,
  371. event, sizeof(*event));
  372. if (ret < 0)
  373. return ret;
  374. else if (ret != sizeof(*event))
  375. return -EC_RES_INVALID_RESPONSE;
  376. return 0;
  377. }
  378. int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
  379. {
  380. struct ec_response_get_version *r;
  381. int ret;
  382. ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  383. (uint8_t **)&r, sizeof(*r));
  384. if (ret != sizeof(*r)) {
  385. log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
  386. return -1;
  387. }
  388. if (maxlen > (int)sizeof(r->version_string_ro))
  389. maxlen = sizeof(r->version_string_ro);
  390. switch (r->current_image) {
  391. case EC_IMAGE_RO:
  392. memcpy(id, r->version_string_ro, maxlen);
  393. break;
  394. case EC_IMAGE_RW:
  395. memcpy(id, r->version_string_rw, maxlen);
  396. break;
  397. default:
  398. log_err("Invalid EC image %d\n", r->current_image);
  399. return -1;
  400. }
  401. id[maxlen - 1] = '\0';
  402. return 0;
  403. }
  404. int cros_ec_read_version(struct udevice *dev,
  405. struct ec_response_get_version **versionp)
  406. {
  407. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  408. (uint8_t **)versionp, sizeof(**versionp))
  409. != sizeof(**versionp))
  410. return -1;
  411. return 0;
  412. }
  413. int cros_ec_read_build_info(struct udevice *dev, char **strp)
  414. {
  415. if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
  416. (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
  417. return -1;
  418. return 0;
  419. }
  420. int cros_ec_read_current_image(struct udevice *dev,
  421. enum ec_current_image *image)
  422. {
  423. struct ec_response_get_version *r;
  424. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  425. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  426. return -1;
  427. *image = r->current_image;
  428. return 0;
  429. }
  430. static int cros_ec_wait_on_hash_done(struct udevice *dev,
  431. struct ec_params_vboot_hash *p,
  432. struct ec_response_vboot_hash *hash)
  433. {
  434. ulong start;
  435. start = get_timer(0);
  436. while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
  437. mdelay(CROS_EC_HASH_CHECK_DELAY_MS);
  438. p->cmd = EC_VBOOT_HASH_GET;
  439. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, p, sizeof(*p), hash,
  440. sizeof(*hash)) < 0)
  441. return -1;
  442. if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
  443. debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
  444. return -EC_RES_TIMEOUT;
  445. }
  446. }
  447. return 0;
  448. }
  449. int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
  450. struct ec_response_vboot_hash *hash)
  451. {
  452. struct ec_params_vboot_hash p;
  453. int rv;
  454. p.cmd = EC_VBOOT_HASH_GET;
  455. p.offset = hash_offset;
  456. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  457. hash, sizeof(*hash)) < 0)
  458. return -1;
  459. /* If the EC is busy calculating the hash, fidget until it's done. */
  460. rv = cros_ec_wait_on_hash_done(dev, &p, hash);
  461. if (rv)
  462. return rv;
  463. /* If the hash is valid, we're done. Otherwise, we have to kick it off
  464. * again and wait for it to complete. Note that we explicitly assume
  465. * that hashing zero bytes is always wrong, even though that would
  466. * produce a valid hash value. */
  467. if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
  468. return 0;
  469. debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
  470. __func__, hash->status, hash->size);
  471. p.cmd = EC_VBOOT_HASH_START;
  472. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  473. p.nonce_size = 0;
  474. p.offset = hash_offset;
  475. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  476. hash, sizeof(*hash)) < 0)
  477. return -1;
  478. rv = cros_ec_wait_on_hash_done(dev, &p, hash);
  479. if (rv)
  480. return rv;
  481. if (hash->status != EC_VBOOT_HASH_STATUS_DONE) {
  482. log_err("Hash did not complete, status=%d\n", hash->status);
  483. return -EIO;
  484. }
  485. debug("%s: hash done\n", __func__);
  486. return 0;
  487. }
  488. static int cros_ec_invalidate_hash(struct udevice *dev)
  489. {
  490. struct ec_params_vboot_hash p;
  491. struct ec_response_vboot_hash *hash;
  492. /* We don't have an explict command for the EC to discard its current
  493. * hash value, so we'll just tell it to calculate one that we know is
  494. * wrong (we claim that hashing zero bytes is always invalid).
  495. */
  496. p.cmd = EC_VBOOT_HASH_RECALC;
  497. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  498. p.nonce_size = 0;
  499. p.offset = 0;
  500. p.size = 0;
  501. debug("%s:\n", __func__);
  502. if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  503. (uint8_t **)&hash, sizeof(*hash)) < 0)
  504. return -1;
  505. /* No need to wait for it to finish */
  506. return 0;
  507. }
  508. int cros_ec_hello(struct udevice *dev, uint *handshakep)
  509. {
  510. struct ec_params_hello req;
  511. struct ec_response_hello *resp;
  512. req.in_data = 0x12345678;
  513. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  514. (uint8_t **)&resp, sizeof(*resp)) < 0)
  515. return -EIO;
  516. if (resp->out_data != req.in_data + 0x01020304) {
  517. printf("Received invalid handshake %x\n", resp->out_data);
  518. if (handshakep)
  519. *handshakep = req.in_data;
  520. return -ENOTSYNC;
  521. }
  522. return 0;
  523. }
  524. int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
  525. {
  526. struct ec_params_reboot_ec p;
  527. p.cmd = cmd;
  528. p.flags = flags;
  529. if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
  530. < 0)
  531. return -1;
  532. if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
  533. ulong start;
  534. /*
  535. * EC reboot will take place immediately so delay to allow it
  536. * to complete. Note that some reboot types (EC_REBOOT_COLD)
  537. * will reboot the AP as well, in which case we won't actually
  538. * get to this point.
  539. */
  540. mdelay(50);
  541. start = get_timer(0);
  542. while (cros_ec_hello(dev, NULL)) {
  543. if (get_timer(start) > 3000) {
  544. log_err("EC did not return from reboot\n");
  545. return -ETIMEDOUT;
  546. }
  547. mdelay(5);
  548. }
  549. }
  550. return 0;
  551. }
  552. int cros_ec_interrupt_pending(struct udevice *dev)
  553. {
  554. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  555. /* no interrupt support : always poll */
  556. if (!dm_gpio_is_valid(&cdev->ec_int))
  557. return -ENOENT;
  558. return dm_gpio_get_value(&cdev->ec_int);
  559. }
  560. int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
  561. {
  562. if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
  563. sizeof(*info)) != sizeof(*info))
  564. return -1;
  565. return 0;
  566. }
  567. int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
  568. {
  569. struct ec_response_host_event_mask rsp;
  570. int ret;
  571. ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
  572. if (ret < 0)
  573. return ret;
  574. else if (ret != sizeof(rsp))
  575. return -EINVAL;
  576. *mask = rsp.mask;
  577. return 0;
  578. }
  579. int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
  580. {
  581. struct ec_params_host_event_mask req;
  582. int ret;
  583. req.mask = mask;
  584. ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
  585. if (ret < 0)
  586. return ret;
  587. return 0;
  588. }
  589. int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
  590. {
  591. struct ec_response_host_event_mask *resp;
  592. /*
  593. * Use the B copy of the event flags, because the main copy is already
  594. * used by ACPI/SMI.
  595. */
  596. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
  597. (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
  598. return -1;
  599. if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
  600. return -1;
  601. *events_ptr = resp->mask;
  602. return 0;
  603. }
  604. int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
  605. {
  606. struct ec_params_host_event_mask params;
  607. params.mask = events;
  608. /*
  609. * Use the B copy of the event flags, so it affects the data returned
  610. * by cros_ec_get_host_events().
  611. */
  612. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
  613. &params, sizeof(params), NULL, 0) < 0)
  614. return -1;
  615. return 0;
  616. }
  617. int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
  618. uint32_t set_flags,
  619. struct ec_response_flash_protect *resp)
  620. {
  621. struct ec_params_flash_protect params;
  622. params.mask = set_mask;
  623. params.flags = set_flags;
  624. if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
  625. &params, sizeof(params),
  626. resp, sizeof(*resp)) != sizeof(*resp))
  627. return -1;
  628. return 0;
  629. }
  630. static int cros_ec_check_version(struct udevice *dev)
  631. {
  632. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  633. struct ec_params_hello req;
  634. struct dm_cros_ec_ops *ops;
  635. int ret;
  636. ops = dm_cros_ec_get_ops(dev);
  637. if (ops->check_version) {
  638. ret = ops->check_version(dev);
  639. if (ret)
  640. return ret;
  641. }
  642. /*
  643. * TODO(sjg@chromium.org).
  644. * There is a strange oddity here with the EC. We could just ignore
  645. * the response, i.e. pass the last two parameters as NULL and 0.
  646. * In this case we won't read back very many bytes from the EC.
  647. * On the I2C bus the EC gets upset about this and will try to send
  648. * the bytes anyway. This means that we will have to wait for that
  649. * to complete before continuing with a new EC command.
  650. *
  651. * This problem is probably unique to the I2C bus.
  652. *
  653. * So for now, just read all the data anyway.
  654. */
  655. /* Try sending a version 3 packet */
  656. cdev->protocol_version = 3;
  657. req.in_data = 0;
  658. ret = cros_ec_hello(dev, NULL);
  659. if (!ret || ret == -ENOTSYNC)
  660. return 0;
  661. /* Try sending a version 2 packet */
  662. cdev->protocol_version = 2;
  663. ret = cros_ec_hello(dev, NULL);
  664. if (!ret || ret == -ENOTSYNC)
  665. return 0;
  666. /*
  667. * Fail if we're still here, since the EC doesn't understand any
  668. * protcol version we speak. Version 1 interface without command
  669. * version is no longer supported, and we don't know about any new
  670. * protocol versions.
  671. */
  672. cdev->protocol_version = 0;
  673. printf("%s: ERROR: old EC interface not supported\n", __func__);
  674. return -1;
  675. }
  676. int cros_ec_test(struct udevice *dev)
  677. {
  678. uint out_data;
  679. int ret;
  680. ret = cros_ec_hello(dev, &out_data);
  681. if (ret == -ENOTSYNC) {
  682. printf("Received invalid handshake %x\n", out_data);
  683. return ret;
  684. } else if (ret) {
  685. printf("ec_command_inptr() returned error\n");
  686. return ret;
  687. }
  688. return 0;
  689. }
  690. int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
  691. uint32_t *offset, uint32_t *size)
  692. {
  693. struct ec_params_flash_region_info p;
  694. struct ec_response_flash_region_info *r;
  695. int ret;
  696. p.region = region;
  697. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  698. EC_VER_FLASH_REGION_INFO,
  699. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  700. if (ret != sizeof(*r))
  701. return -1;
  702. if (offset)
  703. *offset = r->offset;
  704. if (size)
  705. *size = r->size;
  706. return 0;
  707. }
  708. int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
  709. {
  710. struct ec_params_flash_erase p;
  711. p.offset = offset;
  712. p.size = size;
  713. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  714. NULL, 0);
  715. }
  716. /**
  717. * Write a single block to the flash
  718. *
  719. * Write a block of data to the EC flash. The size must not exceed the flash
  720. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  721. *
  722. * The offset starts at 0. You can obtain the region information from
  723. * cros_ec_flash_offset() to find out where to write for a particular region.
  724. *
  725. * Attempting to write to the region where the EC is currently running from
  726. * will result in an error.
  727. *
  728. * @param dev CROS-EC device
  729. * @param data Pointer to data buffer to write
  730. * @param offset Offset within flash to write to.
  731. * @param size Number of bytes to write
  732. * Return: 0 if ok, -1 on error
  733. */
  734. static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
  735. uint32_t offset, uint32_t size)
  736. {
  737. struct ec_params_flash_write *p;
  738. int ret;
  739. p = malloc(sizeof(*p) + size);
  740. if (!p)
  741. return -ENOMEM;
  742. p->offset = offset;
  743. p->size = size;
  744. assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
  745. memcpy(p + 1, data, p->size);
  746. ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  747. p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
  748. free(p);
  749. return ret;
  750. }
  751. /**
  752. * Return optimal flash write burst size
  753. */
  754. static int cros_ec_flash_write_burst_size(struct udevice *dev)
  755. {
  756. return EC_FLASH_WRITE_VER0_SIZE;
  757. }
  758. /**
  759. * Check if a block of data is erased (all 0xff)
  760. *
  761. * This function is useful when dealing with flash, for checking whether a
  762. * data block is erased and thus does not need to be programmed.
  763. *
  764. * @param data Pointer to data to check (must be word-aligned)
  765. * @param size Number of bytes to check (must be word-aligned)
  766. * Return: 0 if erased, non-zero if any word is not erased
  767. */
  768. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  769. {
  770. assert(!(size & 3));
  771. size /= sizeof(uint32_t);
  772. for (; size > 0; size -= 4, data++)
  773. if (*data != -1U)
  774. return 0;
  775. return 1;
  776. }
  777. /**
  778. * Read back flash parameters
  779. *
  780. * This function reads back parameters of the flash as reported by the EC
  781. *
  782. * @param dev Pointer to device
  783. * @param info Pointer to output flash info struct
  784. */
  785. int cros_ec_read_flashinfo(struct udevice *dev,
  786. struct ec_response_flash_info *info)
  787. {
  788. int ret;
  789. ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
  790. NULL, 0, info, sizeof(*info));
  791. if (ret < 0)
  792. return ret;
  793. return ret < sizeof(*info) ? -1 : 0;
  794. }
  795. int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
  796. uint32_t offset, uint32_t size)
  797. {
  798. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  799. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  800. uint32_t end, off;
  801. int ret;
  802. if (!burst)
  803. return -EINVAL;
  804. /*
  805. * TODO: round up to the nearest multiple of write size. Can get away
  806. * without that on link right now because its write size is 4 bytes.
  807. */
  808. end = offset + size;
  809. for (off = offset; off < end; off += burst, data += burst) {
  810. uint32_t todo;
  811. /* If the data is empty, there is no point in programming it */
  812. todo = min(end - off, burst);
  813. if (cdev->optimise_flash_write &&
  814. cros_ec_data_is_erased((uint32_t *)data, todo))
  815. continue;
  816. ret = cros_ec_flash_write_block(dev, data, off, todo);
  817. if (ret)
  818. return ret;
  819. }
  820. return 0;
  821. }
  822. /**
  823. * Run verification on a slot
  824. *
  825. * @param me CrosEc instance
  826. * @param region Region to run verification on
  827. * Return: 0 if success or not applicable. Non-zero if verification failed.
  828. */
  829. int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
  830. {
  831. struct ec_params_efs_verify p;
  832. int rv;
  833. log_info("EFS: EC is verifying updated image...\n");
  834. p.region = region;
  835. rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
  836. if (rv >= 0) {
  837. log_info("EFS: Verification success\n");
  838. return 0;
  839. }
  840. if (rv == -EC_RES_INVALID_COMMAND) {
  841. log_info("EFS: EC doesn't support EFS_VERIFY command\n");
  842. return 0;
  843. }
  844. log_info("EFS: Verification failed\n");
  845. return rv;
  846. }
  847. /**
  848. * Read a single block from the flash
  849. *
  850. * Read a block of data from the EC flash. The size must not exceed the flash
  851. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  852. *
  853. * The offset starts at 0. You can obtain the region information from
  854. * cros_ec_flash_offset() to find out where to read for a particular region.
  855. *
  856. * @param dev CROS-EC device
  857. * @param data Pointer to data buffer to read into
  858. * @param offset Offset within flash to read from
  859. * @param size Number of bytes to read
  860. * Return: 0 if ok, -1 on error
  861. */
  862. static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
  863. uint32_t offset, uint32_t size)
  864. {
  865. struct ec_params_flash_read p;
  866. p.offset = offset;
  867. p.size = size;
  868. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  869. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  870. }
  871. int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
  872. uint32_t size)
  873. {
  874. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  875. uint32_t end, off;
  876. int ret;
  877. end = offset + size;
  878. for (off = offset; off < end; off += burst, data += burst) {
  879. ret = cros_ec_flash_read_block(dev, data, off,
  880. min(end - off, burst));
  881. if (ret)
  882. return ret;
  883. }
  884. return 0;
  885. }
  886. int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
  887. int image_size)
  888. {
  889. uint32_t rw_offset, rw_size;
  890. int ret;
  891. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
  892. &rw_size))
  893. return -1;
  894. if (image_size > (int)rw_size)
  895. return -1;
  896. /* Invalidate the existing hash, just in case the AP reboots
  897. * unexpectedly during the update. If that happened, the EC RW firmware
  898. * would be invalid, but the EC would still have the original hash.
  899. */
  900. ret = cros_ec_invalidate_hash(dev);
  901. if (ret)
  902. return ret;
  903. /*
  904. * Erase the entire RW section, so that the EC doesn't see any garbage
  905. * past the new image if it's smaller than the current image.
  906. *
  907. * TODO: could optimize this to erase just the current image, since
  908. * presumably everything past that is 0xff's. But would still need to
  909. * round up to the nearest multiple of erase size.
  910. */
  911. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  912. if (ret)
  913. return ret;
  914. /* Write the image */
  915. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  916. if (ret)
  917. return ret;
  918. return 0;
  919. }
  920. int cros_ec_get_sku_id(struct udevice *dev)
  921. {
  922. struct ec_sku_id_info *r;
  923. int ret;
  924. ret = ec_command_inptr(dev, EC_CMD_GET_SKU_ID, 0, NULL, 0,
  925. (uint8_t **)&r, sizeof(*r));
  926. if (ret != sizeof(*r))
  927. return -ret;
  928. return r->sku_id;
  929. }
  930. int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
  931. {
  932. struct ec_params_vbnvcontext p;
  933. int len;
  934. if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
  935. return -EINVAL;
  936. p.op = EC_VBNV_CONTEXT_OP_READ;
  937. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  938. &p, sizeof(uint32_t) + size, block, size);
  939. if (len != size) {
  940. log_err("Expected %d bytes, got %d\n", size, len);
  941. return -EIO;
  942. }
  943. return 0;
  944. }
  945. int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
  946. {
  947. struct ec_params_vbnvcontext p;
  948. int len;
  949. if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
  950. return -EINVAL;
  951. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  952. memcpy(p.block, block, size);
  953. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  954. &p, sizeof(uint32_t) + size, NULL, 0);
  955. if (len < 0)
  956. return -1;
  957. return 0;
  958. }
  959. int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
  960. {
  961. struct ec_params_battery_cutoff p;
  962. int len;
  963. p.flags = flags;
  964. len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
  965. NULL, 0);
  966. if (len < 0)
  967. return -1;
  968. return 0;
  969. }
  970. int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty)
  971. {
  972. struct ec_params_pwm_set_duty p;
  973. int ret;
  974. p.duty = duty;
  975. p.pwm_type = EC_PWM_TYPE_GENERIC;
  976. p.index = index;
  977. ret = ec_command(dev, EC_CMD_PWM_SET_DUTY, 0, &p, sizeof(p),
  978. NULL, 0);
  979. if (ret < 0)
  980. return ret;
  981. return 0;
  982. }
  983. int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
  984. {
  985. struct ec_params_ldo_set params;
  986. params.index = index;
  987. params.state = state;
  988. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
  989. NULL, 0))
  990. return -1;
  991. return 0;
  992. }
  993. int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
  994. {
  995. struct ec_params_ldo_get params;
  996. struct ec_response_ldo_get *resp;
  997. params.index = index;
  998. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
  999. (uint8_t **)&resp, sizeof(*resp)) !=
  1000. sizeof(*resp))
  1001. return -1;
  1002. *state = resp->state;
  1003. return 0;
  1004. }
  1005. int cros_ec_register(struct udevice *dev)
  1006. {
  1007. struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
  1008. char id[MSG_BYTES];
  1009. cdev->dev = dev;
  1010. gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
  1011. GPIOD_IS_IN);
  1012. cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
  1013. if (cros_ec_check_version(dev)) {
  1014. debug("%s: Could not detect CROS-EC version\n", __func__);
  1015. return -CROS_EC_ERR_CHECK_VERSION;
  1016. }
  1017. if (cros_ec_read_id(dev, id, sizeof(id))) {
  1018. debug("%s: Could not read KBC ID\n", __func__);
  1019. return -CROS_EC_ERR_READ_ID;
  1020. }
  1021. /* Remember this device for use by the cros_ec command */
  1022. debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
  1023. cdev->protocol_version, id);
  1024. return 0;
  1025. }
  1026. int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
  1027. {
  1028. ofnode flash_node, node;
  1029. flash_node = dev_read_subnode(dev, "flash");
  1030. if (!ofnode_valid(flash_node)) {
  1031. debug("Failed to find flash node\n");
  1032. return -1;
  1033. }
  1034. if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
  1035. debug("Failed to decode flash node in chrome-ec\n");
  1036. return -1;
  1037. }
  1038. config->flash_erase_value = ofnode_read_s32_default(flash_node,
  1039. "erase-value", -1);
  1040. ofnode_for_each_subnode(node, flash_node) {
  1041. const char *name = ofnode_get_name(node);
  1042. enum ec_flash_region region;
  1043. if (0 == strcmp(name, "ro")) {
  1044. region = EC_FLASH_REGION_RO;
  1045. } else if (0 == strcmp(name, "rw")) {
  1046. region = EC_FLASH_REGION_ACTIVE;
  1047. } else if (0 == strcmp(name, "wp-ro")) {
  1048. region = EC_FLASH_REGION_WP_RO;
  1049. } else {
  1050. debug("Unknown EC flash region name '%s'\n", name);
  1051. return -1;
  1052. }
  1053. if (ofnode_read_fmap_entry(node, &config->region[region])) {
  1054. debug("Failed to decode flash region in chrome-ec'\n");
  1055. return -1;
  1056. }
  1057. }
  1058. return 0;
  1059. }
  1060. int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
  1061. int nmsgs)
  1062. {
  1063. union {
  1064. struct ec_params_i2c_passthru p;
  1065. uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1066. } params;
  1067. union {
  1068. struct ec_response_i2c_passthru r;
  1069. uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1070. } response;
  1071. struct ec_params_i2c_passthru *p = &params.p;
  1072. struct ec_response_i2c_passthru *r = &response.r;
  1073. struct ec_params_i2c_passthru_msg *msg;
  1074. uint8_t *pdata, *read_ptr = NULL;
  1075. int read_len;
  1076. int size;
  1077. int rv;
  1078. int i;
  1079. p->port = port;
  1080. p->num_msgs = nmsgs;
  1081. size = sizeof(*p) + p->num_msgs * sizeof(*msg);
  1082. /* Create a message to write the register address and optional data */
  1083. pdata = (uint8_t *)p + size;
  1084. read_len = 0;
  1085. for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
  1086. bool is_read = in->flags & I2C_M_RD;
  1087. msg->addr_flags = in->addr;
  1088. msg->len = in->len;
  1089. if (is_read) {
  1090. msg->addr_flags |= EC_I2C_FLAG_READ;
  1091. read_len += in->len;
  1092. read_ptr = in->buf;
  1093. if (sizeof(*r) + read_len > sizeof(response)) {
  1094. puts("Read length too big for buffer\n");
  1095. return -1;
  1096. }
  1097. } else {
  1098. if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
  1099. puts("Params too large for buffer\n");
  1100. return -1;
  1101. }
  1102. memcpy(pdata, in->buf, in->len);
  1103. pdata += in->len;
  1104. }
  1105. }
  1106. rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
  1107. r, sizeof(*r) + read_len);
  1108. if (rv < 0)
  1109. return rv;
  1110. /* Parse response */
  1111. if (r->i2c_status & EC_I2C_STATUS_ERROR) {
  1112. printf("Transfer failed with status=0x%x\n", r->i2c_status);
  1113. return -1;
  1114. }
  1115. if (rv < sizeof(*r) + read_len) {
  1116. puts("Truncated read response\n");
  1117. return -1;
  1118. }
  1119. /* We only support a single read message for each transfer */
  1120. if (read_len)
  1121. memcpy(read_ptr, r->data, read_len);
  1122. return 0;
  1123. }
  1124. int cros_ec_get_features(struct udevice *dev, u64 *featuresp)
  1125. {
  1126. struct ec_response_get_features r;
  1127. int rv;
  1128. rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
  1129. if (rv != sizeof(r))
  1130. return -EIO;
  1131. *featuresp = r.flags[0] | (u64)r.flags[1] << 32;
  1132. return 0;
  1133. }
  1134. int cros_ec_check_feature(struct udevice *dev, uint feature)
  1135. {
  1136. struct ec_response_get_features r;
  1137. int rv;
  1138. rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
  1139. if (rv != sizeof(r))
  1140. return -EIO;
  1141. if (feature >= 8 * sizeof(r.flags))
  1142. return -EINVAL;
  1143. return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature) ? true :
  1144. false;
  1145. }
  1146. /*
  1147. * Query the EC for specified mask indicating enabled events.
  1148. * The EC maintains separate event masks for SMI, SCI and WAKE.
  1149. */
  1150. static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
  1151. uint64_t *value)
  1152. {
  1153. int ret;
  1154. struct ec_params_host_event req;
  1155. struct ec_response_host_event rsp;
  1156. req.action = action;
  1157. req.mask_type = mask;
  1158. if (action != EC_HOST_EVENT_GET)
  1159. req.value = *value;
  1160. else
  1161. *value = 0;
  1162. ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
  1163. sizeof(rsp));
  1164. if (action != EC_HOST_EVENT_GET)
  1165. return ret;
  1166. if (ret == 0)
  1167. *value = rsp.value;
  1168. return ret;
  1169. }
  1170. static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
  1171. uint action, uint64_t *value)
  1172. {
  1173. int ret = -1;
  1174. struct ec_params_host_event_mask req;
  1175. struct ec_response_host_event_mask rsp;
  1176. if (hcmd == INVALID_HCMD)
  1177. return ret;
  1178. if (action != EC_HOST_EVENT_GET)
  1179. req.mask = (uint32_t)*value;
  1180. else
  1181. *value = 0;
  1182. ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
  1183. if (action != EC_HOST_EVENT_GET)
  1184. return ret;
  1185. if (ret == 0)
  1186. *value = rsp.mask;
  1187. return ret;
  1188. }
  1189. bool cros_ec_is_uhepi_supported(struct udevice *dev)
  1190. {
  1191. #define UHEPI_SUPPORTED 1
  1192. #define UHEPI_NOT_SUPPORTED 2
  1193. static int uhepi_support;
  1194. if (!uhepi_support) {
  1195. uhepi_support = cros_ec_check_feature(dev,
  1196. EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
  1197. UHEPI_NOT_SUPPORTED;
  1198. log_debug("Chrome EC: UHEPI %s\n",
  1199. uhepi_support == UHEPI_SUPPORTED ? "supported" :
  1200. "not supported");
  1201. }
  1202. return uhepi_support == UHEPI_SUPPORTED;
  1203. }
  1204. static int cros_ec_get_mask(struct udevice *dev, uint type)
  1205. {
  1206. u64 value = 0;
  1207. if (cros_ec_is_uhepi_supported(dev)) {
  1208. cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
  1209. } else {
  1210. assert(type < ARRAY_SIZE(event_map));
  1211. cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
  1212. EC_HOST_EVENT_GET, &value);
  1213. }
  1214. return value;
  1215. }
  1216. static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
  1217. {
  1218. if (cros_ec_is_uhepi_supported(dev))
  1219. return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
  1220. assert(type < ARRAY_SIZE(event_map));
  1221. return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
  1222. EC_HOST_EVENT_CLEAR, &mask);
  1223. }
  1224. uint64_t cros_ec_get_events_b(struct udevice *dev)
  1225. {
  1226. return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
  1227. }
  1228. int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
  1229. {
  1230. log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
  1231. return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
  1232. }
  1233. int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
  1234. {
  1235. struct ec_params_charge_state p;
  1236. struct ec_response_charge_state r;
  1237. int ret;
  1238. p.cmd = CHARGE_STATE_CMD_GET_PARAM;
  1239. p.get_param.param = CS_PARAM_LIMIT_POWER;
  1240. ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
  1241. &r, sizeof(r));
  1242. /*
  1243. * If our EC doesn't support the LIMIT_POWER parameter, assume that
  1244. * LIMIT_POWER is not requested.
  1245. */
  1246. if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
  1247. log_warning("PARAM_LIMIT_POWER not supported by EC\n");
  1248. return -ENOSYS;
  1249. }
  1250. if (ret != sizeof(r.get_param))
  1251. return -EINVAL;
  1252. *limit_powerp = r.get_param.value;
  1253. return 0;
  1254. }
  1255. int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
  1256. {
  1257. struct ec_params_config_power_button params;
  1258. int ret;
  1259. params.flags = flags;
  1260. ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
  1261. &params, sizeof(params), NULL, 0);
  1262. if (ret < 0)
  1263. return ret;
  1264. return 0;
  1265. }
  1266. int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
  1267. {
  1268. u32 mask;
  1269. int ret;
  1270. ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
  1271. &mask);
  1272. if (ret < 0)
  1273. return ret;
  1274. return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
  1275. }
  1276. int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
  1277. {
  1278. u32 mask;
  1279. int ret;
  1280. ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
  1281. &mask);
  1282. if (ret < 0)
  1283. return ret;
  1284. /* Set lid close event state in the EC SMI event mask */
  1285. if (enable)
  1286. mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
  1287. else
  1288. mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
  1289. ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
  1290. if (ret < 0)
  1291. return ret;
  1292. printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
  1293. return 0;
  1294. }
  1295. int cros_ec_vstore_supported(struct udevice *dev)
  1296. {
  1297. return cros_ec_check_feature(dev, EC_FEATURE_VSTORE);
  1298. }
  1299. int cros_ec_vstore_info(struct udevice *dev, u32 *lockedp)
  1300. {
  1301. struct ec_response_vstore_info *resp;
  1302. if (ec_command_inptr(dev, EC_CMD_VSTORE_INFO, 0, NULL, 0,
  1303. (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
  1304. return -EIO;
  1305. if (lockedp)
  1306. *lockedp = resp->slot_locked;
  1307. return resp->slot_count;
  1308. }
  1309. /*
  1310. * cros_ec_vstore_read - Read data from EC vstore slot
  1311. *
  1312. * @slot: vstore slot to read from
  1313. * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
  1314. */
  1315. int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data)
  1316. {
  1317. struct ec_params_vstore_read req;
  1318. struct ec_response_vstore_read *resp;
  1319. req.slot = slot;
  1320. if (ec_command_inptr(dev, EC_CMD_VSTORE_READ, 0, &req, sizeof(req),
  1321. (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
  1322. return -EIO;
  1323. if (!data || req.slot >= EC_VSTORE_SLOT_MAX)
  1324. return -EINVAL;
  1325. memcpy(data, resp->data, sizeof(resp->data));
  1326. return 0;
  1327. }
  1328. /*
  1329. * cros_ec_vstore_write - Save data into EC vstore slot
  1330. *
  1331. * @slot: vstore slot to write into
  1332. * @data: data to write
  1333. * @size: size of data in bytes
  1334. *
  1335. * Maximum size of data is EC_VSTORE_SLOT_SIZE. It is the callers
  1336. * responsibility to check the number of implemented slots by
  1337. * querying the vstore info.
  1338. */
  1339. int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data,
  1340. size_t size)
  1341. {
  1342. struct ec_params_vstore_write req;
  1343. if (slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE)
  1344. return -EINVAL;
  1345. req.slot = slot;
  1346. memcpy(req.data, data, size);
  1347. if (ec_command(dev, EC_CMD_VSTORE_WRITE, 0, &req, sizeof(req), NULL, 0))
  1348. return -EIO;
  1349. return 0;
  1350. }
  1351. int cros_ec_get_switches(struct udevice *dev)
  1352. {
  1353. struct dm_cros_ec_ops *ops;
  1354. int ret;
  1355. ops = dm_cros_ec_get_ops(dev);
  1356. if (!ops->get_switches)
  1357. return -ENOSYS;
  1358. ret = ops->get_switches(dev);
  1359. if (ret < 0)
  1360. return log_msg_ret("get", ret);
  1361. return ret;
  1362. }
  1363. int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep)
  1364. {
  1365. struct ec_params_charge_state req;
  1366. struct ec_response_charge_state resp;
  1367. int ret;
  1368. req.cmd = CHARGE_STATE_CMD_GET_STATE;
  1369. ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &req, sizeof(req),
  1370. &resp, sizeof(resp));
  1371. if (ret)
  1372. return log_msg_ret("read", ret);
  1373. *chargep = resp.get_state.batt_state_of_charge;
  1374. return 0;
  1375. }
  1376. UCLASS_DRIVER(cros_ec) = {
  1377. .id = UCLASS_CROS_EC,
  1378. .name = "cros-ec",
  1379. .per_device_auto = sizeof(struct cros_ec_dev),
  1380. #if CONFIG_IS_ENABLED(OF_REAL)
  1381. .post_bind = dm_scan_fdt_dev,
  1382. #endif
  1383. .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
  1384. };