cros_ec.c 41 KB

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