| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <commonlib/endian.h> |
| 19 |
#include <commonlib/helpers.h> |
| 20 |
#include <console/console.h> |
| 21 |
#include <delay.h> |
| 22 |
#include <device/i2c_simple.h> |
| 23 |
#include <drivers/tpm/cr50.h> |
| 24 |
#include <endian.h> |
| 25 |
#include <security/tpm/tis.h> |
| 26 |
#include <string.h> |
| 27 |
#include <types.h> |
| 28 |
#include <timer.h> |
| 29 |
|
| 30 |
#include "tpm.h" |
| 31 |
|
| 32 |
#define CR50_MAX_BUFSIZE 63 |
| 33 |
#define CR50_TIMEOUT_INIT_MS 30000 |
| 34 |
#define CR50_TIMEOUT_LONG_MS 2000 |
| 35 |
#define CR50_TIMEOUT_SHORT_MS 2 |
| 36 |
#define CR50_DID_VID 0x00281ae0L |
| 37 |
#define TI50_DT_DID_VID 0x504a6666L |
| 38 |
#define TI50_OT_DID_VID 0x50666666L |
| 39 |
|
| 40 |
struct tpm_inf_dev { |
| 41 |
int bus; |
| 42 |
int locality; |
| 43 |
unsigned int addr; |
| 44 |
uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)]; |
| 45 |
}; |
| 46 |
|
| 47 |
static struct tpm_inf_dev tpm_dev; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static tpm_result_t cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len) |
| 63 |
{ |
| 64 |
if (tpm_dev.addr == 0) |
| 65 |
return TPM_CB_INVALID_ARG; |
| 66 |
|
| 67 |
|
| 68 |
cr50_plat_irq_status(); |
| 69 |
|
| 70 |
|
| 71 |
if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) { |
| 72 |
printk(BIOS_ERR, "%s: Address write failed\n", __func__); |
| 73 |
return TPM_CB_COMMUNICATION_ERROR; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if (cr50_wait_tpm_ready() != CB_SUCCESS) |
| 78 |
return TPM_CB_TIMEOUT; |
| 79 |
|
| 80 |
|
| 81 |
if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) { |
| 82 |
printk(BIOS_ERR, "%s: Read response failed\n", __func__); |
| 83 |
return TPM_CB_COMMUNICATION_ERROR; |
| 84 |
} |
| 85 |
|
| 86 |
return TPM_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static tpm_result_t cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len) |
| 103 |
{ |
| 104 |
if (tpm_dev.addr == 0) |
| 105 |
return TPM_CB_INVALID_ARG; |
| 106 |
if (len > CR50_MAX_BUFSIZE) |
| 107 |
return TPM_CB_INVALID_ARG; |
| 108 |
|
| 109 |
|
| 110 |
tpm_dev.buf[0] = addr; |
| 111 |
memcpy(tpm_dev.buf + 1, buffer, len); |
| 112 |
|
| 113 |
|
| 114 |
cr50_plat_irq_status(); |
| 115 |
|
| 116 |
|
| 117 |
if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) { |
| 118 |
printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__); |
| 119 |
return TPM_CB_COMMUNICATION_ERROR; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
return cr50_wait_tpm_ready() == CB_SUCCESS ? TPM_SUCCESS : TPM_CB_TIMEOUT; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
static tpm_result_t process_reset(void) |
| 136 |
{ |
| 137 |
struct stopwatch sw; |
| 138 |
tpm_result_t rc = TPM_SUCCESS; |
| 139 |
uint8_t access; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS); |
| 150 |
do { |
| 151 |
const uint8_t mask = |
| 152 |
TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY; |
| 153 |
|
| 154 |
rc = cr50_i2c_read(TPM_ACCESS(0), |
| 155 |
&access, sizeof(access)); |
| 156 |
if (rc || ((access & mask) == mask)) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
mdelay(2); |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
printk(BIOS_INFO, "TPM ready after %lld ms\n", |
| 166 |
stopwatch_duration_msecs(&sw)); |
| 167 |
|
| 168 |
return TPM_SUCCESS; |
| 169 |
} while (!stopwatch_expired(&sw)); |
| 170 |
|
| 171 |
if (rc) { |
| 172 |
printk(BIOS_ERR, "Failed to read TPM with error %d\n", rc); |
| 173 |
return rc; |
| 174 |
} else |
| 175 |
printk(BIOS_ERR, |
| 176 |
"TPM failed to reset after %lld ms, status: %#x\n", |
| 177 |
stopwatch_duration_msecs(&sw), access); |
| 178 |
return TPM_CB_FAIL; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
static tpm_result_t claim_locality(void) |
| 189 |
{ |
| 190 |
uint8_t access; |
| 191 |
const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY; |
| 192 |
tpm_result_t rc = TPM_SUCCESS; |
| 193 |
|
| 194 |
rc = cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)); |
| 195 |
if (rc) |
| 196 |
return rc; |
| 197 |
|
| 198 |
if ((access & mask) == mask) { |
| 199 |
printk(BIOS_INFO, "Locality already claimed\n"); |
| 200 |
return TPM_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
access = TPM_ACCESS_REQUEST_USE; |
| 204 |
rc = cr50_i2c_write(TPM_ACCESS(0), |
| 205 |
&access, sizeof(access)); |
| 206 |
if (rc) |
| 207 |
return rc; |
| 208 |
|
| 209 |
rc = cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)); |
| 210 |
if (rc) |
| 211 |
return rc; |
| 212 |
|
| 213 |
if ((access & mask) != mask) { |
| 214 |
printk(BIOS_INFO, "Failed to claim locality.\n"); |
| 215 |
return TPM_CB_FAIL; |
| 216 |
} |
| 217 |
|
| 218 |
return TPM_SUCCESS; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static uint8_t cr50_i2c_tis_status(void) |
| 229 |
{ |
| 230 |
uint8_t buf[4]; |
| 231 |
tpm_result_t rc = cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)); |
| 232 |
if (rc) { |
| 233 |
printk(BIOS_ERR, "%s: Failed to read status with error %#x\n", __func__, rc); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
return buf[0]; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
static void cr50_i2c_tis_ready(void) |
| 241 |
{ |
| 242 |
uint8_t buf[4] = { TPM_STS_COMMAND_READY }; |
| 243 |
cr50_i2c_write(TPM_STS(tpm_dev.locality), buf, sizeof(buf)); |
| 244 |
mdelay(CR50_TIMEOUT_SHORT_MS); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
static tpm_result_t cr50_i2c_wait_burststs(uint8_t mask, size_t *burst, int *status) |
| 253 |
{ |
| 254 |
uint8_t buf[4]; |
| 255 |
struct stopwatch sw; |
| 256 |
tpm_result_t rc = TPM_SUCCESS; |
| 257 |
|
| 258 |
stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
| 259 |
|
| 260 |
while (!stopwatch_expired(&sw)) { |
| 261 |
rc = cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)); |
| 262 |
if (rc) { |
| 263 |
mdelay(CR50_TIMEOUT_SHORT_MS); |
| 264 |
continue; |
| 265 |
} |
| 266 |
|
| 267 |
*status = buf[0]; |
| 268 |
*burst = read_le16(&buf[1]); |
| 269 |
|
| 270 |
|
| 271 |
if ((*status & mask) == mask && |
| 272 |
*burst > 0 && *burst <= CR50_MAX_BUFSIZE) |
| 273 |
return TPM_SUCCESS; |
| 274 |
|
| 275 |
mdelay(CR50_TIMEOUT_SHORT_MS); |
| 276 |
} |
| 277 |
printk(BIOS_ERR, "%s: Timeout reading burst and status with error %#x\n", __func__, rc); |
| 278 |
if (rc) |
| 279 |
return rc; |
| 280 |
return TPM_CB_COMMUNICATION_ERROR; |
| 281 |
} |
| 282 |
|
| 283 |
static int cr50_i2c_tis_recv(uint8_t *buf, size_t buf_len) |
| 284 |
{ |
| 285 |
size_t burstcnt, current, len, expected; |
| 286 |
uint8_t addr = TPM_DATA_FIFO(tpm_dev.locality); |
| 287 |
uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL; |
| 288 |
int status; |
| 289 |
tpm_result_t rc = TPM_SUCCESS; |
| 290 |
|
| 291 |
if (buf_len < TPM_HEADER_SIZE) |
| 292 |
goto out_err; |
| 293 |
|
| 294 |
rc = cr50_i2c_wait_burststs(mask, &burstcnt, &status); |
| 295 |
if (rc) { |
| 296 |
printk(BIOS_ERR, "%s: First chunk not available with error %#x\n", __func__, rc); |
| 297 |
goto out_err; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
rc = cr50_i2c_read(addr, buf, burstcnt); |
| 302 |
if (rc) { |
| 303 |
printk(BIOS_ERR, "%s: Read failed with error %#x\n", __func__, rc); |
| 304 |
goto out_err; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
expected = read_be32(buf + TPM_RSP_SIZE_BYTE); |
| 309 |
if (expected > buf_len) { |
| 310 |
printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n", |
| 311 |
__func__, expected, buf_len); |
| 312 |
goto out_err; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
current = burstcnt; |
| 317 |
while (current < expected) { |
| 318 |
|
| 319 |
if (cr50_i2c_wait_burststs(mask, &burstcnt, &status)) |
| 320 |
goto out_err; |
| 321 |
|
| 322 |
len = MIN(burstcnt, expected - current); |
| 323 |
rc = cr50_i2c_read(addr, buf + current, len); |
| 324 |
if (rc) { |
| 325 |
printk(BIOS_ERR, "%s: Read failed with error %#x\n", __func__, rc); |
| 326 |
goto out_err; |
| 327 |
} |
| 328 |
|
| 329 |
current += len; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status)) |
| 334 |
goto out_err; |
| 335 |
if (status & TPM_STS_DATA_AVAIL) { |
| 336 |
printk(BIOS_ERR, "%s: Data still available\n", __func__); |
| 337 |
goto out_err; |
| 338 |
} |
| 339 |
|
| 340 |
return current; |
| 341 |
|
| 342 |
out_err: |
| 343 |
|
| 344 |
if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY) |
| 345 |
cr50_i2c_tis_ready(); |
| 346 |
return -1; |
| 347 |
} |
| 348 |
|
| 349 |
static int cr50_i2c_tis_send(uint8_t *buf, size_t len) |
| 350 |
{ |
| 351 |
int status; |
| 352 |
size_t burstcnt, limit, sent = 0; |
| 353 |
uint8_t tpm_go[4] = { TPM_STS_GO }; |
| 354 |
struct stopwatch sw; |
| 355 |
tpm_result_t rc = TPM_SUCCESS; |
| 356 |
|
| 357 |
stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS); |
| 358 |
|
| 359 |
|
| 360 |
while (!(cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)) { |
| 361 |
if (stopwatch_expired(&sw)) { |
| 362 |
printk(BIOS_ERR, "%s: Command ready timeout\n", |
| 363 |
__func__); |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
cr50_i2c_tis_ready(); |
| 368 |
} |
| 369 |
|
| 370 |
while (len > 0) { |
| 371 |
uint8_t mask = TPM_STS_VALID; |
| 372 |
|
| 373 |
|
| 374 |
if (sent > 0) |
| 375 |
mask |= TPM_STS_DATA_EXPECT; |
| 376 |
|
| 377 |
|
| 378 |
if (cr50_i2c_wait_burststs(mask, &burstcnt, &status)) |
| 379 |
goto out_err; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
limit = MIN(burstcnt - 1, len); |
| 384 |
rc = cr50_i2c_write(TPM_DATA_FIFO(tpm_dev.locality), &buf[sent], limit); |
| 385 |
if (rc) { |
| 386 |
printk(BIOS_ERR, "%s: Write failed with error %#x\n", __func__, rc); |
| 387 |
goto out_err; |
| 388 |
} |
| 389 |
|
| 390 |
sent += limit; |
| 391 |
len -= limit; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status)) |
| 396 |
goto out_err; |
| 397 |
if (status & TPM_STS_DATA_EXPECT) { |
| 398 |
printk(BIOS_ERR, "%s: Data still expected\n", __func__); |
| 399 |
goto out_err; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
rc = cr50_i2c_write(TPM_STS(tpm_dev.locality), tpm_go, sizeof(tpm_go)); |
| 404 |
if (rc) { |
| 405 |
printk(BIOS_ERR, "%s: Start command failed with error %#x\n", __func__, rc); |
| 406 |
goto out_err; |
| 407 |
} |
| 408 |
return sent; |
| 409 |
|
| 410 |
out_err: |
| 411 |
|
| 412 |
if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY) |
| 413 |
cr50_i2c_tis_ready(); |
| 414 |
return -1; |
| 415 |
} |
| 416 |
|
| 417 |
static void cr50_vendor_init(struct tpm_chip *chip) |
| 418 |
{ |
| 419 |
chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 420 |
chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 421 |
chip->req_canceled = TPM_STS_COMMAND_READY; |
| 422 |
chip->status = &cr50_i2c_tis_status; |
| 423 |
chip->recv = &cr50_i2c_tis_recv; |
| 424 |
chip->send = &cr50_i2c_tis_send; |
| 425 |
chip->cancel = &cr50_i2c_tis_ready; |
| 426 |
} |
| 427 |
|
| 428 |
tpm_result_t tpm_vendor_probe(unsigned int bus, uint32_t addr, enum tpm_family *family) |
| 429 |
{ |
| 430 |
|
| 431 |
if (family != NULL) |
| 432 |
*family = TPM_2; |
| 433 |
return TPM_SUCCESS; |
| 434 |
} |
| 435 |
|
| 436 |
static tpm_result_t cr50_i2c_probe(uint32_t *did_vid) |
| 437 |
{ |
| 438 |
int retries; |
| 439 |
tpm_result_t rc = TPM_SUCCESS; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
printk(BIOS_INFO, "Probing TPM I2C: "); |
| 448 |
|
| 449 |
for (retries = 100; retries > 0; retries--) { |
| 450 |
rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4); |
| 451 |
|
| 452 |
|
| 453 |
if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DT_DID_VID || |
| 454 |
*did_vid == TI50_OT_DID_VID)) { |
| 455 |
printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid); |
| 456 |
return TPM_SUCCESS; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
mdelay(10); |
| 461 |
printk(BIOS_INFO, "."); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
if (!rc) { |
| 468 |
printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid); |
| 469 |
return TPM_CB_FAIL; |
| 470 |
} |
| 471 |
return TPM_CB_COMMUNICATION_ERROR; |
| 472 |
} |
| 473 |
|
| 474 |
tpm_result_t tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr) |
| 475 |
{ |
| 476 |
uint32_t did_vid = 0; |
| 477 |
tpm_result_t rc = TPM_SUCCESS; |
| 478 |
|
| 479 |
if (dev_addr == 0) { |
| 480 |
printk(BIOS_ERR, "%s: missing device address\n", __func__); |
| 481 |
return TPM_CB_INVALID_ARG; |
| 482 |
} |
| 483 |
|
| 484 |
tpm_dev.bus = bus; |
| 485 |
tpm_dev.addr = dev_addr; |
| 486 |
|
| 487 |
cr50_vendor_init(chip); |
| 488 |
|
| 489 |
rc = cr50_i2c_probe(&did_vid); |
| 490 |
if (rc) |
| 491 |
return rc; |
| 492 |
|
| 493 |
if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK) { |
| 494 |
rc = process_reset(); |
| 495 |
if (rc) |
| 496 |
return rc; |
| 497 |
} |
| 498 |
|
| 499 |
rc = claim_locality(); |
| 500 |
if (rc) |
| 501 |
return rc; |
| 502 |
|
| 503 |
printk(BIOS_DEBUG, "GSC TPM 2.0 (i2c %u:0x%02x id %#x)\n", |
| 504 |
bus, dev_addr, did_vid >> 16); |
| 505 |
|
| 506 |
if (tpm_first_access_this_boot()) { |
| 507 |
|
| 508 |
cr50_get_firmware_version(NULL); |
| 509 |
cr50_set_board_cfg(); |
| 510 |
} |
| 511 |
|
| 512 |
return TPM_SUCCESS; |
| 513 |
} |
| 514 |
|
| 515 |
enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes) |
| 516 |
{ |
| 517 |
return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS; |
| 518 |
} |
| 519 |
|
| 520 |
enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes) |
| 521 |
{ |
| 522 |
return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS; |
| 523 |
} |