cros_ec.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. /*
  2. * Chromium OS cros_ec driver
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * This is the interface to the Chrome OS EC. It provides keyboard functions,
  10. * power control and battery management. Quite a few other functions are
  11. * provided to enable the EC software to be updated, talk to the EC's I2C bus
  12. * and store a small amount of data in a memory which persists while the EC
  13. * is not reset.
  14. */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <i2c.h>
  18. #include <cros_ec.h>
  19. #include <fdtdec.h>
  20. #include <malloc.h>
  21. #include <spi.h>
  22. #include <asm/errno.h>
  23. #include <asm/io.h>
  24. #include <asm-generic/gpio.h>
  25. #ifdef DEBUG_TRACE
  26. #define debug_trace(fmt, b...) debug(fmt, #b)
  27. #else
  28. #define debug_trace(fmt, b...)
  29. #endif
  30. enum {
  31. /* Timeout waiting for a flash erase command to complete */
  32. CROS_EC_CMD_TIMEOUT_MS = 5000,
  33. /* Timeout waiting for a synchronous hash to be recomputed */
  34. CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
  35. };
  36. static struct cros_ec_dev static_dev, *last_dev;
  37. DECLARE_GLOBAL_DATA_PTR;
  38. /* Note: depends on enum ec_current_image */
  39. static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
  40. void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
  41. {
  42. #ifdef DEBUG
  43. int i;
  44. printf("%s: ", name);
  45. if (cmd != -1)
  46. printf("cmd=%#x: ", cmd);
  47. for (i = 0; i < len; i++)
  48. printf("%02x ", data[i]);
  49. printf("\n");
  50. #endif
  51. }
  52. /*
  53. * Calculate a simple 8-bit checksum of a data block
  54. *
  55. * @param data Data block to checksum
  56. * @param size Size of data block in bytes
  57. * @return checksum value (0 to 255)
  58. */
  59. int cros_ec_calc_checksum(const uint8_t *data, int size)
  60. {
  61. int csum, i;
  62. for (i = csum = 0; i < size; i++)
  63. csum += data[i];
  64. return csum & 0xff;
  65. }
  66. /**
  67. * Create a request packet for protocol version 3.
  68. *
  69. * The packet is stored in the device's internal output buffer.
  70. *
  71. * @param dev CROS-EC device
  72. * @param cmd Command to send (EC_CMD_...)
  73. * @param cmd_version Version of command to send (EC_VER_...)
  74. * @param dout Output data (may be NULL If dout_len=0)
  75. * @param dout_len Size of output data in bytes
  76. * @return packet size in bytes, or <0 if error.
  77. */
  78. static int create_proto3_request(struct cros_ec_dev *dev,
  79. int cmd, int cmd_version,
  80. const void *dout, int dout_len)
  81. {
  82. struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
  83. int out_bytes = dout_len + sizeof(*rq);
  84. /* Fail if output size is too big */
  85. if (out_bytes > (int)sizeof(dev->dout)) {
  86. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  87. return -EC_RES_REQUEST_TRUNCATED;
  88. }
  89. /* Fill in request packet */
  90. rq->struct_version = EC_HOST_REQUEST_VERSION;
  91. rq->checksum = 0;
  92. rq->command = cmd;
  93. rq->command_version = cmd_version;
  94. rq->reserved = 0;
  95. rq->data_len = dout_len;
  96. /* Copy data after header */
  97. memcpy(rq + 1, dout, dout_len);
  98. /* Write checksum field so the entire packet sums to 0 */
  99. rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
  100. cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
  101. /* Return size of request packet */
  102. return out_bytes;
  103. }
  104. /**
  105. * Prepare the device to receive a protocol version 3 response.
  106. *
  107. * @param dev CROS-EC device
  108. * @param din_len Maximum size of response in bytes
  109. * @return maximum expected number of bytes in response, or <0 if error.
  110. */
  111. static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
  112. {
  113. int in_bytes = din_len + sizeof(struct ec_host_response);
  114. /* Fail if input size is too big */
  115. if (in_bytes > (int)sizeof(dev->din)) {
  116. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  117. return -EC_RES_RESPONSE_TOO_BIG;
  118. }
  119. /* Return expected size of response packet */
  120. return in_bytes;
  121. }
  122. /**
  123. * Handle a protocol version 3 response packet.
  124. *
  125. * The packet must already be stored in the device's internal input buffer.
  126. *
  127. * @param dev CROS-EC device
  128. * @param dinp Returns pointer to response data
  129. * @param din_len Maximum size of response in bytes
  130. * @return number of bytes of response data, or <0 if error
  131. */
  132. static int handle_proto3_response(struct cros_ec_dev *dev,
  133. uint8_t **dinp, int din_len)
  134. {
  135. struct ec_host_response *rs = (struct ec_host_response *)dev->din;
  136. int in_bytes;
  137. int csum;
  138. cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
  139. /* Check input data */
  140. if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
  141. debug("%s: EC response version mismatch\n", __func__);
  142. return -EC_RES_INVALID_RESPONSE;
  143. }
  144. if (rs->reserved) {
  145. debug("%s: EC response reserved != 0\n", __func__);
  146. return -EC_RES_INVALID_RESPONSE;
  147. }
  148. if (rs->data_len > din_len) {
  149. debug("%s: EC returned too much data\n", __func__);
  150. return -EC_RES_RESPONSE_TOO_BIG;
  151. }
  152. cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
  153. /* Update in_bytes to actual data size */
  154. in_bytes = sizeof(*rs) + rs->data_len;
  155. /* Verify checksum */
  156. csum = cros_ec_calc_checksum(dev->din, in_bytes);
  157. if (csum) {
  158. debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
  159. csum);
  160. return -EC_RES_INVALID_CHECKSUM;
  161. }
  162. /* Return error result, if any */
  163. if (rs->result)
  164. return -(int)rs->result;
  165. /* If we're still here, set response data pointer and return length */
  166. *dinp = (uint8_t *)(rs + 1);
  167. return rs->data_len;
  168. }
  169. static int send_command_proto3(struct cros_ec_dev *dev,
  170. int cmd, int cmd_version,
  171. const void *dout, int dout_len,
  172. uint8_t **dinp, int din_len)
  173. {
  174. int out_bytes, in_bytes;
  175. int rv;
  176. /* Create request packet */
  177. out_bytes = create_proto3_request(dev, cmd, cmd_version,
  178. dout, dout_len);
  179. if (out_bytes < 0)
  180. return out_bytes;
  181. /* Prepare response buffer */
  182. in_bytes = prepare_proto3_response_buffer(dev, din_len);
  183. if (in_bytes < 0)
  184. return in_bytes;
  185. switch (dev->interface) {
  186. #ifdef CONFIG_CROS_EC_SPI
  187. case CROS_EC_IF_SPI:
  188. rv = cros_ec_spi_packet(dev, out_bytes, in_bytes);
  189. break;
  190. #endif
  191. case CROS_EC_IF_NONE:
  192. /* TODO: support protocol 3 for LPC, I2C; for now fall through */
  193. default:
  194. debug("%s: Unsupported interface\n", __func__);
  195. rv = -1;
  196. }
  197. if (rv < 0)
  198. return rv;
  199. /* Process the response */
  200. return handle_proto3_response(dev, dinp, din_len);
  201. }
  202. static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  203. const void *dout, int dout_len,
  204. uint8_t **dinp, int din_len)
  205. {
  206. int ret = -1;
  207. /* Handle protocol version 3 support */
  208. if (dev->protocol_version == 3) {
  209. return send_command_proto3(dev, cmd, cmd_version,
  210. dout, dout_len, dinp, din_len);
  211. }
  212. switch (dev->interface) {
  213. #ifdef CONFIG_CROS_EC_SPI
  214. case CROS_EC_IF_SPI:
  215. ret = cros_ec_spi_command(dev, cmd, cmd_version,
  216. (const uint8_t *)dout, dout_len,
  217. dinp, din_len);
  218. break;
  219. #endif
  220. #ifdef CONFIG_CROS_EC_I2C
  221. case CROS_EC_IF_I2C:
  222. ret = cros_ec_i2c_command(dev, cmd, cmd_version,
  223. (const uint8_t *)dout, dout_len,
  224. dinp, din_len);
  225. break;
  226. #endif
  227. #ifdef CONFIG_CROS_EC_LPC
  228. case CROS_EC_IF_LPC:
  229. ret = cros_ec_lpc_command(dev, cmd, cmd_version,
  230. (const uint8_t *)dout, dout_len,
  231. dinp, din_len);
  232. break;
  233. #endif
  234. case CROS_EC_IF_NONE:
  235. default:
  236. ret = -1;
  237. }
  238. return ret;
  239. }
  240. /**
  241. * Send a command to the CROS-EC device and return the reply.
  242. *
  243. * The device's internal input/output buffers are used.
  244. *
  245. * @param dev CROS-EC device
  246. * @param cmd Command to send (EC_CMD_...)
  247. * @param cmd_version Version of command to send (EC_VER_...)
  248. * @param dout Output data (may be NULL If dout_len=0)
  249. * @param dout_len Size of output data in bytes
  250. * @param dinp Response data (may be NULL If din_len=0).
  251. * If not NULL, it will be updated to point to the data
  252. * and will always be double word aligned (64-bits)
  253. * @param din_len Maximum size of response in bytes
  254. * @return number of bytes in response, or -1 on error
  255. */
  256. static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
  257. int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
  258. int din_len)
  259. {
  260. uint8_t *din = NULL;
  261. int len;
  262. len = send_command(dev, cmd, cmd_version, dout, dout_len,
  263. &din, din_len);
  264. /* If the command doesn't complete, wait a while */
  265. if (len == -EC_RES_IN_PROGRESS) {
  266. struct ec_response_get_comms_status *resp = NULL;
  267. ulong start;
  268. /* Wait for command to complete */
  269. start = get_timer(0);
  270. do {
  271. int ret;
  272. mdelay(50); /* Insert some reasonable delay */
  273. ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
  274. NULL, 0,
  275. (uint8_t **)&resp, sizeof(*resp));
  276. if (ret < 0)
  277. return ret;
  278. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  279. debug("%s: Command %#02x timeout\n",
  280. __func__, cmd);
  281. return -EC_RES_TIMEOUT;
  282. }
  283. } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
  284. /* OK it completed, so read the status response */
  285. /* not sure why it was 0 for the last argument */
  286. len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
  287. NULL, 0, &din, din_len);
  288. }
  289. debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
  290. dinp ? *dinp : NULL);
  291. if (dinp) {
  292. /* If we have any data to return, it must be 64bit-aligned */
  293. assert(len <= 0 || !((uintptr_t)din & 7));
  294. *dinp = din;
  295. }
  296. return len;
  297. }
  298. /**
  299. * Send a command to the CROS-EC device and return the reply.
  300. *
  301. * The device's internal input/output buffers are used.
  302. *
  303. * @param dev CROS-EC device
  304. * @param cmd Command to send (EC_CMD_...)
  305. * @param cmd_version Version of command to send (EC_VER_...)
  306. * @param dout Output data (may be NULL If dout_len=0)
  307. * @param dout_len Size of output data in bytes
  308. * @param din Response data (may be NULL If din_len=0).
  309. * It not NULL, it is a place for ec_command() to copy the
  310. * data to.
  311. * @param din_len Maximum size of response in bytes
  312. * @return number of bytes in response, or -1 on error
  313. */
  314. static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  315. const void *dout, int dout_len,
  316. void *din, int din_len)
  317. {
  318. uint8_t *in_buffer;
  319. int len;
  320. assert((din_len == 0) || din);
  321. len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
  322. &in_buffer, din_len);
  323. if (len > 0) {
  324. /*
  325. * If we were asked to put it somewhere, do so, otherwise just
  326. * disregard the result.
  327. */
  328. if (din && in_buffer) {
  329. assert(len <= din_len);
  330. memmove(din, in_buffer, len);
  331. }
  332. }
  333. return len;
  334. }
  335. int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
  336. {
  337. if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
  338. sizeof(scan->data)) != sizeof(scan->data))
  339. return -1;
  340. return 0;
  341. }
  342. int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
  343. {
  344. struct ec_response_get_version *r;
  345. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  346. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  347. return -1;
  348. if (maxlen > (int)sizeof(r->version_string_ro))
  349. maxlen = sizeof(r->version_string_ro);
  350. switch (r->current_image) {
  351. case EC_IMAGE_RO:
  352. memcpy(id, r->version_string_ro, maxlen);
  353. break;
  354. case EC_IMAGE_RW:
  355. memcpy(id, r->version_string_rw, maxlen);
  356. break;
  357. default:
  358. return -1;
  359. }
  360. id[maxlen - 1] = '\0';
  361. return 0;
  362. }
  363. int cros_ec_read_version(struct cros_ec_dev *dev,
  364. struct ec_response_get_version **versionp)
  365. {
  366. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  367. (uint8_t **)versionp, sizeof(**versionp))
  368. != sizeof(**versionp))
  369. return -1;
  370. return 0;
  371. }
  372. int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
  373. {
  374. if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
  375. (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
  376. return -1;
  377. return 0;
  378. }
  379. int cros_ec_read_current_image(struct cros_ec_dev *dev,
  380. enum ec_current_image *image)
  381. {
  382. struct ec_response_get_version *r;
  383. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  384. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  385. return -1;
  386. *image = r->current_image;
  387. return 0;
  388. }
  389. static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
  390. struct ec_response_vboot_hash *hash)
  391. {
  392. struct ec_params_vboot_hash p;
  393. ulong start;
  394. start = get_timer(0);
  395. while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
  396. mdelay(50); /* Insert some reasonable delay */
  397. p.cmd = EC_VBOOT_HASH_GET;
  398. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  399. hash, sizeof(*hash)) < 0)
  400. return -1;
  401. if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
  402. debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
  403. return -EC_RES_TIMEOUT;
  404. }
  405. }
  406. return 0;
  407. }
  408. int cros_ec_read_hash(struct cros_ec_dev *dev,
  409. struct ec_response_vboot_hash *hash)
  410. {
  411. struct ec_params_vboot_hash p;
  412. int rv;
  413. p.cmd = EC_VBOOT_HASH_GET;
  414. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  415. hash, sizeof(*hash)) < 0)
  416. return -1;
  417. /* If the EC is busy calculating the hash, fidget until it's done. */
  418. rv = cros_ec_wait_on_hash_done(dev, hash);
  419. if (rv)
  420. return rv;
  421. /* If the hash is valid, we're done. Otherwise, we have to kick it off
  422. * again and wait for it to complete. Note that we explicitly assume
  423. * that hashing zero bytes is always wrong, even though that would
  424. * produce a valid hash value. */
  425. if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
  426. return 0;
  427. debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
  428. __func__, hash->status, hash->size);
  429. p.cmd = EC_VBOOT_HASH_START;
  430. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  431. p.nonce_size = 0;
  432. p.offset = EC_VBOOT_HASH_OFFSET_RW;
  433. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  434. hash, sizeof(*hash)) < 0)
  435. return -1;
  436. rv = cros_ec_wait_on_hash_done(dev, hash);
  437. if (rv)
  438. return rv;
  439. debug("%s: hash done\n", __func__);
  440. return 0;
  441. }
  442. static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
  443. {
  444. struct ec_params_vboot_hash p;
  445. struct ec_response_vboot_hash *hash;
  446. /* We don't have an explict command for the EC to discard its current
  447. * hash value, so we'll just tell it to calculate one that we know is
  448. * wrong (we claim that hashing zero bytes is always invalid).
  449. */
  450. p.cmd = EC_VBOOT_HASH_RECALC;
  451. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  452. p.nonce_size = 0;
  453. p.offset = 0;
  454. p.size = 0;
  455. debug("%s:\n", __func__);
  456. if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  457. (uint8_t **)&hash, sizeof(*hash)) < 0)
  458. return -1;
  459. /* No need to wait for it to finish */
  460. return 0;
  461. }
  462. int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
  463. uint8_t flags)
  464. {
  465. struct ec_params_reboot_ec p;
  466. p.cmd = cmd;
  467. p.flags = flags;
  468. if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
  469. < 0)
  470. return -1;
  471. if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
  472. /*
  473. * EC reboot will take place immediately so delay to allow it
  474. * to complete. Note that some reboot types (EC_REBOOT_COLD)
  475. * will reboot the AP as well, in which case we won't actually
  476. * get to this point.
  477. */
  478. /*
  479. * TODO(rspangler@chromium.org): Would be nice if we had a
  480. * better way to determine when the reboot is complete. Could
  481. * we poll a memory-mapped LPC value?
  482. */
  483. udelay(50000);
  484. }
  485. return 0;
  486. }
  487. int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
  488. {
  489. /* no interrupt support : always poll */
  490. if (!fdt_gpio_isvalid(&dev->ec_int))
  491. return -ENOENT;
  492. return !gpio_get_value(dev->ec_int.gpio);
  493. }
  494. int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
  495. {
  496. if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
  497. sizeof(*info)) != sizeof(*info))
  498. return -1;
  499. return 0;
  500. }
  501. int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
  502. {
  503. struct ec_response_host_event_mask *resp;
  504. /*
  505. * Use the B copy of the event flags, because the main copy is already
  506. * used by ACPI/SMI.
  507. */
  508. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
  509. (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
  510. return -1;
  511. if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
  512. return -1;
  513. *events_ptr = resp->mask;
  514. return 0;
  515. }
  516. int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
  517. {
  518. struct ec_params_host_event_mask params;
  519. params.mask = events;
  520. /*
  521. * Use the B copy of the event flags, so it affects the data returned
  522. * by cros_ec_get_host_events().
  523. */
  524. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
  525. &params, sizeof(params), NULL, 0) < 0)
  526. return -1;
  527. return 0;
  528. }
  529. int cros_ec_flash_protect(struct cros_ec_dev *dev,
  530. uint32_t set_mask, uint32_t set_flags,
  531. struct ec_response_flash_protect *resp)
  532. {
  533. struct ec_params_flash_protect params;
  534. params.mask = set_mask;
  535. params.flags = set_flags;
  536. if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
  537. &params, sizeof(params),
  538. resp, sizeof(*resp)) != sizeof(*resp))
  539. return -1;
  540. return 0;
  541. }
  542. static int cros_ec_check_version(struct cros_ec_dev *dev)
  543. {
  544. struct ec_params_hello req;
  545. struct ec_response_hello *resp;
  546. #ifdef CONFIG_CROS_EC_LPC
  547. /* LPC has its own way of doing this */
  548. if (dev->interface == CROS_EC_IF_LPC)
  549. return cros_ec_lpc_check_version(dev);
  550. #endif
  551. /*
  552. * TODO(sjg@chromium.org).
  553. * There is a strange oddity here with the EC. We could just ignore
  554. * the response, i.e. pass the last two parameters as NULL and 0.
  555. * In this case we won't read back very many bytes from the EC.
  556. * On the I2C bus the EC gets upset about this and will try to send
  557. * the bytes anyway. This means that we will have to wait for that
  558. * to complete before continuing with a new EC command.
  559. *
  560. * This problem is probably unique to the I2C bus.
  561. *
  562. * So for now, just read all the data anyway.
  563. */
  564. /* Try sending a version 3 packet */
  565. dev->protocol_version = 3;
  566. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  567. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  568. return 0;
  569. }
  570. /* Try sending a version 2 packet */
  571. dev->protocol_version = 2;
  572. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  573. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  574. return 0;
  575. }
  576. /*
  577. * Fail if we're still here, since the EC doesn't understand any
  578. * protcol version we speak. Version 1 interface without command
  579. * version is no longer supported, and we don't know about any new
  580. * protocol versions.
  581. */
  582. dev->protocol_version = 0;
  583. printf("%s: ERROR: old EC interface not supported\n", __func__);
  584. return -1;
  585. }
  586. int cros_ec_test(struct cros_ec_dev *dev)
  587. {
  588. struct ec_params_hello req;
  589. struct ec_response_hello *resp;
  590. req.in_data = 0x12345678;
  591. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  592. (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
  593. printf("ec_command_inptr() returned error\n");
  594. return -1;
  595. }
  596. if (resp->out_data != req.in_data + 0x01020304) {
  597. printf("Received invalid handshake %x\n", resp->out_data);
  598. return -1;
  599. }
  600. return 0;
  601. }
  602. int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
  603. uint32_t *offset, uint32_t *size)
  604. {
  605. struct ec_params_flash_region_info p;
  606. struct ec_response_flash_region_info *r;
  607. int ret;
  608. p.region = region;
  609. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  610. EC_VER_FLASH_REGION_INFO,
  611. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  612. if (ret != sizeof(*r))
  613. return -1;
  614. if (offset)
  615. *offset = r->offset;
  616. if (size)
  617. *size = r->size;
  618. return 0;
  619. }
  620. int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
  621. {
  622. struct ec_params_flash_erase p;
  623. p.offset = offset;
  624. p.size = size;
  625. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  626. NULL, 0);
  627. }
  628. /**
  629. * Write a single block to the flash
  630. *
  631. * Write a block of data to the EC flash. The size must not exceed the flash
  632. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  633. *
  634. * The offset starts at 0. You can obtain the region information from
  635. * cros_ec_flash_offset() to find out where to write for a particular region.
  636. *
  637. * Attempting to write to the region where the EC is currently running from
  638. * will result in an error.
  639. *
  640. * @param dev CROS-EC device
  641. * @param data Pointer to data buffer to write
  642. * @param offset Offset within flash to write to.
  643. * @param size Number of bytes to write
  644. * @return 0 if ok, -1 on error
  645. */
  646. static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
  647. const uint8_t *data, uint32_t offset, uint32_t size)
  648. {
  649. struct ec_params_flash_write p;
  650. p.offset = offset;
  651. p.size = size;
  652. assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
  653. memcpy(&p + 1, data, p.size);
  654. return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  655. &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
  656. }
  657. /**
  658. * Return optimal flash write burst size
  659. */
  660. static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
  661. {
  662. return EC_FLASH_WRITE_VER0_SIZE;
  663. }
  664. /**
  665. * Check if a block of data is erased (all 0xff)
  666. *
  667. * This function is useful when dealing with flash, for checking whether a
  668. * data block is erased and thus does not need to be programmed.
  669. *
  670. * @param data Pointer to data to check (must be word-aligned)
  671. * @param size Number of bytes to check (must be word-aligned)
  672. * @return 0 if erased, non-zero if any word is not erased
  673. */
  674. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  675. {
  676. assert(!(size & 3));
  677. size /= sizeof(uint32_t);
  678. for (; size > 0; size -= 4, data++)
  679. if (*data != -1U)
  680. return 0;
  681. return 1;
  682. }
  683. int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  684. uint32_t offset, uint32_t size)
  685. {
  686. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  687. uint32_t end, off;
  688. int ret;
  689. /*
  690. * TODO: round up to the nearest multiple of write size. Can get away
  691. * without that on link right now because its write size is 4 bytes.
  692. */
  693. end = offset + size;
  694. for (off = offset; off < end; off += burst, data += burst) {
  695. uint32_t todo;
  696. /* If the data is empty, there is no point in programming it */
  697. todo = min(end - off, burst);
  698. if (dev->optimise_flash_write &&
  699. cros_ec_data_is_erased((uint32_t *)data, todo))
  700. continue;
  701. ret = cros_ec_flash_write_block(dev, data, off, todo);
  702. if (ret)
  703. return ret;
  704. }
  705. return 0;
  706. }
  707. /**
  708. * Read a single block from the flash
  709. *
  710. * Read a block of data from the EC flash. The size must not exceed the flash
  711. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  712. *
  713. * The offset starts at 0. You can obtain the region information from
  714. * cros_ec_flash_offset() to find out where to read for a particular region.
  715. *
  716. * @param dev CROS-EC device
  717. * @param data Pointer to data buffer to read into
  718. * @param offset Offset within flash to read from
  719. * @param size Number of bytes to read
  720. * @return 0 if ok, -1 on error
  721. */
  722. static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
  723. uint32_t offset, uint32_t size)
  724. {
  725. struct ec_params_flash_read p;
  726. p.offset = offset;
  727. p.size = size;
  728. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  729. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  730. }
  731. int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
  732. uint32_t size)
  733. {
  734. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  735. uint32_t end, off;
  736. int ret;
  737. end = offset + size;
  738. for (off = offset; off < end; off += burst, data += burst) {
  739. ret = cros_ec_flash_read_block(dev, data, off,
  740. min(end - off, burst));
  741. if (ret)
  742. return ret;
  743. }
  744. return 0;
  745. }
  746. int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  747. const uint8_t *image, int image_size)
  748. {
  749. uint32_t rw_offset, rw_size;
  750. int ret;
  751. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
  752. return -1;
  753. if (image_size > (int)rw_size)
  754. return -1;
  755. /* Invalidate the existing hash, just in case the AP reboots
  756. * unexpectedly during the update. If that happened, the EC RW firmware
  757. * would be invalid, but the EC would still have the original hash.
  758. */
  759. ret = cros_ec_invalidate_hash(dev);
  760. if (ret)
  761. return ret;
  762. /*
  763. * Erase the entire RW section, so that the EC doesn't see any garbage
  764. * past the new image if it's smaller than the current image.
  765. *
  766. * TODO: could optimize this to erase just the current image, since
  767. * presumably everything past that is 0xff's. But would still need to
  768. * round up to the nearest multiple of erase size.
  769. */
  770. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  771. if (ret)
  772. return ret;
  773. /* Write the image */
  774. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  775. if (ret)
  776. return ret;
  777. return 0;
  778. }
  779. int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
  780. {
  781. struct ec_params_vbnvcontext p;
  782. int len;
  783. p.op = EC_VBNV_CONTEXT_OP_READ;
  784. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  785. &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
  786. if (len < EC_VBNV_BLOCK_SIZE)
  787. return -1;
  788. return 0;
  789. }
  790. int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
  791. {
  792. struct ec_params_vbnvcontext p;
  793. int len;
  794. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  795. memcpy(p.block, block, sizeof(p.block));
  796. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  797. &p, sizeof(p), NULL, 0);
  798. if (len < 0)
  799. return -1;
  800. return 0;
  801. }
  802. int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
  803. {
  804. struct ec_params_ldo_set params;
  805. params.index = index;
  806. params.state = state;
  807. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
  808. &params, sizeof(params),
  809. NULL, 0))
  810. return -1;
  811. return 0;
  812. }
  813. int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
  814. {
  815. struct ec_params_ldo_get params;
  816. struct ec_response_ldo_get *resp;
  817. params.index = index;
  818. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
  819. &params, sizeof(params),
  820. (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
  821. return -1;
  822. *state = resp->state;
  823. return 0;
  824. }
  825. /**
  826. * Decode EC interface details from the device tree and allocate a suitable
  827. * device.
  828. *
  829. * @param blob Device tree blob
  830. * @param node Node to decode from
  831. * @param devp Returns a pointer to the new allocated device
  832. * @return 0 if ok, -1 on error
  833. */
  834. static int cros_ec_decode_fdt(const void *blob, int node,
  835. struct cros_ec_dev **devp)
  836. {
  837. enum fdt_compat_id compat;
  838. struct cros_ec_dev *dev;
  839. int parent;
  840. /* See what type of parent we are inside (this is expensive) */
  841. parent = fdt_parent_offset(blob, node);
  842. if (parent < 0) {
  843. debug("%s: Cannot find node parent\n", __func__);
  844. return -1;
  845. }
  846. dev = &static_dev;
  847. dev->node = node;
  848. dev->parent_node = parent;
  849. compat = fdtdec_lookup(blob, parent);
  850. switch (compat) {
  851. #ifdef CONFIG_CROS_EC_SPI
  852. case COMPAT_SAMSUNG_EXYNOS_SPI:
  853. dev->interface = CROS_EC_IF_SPI;
  854. if (cros_ec_spi_decode_fdt(dev, blob))
  855. return -1;
  856. break;
  857. #endif
  858. #ifdef CONFIG_CROS_EC_I2C
  859. case COMPAT_SAMSUNG_S3C2440_I2C:
  860. dev->interface = CROS_EC_IF_I2C;
  861. if (cros_ec_i2c_decode_fdt(dev, blob))
  862. return -1;
  863. break;
  864. #endif
  865. #ifdef CONFIG_CROS_EC_LPC
  866. case COMPAT_INTEL_LPC:
  867. dev->interface = CROS_EC_IF_LPC;
  868. break;
  869. #endif
  870. default:
  871. debug("%s: Unknown compat id %d\n", __func__, compat);
  872. return -1;
  873. }
  874. fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
  875. dev->optimise_flash_write = fdtdec_get_bool(blob, node,
  876. "optimise-flash-write");
  877. *devp = dev;
  878. return 0;
  879. }
  880. int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
  881. {
  882. char id[MSG_BYTES];
  883. struct cros_ec_dev *dev;
  884. int node = 0;
  885. *cros_ecp = NULL;
  886. do {
  887. node = fdtdec_next_compatible(blob, node,
  888. COMPAT_GOOGLE_CROS_EC);
  889. if (node < 0) {
  890. debug("%s: Node not found\n", __func__);
  891. return 0;
  892. }
  893. } while (!fdtdec_get_is_enabled(blob, node));
  894. if (cros_ec_decode_fdt(blob, node, &dev)) {
  895. debug("%s: Failed to decode device.\n", __func__);
  896. return -CROS_EC_ERR_FDT_DECODE;
  897. }
  898. switch (dev->interface) {
  899. #ifdef CONFIG_CROS_EC_SPI
  900. case CROS_EC_IF_SPI:
  901. if (cros_ec_spi_init(dev, blob)) {
  902. debug("%s: Could not setup SPI interface\n", __func__);
  903. return -CROS_EC_ERR_DEV_INIT;
  904. }
  905. break;
  906. #endif
  907. #ifdef CONFIG_CROS_EC_I2C
  908. case CROS_EC_IF_I2C:
  909. if (cros_ec_i2c_init(dev, blob))
  910. return -CROS_EC_ERR_DEV_INIT;
  911. break;
  912. #endif
  913. #ifdef CONFIG_CROS_EC_LPC
  914. case CROS_EC_IF_LPC:
  915. if (cros_ec_lpc_init(dev, blob))
  916. return -CROS_EC_ERR_DEV_INIT;
  917. break;
  918. #endif
  919. case CROS_EC_IF_NONE:
  920. default:
  921. return 0;
  922. }
  923. /* we will poll the EC interrupt line */
  924. fdtdec_setup_gpio(&dev->ec_int);
  925. if (fdt_gpio_isvalid(&dev->ec_int))
  926. gpio_direction_input(dev->ec_int.gpio);
  927. if (cros_ec_check_version(dev)) {
  928. debug("%s: Could not detect CROS-EC version\n", __func__);
  929. return -CROS_EC_ERR_CHECK_VERSION;
  930. }
  931. if (cros_ec_read_id(dev, id, sizeof(id))) {
  932. debug("%s: Could not read KBC ID\n", __func__);
  933. return -CROS_EC_ERR_READ_ID;
  934. }
  935. /* Remember this device for use by the cros_ec command */
  936. last_dev = *cros_ecp = dev;
  937. debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
  938. return 0;
  939. }
  940. int cros_ec_decode_region(int argc, char * const argv[])
  941. {
  942. if (argc > 0) {
  943. if (0 == strcmp(*argv, "rw"))
  944. return EC_FLASH_REGION_RW;
  945. else if (0 == strcmp(*argv, "ro"))
  946. return EC_FLASH_REGION_RO;
  947. debug("%s: Invalid region '%s'\n", __func__, *argv);
  948. } else {
  949. debug("%s: Missing region parameter\n", __func__);
  950. }
  951. return -1;
  952. }
  953. int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
  954. {
  955. int flash_node, node;
  956. node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
  957. if (node < 0) {
  958. debug("Failed to find chrome-ec node'\n");
  959. return -1;
  960. }
  961. flash_node = fdt_subnode_offset(blob, node, "flash");
  962. if (flash_node < 0) {
  963. debug("Failed to find flash node\n");
  964. return -1;
  965. }
  966. if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
  967. &config->flash)) {
  968. debug("Failed to decode flash node in chrome-ec'\n");
  969. return -1;
  970. }
  971. config->flash_erase_value = fdtdec_get_int(blob, flash_node,
  972. "erase-value", -1);
  973. for (node = fdt_first_subnode(blob, flash_node); node >= 0;
  974. node = fdt_next_subnode(blob, node)) {
  975. const char *name = fdt_get_name(blob, node, NULL);
  976. enum ec_flash_region region;
  977. if (0 == strcmp(name, "ro")) {
  978. region = EC_FLASH_REGION_RO;
  979. } else if (0 == strcmp(name, "rw")) {
  980. region = EC_FLASH_REGION_RW;
  981. } else if (0 == strcmp(name, "wp-ro")) {
  982. region = EC_FLASH_REGION_WP_RO;
  983. } else {
  984. debug("Unknown EC flash region name '%s'\n", name);
  985. return -1;
  986. }
  987. if (fdtdec_read_fmap_entry(blob, node, "reg",
  988. &config->region[region])) {
  989. debug("Failed to decode flash region in chrome-ec'\n");
  990. return -1;
  991. }
  992. }
  993. return 0;
  994. }
  995. #ifdef CONFIG_CMD_CROS_EC
  996. /**
  997. * Perform a flash read or write command
  998. *
  999. * @param dev CROS-EC device to read/write
  1000. * @param is_write 1 do to a write, 0 to do a read
  1001. * @param argc Number of arguments
  1002. * @param argv Arguments (2 is region, 3 is address)
  1003. * @return 0 for ok, 1 for a usage error or -ve for ec command error
  1004. * (negative EC_RES_...)
  1005. */
  1006. static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
  1007. char * const argv[])
  1008. {
  1009. uint32_t offset, size = -1U, region_size;
  1010. unsigned long addr;
  1011. char *endp;
  1012. int region;
  1013. int ret;
  1014. region = cros_ec_decode_region(argc - 2, argv + 2);
  1015. if (region == -1)
  1016. return 1;
  1017. if (argc < 4)
  1018. return 1;
  1019. addr = simple_strtoul(argv[3], &endp, 16);
  1020. if (*argv[3] == 0 || *endp != 0)
  1021. return 1;
  1022. if (argc > 4) {
  1023. size = simple_strtoul(argv[4], &endp, 16);
  1024. if (*argv[4] == 0 || *endp != 0)
  1025. return 1;
  1026. }
  1027. ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
  1028. if (ret) {
  1029. debug("%s: Could not read region info\n", __func__);
  1030. return ret;
  1031. }
  1032. if (size == -1U)
  1033. size = region_size;
  1034. ret = is_write ?
  1035. cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
  1036. cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
  1037. if (ret) {
  1038. debug("%s: Could not %s region\n", __func__,
  1039. is_write ? "write" : "read");
  1040. return ret;
  1041. }
  1042. return 0;
  1043. }
  1044. static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1045. {
  1046. struct cros_ec_dev *dev = last_dev;
  1047. const char *cmd;
  1048. int ret = 0;
  1049. if (argc < 2)
  1050. return CMD_RET_USAGE;
  1051. cmd = argv[1];
  1052. if (0 == strcmp("init", cmd)) {
  1053. ret = cros_ec_init(gd->fdt_blob, &dev);
  1054. if (ret) {
  1055. printf("Could not init cros_ec device (err %d)\n", ret);
  1056. return 1;
  1057. }
  1058. return 0;
  1059. }
  1060. /* Just use the last allocated device; there should be only one */
  1061. if (!last_dev) {
  1062. printf("No CROS-EC device available\n");
  1063. return 1;
  1064. }
  1065. if (0 == strcmp("id", cmd)) {
  1066. char id[MSG_BYTES];
  1067. if (cros_ec_read_id(dev, id, sizeof(id))) {
  1068. debug("%s: Could not read KBC ID\n", __func__);
  1069. return 1;
  1070. }
  1071. printf("%s\n", id);
  1072. } else if (0 == strcmp("info", cmd)) {
  1073. struct ec_response_mkbp_info info;
  1074. if (cros_ec_info(dev, &info)) {
  1075. debug("%s: Could not read KBC info\n", __func__);
  1076. return 1;
  1077. }
  1078. printf("rows = %u\n", info.rows);
  1079. printf("cols = %u\n", info.cols);
  1080. printf("switches = %#x\n", info.switches);
  1081. } else if (0 == strcmp("curimage", cmd)) {
  1082. enum ec_current_image image;
  1083. if (cros_ec_read_current_image(dev, &image)) {
  1084. debug("%s: Could not read KBC image\n", __func__);
  1085. return 1;
  1086. }
  1087. printf("%d\n", image);
  1088. } else if (0 == strcmp("hash", cmd)) {
  1089. struct ec_response_vboot_hash hash;
  1090. int i;
  1091. if (cros_ec_read_hash(dev, &hash)) {
  1092. debug("%s: Could not read KBC hash\n", __func__);
  1093. return 1;
  1094. }
  1095. if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
  1096. printf("type: SHA-256\n");
  1097. else
  1098. printf("type: %d\n", hash.hash_type);
  1099. printf("offset: 0x%08x\n", hash.offset);
  1100. printf("size: 0x%08x\n", hash.size);
  1101. printf("digest: ");
  1102. for (i = 0; i < hash.digest_size; i++)
  1103. printf("%02x", hash.hash_digest[i]);
  1104. printf("\n");
  1105. } else if (0 == strcmp("reboot", cmd)) {
  1106. int region;
  1107. enum ec_reboot_cmd cmd;
  1108. if (argc >= 3 && !strcmp(argv[2], "cold"))
  1109. cmd = EC_REBOOT_COLD;
  1110. else {
  1111. region = cros_ec_decode_region(argc - 2, argv + 2);
  1112. if (region == EC_FLASH_REGION_RO)
  1113. cmd = EC_REBOOT_JUMP_RO;
  1114. else if (region == EC_FLASH_REGION_RW)
  1115. cmd = EC_REBOOT_JUMP_RW;
  1116. else
  1117. return CMD_RET_USAGE;
  1118. }
  1119. if (cros_ec_reboot(dev, cmd, 0)) {
  1120. debug("%s: Could not reboot KBC\n", __func__);
  1121. return 1;
  1122. }
  1123. } else if (0 == strcmp("events", cmd)) {
  1124. uint32_t events;
  1125. if (cros_ec_get_host_events(dev, &events)) {
  1126. debug("%s: Could not read host events\n", __func__);
  1127. return 1;
  1128. }
  1129. printf("0x%08x\n", events);
  1130. } else if (0 == strcmp("clrevents", cmd)) {
  1131. uint32_t events = 0x7fffffff;
  1132. if (argc >= 3)
  1133. events = simple_strtol(argv[2], NULL, 0);
  1134. if (cros_ec_clear_host_events(dev, events)) {
  1135. debug("%s: Could not clear host events\n", __func__);
  1136. return 1;
  1137. }
  1138. } else if (0 == strcmp("read", cmd)) {
  1139. ret = do_read_write(dev, 0, argc, argv);
  1140. if (ret > 0)
  1141. return CMD_RET_USAGE;
  1142. } else if (0 == strcmp("write", cmd)) {
  1143. ret = do_read_write(dev, 1, argc, argv);
  1144. if (ret > 0)
  1145. return CMD_RET_USAGE;
  1146. } else if (0 == strcmp("erase", cmd)) {
  1147. int region = cros_ec_decode_region(argc - 2, argv + 2);
  1148. uint32_t offset, size;
  1149. if (region == -1)
  1150. return CMD_RET_USAGE;
  1151. if (cros_ec_flash_offset(dev, region, &offset, &size)) {
  1152. debug("%s: Could not read region info\n", __func__);
  1153. ret = -1;
  1154. } else {
  1155. ret = cros_ec_flash_erase(dev, offset, size);
  1156. if (ret) {
  1157. debug("%s: Could not erase region\n",
  1158. __func__);
  1159. }
  1160. }
  1161. } else if (0 == strcmp("regioninfo", cmd)) {
  1162. int region = cros_ec_decode_region(argc - 2, argv + 2);
  1163. uint32_t offset, size;
  1164. if (region == -1)
  1165. return CMD_RET_USAGE;
  1166. ret = cros_ec_flash_offset(dev, region, &offset, &size);
  1167. if (ret) {
  1168. debug("%s: Could not read region info\n", __func__);
  1169. } else {
  1170. printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
  1171. "RO" : "RW");
  1172. printf("Offset: %x\n", offset);
  1173. printf("Size: %x\n", size);
  1174. }
  1175. } else if (0 == strcmp("vbnvcontext", cmd)) {
  1176. uint8_t block[EC_VBNV_BLOCK_SIZE];
  1177. char buf[3];
  1178. int i, len;
  1179. unsigned long result;
  1180. if (argc <= 2) {
  1181. ret = cros_ec_read_vbnvcontext(dev, block);
  1182. if (!ret) {
  1183. printf("vbnv_block: ");
  1184. for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
  1185. printf("%02x", block[i]);
  1186. putc('\n');
  1187. }
  1188. } else {
  1189. /*
  1190. * TODO(clchiou): Move this to a utility function as
  1191. * cmd_spi might want to call it.
  1192. */
  1193. memset(block, 0, EC_VBNV_BLOCK_SIZE);
  1194. len = strlen(argv[2]);
  1195. buf[2] = '\0';
  1196. for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
  1197. if (i * 2 >= len)
  1198. break;
  1199. buf[0] = argv[2][i * 2];
  1200. if (i * 2 + 1 >= len)
  1201. buf[1] = '0';
  1202. else
  1203. buf[1] = argv[2][i * 2 + 1];
  1204. strict_strtoul(buf, 16, &result);
  1205. block[i] = result;
  1206. }
  1207. ret = cros_ec_write_vbnvcontext(dev, block);
  1208. }
  1209. if (ret) {
  1210. debug("%s: Could not %s VbNvContext\n", __func__,
  1211. argc <= 2 ? "read" : "write");
  1212. }
  1213. } else if (0 == strcmp("test", cmd)) {
  1214. int result = cros_ec_test(dev);
  1215. if (result)
  1216. printf("Test failed with error %d\n", result);
  1217. else
  1218. puts("Test passed\n");
  1219. } else if (0 == strcmp("version", cmd)) {
  1220. struct ec_response_get_version *p;
  1221. char *build_string;
  1222. ret = cros_ec_read_version(dev, &p);
  1223. if (!ret) {
  1224. /* Print versions */
  1225. printf("RO version: %1.*s\n",
  1226. (int)sizeof(p->version_string_ro),
  1227. p->version_string_ro);
  1228. printf("RW version: %1.*s\n",
  1229. (int)sizeof(p->version_string_rw),
  1230. p->version_string_rw);
  1231. printf("Firmware copy: %s\n",
  1232. (p->current_image <
  1233. ARRAY_SIZE(ec_current_image_name) ?
  1234. ec_current_image_name[p->current_image] :
  1235. "?"));
  1236. ret = cros_ec_read_build_info(dev, &build_string);
  1237. if (!ret)
  1238. printf("Build info: %s\n", build_string);
  1239. }
  1240. } else if (0 == strcmp("ldo", cmd)) {
  1241. uint8_t index, state;
  1242. char *endp;
  1243. if (argc < 3)
  1244. return CMD_RET_USAGE;
  1245. index = simple_strtoul(argv[2], &endp, 10);
  1246. if (*argv[2] == 0 || *endp != 0)
  1247. return CMD_RET_USAGE;
  1248. if (argc > 3) {
  1249. state = simple_strtoul(argv[3], &endp, 10);
  1250. if (*argv[3] == 0 || *endp != 0)
  1251. return CMD_RET_USAGE;
  1252. ret = cros_ec_set_ldo(dev, index, state);
  1253. } else {
  1254. ret = cros_ec_get_ldo(dev, index, &state);
  1255. if (!ret) {
  1256. printf("LDO%d: %s\n", index,
  1257. state == EC_LDO_STATE_ON ?
  1258. "on" : "off");
  1259. }
  1260. }
  1261. if (ret) {
  1262. debug("%s: Could not access LDO%d\n", __func__, index);
  1263. return ret;
  1264. }
  1265. } else {
  1266. return CMD_RET_USAGE;
  1267. }
  1268. if (ret < 0) {
  1269. printf("Error: CROS-EC command failed (error %d)\n", ret);
  1270. ret = 1;
  1271. }
  1272. return ret;
  1273. }
  1274. U_BOOT_CMD(
  1275. crosec, 5, 1, do_cros_ec,
  1276. "CROS-EC utility command",
  1277. "init Re-init CROS-EC (done on startup automatically)\n"
  1278. "crosec id Read CROS-EC ID\n"
  1279. "crosec info Read CROS-EC info\n"
  1280. "crosec curimage Read CROS-EC current image\n"
  1281. "crosec hash Read CROS-EC hash\n"
  1282. "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
  1283. "crosec events Read CROS-EC host events\n"
  1284. "crosec clrevents [mask] Clear CROS-EC host events\n"
  1285. "crosec regioninfo <ro|rw> Read image info\n"
  1286. "crosec erase <ro|rw> Erase EC image\n"
  1287. "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
  1288. "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
  1289. "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
  1290. "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
  1291. "crosec test run tests on cros_ec\n"
  1292. "crosec version Read CROS-EC version"
  1293. );
  1294. #endif