cros_ec.c 40 KB

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