cros_ec.c 37 KB

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