cros_ec.c 43 KB

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