Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
/* See what type of parent we are inside (this is expensive) */
parent = fdt_parent_offset(blob, node);
if (parent < 0) {
debug("%s: Cannot find node parent\n", __func__);
return -1;
}
dev = &static_dev;
dev->node = node;
dev->parent_node = parent;
compat = fdtdec_lookup(blob, parent);
switch (compat) {
#ifdef CONFIG_CROS_EC_SPI
case COMPAT_SAMSUNG_EXYNOS_SPI:
dev->interface = CROS_EC_IF_SPI;
if (cros_ec_spi_decode_fdt(dev, blob))
return -1;
break;
#endif
#ifdef CONFIG_CROS_EC_I2C
case COMPAT_SAMSUNG_S3C2440_I2C:
dev->interface = CROS_EC_IF_I2C;
if (cros_ec_i2c_decode_fdt(dev, blob))
return -1;
break;
#endif
#ifdef CONFIG_CROS_EC_LPC
case COMPAT_INTEL_LPC:
dev->interface = CROS_EC_IF_LPC;
break;
#endif
default:
debug("%s: Unknown compat id %d\n", __func__, compat);
return -1;
}
fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
dev->optimise_flash_write = fdtdec_get_bool(blob, node,
"optimise-flash-write");
*devp = dev;
return 0;
}
int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
{
char id[MSG_BYTES];
struct cros_ec_dev *dev;
int node = 0;
*cros_ecp = NULL;
do {
node = fdtdec_next_compatible(blob, node,
COMPAT_GOOGLE_CROS_EC);
if (node < 0) {
debug("%s: Node not found\n", __func__);
return 0;
}
} while (!fdtdec_get_is_enabled(blob, node));
if (cros_ec_decode_fdt(blob, node, &dev)) {
debug("%s: Failed to decode device.\n", __func__);
return -CROS_EC_ERR_FDT_DECODE;
}
switch (dev->interface) {
#ifdef CONFIG_CROS_EC_SPI
case CROS_EC_IF_SPI:
if (cros_ec_spi_init(dev, blob)) {
debug("%s: Could not setup SPI interface\n", __func__);
return -CROS_EC_ERR_DEV_INIT;
}
break;
#endif
#ifdef CONFIG_CROS_EC_I2C
case CROS_EC_IF_I2C:
if (cros_ec_i2c_init(dev, blob))
return -CROS_EC_ERR_DEV_INIT;
break;
#endif
#ifdef CONFIG_CROS_EC_LPC
case CROS_EC_IF_LPC:
if (cros_ec_lpc_init(dev, blob))
return -CROS_EC_ERR_DEV_INIT;
break;
#endif
case CROS_EC_IF_NONE:
default:
return 0;
}
/* we will poll the EC interrupt line */
fdtdec_setup_gpio(&dev->ec_int);
if (fdt_gpio_isvalid(&dev->ec_int))
gpio_direction_input(dev->ec_int.gpio);
if (cros_ec_check_version(dev)) {
debug("%s: Could not detect CROS-EC version\n", __func__);
return -CROS_EC_ERR_CHECK_VERSION;
}
if (cros_ec_read_id(dev, id, sizeof(id))) {
debug("%s: Could not read KBC ID\n", __func__);
return -CROS_EC_ERR_READ_ID;
}
/* Remember this device for use by the cros_ec command */
last_dev = *cros_ecp = dev;
debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
return 0;
}
int cros_ec_decode_region(int argc, char * const argv[])
{
if (argc > 0) {
if (0 == strcmp(*argv, "rw"))
return EC_FLASH_REGION_RW;
else if (0 == strcmp(*argv, "ro"))
return EC_FLASH_REGION_RO;
debug("%s: Invalid region '%s'\n", __func__, *argv);
} else {
debug("%s: Missing region parameter\n", __func__);
}
return -1;
}
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
{
int flash_node, node;
node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
if (node < 0) {
debug("Failed to find chrome-ec node'\n");
return -1;
}
flash_node = fdt_subnode_offset(blob, node, "flash");
if (flash_node < 0) {
debug("Failed to find flash node\n");
return -1;
}
if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
&config->flash)) {
debug("Failed to decode flash node in chrome-ec'\n");
return -1;
}
config->flash_erase_value = fdtdec_get_int(blob, flash_node,
"erase-value", -1);
for (node = fdt_first_subnode(blob, flash_node); node >= 0;
node = fdt_next_subnode(blob, node)) {
const char *name = fdt_get_name(blob, node, NULL);
enum ec_flash_region region;
if (0 == strcmp(name, "ro")) {
region = EC_FLASH_REGION_RO;
} else if (0 == strcmp(name, "rw")) {
region = EC_FLASH_REGION_RW;
} else if (0 == strcmp(name, "wp-ro")) {
region = EC_FLASH_REGION_WP_RO;
} else {
debug("Unknown EC flash region name '%s'\n", name);
return -1;
}
if (fdtdec_read_fmap_entry(blob, node, "reg",
&config->region[region])) {
debug("Failed to decode flash region in chrome-ec'\n");
return -1;
}
}
return 0;
}
#ifdef CONFIG_CMD_CROS_EC
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
/**
* Perform a flash read or write command
*
* @param dev CROS-EC device to read/write
* @param is_write 1 do to a write, 0 to do a read
* @param argc Number of arguments
* @param argv Arguments (2 is region, 3 is address)
* @return 0 for ok, 1 for a usage error or -ve for ec command error
* (negative EC_RES_...)
*/
static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
char * const argv[])
{
uint32_t offset, size = -1U, region_size;
unsigned long addr;
char *endp;
int region;
int ret;
region = cros_ec_decode_region(argc - 2, argv + 2);
if (region == -1)
return 1;
if (argc < 4)
return 1;
addr = simple_strtoul(argv[3], &endp, 16);
if (*argv[3] == 0 || *endp != 0)
return 1;
if (argc > 4) {
size = simple_strtoul(argv[4], &endp, 16);
if (*argv[4] == 0 || *endp != 0)
return 1;
}
ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
if (ret) {
debug("%s: Could not read region info\n", __func__);
return ret;
}
if (size == -1U)
size = region_size;
ret = is_write ?
cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
if (ret) {
debug("%s: Could not %s region\n", __func__,
is_write ? "write" : "read");
return ret;
}
return 0;
}
static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
struct cros_ec_dev *dev = last_dev;
const char *cmd;
int ret = 0;
if (argc < 2)
return CMD_RET_USAGE;
cmd = argv[1];
if (0 == strcmp("init", cmd)) {
ret = cros_ec_init(gd->fdt_blob, &dev);
if (ret) {
printf("Could not init cros_ec device (err %d)\n", ret);
return 1;
}
return 0;
}
/* Just use the last allocated device; there should be only one */
if (!last_dev) {
printf("No CROS-EC device available\n");
return 1;
}
if (0 == strcmp("id", cmd)) {
char id[MSG_BYTES];
if (cros_ec_read_id(dev, id, sizeof(id))) {
debug("%s: Could not read KBC ID\n", __func__);
return 1;
}
printf("%s\n", id);
} else if (0 == strcmp("info", cmd)) {
struct ec_response_mkbp_info info;
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
if (cros_ec_info(dev, &info)) {
debug("%s: Could not read KBC info\n", __func__);
return 1;
}
printf("rows = %u\n", info.rows);
printf("cols = %u\n", info.cols);
printf("switches = %#x\n", info.switches);
} else if (0 == strcmp("curimage", cmd)) {
enum ec_current_image image;
if (cros_ec_read_current_image(dev, &image)) {
debug("%s: Could not read KBC image\n", __func__);
return 1;
}
printf("%d\n", image);
} else if (0 == strcmp("hash", cmd)) {
struct ec_response_vboot_hash hash;
int i;
if (cros_ec_read_hash(dev, &hash)) {
debug("%s: Could not read KBC hash\n", __func__);
return 1;
}
if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
printf("type: SHA-256\n");
else
printf("type: %d\n", hash.hash_type);
printf("offset: 0x%08x\n", hash.offset);
printf("size: 0x%08x\n", hash.size);
printf("digest: ");
for (i = 0; i < hash.digest_size; i++)
printf("%02x", hash.hash_digest[i]);
printf("\n");
} else if (0 == strcmp("reboot", cmd)) {
int region;
enum ec_reboot_cmd cmd;
if (argc >= 3 && !strcmp(argv[2], "cold"))
cmd = EC_REBOOT_COLD;
else {
region = cros_ec_decode_region(argc - 2, argv + 2);
if (region == EC_FLASH_REGION_RO)
cmd = EC_REBOOT_JUMP_RO;
else if (region == EC_FLASH_REGION_RW)
cmd = EC_REBOOT_JUMP_RW;
else
return CMD_RET_USAGE;
}
if (cros_ec_reboot(dev, cmd, 0)) {
debug("%s: Could not reboot KBC\n", __func__);
return 1;
}
} else if (0 == strcmp("events", cmd)) {
uint32_t events;
if (cros_ec_get_host_events(dev, &events)) {
debug("%s: Could not read host events\n", __func__);
return 1;
}
printf("0x%08x\n", events);
} else if (0 == strcmp("clrevents", cmd)) {
uint32_t events = 0x7fffffff;
if (argc >= 3)
events = simple_strtol(argv[2], NULL, 0);
if (cros_ec_clear_host_events(dev, events)) {
debug("%s: Could not clear host events\n", __func__);
return 1;
}
} else if (0 == strcmp("read", cmd)) {
ret = do_read_write(dev, 0, argc, argv);
if (ret > 0)
return CMD_RET_USAGE;
} else if (0 == strcmp("write", cmd)) {
ret = do_read_write(dev, 1, argc, argv);
if (ret > 0)
return CMD_RET_USAGE;
} else if (0 == strcmp("erase", cmd)) {
int region = cros_ec_decode_region(argc - 2, argv + 2);
uint32_t offset, size;
if (region == -1)
return CMD_RET_USAGE;
if (cros_ec_flash_offset(dev, region, &offset, &size)) {
debug("%s: Could not read region info\n", __func__);
ret = -1;
} else {
ret = cros_ec_flash_erase(dev, offset, size);
if (ret) {
debug("%s: Could not erase region\n",
__func__);
}
}
} else if (0 == strcmp("regioninfo", cmd)) {
int region = cros_ec_decode_region(argc - 2, argv + 2);
uint32_t offset, size;
if (region == -1)
return CMD_RET_USAGE;
ret = cros_ec_flash_offset(dev, region, &offset, &size);
if (ret) {
debug("%s: Could not read region info\n", __func__);
} else {
printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
"RO" : "RW");
printf("Offset: %x\n", offset);
printf("Size: %x\n", size);
}
} else if (0 == strcmp("vbnvcontext", cmd)) {
uint8_t block[EC_VBNV_BLOCK_SIZE];
char buf[3];
int i, len;
unsigned long result;
if (argc <= 2) {
ret = cros_ec_read_vbnvcontext(dev, block);
if (!ret) {
printf("vbnv_block: ");
for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
printf("%02x", block[i]);
putc('\n');
}
} else {
/*
* TODO(clchiou): Move this to a utility function as
* cmd_spi might want to call it.
*/
memset(block, 0, EC_VBNV_BLOCK_SIZE);
len = strlen(argv[2]);
buf[2] = '\0';
for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
if (i * 2 >= len)
break;
buf[0] = argv[2][i * 2];
if (i * 2 + 1 >= len)
buf[1] = '0';
else
buf[1] = argv[2][i * 2 + 1];
strict_strtoul(buf, 16, &result);
block[i] = result;
}
ret = cros_ec_write_vbnvcontext(dev, block);
}
if (ret) {
debug("%s: Could not %s VbNvContext\n", __func__,
argc <= 2 ? "read" : "write");
}
} else if (0 == strcmp("test", cmd)) {
int result = cros_ec_test(dev);
if (result)
printf("Test failed with error %d\n", result);
else
puts("Test passed\n");
} else if (0 == strcmp("version", cmd)) {
struct ec_response_get_version *p;
char *build_string;
ret = cros_ec_read_version(dev, &p);
if (!ret) {
/* Print versions */
printf("RO version: %1.*s\n",
sizeof(p->version_string_ro),
p->version_string_ro);
printf("RW version: %1.*s\n",
sizeof(p->version_string_rw),
p->version_string_rw);
printf("Firmware copy: %s\n",
(p->current_image <
ARRAY_SIZE(ec_current_image_name) ?
ec_current_image_name[p->current_image] :
"?"));
ret = cros_ec_read_build_info(dev, &build_string);
if (!ret)
printf("Build info: %s\n", build_string);
}
} else if (0 == strcmp("ldo", cmd)) {
uint8_t index, state;
char *endp;
if (argc < 3)
return CMD_RET_USAGE;
index = simple_strtoul(argv[2], &endp, 10);
if (*argv[2] == 0 || *endp != 0)
return CMD_RET_USAGE;
if (argc > 3) {
state = simple_strtoul(argv[3], &endp, 10);
if (*argv[3] == 0 || *endp != 0)
return CMD_RET_USAGE;
ret = cros_ec_set_ldo(dev, index, state);
} else {
ret = cros_ec_get_ldo(dev, index, &state);
if (!ret) {
printf("LDO%d: %s\n", index,
state == EC_LDO_STATE_ON ?
"on" : "off");
}
}
if (ret) {
debug("%s: Could not access LDO%d\n", __func__, index);
return ret;
}
} else {
return CMD_RET_USAGE;
}
if (ret < 0) {
printf("Error: CROS-EC command failed (error %d)\n", ret);
ret = 1;
}
return ret;
}
U_BOOT_CMD(
crosec, 5, 1, do_cros_ec,
"CROS-EC utility command",
"init Re-init CROS-EC (done on startup automatically)\n"
"crosec id Read CROS-EC ID\n"
"crosec info Read CROS-EC info\n"
"crosec curimage Read CROS-EC current image\n"
"crosec hash Read CROS-EC hash\n"
"crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
"crosec events Read CROS-EC host events\n"
"crosec clrevents [mask] Clear CROS-EC host events\n"
"crosec regioninfo <ro|rw> Read image info\n"
"crosec erase <ro|rw> Erase EC image\n"
"crosec read <ro|rw> <addr> [<size>] Read EC image\n"
"crosec write <ro|rw> <addr> [<size>] Write EC image\n"
"crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
"crosec ldo <idx> [<state>] Switch/Read LDO state\n"
"crosec test run tests on cros_ec\n"
"crosec version Read CROS-EC version"
);
#endif