cros_ec.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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. #ifdef CONFIG_CROS_EC_SANDBOX
  192. case CROS_EC_IF_SANDBOX:
  193. rv = cros_ec_sandbox_packet(dev, out_bytes, in_bytes);
  194. break;
  195. #endif
  196. case CROS_EC_IF_NONE:
  197. /* TODO: support protocol 3 for LPC, I2C; for now fall through */
  198. default:
  199. debug("%s: Unsupported interface\n", __func__);
  200. rv = -1;
  201. }
  202. if (rv < 0)
  203. return rv;
  204. /* Process the response */
  205. return handle_proto3_response(dev, dinp, din_len);
  206. }
  207. static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  208. const void *dout, int dout_len,
  209. uint8_t **dinp, int din_len)
  210. {
  211. int ret = -1;
  212. /* Handle protocol version 3 support */
  213. if (dev->protocol_version == 3) {
  214. return send_command_proto3(dev, cmd, cmd_version,
  215. dout, dout_len, dinp, din_len);
  216. }
  217. switch (dev->interface) {
  218. #ifdef CONFIG_CROS_EC_SPI
  219. case CROS_EC_IF_SPI:
  220. ret = cros_ec_spi_command(dev, cmd, cmd_version,
  221. (const uint8_t *)dout, dout_len,
  222. dinp, din_len);
  223. break;
  224. #endif
  225. #ifdef CONFIG_CROS_EC_I2C
  226. case CROS_EC_IF_I2C:
  227. ret = cros_ec_i2c_command(dev, cmd, cmd_version,
  228. (const uint8_t *)dout, dout_len,
  229. dinp, din_len);
  230. break;
  231. #endif
  232. #ifdef CONFIG_CROS_EC_LPC
  233. case CROS_EC_IF_LPC:
  234. ret = cros_ec_lpc_command(dev, cmd, cmd_version,
  235. (const uint8_t *)dout, dout_len,
  236. dinp, din_len);
  237. break;
  238. #endif
  239. case CROS_EC_IF_NONE:
  240. default:
  241. ret = -1;
  242. }
  243. return ret;
  244. }
  245. /**
  246. * Send a command to the CROS-EC device and return the reply.
  247. *
  248. * The device's internal input/output buffers are used.
  249. *
  250. * @param dev CROS-EC device
  251. * @param cmd Command to send (EC_CMD_...)
  252. * @param cmd_version Version of command to send (EC_VER_...)
  253. * @param dout Output data (may be NULL If dout_len=0)
  254. * @param dout_len Size of output data in bytes
  255. * @param dinp Response data (may be NULL If din_len=0).
  256. * If not NULL, it will be updated to point to the data
  257. * and will always be double word aligned (64-bits)
  258. * @param din_len Maximum size of response in bytes
  259. * @return number of bytes in response, or -1 on error
  260. */
  261. static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
  262. int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
  263. int din_len)
  264. {
  265. uint8_t *din = NULL;
  266. int len;
  267. len = send_command(dev, cmd, cmd_version, dout, dout_len,
  268. &din, din_len);
  269. /* If the command doesn't complete, wait a while */
  270. if (len == -EC_RES_IN_PROGRESS) {
  271. struct ec_response_get_comms_status *resp = NULL;
  272. ulong start;
  273. /* Wait for command to complete */
  274. start = get_timer(0);
  275. do {
  276. int ret;
  277. mdelay(50); /* Insert some reasonable delay */
  278. ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
  279. NULL, 0,
  280. (uint8_t **)&resp, sizeof(*resp));
  281. if (ret < 0)
  282. return ret;
  283. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  284. debug("%s: Command %#02x timeout\n",
  285. __func__, cmd);
  286. return -EC_RES_TIMEOUT;
  287. }
  288. } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
  289. /* OK it completed, so read the status response */
  290. /* not sure why it was 0 for the last argument */
  291. len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
  292. NULL, 0, &din, din_len);
  293. }
  294. debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
  295. dinp ? *dinp : NULL);
  296. if (dinp) {
  297. /* If we have any data to return, it must be 64bit-aligned */
  298. assert(len <= 0 || !((uintptr_t)din & 7));
  299. *dinp = din;
  300. }
  301. return len;
  302. }
  303. /**
  304. * Send a command to the CROS-EC device and return the reply.
  305. *
  306. * The device's internal input/output buffers are used.
  307. *
  308. * @param dev CROS-EC device
  309. * @param cmd Command to send (EC_CMD_...)
  310. * @param cmd_version Version of command to send (EC_VER_...)
  311. * @param dout Output data (may be NULL If dout_len=0)
  312. * @param dout_len Size of output data in bytes
  313. * @param din Response data (may be NULL If din_len=0).
  314. * It not NULL, it is a place for ec_command() to copy the
  315. * data to.
  316. * @param din_len Maximum size of response in bytes
  317. * @return number of bytes in response, or -1 on error
  318. */
  319. static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  320. const void *dout, int dout_len,
  321. void *din, int din_len)
  322. {
  323. uint8_t *in_buffer;
  324. int len;
  325. assert((din_len == 0) || din);
  326. len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
  327. &in_buffer, din_len);
  328. if (len > 0) {
  329. /*
  330. * If we were asked to put it somewhere, do so, otherwise just
  331. * disregard the result.
  332. */
  333. if (din && in_buffer) {
  334. assert(len <= din_len);
  335. memmove(din, in_buffer, len);
  336. }
  337. }
  338. return len;
  339. }
  340. int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
  341. {
  342. if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
  343. sizeof(scan->data)) != sizeof(scan->data))
  344. return -1;
  345. return 0;
  346. }
  347. int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
  348. {
  349. struct ec_response_get_version *r;
  350. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  351. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  352. return -1;
  353. if (maxlen > (int)sizeof(r->version_string_ro))
  354. maxlen = sizeof(r->version_string_ro);
  355. switch (r->current_image) {
  356. case EC_IMAGE_RO:
  357. memcpy(id, r->version_string_ro, maxlen);
  358. break;
  359. case EC_IMAGE_RW:
  360. memcpy(id, r->version_string_rw, maxlen);
  361. break;
  362. default:
  363. return -1;
  364. }
  365. id[maxlen - 1] = '\0';
  366. return 0;
  367. }
  368. int cros_ec_read_version(struct cros_ec_dev *dev,
  369. struct ec_response_get_version **versionp)
  370. {
  371. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  372. (uint8_t **)versionp, sizeof(**versionp))
  373. != sizeof(**versionp))
  374. return -1;
  375. return 0;
  376. }
  377. int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
  378. {
  379. if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
  380. (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
  381. return -1;
  382. return 0;
  383. }
  384. int cros_ec_read_current_image(struct cros_ec_dev *dev,
  385. enum ec_current_image *image)
  386. {
  387. struct ec_response_get_version *r;
  388. if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
  389. (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
  390. return -1;
  391. *image = r->current_image;
  392. return 0;
  393. }
  394. static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
  395. struct ec_response_vboot_hash *hash)
  396. {
  397. struct ec_params_vboot_hash p;
  398. ulong start;
  399. start = get_timer(0);
  400. while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
  401. mdelay(50); /* Insert some reasonable delay */
  402. p.cmd = EC_VBOOT_HASH_GET;
  403. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  404. hash, sizeof(*hash)) < 0)
  405. return -1;
  406. if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
  407. debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
  408. return -EC_RES_TIMEOUT;
  409. }
  410. }
  411. return 0;
  412. }
  413. int cros_ec_read_hash(struct cros_ec_dev *dev,
  414. struct ec_response_vboot_hash *hash)
  415. {
  416. struct ec_params_vboot_hash p;
  417. int rv;
  418. p.cmd = EC_VBOOT_HASH_GET;
  419. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  420. hash, sizeof(*hash)) < 0)
  421. return -1;
  422. /* If the EC is busy calculating the hash, fidget until it's done. */
  423. rv = cros_ec_wait_on_hash_done(dev, hash);
  424. if (rv)
  425. return rv;
  426. /* If the hash is valid, we're done. Otherwise, we have to kick it off
  427. * again and wait for it to complete. Note that we explicitly assume
  428. * that hashing zero bytes is always wrong, even though that would
  429. * produce a valid hash value. */
  430. if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
  431. return 0;
  432. debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
  433. __func__, hash->status, hash->size);
  434. p.cmd = EC_VBOOT_HASH_START;
  435. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  436. p.nonce_size = 0;
  437. p.offset = EC_VBOOT_HASH_OFFSET_RW;
  438. if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  439. hash, sizeof(*hash)) < 0)
  440. return -1;
  441. rv = cros_ec_wait_on_hash_done(dev, hash);
  442. if (rv)
  443. return rv;
  444. debug("%s: hash done\n", __func__);
  445. return 0;
  446. }
  447. static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
  448. {
  449. struct ec_params_vboot_hash p;
  450. struct ec_response_vboot_hash *hash;
  451. /* We don't have an explict command for the EC to discard its current
  452. * hash value, so we'll just tell it to calculate one that we know is
  453. * wrong (we claim that hashing zero bytes is always invalid).
  454. */
  455. p.cmd = EC_VBOOT_HASH_RECALC;
  456. p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
  457. p.nonce_size = 0;
  458. p.offset = 0;
  459. p.size = 0;
  460. debug("%s:\n", __func__);
  461. if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
  462. (uint8_t **)&hash, sizeof(*hash)) < 0)
  463. return -1;
  464. /* No need to wait for it to finish */
  465. return 0;
  466. }
  467. int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
  468. uint8_t flags)
  469. {
  470. struct ec_params_reboot_ec p;
  471. p.cmd = cmd;
  472. p.flags = flags;
  473. if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
  474. < 0)
  475. return -1;
  476. if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
  477. /*
  478. * EC reboot will take place immediately so delay to allow it
  479. * to complete. Note that some reboot types (EC_REBOOT_COLD)
  480. * will reboot the AP as well, in which case we won't actually
  481. * get to this point.
  482. */
  483. /*
  484. * TODO(rspangler@chromium.org): Would be nice if we had a
  485. * better way to determine when the reboot is complete. Could
  486. * we poll a memory-mapped LPC value?
  487. */
  488. udelay(50000);
  489. }
  490. return 0;
  491. }
  492. int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
  493. {
  494. /* no interrupt support : always poll */
  495. if (!fdt_gpio_isvalid(&dev->ec_int))
  496. return -ENOENT;
  497. return !gpio_get_value(dev->ec_int.gpio);
  498. }
  499. int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
  500. {
  501. if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
  502. sizeof(*info)) != sizeof(*info))
  503. return -1;
  504. return 0;
  505. }
  506. int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
  507. {
  508. struct ec_response_host_event_mask *resp;
  509. /*
  510. * Use the B copy of the event flags, because the main copy is already
  511. * used by ACPI/SMI.
  512. */
  513. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
  514. (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
  515. return -1;
  516. if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
  517. return -1;
  518. *events_ptr = resp->mask;
  519. return 0;
  520. }
  521. int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
  522. {
  523. struct ec_params_host_event_mask params;
  524. params.mask = events;
  525. /*
  526. * Use the B copy of the event flags, so it affects the data returned
  527. * by cros_ec_get_host_events().
  528. */
  529. if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
  530. &params, sizeof(params), NULL, 0) < 0)
  531. return -1;
  532. return 0;
  533. }
  534. int cros_ec_flash_protect(struct cros_ec_dev *dev,
  535. uint32_t set_mask, uint32_t set_flags,
  536. struct ec_response_flash_protect *resp)
  537. {
  538. struct ec_params_flash_protect params;
  539. params.mask = set_mask;
  540. params.flags = set_flags;
  541. if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
  542. &params, sizeof(params),
  543. resp, sizeof(*resp)) != sizeof(*resp))
  544. return -1;
  545. return 0;
  546. }
  547. static int cros_ec_check_version(struct cros_ec_dev *dev)
  548. {
  549. struct ec_params_hello req;
  550. struct ec_response_hello *resp;
  551. #ifdef CONFIG_CROS_EC_LPC
  552. /* LPC has its own way of doing this */
  553. if (dev->interface == CROS_EC_IF_LPC)
  554. return cros_ec_lpc_check_version(dev);
  555. #endif
  556. /*
  557. * TODO(sjg@chromium.org).
  558. * There is a strange oddity here with the EC. We could just ignore
  559. * the response, i.e. pass the last two parameters as NULL and 0.
  560. * In this case we won't read back very many bytes from the EC.
  561. * On the I2C bus the EC gets upset about this and will try to send
  562. * the bytes anyway. This means that we will have to wait for that
  563. * to complete before continuing with a new EC command.
  564. *
  565. * This problem is probably unique to the I2C bus.
  566. *
  567. * So for now, just read all the data anyway.
  568. */
  569. /* Try sending a version 3 packet */
  570. dev->protocol_version = 3;
  571. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  572. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  573. return 0;
  574. }
  575. /* Try sending a version 2 packet */
  576. dev->protocol_version = 2;
  577. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  578. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  579. return 0;
  580. }
  581. /*
  582. * Fail if we're still here, since the EC doesn't understand any
  583. * protcol version we speak. Version 1 interface without command
  584. * version is no longer supported, and we don't know about any new
  585. * protocol versions.
  586. */
  587. dev->protocol_version = 0;
  588. printf("%s: ERROR: old EC interface not supported\n", __func__);
  589. return -1;
  590. }
  591. int cros_ec_test(struct cros_ec_dev *dev)
  592. {
  593. struct ec_params_hello req;
  594. struct ec_response_hello *resp;
  595. req.in_data = 0x12345678;
  596. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  597. (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
  598. printf("ec_command_inptr() returned error\n");
  599. return -1;
  600. }
  601. if (resp->out_data != req.in_data + 0x01020304) {
  602. printf("Received invalid handshake %x\n", resp->out_data);
  603. return -1;
  604. }
  605. return 0;
  606. }
  607. int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
  608. uint32_t *offset, uint32_t *size)
  609. {
  610. struct ec_params_flash_region_info p;
  611. struct ec_response_flash_region_info *r;
  612. int ret;
  613. p.region = region;
  614. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  615. EC_VER_FLASH_REGION_INFO,
  616. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  617. if (ret != sizeof(*r))
  618. return -1;
  619. if (offset)
  620. *offset = r->offset;
  621. if (size)
  622. *size = r->size;
  623. return 0;
  624. }
  625. int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
  626. {
  627. struct ec_params_flash_erase p;
  628. p.offset = offset;
  629. p.size = size;
  630. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  631. NULL, 0);
  632. }
  633. /**
  634. * Write a single block to the flash
  635. *
  636. * Write a block of data to the EC flash. The size must not exceed the flash
  637. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  638. *
  639. * The offset starts at 0. You can obtain the region information from
  640. * cros_ec_flash_offset() to find out where to write for a particular region.
  641. *
  642. * Attempting to write to the region where the EC is currently running from
  643. * will result in an error.
  644. *
  645. * @param dev CROS-EC device
  646. * @param data Pointer to data buffer to write
  647. * @param offset Offset within flash to write to.
  648. * @param size Number of bytes to write
  649. * @return 0 if ok, -1 on error
  650. */
  651. static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
  652. const uint8_t *data, uint32_t offset, uint32_t size)
  653. {
  654. struct ec_params_flash_write p;
  655. p.offset = offset;
  656. p.size = size;
  657. assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
  658. memcpy(&p + 1, data, p.size);
  659. return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  660. &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
  661. }
  662. /**
  663. * Return optimal flash write burst size
  664. */
  665. static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
  666. {
  667. return EC_FLASH_WRITE_VER0_SIZE;
  668. }
  669. /**
  670. * Check if a block of data is erased (all 0xff)
  671. *
  672. * This function is useful when dealing with flash, for checking whether a
  673. * data block is erased and thus does not need to be programmed.
  674. *
  675. * @param data Pointer to data to check (must be word-aligned)
  676. * @param size Number of bytes to check (must be word-aligned)
  677. * @return 0 if erased, non-zero if any word is not erased
  678. */
  679. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  680. {
  681. assert(!(size & 3));
  682. size /= sizeof(uint32_t);
  683. for (; size > 0; size -= 4, data++)
  684. if (*data != -1U)
  685. return 0;
  686. return 1;
  687. }
  688. int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  689. uint32_t offset, uint32_t size)
  690. {
  691. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  692. uint32_t end, off;
  693. int ret;
  694. /*
  695. * TODO: round up to the nearest multiple of write size. Can get away
  696. * without that on link right now because its write size is 4 bytes.
  697. */
  698. end = offset + size;
  699. for (off = offset; off < end; off += burst, data += burst) {
  700. uint32_t todo;
  701. /* If the data is empty, there is no point in programming it */
  702. todo = min(end - off, burst);
  703. if (dev->optimise_flash_write &&
  704. cros_ec_data_is_erased((uint32_t *)data, todo))
  705. continue;
  706. ret = cros_ec_flash_write_block(dev, data, off, todo);
  707. if (ret)
  708. return ret;
  709. }
  710. return 0;
  711. }
  712. /**
  713. * Read a single block from the flash
  714. *
  715. * Read a block of data from the EC flash. The size must not exceed the flash
  716. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  717. *
  718. * The offset starts at 0. You can obtain the region information from
  719. * cros_ec_flash_offset() to find out where to read for a particular region.
  720. *
  721. * @param dev CROS-EC device
  722. * @param data Pointer to data buffer to read into
  723. * @param offset Offset within flash to read from
  724. * @param size Number of bytes to read
  725. * @return 0 if ok, -1 on error
  726. */
  727. static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
  728. uint32_t offset, uint32_t size)
  729. {
  730. struct ec_params_flash_read p;
  731. p.offset = offset;
  732. p.size = size;
  733. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  734. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  735. }
  736. int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
  737. uint32_t size)
  738. {
  739. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  740. uint32_t end, off;
  741. int ret;
  742. end = offset + size;
  743. for (off = offset; off < end; off += burst, data += burst) {
  744. ret = cros_ec_flash_read_block(dev, data, off,
  745. min(end - off, burst));
  746. if (ret)
  747. return ret;
  748. }
  749. return 0;
  750. }
  751. int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  752. const uint8_t *image, int image_size)
  753. {
  754. uint32_t rw_offset, rw_size;
  755. int ret;
  756. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
  757. return -1;
  758. if (image_size > (int)rw_size)
  759. return -1;
  760. /* Invalidate the existing hash, just in case the AP reboots
  761. * unexpectedly during the update. If that happened, the EC RW firmware
  762. * would be invalid, but the EC would still have the original hash.
  763. */
  764. ret = cros_ec_invalidate_hash(dev);
  765. if (ret)
  766. return ret;
  767. /*
  768. * Erase the entire RW section, so that the EC doesn't see any garbage
  769. * past the new image if it's smaller than the current image.
  770. *
  771. * TODO: could optimize this to erase just the current image, since
  772. * presumably everything past that is 0xff's. But would still need to
  773. * round up to the nearest multiple of erase size.
  774. */
  775. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  776. if (ret)
  777. return ret;
  778. /* Write the image */
  779. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  780. if (ret)
  781. return ret;
  782. return 0;
  783. }
  784. int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
  785. {
  786. struct ec_params_vbnvcontext p;
  787. int len;
  788. p.op = EC_VBNV_CONTEXT_OP_READ;
  789. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  790. &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
  791. if (len < EC_VBNV_BLOCK_SIZE)
  792. return -1;
  793. return 0;
  794. }
  795. int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
  796. {
  797. struct ec_params_vbnvcontext p;
  798. int len;
  799. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  800. memcpy(p.block, block, sizeof(p.block));
  801. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  802. &p, sizeof(p), NULL, 0);
  803. if (len < 0)
  804. return -1;
  805. return 0;
  806. }
  807. int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
  808. {
  809. struct ec_params_ldo_set params;
  810. params.index = index;
  811. params.state = state;
  812. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
  813. &params, sizeof(params),
  814. NULL, 0))
  815. return -1;
  816. return 0;
  817. }
  818. int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
  819. {
  820. struct ec_params_ldo_get params;
  821. struct ec_response_ldo_get *resp;
  822. params.index = index;
  823. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
  824. &params, sizeof(params),
  825. (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
  826. return -1;
  827. *state = resp->state;
  828. return 0;
  829. }
  830. /**
  831. * Decode EC interface details from the device tree and allocate a suitable
  832. * device.
  833. *
  834. * @param blob Device tree blob
  835. * @param node Node to decode from
  836. * @param devp Returns a pointer to the new allocated device
  837. * @return 0 if ok, -1 on error
  838. */
  839. static int cros_ec_decode_fdt(const void *blob, int node,
  840. struct cros_ec_dev **devp)
  841. {
  842. enum fdt_compat_id compat;
  843. struct cros_ec_dev *dev;
  844. int parent;
  845. /* See what type of parent we are inside (this is expensive) */
  846. parent = fdt_parent_offset(blob, node);
  847. if (parent < 0) {
  848. debug("%s: Cannot find node parent\n", __func__);
  849. return -1;
  850. }
  851. dev = &static_dev;
  852. dev->node = node;
  853. dev->parent_node = parent;
  854. compat = fdtdec_lookup(blob, parent);
  855. switch (compat) {
  856. #ifdef CONFIG_CROS_EC_SPI
  857. case COMPAT_SAMSUNG_EXYNOS_SPI:
  858. dev->interface = CROS_EC_IF_SPI;
  859. if (cros_ec_spi_decode_fdt(dev, blob))
  860. return -1;
  861. break;
  862. #endif
  863. #ifdef CONFIG_CROS_EC_I2C
  864. case COMPAT_SAMSUNG_S3C2440_I2C:
  865. dev->interface = CROS_EC_IF_I2C;
  866. if (cros_ec_i2c_decode_fdt(dev, blob))
  867. return -1;
  868. break;
  869. #endif
  870. #ifdef CONFIG_CROS_EC_LPC
  871. case COMPAT_INTEL_LPC:
  872. dev->interface = CROS_EC_IF_LPC;
  873. break;
  874. #endif
  875. #ifdef CONFIG_CROS_EC_SANDBOX
  876. case COMPAT_SANDBOX_HOST_EMULATION:
  877. dev->interface = CROS_EC_IF_SANDBOX;
  878. break;
  879. #endif
  880. default:
  881. debug("%s: Unknown compat id %d\n", __func__, compat);
  882. return -1;
  883. }
  884. fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
  885. dev->optimise_flash_write = fdtdec_get_bool(blob, node,
  886. "optimise-flash-write");
  887. *devp = dev;
  888. return 0;
  889. }
  890. int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
  891. {
  892. char id[MSG_BYTES];
  893. struct cros_ec_dev *dev;
  894. int node = 0;
  895. *cros_ecp = NULL;
  896. do {
  897. node = fdtdec_next_compatible(blob, node,
  898. COMPAT_GOOGLE_CROS_EC);
  899. if (node < 0) {
  900. debug("%s: Node not found\n", __func__);
  901. return 0;
  902. }
  903. } while (!fdtdec_get_is_enabled(blob, node));
  904. if (cros_ec_decode_fdt(blob, node, &dev)) {
  905. debug("%s: Failed to decode device.\n", __func__);
  906. return -CROS_EC_ERR_FDT_DECODE;
  907. }
  908. switch (dev->interface) {
  909. #ifdef CONFIG_CROS_EC_SPI
  910. case CROS_EC_IF_SPI:
  911. if (cros_ec_spi_init(dev, blob)) {
  912. debug("%s: Could not setup SPI interface\n", __func__);
  913. return -CROS_EC_ERR_DEV_INIT;
  914. }
  915. break;
  916. #endif
  917. #ifdef CONFIG_CROS_EC_I2C
  918. case CROS_EC_IF_I2C:
  919. if (cros_ec_i2c_init(dev, blob))
  920. return -CROS_EC_ERR_DEV_INIT;
  921. break;
  922. #endif
  923. #ifdef CONFIG_CROS_EC_LPC
  924. case CROS_EC_IF_LPC:
  925. if (cros_ec_lpc_init(dev, blob))
  926. return -CROS_EC_ERR_DEV_INIT;
  927. break;
  928. #endif
  929. #ifdef CONFIG_CROS_EC_SANDBOX
  930. case CROS_EC_IF_SANDBOX:
  931. if (cros_ec_sandbox_init(dev, blob))
  932. return -CROS_EC_ERR_DEV_INIT;
  933. break;
  934. #endif
  935. case CROS_EC_IF_NONE:
  936. default:
  937. return 0;
  938. }
  939. /* we will poll the EC interrupt line */
  940. fdtdec_setup_gpio(&dev->ec_int);
  941. if (fdt_gpio_isvalid(&dev->ec_int))
  942. gpio_direction_input(dev->ec_int.gpio);
  943. if (cros_ec_check_version(dev)) {
  944. debug("%s: Could not detect CROS-EC version\n", __func__);
  945. return -CROS_EC_ERR_CHECK_VERSION;
  946. }
  947. if (cros_ec_read_id(dev, id, sizeof(id))) {
  948. debug("%s: Could not read KBC ID\n", __func__);
  949. return -CROS_EC_ERR_READ_ID;
  950. }
  951. /* Remember this device for use by the cros_ec command */
  952. last_dev = *cros_ecp = dev;
  953. debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
  954. return 0;
  955. }
  956. int cros_ec_decode_region(int argc, char * const argv[])
  957. {
  958. if (argc > 0) {
  959. if (0 == strcmp(*argv, "rw"))
  960. return EC_FLASH_REGION_RW;
  961. else if (0 == strcmp(*argv, "ro"))
  962. return EC_FLASH_REGION_RO;
  963. debug("%s: Invalid region '%s'\n", __func__, *argv);
  964. } else {
  965. debug("%s: Missing region parameter\n", __func__);
  966. }
  967. return -1;
  968. }
  969. int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
  970. {
  971. int flash_node, node;
  972. node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
  973. if (node < 0) {
  974. debug("Failed to find chrome-ec node'\n");
  975. return -1;
  976. }
  977. flash_node = fdt_subnode_offset(blob, node, "flash");
  978. if (flash_node < 0) {
  979. debug("Failed to find flash node\n");
  980. return -1;
  981. }
  982. if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
  983. &config->flash)) {
  984. debug("Failed to decode flash node in chrome-ec'\n");
  985. return -1;
  986. }
  987. config->flash_erase_value = fdtdec_get_int(blob, flash_node,
  988. "erase-value", -1);
  989. for (node = fdt_first_subnode(blob, flash_node); node >= 0;
  990. node = fdt_next_subnode(blob, node)) {
  991. const char *name = fdt_get_name(blob, node, NULL);
  992. enum ec_flash_region region;
  993. if (0 == strcmp(name, "ro")) {
  994. region = EC_FLASH_REGION_RO;
  995. } else if (0 == strcmp(name, "rw")) {
  996. region = EC_FLASH_REGION_RW;
  997. } else if (0 == strcmp(name, "wp-ro")) {
  998. region = EC_FLASH_REGION_WP_RO;
  999. } else {
  1000. debug("Unknown EC flash region name '%s'\n", name);
  1001. return -1;
  1002. }
  1003. if (fdtdec_read_fmap_entry(blob, node, "reg",
  1004. &config->region[region])) {
  1005. debug("Failed to decode flash region in chrome-ec'\n");
  1006. return -1;
  1007. }
  1008. }
  1009. return 0;
  1010. }
  1011. int cros_ec_i2c_xfer(struct cros_ec_dev *dev, uchar chip, uint addr,
  1012. int alen, uchar *buffer, int len, int is_read)
  1013. {
  1014. union {
  1015. struct ec_params_i2c_passthru p;
  1016. uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1017. } params;
  1018. union {
  1019. struct ec_response_i2c_passthru r;
  1020. uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1021. } response;
  1022. struct ec_params_i2c_passthru *p = &params.p;
  1023. struct ec_response_i2c_passthru *r = &response.r;
  1024. struct ec_params_i2c_passthru_msg *msg = p->msg;
  1025. uint8_t *pdata;
  1026. int read_len, write_len;
  1027. int size;
  1028. int rv;
  1029. p->port = 0;
  1030. if (alen != 1) {
  1031. printf("Unsupported address length %d\n", alen);
  1032. return -1;
  1033. }
  1034. if (is_read) {
  1035. read_len = len;
  1036. write_len = alen;
  1037. p->num_msgs = 2;
  1038. } else {
  1039. read_len = 0;
  1040. write_len = alen + len;
  1041. p->num_msgs = 1;
  1042. }
  1043. size = sizeof(*p) + p->num_msgs * sizeof(*msg);
  1044. if (size + write_len > sizeof(params)) {
  1045. puts("Params too large for buffer\n");
  1046. return -1;
  1047. }
  1048. if (sizeof(*r) + read_len > sizeof(response)) {
  1049. puts("Read length too big for buffer\n");
  1050. return -1;
  1051. }
  1052. /* Create a message to write the register address and optional data */
  1053. pdata = (uint8_t *)p + size;
  1054. msg->addr_flags = chip;
  1055. msg->len = write_len;
  1056. pdata[0] = addr;
  1057. if (!is_read)
  1058. memcpy(pdata + 1, buffer, len);
  1059. msg++;
  1060. if (read_len) {
  1061. msg->addr_flags = chip | EC_I2C_FLAG_READ;
  1062. msg->len = read_len;
  1063. }
  1064. rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, size + write_len,
  1065. r, sizeof(*r) + read_len);
  1066. if (rv < 0)
  1067. return rv;
  1068. /* Parse response */
  1069. if (r->i2c_status & EC_I2C_STATUS_ERROR) {
  1070. printf("Transfer failed with status=0x%x\n", r->i2c_status);
  1071. return -1;
  1072. }
  1073. if (rv < sizeof(*r) + read_len) {
  1074. puts("Truncated read response\n");
  1075. return -1;
  1076. }
  1077. if (read_len)
  1078. memcpy(buffer, r->data, read_len);
  1079. return 0;
  1080. }
  1081. #ifdef CONFIG_CMD_CROS_EC
  1082. /**
  1083. * Perform a flash read or write command
  1084. *
  1085. * @param dev CROS-EC device to read/write
  1086. * @param is_write 1 do to a write, 0 to do a read
  1087. * @param argc Number of arguments
  1088. * @param argv Arguments (2 is region, 3 is address)
  1089. * @return 0 for ok, 1 for a usage error or -ve for ec command error
  1090. * (negative EC_RES_...)
  1091. */
  1092. static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
  1093. char * const argv[])
  1094. {
  1095. uint32_t offset, size = -1U, region_size;
  1096. unsigned long addr;
  1097. char *endp;
  1098. int region;
  1099. int ret;
  1100. region = cros_ec_decode_region(argc - 2, argv + 2);
  1101. if (region == -1)
  1102. return 1;
  1103. if (argc < 4)
  1104. return 1;
  1105. addr = simple_strtoul(argv[3], &endp, 16);
  1106. if (*argv[3] == 0 || *endp != 0)
  1107. return 1;
  1108. if (argc > 4) {
  1109. size = simple_strtoul(argv[4], &endp, 16);
  1110. if (*argv[4] == 0 || *endp != 0)
  1111. return 1;
  1112. }
  1113. ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
  1114. if (ret) {
  1115. debug("%s: Could not read region info\n", __func__);
  1116. return ret;
  1117. }
  1118. if (size == -1U)
  1119. size = region_size;
  1120. ret = is_write ?
  1121. cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
  1122. cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
  1123. if (ret) {
  1124. debug("%s: Could not %s region\n", __func__,
  1125. is_write ? "write" : "read");
  1126. return ret;
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * get_alen() - Small parser helper function to get address length
  1132. *
  1133. * Returns the address length.
  1134. */
  1135. static uint get_alen(char *arg)
  1136. {
  1137. int j;
  1138. int alen;
  1139. alen = 1;
  1140. for (j = 0; j < 8; j++) {
  1141. if (arg[j] == '.') {
  1142. alen = arg[j+1] - '0';
  1143. break;
  1144. } else if (arg[j] == '\0') {
  1145. break;
  1146. }
  1147. }
  1148. return alen;
  1149. }
  1150. #define DISP_LINE_LEN 16
  1151. /*
  1152. * TODO(sjg@chromium.org): This code copied almost verbatim from cmd_i2c.c
  1153. * so we can remove it later.
  1154. */
  1155. static int cros_ec_i2c_md(struct cros_ec_dev *dev, int flag, int argc,
  1156. char * const argv[])
  1157. {
  1158. u_char chip;
  1159. uint addr, alen, length = 0x10;
  1160. int j, nbytes, linebytes;
  1161. if (argc < 2)
  1162. return CMD_RET_USAGE;
  1163. if (1 || (flag & CMD_FLAG_REPEAT) == 0) {
  1164. /*
  1165. * New command specified.
  1166. */
  1167. /*
  1168. * I2C chip address
  1169. */
  1170. chip = simple_strtoul(argv[0], NULL, 16);
  1171. /*
  1172. * I2C data address within the chip. This can be 1 or
  1173. * 2 bytes long. Some day it might be 3 bytes long :-).
  1174. */
  1175. addr = simple_strtoul(argv[1], NULL, 16);
  1176. alen = get_alen(argv[1]);
  1177. if (alen > 3)
  1178. return CMD_RET_USAGE;
  1179. /*
  1180. * If another parameter, it is the length to display.
  1181. * Length is the number of objects, not number of bytes.
  1182. */
  1183. if (argc > 2)
  1184. length = simple_strtoul(argv[2], NULL, 16);
  1185. }
  1186. /*
  1187. * Print the lines.
  1188. *
  1189. * We buffer all read data, so we can make sure data is read only
  1190. * once.
  1191. */
  1192. nbytes = length;
  1193. do {
  1194. unsigned char linebuf[DISP_LINE_LEN];
  1195. unsigned char *cp;
  1196. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  1197. if (cros_ec_i2c_xfer(dev, chip, addr, alen, linebuf, linebytes,
  1198. 1))
  1199. puts("Error reading the chip.\n");
  1200. else {
  1201. printf("%04x:", addr);
  1202. cp = linebuf;
  1203. for (j = 0; j < linebytes; j++) {
  1204. printf(" %02x", *cp++);
  1205. addr++;
  1206. }
  1207. puts(" ");
  1208. cp = linebuf;
  1209. for (j = 0; j < linebytes; j++) {
  1210. if ((*cp < 0x20) || (*cp > 0x7e))
  1211. puts(".");
  1212. else
  1213. printf("%c", *cp);
  1214. cp++;
  1215. }
  1216. putc('\n');
  1217. }
  1218. nbytes -= linebytes;
  1219. } while (nbytes > 0);
  1220. return 0;
  1221. }
  1222. static int cros_ec_i2c_mw(struct cros_ec_dev *dev, int flag, int argc,
  1223. char * const argv[])
  1224. {
  1225. uchar chip;
  1226. ulong addr;
  1227. uint alen;
  1228. uchar byte;
  1229. int count;
  1230. if ((argc < 3) || (argc > 4))
  1231. return CMD_RET_USAGE;
  1232. /*
  1233. * Chip is always specified.
  1234. */
  1235. chip = simple_strtoul(argv[0], NULL, 16);
  1236. /*
  1237. * Address is always specified.
  1238. */
  1239. addr = simple_strtoul(argv[1], NULL, 16);
  1240. alen = get_alen(argv[1]);
  1241. if (alen > 3)
  1242. return CMD_RET_USAGE;
  1243. /*
  1244. * Value to write is always specified.
  1245. */
  1246. byte = simple_strtoul(argv[2], NULL, 16);
  1247. /*
  1248. * Optional count
  1249. */
  1250. if (argc == 4)
  1251. count = simple_strtoul(argv[3], NULL, 16);
  1252. else
  1253. count = 1;
  1254. while (count-- > 0) {
  1255. if (cros_ec_i2c_xfer(dev, chip, addr++, alen, &byte, 1, 0))
  1256. puts("Error writing the chip.\n");
  1257. /*
  1258. * Wait for the write to complete. The write can take
  1259. * up to 10mSec (we allow a little more time).
  1260. */
  1261. /*
  1262. * No write delay with FRAM devices.
  1263. */
  1264. #if !defined(CONFIG_SYS_I2C_FRAM)
  1265. udelay(11000);
  1266. #endif
  1267. }
  1268. return 0;
  1269. }
  1270. /* Temporary code until we have driver model and can use the i2c command */
  1271. static int cros_ec_i2c_passthrough(struct cros_ec_dev *dev, int flag,
  1272. int argc, char * const argv[])
  1273. {
  1274. const char *cmd;
  1275. if (argc < 1)
  1276. return CMD_RET_USAGE;
  1277. cmd = *argv++;
  1278. argc--;
  1279. if (0 == strcmp("md", cmd))
  1280. cros_ec_i2c_md(dev, flag, argc, argv);
  1281. else if (0 == strcmp("mw", cmd))
  1282. cros_ec_i2c_mw(dev, flag, argc, argv);
  1283. else
  1284. return CMD_RET_USAGE;
  1285. return 0;
  1286. }
  1287. static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1288. {
  1289. struct cros_ec_dev *dev = last_dev;
  1290. const char *cmd;
  1291. int ret = 0;
  1292. if (argc < 2)
  1293. return CMD_RET_USAGE;
  1294. cmd = argv[1];
  1295. if (0 == strcmp("init", cmd)) {
  1296. ret = cros_ec_init(gd->fdt_blob, &dev);
  1297. if (ret) {
  1298. printf("Could not init cros_ec device (err %d)\n", ret);
  1299. return 1;
  1300. }
  1301. return 0;
  1302. }
  1303. /* Just use the last allocated device; there should be only one */
  1304. if (!last_dev) {
  1305. printf("No CROS-EC device available\n");
  1306. return 1;
  1307. }
  1308. if (0 == strcmp("id", cmd)) {
  1309. char id[MSG_BYTES];
  1310. if (cros_ec_read_id(dev, id, sizeof(id))) {
  1311. debug("%s: Could not read KBC ID\n", __func__);
  1312. return 1;
  1313. }
  1314. printf("%s\n", id);
  1315. } else if (0 == strcmp("info", cmd)) {
  1316. struct ec_response_mkbp_info info;
  1317. if (cros_ec_info(dev, &info)) {
  1318. debug("%s: Could not read KBC info\n", __func__);
  1319. return 1;
  1320. }
  1321. printf("rows = %u\n", info.rows);
  1322. printf("cols = %u\n", info.cols);
  1323. printf("switches = %#x\n", info.switches);
  1324. } else if (0 == strcmp("curimage", cmd)) {
  1325. enum ec_current_image image;
  1326. if (cros_ec_read_current_image(dev, &image)) {
  1327. debug("%s: Could not read KBC image\n", __func__);
  1328. return 1;
  1329. }
  1330. printf("%d\n", image);
  1331. } else if (0 == strcmp("hash", cmd)) {
  1332. struct ec_response_vboot_hash hash;
  1333. int i;
  1334. if (cros_ec_read_hash(dev, &hash)) {
  1335. debug("%s: Could not read KBC hash\n", __func__);
  1336. return 1;
  1337. }
  1338. if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
  1339. printf("type: SHA-256\n");
  1340. else
  1341. printf("type: %d\n", hash.hash_type);
  1342. printf("offset: 0x%08x\n", hash.offset);
  1343. printf("size: 0x%08x\n", hash.size);
  1344. printf("digest: ");
  1345. for (i = 0; i < hash.digest_size; i++)
  1346. printf("%02x", hash.hash_digest[i]);
  1347. printf("\n");
  1348. } else if (0 == strcmp("reboot", cmd)) {
  1349. int region;
  1350. enum ec_reboot_cmd cmd;
  1351. if (argc >= 3 && !strcmp(argv[2], "cold"))
  1352. cmd = EC_REBOOT_COLD;
  1353. else {
  1354. region = cros_ec_decode_region(argc - 2, argv + 2);
  1355. if (region == EC_FLASH_REGION_RO)
  1356. cmd = EC_REBOOT_JUMP_RO;
  1357. else if (region == EC_FLASH_REGION_RW)
  1358. cmd = EC_REBOOT_JUMP_RW;
  1359. else
  1360. return CMD_RET_USAGE;
  1361. }
  1362. if (cros_ec_reboot(dev, cmd, 0)) {
  1363. debug("%s: Could not reboot KBC\n", __func__);
  1364. return 1;
  1365. }
  1366. } else if (0 == strcmp("events", cmd)) {
  1367. uint32_t events;
  1368. if (cros_ec_get_host_events(dev, &events)) {
  1369. debug("%s: Could not read host events\n", __func__);
  1370. return 1;
  1371. }
  1372. printf("0x%08x\n", events);
  1373. } else if (0 == strcmp("clrevents", cmd)) {
  1374. uint32_t events = 0x7fffffff;
  1375. if (argc >= 3)
  1376. events = simple_strtol(argv[2], NULL, 0);
  1377. if (cros_ec_clear_host_events(dev, events)) {
  1378. debug("%s: Could not clear host events\n", __func__);
  1379. return 1;
  1380. }
  1381. } else if (0 == strcmp("read", cmd)) {
  1382. ret = do_read_write(dev, 0, argc, argv);
  1383. if (ret > 0)
  1384. return CMD_RET_USAGE;
  1385. } else if (0 == strcmp("write", cmd)) {
  1386. ret = do_read_write(dev, 1, argc, argv);
  1387. if (ret > 0)
  1388. return CMD_RET_USAGE;
  1389. } else if (0 == strcmp("erase", cmd)) {
  1390. int region = cros_ec_decode_region(argc - 2, argv + 2);
  1391. uint32_t offset, size;
  1392. if (region == -1)
  1393. return CMD_RET_USAGE;
  1394. if (cros_ec_flash_offset(dev, region, &offset, &size)) {
  1395. debug("%s: Could not read region info\n", __func__);
  1396. ret = -1;
  1397. } else {
  1398. ret = cros_ec_flash_erase(dev, offset, size);
  1399. if (ret) {
  1400. debug("%s: Could not erase region\n",
  1401. __func__);
  1402. }
  1403. }
  1404. } else if (0 == strcmp("regioninfo", cmd)) {
  1405. int region = cros_ec_decode_region(argc - 2, argv + 2);
  1406. uint32_t offset, size;
  1407. if (region == -1)
  1408. return CMD_RET_USAGE;
  1409. ret = cros_ec_flash_offset(dev, region, &offset, &size);
  1410. if (ret) {
  1411. debug("%s: Could not read region info\n", __func__);
  1412. } else {
  1413. printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
  1414. "RO" : "RW");
  1415. printf("Offset: %x\n", offset);
  1416. printf("Size: %x\n", size);
  1417. }
  1418. } else if (0 == strcmp("vbnvcontext", cmd)) {
  1419. uint8_t block[EC_VBNV_BLOCK_SIZE];
  1420. char buf[3];
  1421. int i, len;
  1422. unsigned long result;
  1423. if (argc <= 2) {
  1424. ret = cros_ec_read_vbnvcontext(dev, block);
  1425. if (!ret) {
  1426. printf("vbnv_block: ");
  1427. for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
  1428. printf("%02x", block[i]);
  1429. putc('\n');
  1430. }
  1431. } else {
  1432. /*
  1433. * TODO(clchiou): Move this to a utility function as
  1434. * cmd_spi might want to call it.
  1435. */
  1436. memset(block, 0, EC_VBNV_BLOCK_SIZE);
  1437. len = strlen(argv[2]);
  1438. buf[2] = '\0';
  1439. for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
  1440. if (i * 2 >= len)
  1441. break;
  1442. buf[0] = argv[2][i * 2];
  1443. if (i * 2 + 1 >= len)
  1444. buf[1] = '0';
  1445. else
  1446. buf[1] = argv[2][i * 2 + 1];
  1447. strict_strtoul(buf, 16, &result);
  1448. block[i] = result;
  1449. }
  1450. ret = cros_ec_write_vbnvcontext(dev, block);
  1451. }
  1452. if (ret) {
  1453. debug("%s: Could not %s VbNvContext\n", __func__,
  1454. argc <= 2 ? "read" : "write");
  1455. }
  1456. } else if (0 == strcmp("test", cmd)) {
  1457. int result = cros_ec_test(dev);
  1458. if (result)
  1459. printf("Test failed with error %d\n", result);
  1460. else
  1461. puts("Test passed\n");
  1462. } else if (0 == strcmp("version", cmd)) {
  1463. struct ec_response_get_version *p;
  1464. char *build_string;
  1465. ret = cros_ec_read_version(dev, &p);
  1466. if (!ret) {
  1467. /* Print versions */
  1468. printf("RO version: %1.*s\n",
  1469. (int)sizeof(p->version_string_ro),
  1470. p->version_string_ro);
  1471. printf("RW version: %1.*s\n",
  1472. (int)sizeof(p->version_string_rw),
  1473. p->version_string_rw);
  1474. printf("Firmware copy: %s\n",
  1475. (p->current_image <
  1476. ARRAY_SIZE(ec_current_image_name) ?
  1477. ec_current_image_name[p->current_image] :
  1478. "?"));
  1479. ret = cros_ec_read_build_info(dev, &build_string);
  1480. if (!ret)
  1481. printf("Build info: %s\n", build_string);
  1482. }
  1483. } else if (0 == strcmp("ldo", cmd)) {
  1484. uint8_t index, state;
  1485. char *endp;
  1486. if (argc < 3)
  1487. return CMD_RET_USAGE;
  1488. index = simple_strtoul(argv[2], &endp, 10);
  1489. if (*argv[2] == 0 || *endp != 0)
  1490. return CMD_RET_USAGE;
  1491. if (argc > 3) {
  1492. state = simple_strtoul(argv[3], &endp, 10);
  1493. if (*argv[3] == 0 || *endp != 0)
  1494. return CMD_RET_USAGE;
  1495. ret = cros_ec_set_ldo(dev, index, state);
  1496. } else {
  1497. ret = cros_ec_get_ldo(dev, index, &state);
  1498. if (!ret) {
  1499. printf("LDO%d: %s\n", index,
  1500. state == EC_LDO_STATE_ON ?
  1501. "on" : "off");
  1502. }
  1503. }
  1504. if (ret) {
  1505. debug("%s: Could not access LDO%d\n", __func__, index);
  1506. return ret;
  1507. }
  1508. } else if (0 == strcmp("i2c", cmd)) {
  1509. ret = cros_ec_i2c_passthrough(dev, flag, argc - 2, argv + 2);
  1510. } else {
  1511. return CMD_RET_USAGE;
  1512. }
  1513. if (ret < 0) {
  1514. printf("Error: CROS-EC command failed (error %d)\n", ret);
  1515. ret = 1;
  1516. }
  1517. return ret;
  1518. }
  1519. U_BOOT_CMD(
  1520. crosec, 6, 1, do_cros_ec,
  1521. "CROS-EC utility command",
  1522. "init Re-init CROS-EC (done on startup automatically)\n"
  1523. "crosec id Read CROS-EC ID\n"
  1524. "crosec info Read CROS-EC info\n"
  1525. "crosec curimage Read CROS-EC current image\n"
  1526. "crosec hash Read CROS-EC hash\n"
  1527. "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
  1528. "crosec events Read CROS-EC host events\n"
  1529. "crosec clrevents [mask] Clear CROS-EC host events\n"
  1530. "crosec regioninfo <ro|rw> Read image info\n"
  1531. "crosec erase <ro|rw> Erase EC image\n"
  1532. "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
  1533. "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
  1534. "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
  1535. "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
  1536. "crosec test run tests on cros_ec\n"
  1537. "crosec version Read CROS-EC version\n"
  1538. "crosec i2c md chip address[.0, .1, .2] [# of objects] - read from I2C passthru\n"
  1539. "crosec i2c mw chip address[.0, .1, .2] value [count] - write to I2C passthru (fill)"
  1540. );
  1541. #endif