cros_ec.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  594. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  595. return 0;
  596. }
  597. /* Try sending a version 2 packet */
  598. dev->protocol_version = 2;
  599. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  600. (uint8_t **)&resp, sizeof(*resp)) > 0) {
  601. return 0;
  602. }
  603. /*
  604. * Fail if we're still here, since the EC doesn't understand any
  605. * protcol version we speak. Version 1 interface without command
  606. * version is no longer supported, and we don't know about any new
  607. * protocol versions.
  608. */
  609. dev->protocol_version = 0;
  610. printf("%s: ERROR: old EC interface not supported\n", __func__);
  611. return -1;
  612. }
  613. int cros_ec_test(struct cros_ec_dev *dev)
  614. {
  615. struct ec_params_hello req;
  616. struct ec_response_hello *resp;
  617. req.in_data = 0x12345678;
  618. if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
  619. (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
  620. printf("ec_command_inptr() returned error\n");
  621. return -1;
  622. }
  623. if (resp->out_data != req.in_data + 0x01020304) {
  624. printf("Received invalid handshake %x\n", resp->out_data);
  625. return -1;
  626. }
  627. return 0;
  628. }
  629. int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
  630. uint32_t *offset, uint32_t *size)
  631. {
  632. struct ec_params_flash_region_info p;
  633. struct ec_response_flash_region_info *r;
  634. int ret;
  635. p.region = region;
  636. ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
  637. EC_VER_FLASH_REGION_INFO,
  638. &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
  639. if (ret != sizeof(*r))
  640. return -1;
  641. if (offset)
  642. *offset = r->offset;
  643. if (size)
  644. *size = r->size;
  645. return 0;
  646. }
  647. int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
  648. {
  649. struct ec_params_flash_erase p;
  650. p.offset = offset;
  651. p.size = size;
  652. return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
  653. NULL, 0);
  654. }
  655. /**
  656. * Write a single block to the flash
  657. *
  658. * Write a block of data to the EC flash. The size must not exceed the flash
  659. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  660. *
  661. * The offset starts at 0. You can obtain the region information from
  662. * cros_ec_flash_offset() to find out where to write for a particular region.
  663. *
  664. * Attempting to write to the region where the EC is currently running from
  665. * will result in an error.
  666. *
  667. * @param dev CROS-EC device
  668. * @param data Pointer to data buffer to write
  669. * @param offset Offset within flash to write to.
  670. * @param size Number of bytes to write
  671. * @return 0 if ok, -1 on error
  672. */
  673. static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
  674. const uint8_t *data, uint32_t offset, uint32_t size)
  675. {
  676. struct ec_params_flash_write p;
  677. p.offset = offset;
  678. p.size = size;
  679. assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
  680. memcpy(&p + 1, data, p.size);
  681. return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
  682. &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
  683. }
  684. /**
  685. * Return optimal flash write burst size
  686. */
  687. static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
  688. {
  689. return EC_FLASH_WRITE_VER0_SIZE;
  690. }
  691. /**
  692. * Check if a block of data is erased (all 0xff)
  693. *
  694. * This function is useful when dealing with flash, for checking whether a
  695. * data block is erased and thus does not need to be programmed.
  696. *
  697. * @param data Pointer to data to check (must be word-aligned)
  698. * @param size Number of bytes to check (must be word-aligned)
  699. * @return 0 if erased, non-zero if any word is not erased
  700. */
  701. static int cros_ec_data_is_erased(const uint32_t *data, int size)
  702. {
  703. assert(!(size & 3));
  704. size /= sizeof(uint32_t);
  705. for (; size > 0; size -= 4, data++)
  706. if (*data != -1U)
  707. return 0;
  708. return 1;
  709. }
  710. int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  711. uint32_t offset, uint32_t size)
  712. {
  713. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  714. uint32_t end, off;
  715. int ret;
  716. /*
  717. * TODO: round up to the nearest multiple of write size. Can get away
  718. * without that on link right now because its write size is 4 bytes.
  719. */
  720. end = offset + size;
  721. for (off = offset; off < end; off += burst, data += burst) {
  722. uint32_t todo;
  723. /* If the data is empty, there is no point in programming it */
  724. todo = min(end - off, burst);
  725. if (dev->optimise_flash_write &&
  726. cros_ec_data_is_erased((uint32_t *)data, todo))
  727. continue;
  728. ret = cros_ec_flash_write_block(dev, data, off, todo);
  729. if (ret)
  730. return ret;
  731. }
  732. return 0;
  733. }
  734. /**
  735. * Read a single block from the flash
  736. *
  737. * Read a block of data from the EC flash. The size must not exceed the flash
  738. * write block size which you can obtain from cros_ec_flash_write_burst_size().
  739. *
  740. * The offset starts at 0. You can obtain the region information from
  741. * cros_ec_flash_offset() to find out where to read for a particular region.
  742. *
  743. * @param dev CROS-EC device
  744. * @param data Pointer to data buffer to read into
  745. * @param offset Offset within flash to read from
  746. * @param size Number of bytes to read
  747. * @return 0 if ok, -1 on error
  748. */
  749. static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
  750. uint32_t offset, uint32_t size)
  751. {
  752. struct ec_params_flash_read p;
  753. p.offset = offset;
  754. p.size = size;
  755. return ec_command(dev, EC_CMD_FLASH_READ, 0,
  756. &p, sizeof(p), data, size) >= 0 ? 0 : -1;
  757. }
  758. int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
  759. uint32_t size)
  760. {
  761. uint32_t burst = cros_ec_flash_write_burst_size(dev);
  762. uint32_t end, off;
  763. int ret;
  764. end = offset + size;
  765. for (off = offset; off < end; off += burst, data += burst) {
  766. ret = cros_ec_flash_read_block(dev, data, off,
  767. min(end - off, burst));
  768. if (ret)
  769. return ret;
  770. }
  771. return 0;
  772. }
  773. int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  774. const uint8_t *image, int image_size)
  775. {
  776. uint32_t rw_offset, rw_size;
  777. int ret;
  778. if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
  779. return -1;
  780. if (image_size > (int)rw_size)
  781. return -1;
  782. /* Invalidate the existing hash, just in case the AP reboots
  783. * unexpectedly during the update. If that happened, the EC RW firmware
  784. * would be invalid, but the EC would still have the original hash.
  785. */
  786. ret = cros_ec_invalidate_hash(dev);
  787. if (ret)
  788. return ret;
  789. /*
  790. * Erase the entire RW section, so that the EC doesn't see any garbage
  791. * past the new image if it's smaller than the current image.
  792. *
  793. * TODO: could optimize this to erase just the current image, since
  794. * presumably everything past that is 0xff's. But would still need to
  795. * round up to the nearest multiple of erase size.
  796. */
  797. ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
  798. if (ret)
  799. return ret;
  800. /* Write the image */
  801. ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
  802. if (ret)
  803. return ret;
  804. return 0;
  805. }
  806. int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
  807. {
  808. struct ec_params_vbnvcontext p;
  809. int len;
  810. p.op = EC_VBNV_CONTEXT_OP_READ;
  811. len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  812. &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
  813. if (len < EC_VBNV_BLOCK_SIZE)
  814. return -1;
  815. return 0;
  816. }
  817. int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
  818. {
  819. struct ec_params_vbnvcontext p;
  820. int len;
  821. p.op = EC_VBNV_CONTEXT_OP_WRITE;
  822. memcpy(p.block, block, sizeof(p.block));
  823. len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
  824. &p, sizeof(p), NULL, 0);
  825. if (len < 0)
  826. return -1;
  827. return 0;
  828. }
  829. int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
  830. {
  831. struct ec_params_ldo_set params;
  832. params.index = index;
  833. params.state = state;
  834. if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
  835. &params, sizeof(params),
  836. NULL, 0))
  837. return -1;
  838. return 0;
  839. }
  840. int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
  841. {
  842. struct ec_params_ldo_get params;
  843. struct ec_response_ldo_get *resp;
  844. params.index = index;
  845. if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
  846. &params, sizeof(params),
  847. (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
  848. return -1;
  849. *state = resp->state;
  850. return 0;
  851. }
  852. #ifndef CONFIG_DM_CROS_EC
  853. /**
  854. * Decode EC interface details from the device tree and allocate a suitable
  855. * device.
  856. *
  857. * @param blob Device tree blob
  858. * @param node Node to decode from
  859. * @param devp Returns a pointer to the new allocated device
  860. * @return 0 if ok, -1 on error
  861. */
  862. static int cros_ec_decode_fdt(const void *blob, int node,
  863. struct cros_ec_dev **devp)
  864. {
  865. enum fdt_compat_id compat;
  866. struct cros_ec_dev *dev;
  867. int parent;
  868. /* See what type of parent we are inside (this is expensive) */
  869. parent = fdt_parent_offset(blob, node);
  870. if (parent < 0) {
  871. debug("%s: Cannot find node parent\n", __func__);
  872. return -1;
  873. }
  874. dev = &static_dev;
  875. dev->node = node;
  876. dev->parent_node = parent;
  877. compat = fdtdec_lookup(blob, parent);
  878. switch (compat) {
  879. #ifdef CONFIG_CROS_EC_SPI
  880. case COMPAT_SAMSUNG_EXYNOS_SPI:
  881. dev->interface = CROS_EC_IF_SPI;
  882. if (cros_ec_spi_decode_fdt(dev, blob))
  883. return -1;
  884. break;
  885. #endif
  886. #ifdef CONFIG_CROS_EC_I2C
  887. case COMPAT_SAMSUNG_S3C2440_I2C:
  888. dev->interface = CROS_EC_IF_I2C;
  889. if (cros_ec_i2c_decode_fdt(dev, blob))
  890. return -1;
  891. break;
  892. #endif
  893. #ifdef CONFIG_CROS_EC_LPC
  894. case COMPAT_INTEL_LPC:
  895. dev->interface = CROS_EC_IF_LPC;
  896. break;
  897. #endif
  898. #ifdef CONFIG_CROS_EC_SANDBOX
  899. case COMPAT_SANDBOX_HOST_EMULATION:
  900. dev->interface = CROS_EC_IF_SANDBOX;
  901. break;
  902. #endif
  903. default:
  904. debug("%s: Unknown compat id %d\n", __func__, compat);
  905. return -1;
  906. }
  907. fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
  908. dev->optimise_flash_write = fdtdec_get_bool(blob, node,
  909. "optimise-flash-write");
  910. *devp = dev;
  911. return 0;
  912. }
  913. #endif
  914. #ifdef CONFIG_DM_CROS_EC
  915. int cros_ec_register(struct udevice *dev)
  916. {
  917. struct cros_ec_dev *cdev = dev->uclass_priv;
  918. const void *blob = gd->fdt_blob;
  919. int node = dev->of_offset;
  920. char id[MSG_BYTES];
  921. cdev->dev = dev;
  922. fdtdec_decode_gpio(blob, node, "ec-interrupt", &cdev->ec_int);
  923. cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
  924. "optimise-flash-write");
  925. /* we will poll the EC interrupt line */
  926. fdtdec_setup_gpio(&cdev->ec_int);
  927. if (fdt_gpio_isvalid(&cdev->ec_int)) {
  928. gpio_request(cdev->ec_int.gpio, "cros-ec-irq");
  929. gpio_direction_input(cdev->ec_int.gpio);
  930. }
  931. if (cros_ec_check_version(cdev)) {
  932. debug("%s: Could not detect CROS-EC version\n", __func__);
  933. return -CROS_EC_ERR_CHECK_VERSION;
  934. }
  935. if (cros_ec_read_id(cdev, id, sizeof(id))) {
  936. debug("%s: Could not read KBC ID\n", __func__);
  937. return -CROS_EC_ERR_READ_ID;
  938. }
  939. /* Remember this device for use by the cros_ec command */
  940. debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
  941. return 0;
  942. }
  943. #else
  944. int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
  945. {
  946. struct cros_ec_dev *dev;
  947. char id[MSG_BYTES];
  948. #ifdef CONFIG_DM_CROS_EC
  949. struct udevice *udev;
  950. int ret;
  951. ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
  952. if (!ret)
  953. device_remove(udev);
  954. ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
  955. if (ret)
  956. return ret;
  957. dev = udev->uclass_priv;
  958. return 0;
  959. #else
  960. int node = 0;
  961. *cros_ecp = NULL;
  962. do {
  963. node = fdtdec_next_compatible(blob, node,
  964. COMPAT_GOOGLE_CROS_EC);
  965. if (node < 0) {
  966. debug("%s: Node not found\n", __func__);
  967. return 0;
  968. }
  969. } while (!fdtdec_get_is_enabled(blob, node));
  970. if (cros_ec_decode_fdt(blob, node, &dev)) {
  971. debug("%s: Failed to decode device.\n", __func__);
  972. return -CROS_EC_ERR_FDT_DECODE;
  973. }
  974. switch (dev->interface) {
  975. #ifdef CONFIG_CROS_EC_SPI
  976. case CROS_EC_IF_SPI:
  977. if (cros_ec_spi_init(dev, blob)) {
  978. debug("%s: Could not setup SPI interface\n", __func__);
  979. return -CROS_EC_ERR_DEV_INIT;
  980. }
  981. break;
  982. #endif
  983. #ifdef CONFIG_CROS_EC_I2C
  984. case CROS_EC_IF_I2C:
  985. if (cros_ec_i2c_init(dev, blob))
  986. return -CROS_EC_ERR_DEV_INIT;
  987. break;
  988. #endif
  989. #ifdef CONFIG_CROS_EC_LPC
  990. case CROS_EC_IF_LPC:
  991. if (cros_ec_lpc_init(dev, blob))
  992. return -CROS_EC_ERR_DEV_INIT;
  993. break;
  994. #endif
  995. #ifdef CONFIG_CROS_EC_SANDBOX
  996. case CROS_EC_IF_SANDBOX:
  997. if (cros_ec_sandbox_init(dev, blob))
  998. return -CROS_EC_ERR_DEV_INIT;
  999. break;
  1000. #endif
  1001. case CROS_EC_IF_NONE:
  1002. default:
  1003. return 0;
  1004. }
  1005. #endif
  1006. /* we will poll the EC interrupt line */
  1007. fdtdec_setup_gpio(&dev->ec_int);
  1008. if (fdt_gpio_isvalid(&dev->ec_int)) {
  1009. gpio_request(dev->ec_int.gpio, "cros-ec-irq");
  1010. gpio_direction_input(dev->ec_int.gpio);
  1011. }
  1012. if (cros_ec_check_version(dev)) {
  1013. debug("%s: Could not detect CROS-EC version\n", __func__);
  1014. return -CROS_EC_ERR_CHECK_VERSION;
  1015. }
  1016. if (cros_ec_read_id(dev, id, sizeof(id))) {
  1017. debug("%s: Could not read KBC ID\n", __func__);
  1018. return -CROS_EC_ERR_READ_ID;
  1019. }
  1020. /* Remember this device for use by the cros_ec command */
  1021. *cros_ecp = dev;
  1022. #ifndef CONFIG_DM_CROS_EC
  1023. last_dev = dev;
  1024. #endif
  1025. debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
  1026. return 0;
  1027. }
  1028. #endif
  1029. int cros_ec_decode_region(int argc, char * const argv[])
  1030. {
  1031. if (argc > 0) {
  1032. if (0 == strcmp(*argv, "rw"))
  1033. return EC_FLASH_REGION_RW;
  1034. else if (0 == strcmp(*argv, "ro"))
  1035. return EC_FLASH_REGION_RO;
  1036. debug("%s: Invalid region '%s'\n", __func__, *argv);
  1037. } else {
  1038. debug("%s: Missing region parameter\n", __func__);
  1039. }
  1040. return -1;
  1041. }
  1042. int cros_ec_decode_ec_flash(const void *blob, int node,
  1043. struct fdt_cros_ec *config)
  1044. {
  1045. int flash_node;
  1046. flash_node = fdt_subnode_offset(blob, node, "flash");
  1047. if (flash_node < 0) {
  1048. debug("Failed to find flash node\n");
  1049. return -1;
  1050. }
  1051. if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
  1052. &config->flash)) {
  1053. debug("Failed to decode flash node in chrome-ec'\n");
  1054. return -1;
  1055. }
  1056. config->flash_erase_value = fdtdec_get_int(blob, flash_node,
  1057. "erase-value", -1);
  1058. for (node = fdt_first_subnode(blob, flash_node); node >= 0;
  1059. node = fdt_next_subnode(blob, node)) {
  1060. const char *name = fdt_get_name(blob, node, NULL);
  1061. enum ec_flash_region region;
  1062. if (0 == strcmp(name, "ro")) {
  1063. region = EC_FLASH_REGION_RO;
  1064. } else if (0 == strcmp(name, "rw")) {
  1065. region = EC_FLASH_REGION_RW;
  1066. } else if (0 == strcmp(name, "wp-ro")) {
  1067. region = EC_FLASH_REGION_WP_RO;
  1068. } else {
  1069. debug("Unknown EC flash region name '%s'\n", name);
  1070. return -1;
  1071. }
  1072. if (fdtdec_read_fmap_entry(blob, node, "reg",
  1073. &config->region[region])) {
  1074. debug("Failed to decode flash region in chrome-ec'\n");
  1075. return -1;
  1076. }
  1077. }
  1078. return 0;
  1079. }
  1080. int cros_ec_i2c_xfer(struct cros_ec_dev *dev, uchar chip, uint addr,
  1081. int alen, uchar *buffer, int len, int is_read)
  1082. {
  1083. union {
  1084. struct ec_params_i2c_passthru p;
  1085. uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1086. } params;
  1087. union {
  1088. struct ec_response_i2c_passthru r;
  1089. uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
  1090. } response;
  1091. struct ec_params_i2c_passthru *p = &params.p;
  1092. struct ec_response_i2c_passthru *r = &response.r;
  1093. struct ec_params_i2c_passthru_msg *msg = p->msg;
  1094. uint8_t *pdata;
  1095. int read_len, write_len;
  1096. int size;
  1097. int rv;
  1098. p->port = 0;
  1099. if (alen != 1) {
  1100. printf("Unsupported address length %d\n", alen);
  1101. return -1;
  1102. }
  1103. if (is_read) {
  1104. read_len = len;
  1105. write_len = alen;
  1106. p->num_msgs = 2;
  1107. } else {
  1108. read_len = 0;
  1109. write_len = alen + len;
  1110. p->num_msgs = 1;
  1111. }
  1112. size = sizeof(*p) + p->num_msgs * sizeof(*msg);
  1113. if (size + write_len > sizeof(params)) {
  1114. puts("Params too large for buffer\n");
  1115. return -1;
  1116. }
  1117. if (sizeof(*r) + read_len > sizeof(response)) {
  1118. puts("Read length too big for buffer\n");
  1119. return -1;
  1120. }
  1121. /* Create a message to write the register address and optional data */
  1122. pdata = (uint8_t *)p + size;
  1123. msg->addr_flags = chip;
  1124. msg->len = write_len;
  1125. pdata[0] = addr;
  1126. if (!is_read)
  1127. memcpy(pdata + 1, buffer, len);
  1128. msg++;
  1129. if (read_len) {
  1130. msg->addr_flags = chip | EC_I2C_FLAG_READ;
  1131. msg->len = read_len;
  1132. }
  1133. rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, size + write_len,
  1134. r, sizeof(*r) + read_len);
  1135. if (rv < 0)
  1136. return rv;
  1137. /* Parse response */
  1138. if (r->i2c_status & EC_I2C_STATUS_ERROR) {
  1139. printf("Transfer failed with status=0x%x\n", r->i2c_status);
  1140. return -1;
  1141. }
  1142. if (rv < sizeof(*r) + read_len) {
  1143. puts("Truncated read response\n");
  1144. return -1;
  1145. }
  1146. if (read_len)
  1147. memcpy(buffer, r->data, read_len);
  1148. return 0;
  1149. }
  1150. #ifdef CONFIG_CMD_CROS_EC
  1151. /**
  1152. * Perform a flash read or write command
  1153. *
  1154. * @param dev CROS-EC device to read/write
  1155. * @param is_write 1 do to a write, 0 to do a read
  1156. * @param argc Number of arguments
  1157. * @param argv Arguments (2 is region, 3 is address)
  1158. * @return 0 for ok, 1 for a usage error or -ve for ec command error
  1159. * (negative EC_RES_...)
  1160. */
  1161. static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
  1162. char * const argv[])
  1163. {
  1164. uint32_t offset, size = -1U, region_size;
  1165. unsigned long addr;
  1166. char *endp;
  1167. int region;
  1168. int ret;
  1169. region = cros_ec_decode_region(argc - 2, argv + 2);
  1170. if (region == -1)
  1171. return 1;
  1172. if (argc < 4)
  1173. return 1;
  1174. addr = simple_strtoul(argv[3], &endp, 16);
  1175. if (*argv[3] == 0 || *endp != 0)
  1176. return 1;
  1177. if (argc > 4) {
  1178. size = simple_strtoul(argv[4], &endp, 16);
  1179. if (*argv[4] == 0 || *endp != 0)
  1180. return 1;
  1181. }
  1182. ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
  1183. if (ret) {
  1184. debug("%s: Could not read region info\n", __func__);
  1185. return ret;
  1186. }
  1187. if (size == -1U)
  1188. size = region_size;
  1189. ret = is_write ?
  1190. cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
  1191. cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
  1192. if (ret) {
  1193. debug("%s: Could not %s region\n", __func__,
  1194. is_write ? "write" : "read");
  1195. return ret;
  1196. }
  1197. return 0;
  1198. }
  1199. /**
  1200. * get_alen() - Small parser helper function to get address length
  1201. *
  1202. * Returns the address length.
  1203. */
  1204. static uint get_alen(char *arg)
  1205. {
  1206. int j;
  1207. int alen;
  1208. alen = 1;
  1209. for (j = 0; j < 8; j++) {
  1210. if (arg[j] == '.') {
  1211. alen = arg[j+1] - '0';
  1212. break;
  1213. } else if (arg[j] == '\0') {
  1214. break;
  1215. }
  1216. }
  1217. return alen;
  1218. }
  1219. #define DISP_LINE_LEN 16
  1220. /*
  1221. * TODO(sjg@chromium.org): This code copied almost verbatim from cmd_i2c.c
  1222. * so we can remove it later.
  1223. */
  1224. static int cros_ec_i2c_md(struct cros_ec_dev *dev, int flag, int argc,
  1225. char * const argv[])
  1226. {
  1227. u_char chip;
  1228. uint addr, alen, length = 0x10;
  1229. int j, nbytes, linebytes;
  1230. if (argc < 2)
  1231. return CMD_RET_USAGE;
  1232. if (1 || (flag & CMD_FLAG_REPEAT) == 0) {
  1233. /*
  1234. * New command specified.
  1235. */
  1236. /*
  1237. * I2C chip address
  1238. */
  1239. chip = simple_strtoul(argv[0], NULL, 16);
  1240. /*
  1241. * I2C data address within the chip. This can be 1 or
  1242. * 2 bytes long. Some day it might be 3 bytes long :-).
  1243. */
  1244. addr = simple_strtoul(argv[1], NULL, 16);
  1245. alen = get_alen(argv[1]);
  1246. if (alen > 3)
  1247. return CMD_RET_USAGE;
  1248. /*
  1249. * If another parameter, it is the length to display.
  1250. * Length is the number of objects, not number of bytes.
  1251. */
  1252. if (argc > 2)
  1253. length = simple_strtoul(argv[2], NULL, 16);
  1254. }
  1255. /*
  1256. * Print the lines.
  1257. *
  1258. * We buffer all read data, so we can make sure data is read only
  1259. * once.
  1260. */
  1261. nbytes = length;
  1262. do {
  1263. unsigned char linebuf[DISP_LINE_LEN];
  1264. unsigned char *cp;
  1265. linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
  1266. if (cros_ec_i2c_xfer(dev, chip, addr, alen, linebuf, linebytes,
  1267. 1))
  1268. puts("Error reading the chip.\n");
  1269. else {
  1270. printf("%04x:", addr);
  1271. cp = linebuf;
  1272. for (j = 0; j < linebytes; j++) {
  1273. printf(" %02x", *cp++);
  1274. addr++;
  1275. }
  1276. puts(" ");
  1277. cp = linebuf;
  1278. for (j = 0; j < linebytes; j++) {
  1279. if ((*cp < 0x20) || (*cp > 0x7e))
  1280. puts(".");
  1281. else
  1282. printf("%c", *cp);
  1283. cp++;
  1284. }
  1285. putc('\n');
  1286. }
  1287. nbytes -= linebytes;
  1288. } while (nbytes > 0);
  1289. return 0;
  1290. }
  1291. static int cros_ec_i2c_mw(struct cros_ec_dev *dev, int flag, int argc,
  1292. char * const argv[])
  1293. {
  1294. uchar chip;
  1295. ulong addr;
  1296. uint alen;
  1297. uchar byte;
  1298. int count;
  1299. if ((argc < 3) || (argc > 4))
  1300. return CMD_RET_USAGE;
  1301. /*
  1302. * Chip is always specified.
  1303. */
  1304. chip = simple_strtoul(argv[0], NULL, 16);
  1305. /*
  1306. * Address is always specified.
  1307. */
  1308. addr = simple_strtoul(argv[1], NULL, 16);
  1309. alen = get_alen(argv[1]);
  1310. if (alen > 3)
  1311. return CMD_RET_USAGE;
  1312. /*
  1313. * Value to write is always specified.
  1314. */
  1315. byte = simple_strtoul(argv[2], NULL, 16);
  1316. /*
  1317. * Optional count
  1318. */
  1319. if (argc == 4)
  1320. count = simple_strtoul(argv[3], NULL, 16);
  1321. else
  1322. count = 1;
  1323. while (count-- > 0) {
  1324. if (cros_ec_i2c_xfer(dev, chip, addr++, alen, &byte, 1, 0))
  1325. puts("Error writing the chip.\n");
  1326. /*
  1327. * Wait for the write to complete. The write can take
  1328. * up to 10mSec (we allow a little more time).
  1329. */
  1330. /*
  1331. * No write delay with FRAM devices.
  1332. */
  1333. #if !defined(CONFIG_SYS_I2C_FRAM)
  1334. udelay(11000);
  1335. #endif
  1336. }
  1337. return 0;
  1338. }
  1339. /* Temporary code until we have driver model and can use the i2c command */
  1340. static int cros_ec_i2c_passthrough(struct cros_ec_dev *dev, int flag,
  1341. int argc, char * const argv[])
  1342. {
  1343. const char *cmd;
  1344. if (argc < 1)
  1345. return CMD_RET_USAGE;
  1346. cmd = *argv++;
  1347. argc--;
  1348. if (0 == strcmp("md", cmd))
  1349. cros_ec_i2c_md(dev, flag, argc, argv);
  1350. else if (0 == strcmp("mw", cmd))
  1351. cros_ec_i2c_mw(dev, flag, argc, argv);
  1352. else
  1353. return CMD_RET_USAGE;
  1354. return 0;
  1355. }
  1356. static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1357. {
  1358. struct cros_ec_dev *dev;
  1359. #ifdef CONFIG_DM_CROS_EC
  1360. struct udevice *udev;
  1361. #endif
  1362. const char *cmd;
  1363. int ret = 0;
  1364. if (argc < 2)
  1365. return CMD_RET_USAGE;
  1366. cmd = argv[1];
  1367. if (0 == strcmp("init", cmd)) {
  1368. #ifndef CONFIG_DM_CROS_EC
  1369. ret = cros_ec_init(gd->fdt_blob, &dev);
  1370. if (ret) {
  1371. printf("Could not init cros_ec device (err %d)\n", ret);
  1372. return 1;
  1373. }
  1374. #endif
  1375. return 0;
  1376. }
  1377. #ifdef CONFIG_DM_CROS_EC
  1378. ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
  1379. if (ret) {
  1380. printf("Cannot get cros-ec device (err=%d)\n", ret);
  1381. return 1;
  1382. }
  1383. dev = udev->uclass_priv;
  1384. #else
  1385. /* Just use the last allocated device; there should be only one */
  1386. if (!last_dev) {
  1387. printf("No CROS-EC device available\n");
  1388. return 1;
  1389. }
  1390. dev = last_dev;
  1391. #endif
  1392. if (0 == strcmp("id", cmd)) {
  1393. char id[MSG_BYTES];
  1394. if (cros_ec_read_id(dev, id, sizeof(id))) {
  1395. debug("%s: Could not read KBC ID\n", __func__);
  1396. return 1;
  1397. }
  1398. printf("%s\n", id);
  1399. } else if (0 == strcmp("info", cmd)) {
  1400. struct ec_response_mkbp_info info;
  1401. if (cros_ec_info(dev, &info)) {
  1402. debug("%s: Could not read KBC info\n", __func__);
  1403. return 1;
  1404. }
  1405. printf("rows = %u\n", info.rows);
  1406. printf("cols = %u\n", info.cols);
  1407. printf("switches = %#x\n", info.switches);
  1408. } else if (0 == strcmp("curimage", cmd)) {
  1409. enum ec_current_image image;
  1410. if (cros_ec_read_current_image(dev, &image)) {
  1411. debug("%s: Could not read KBC image\n", __func__);
  1412. return 1;
  1413. }
  1414. printf("%d\n", image);
  1415. } else if (0 == strcmp("hash", cmd)) {
  1416. struct ec_response_vboot_hash hash;
  1417. int i;
  1418. if (cros_ec_read_hash(dev, &hash)) {
  1419. debug("%s: Could not read KBC hash\n", __func__);
  1420. return 1;
  1421. }
  1422. if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
  1423. printf("type: SHA-256\n");
  1424. else
  1425. printf("type: %d\n", hash.hash_type);
  1426. printf("offset: 0x%08x\n", hash.offset);
  1427. printf("size: 0x%08x\n", hash.size);
  1428. printf("digest: ");
  1429. for (i = 0; i < hash.digest_size; i++)
  1430. printf("%02x", hash.hash_digest[i]);
  1431. printf("\n");
  1432. } else if (0 == strcmp("reboot", cmd)) {
  1433. int region;
  1434. enum ec_reboot_cmd cmd;
  1435. if (argc >= 3 && !strcmp(argv[2], "cold"))
  1436. cmd = EC_REBOOT_COLD;
  1437. else {
  1438. region = cros_ec_decode_region(argc - 2, argv + 2);
  1439. if (region == EC_FLASH_REGION_RO)
  1440. cmd = EC_REBOOT_JUMP_RO;
  1441. else if (region == EC_FLASH_REGION_RW)
  1442. cmd = EC_REBOOT_JUMP_RW;
  1443. else
  1444. return CMD_RET_USAGE;
  1445. }
  1446. if (cros_ec_reboot(dev, cmd, 0)) {
  1447. debug("%s: Could not reboot KBC\n", __func__);
  1448. return 1;
  1449. }
  1450. } else if (0 == strcmp("events", cmd)) {
  1451. uint32_t events;
  1452. if (cros_ec_get_host_events(dev, &events)) {
  1453. debug("%s: Could not read host events\n", __func__);
  1454. return 1;
  1455. }
  1456. printf("0x%08x\n", events);
  1457. } else if (0 == strcmp("clrevents", cmd)) {
  1458. uint32_t events = 0x7fffffff;
  1459. if (argc >= 3)
  1460. events = simple_strtol(argv[2], NULL, 0);
  1461. if (cros_ec_clear_host_events(dev, events)) {
  1462. debug("%s: Could not clear host events\n", __func__);
  1463. return 1;
  1464. }
  1465. } else if (0 == strcmp("read", cmd)) {
  1466. ret = do_read_write(dev, 0, argc, argv);
  1467. if (ret > 0)
  1468. return CMD_RET_USAGE;
  1469. } else if (0 == strcmp("write", cmd)) {
  1470. ret = do_read_write(dev, 1, argc, argv);
  1471. if (ret > 0)
  1472. return CMD_RET_USAGE;
  1473. } else if (0 == strcmp("erase", cmd)) {
  1474. int region = cros_ec_decode_region(argc - 2, argv + 2);
  1475. uint32_t offset, size;
  1476. if (region == -1)
  1477. return CMD_RET_USAGE;
  1478. if (cros_ec_flash_offset(dev, region, &offset, &size)) {
  1479. debug("%s: Could not read region info\n", __func__);
  1480. ret = -1;
  1481. } else {
  1482. ret = cros_ec_flash_erase(dev, offset, size);
  1483. if (ret) {
  1484. debug("%s: Could not erase region\n",
  1485. __func__);
  1486. }
  1487. }
  1488. } else if (0 == strcmp("regioninfo", cmd)) {
  1489. int region = cros_ec_decode_region(argc - 2, argv + 2);
  1490. uint32_t offset, size;
  1491. if (region == -1)
  1492. return CMD_RET_USAGE;
  1493. ret = cros_ec_flash_offset(dev, region, &offset, &size);
  1494. if (ret) {
  1495. debug("%s: Could not read region info\n", __func__);
  1496. } else {
  1497. printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
  1498. "RO" : "RW");
  1499. printf("Offset: %x\n", offset);
  1500. printf("Size: %x\n", size);
  1501. }
  1502. } else if (0 == strcmp("vbnvcontext", cmd)) {
  1503. uint8_t block[EC_VBNV_BLOCK_SIZE];
  1504. char buf[3];
  1505. int i, len;
  1506. unsigned long result;
  1507. if (argc <= 2) {
  1508. ret = cros_ec_read_vbnvcontext(dev, block);
  1509. if (!ret) {
  1510. printf("vbnv_block: ");
  1511. for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
  1512. printf("%02x", block[i]);
  1513. putc('\n');
  1514. }
  1515. } else {
  1516. /*
  1517. * TODO(clchiou): Move this to a utility function as
  1518. * cmd_spi might want to call it.
  1519. */
  1520. memset(block, 0, EC_VBNV_BLOCK_SIZE);
  1521. len = strlen(argv[2]);
  1522. buf[2] = '\0';
  1523. for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
  1524. if (i * 2 >= len)
  1525. break;
  1526. buf[0] = argv[2][i * 2];
  1527. if (i * 2 + 1 >= len)
  1528. buf[1] = '0';
  1529. else
  1530. buf[1] = argv[2][i * 2 + 1];
  1531. strict_strtoul(buf, 16, &result);
  1532. block[i] = result;
  1533. }
  1534. ret = cros_ec_write_vbnvcontext(dev, block);
  1535. }
  1536. if (ret) {
  1537. debug("%s: Could not %s VbNvContext\n", __func__,
  1538. argc <= 2 ? "read" : "write");
  1539. }
  1540. } else if (0 == strcmp("test", cmd)) {
  1541. int result = cros_ec_test(dev);
  1542. if (result)
  1543. printf("Test failed with error %d\n", result);
  1544. else
  1545. puts("Test passed\n");
  1546. } else if (0 == strcmp("version", cmd)) {
  1547. struct ec_response_get_version *p;
  1548. char *build_string;
  1549. ret = cros_ec_read_version(dev, &p);
  1550. if (!ret) {
  1551. /* Print versions */
  1552. printf("RO version: %1.*s\n",
  1553. (int)sizeof(p->version_string_ro),
  1554. p->version_string_ro);
  1555. printf("RW version: %1.*s\n",
  1556. (int)sizeof(p->version_string_rw),
  1557. p->version_string_rw);
  1558. printf("Firmware copy: %s\n",
  1559. (p->current_image <
  1560. ARRAY_SIZE(ec_current_image_name) ?
  1561. ec_current_image_name[p->current_image] :
  1562. "?"));
  1563. ret = cros_ec_read_build_info(dev, &build_string);
  1564. if (!ret)
  1565. printf("Build info: %s\n", build_string);
  1566. }
  1567. } else if (0 == strcmp("ldo", cmd)) {
  1568. uint8_t index, state;
  1569. char *endp;
  1570. if (argc < 3)
  1571. return CMD_RET_USAGE;
  1572. index = simple_strtoul(argv[2], &endp, 10);
  1573. if (*argv[2] == 0 || *endp != 0)
  1574. return CMD_RET_USAGE;
  1575. if (argc > 3) {
  1576. state = simple_strtoul(argv[3], &endp, 10);
  1577. if (*argv[3] == 0 || *endp != 0)
  1578. return CMD_RET_USAGE;
  1579. ret = cros_ec_set_ldo(dev, index, state);
  1580. } else {
  1581. ret = cros_ec_get_ldo(dev, index, &state);
  1582. if (!ret) {
  1583. printf("LDO%d: %s\n", index,
  1584. state == EC_LDO_STATE_ON ?
  1585. "on" : "off");
  1586. }
  1587. }
  1588. if (ret) {
  1589. debug("%s: Could not access LDO%d\n", __func__, index);
  1590. return ret;
  1591. }
  1592. } else if (0 == strcmp("i2c", cmd)) {
  1593. ret = cros_ec_i2c_passthrough(dev, flag, argc - 2, argv + 2);
  1594. } else {
  1595. return CMD_RET_USAGE;
  1596. }
  1597. if (ret < 0) {
  1598. printf("Error: CROS-EC command failed (error %d)\n", ret);
  1599. ret = 1;
  1600. }
  1601. return ret;
  1602. }
  1603. U_BOOT_CMD(
  1604. crosec, 6, 1, do_cros_ec,
  1605. "CROS-EC utility command",
  1606. "init Re-init CROS-EC (done on startup automatically)\n"
  1607. "crosec id Read CROS-EC ID\n"
  1608. "crosec info Read CROS-EC info\n"
  1609. "crosec curimage Read CROS-EC current image\n"
  1610. "crosec hash Read CROS-EC hash\n"
  1611. "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
  1612. "crosec events Read CROS-EC host events\n"
  1613. "crosec clrevents [mask] Clear CROS-EC host events\n"
  1614. "crosec regioninfo <ro|rw> Read image info\n"
  1615. "crosec erase <ro|rw> Erase EC image\n"
  1616. "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
  1617. "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
  1618. "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
  1619. "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
  1620. "crosec test run tests on cros_ec\n"
  1621. "crosec version Read CROS-EC version\n"
  1622. "crosec i2c md chip address[.0, .1, .2] [# of objects] - read from I2C passthru\n"
  1623. "crosec i2c mw chip address[.0, .1, .2] value [count] - write to I2C passthru (fill)"
  1624. );
  1625. #endif
  1626. #ifdef CONFIG_DM_CROS_EC
  1627. UCLASS_DRIVER(cros_ec) = {
  1628. .id = UCLASS_CROS_EC,
  1629. .name = "cros_ec",
  1630. .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
  1631. };
  1632. #endif