cros_ec.c 37 KB

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