cros_ec.c 46 KB

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