cros_ec.c 41 KB

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