| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include <commonlib/helpers.h> |
| 14 |
#include <string.h> |
| 15 |
#include <delay.h> |
| 16 |
#include <device/mmio.h> |
| 17 |
#include <acpi/acpi.h> |
| 18 |
#include <acpi/acpigen.h> |
| 19 |
#include <acpi/acpi_device.h> |
| 20 |
#include <device/device.h> |
| 21 |
#include <console/console.h> |
| 22 |
#include <security/tpm/tis.h> |
| 23 |
#include <security/tpm/tss.h> |
| 24 |
#include <device/pnp.h> |
| 25 |
#include <drivers/tpm/tpm_ppi.h> |
| 26 |
#include <timer.h> |
| 27 |
|
| 28 |
#include "chip.h" |
| 29 |
#include "tpm.h" |
| 30 |
|
| 31 |
#define PREFIX "lpc_tpm: " |
| 32 |
|
| 33 |
|
| 34 |
#define TPM_DEBUG(fmt, args...) \ |
| 35 |
if (CONFIG(DEBUG_TPM)) { \ |
| 36 |
printk(BIOS_DEBUG, PREFIX); \ |
| 37 |
printk(BIOS_DEBUG, fmt, ##args); \ |
| 38 |
} |
| 39 |
#define TPM_DEBUG_IO_READ(reg_, val_) \ |
| 40 |
TPM_DEBUG("Read reg %#x returns %#x\n", (reg_), (val_)) |
| 41 |
#define TPM_DEBUG_IO_WRITE(reg_, val_) \ |
| 42 |
TPM_DEBUG("Write reg %#x with %#x\n", (reg_), (val_)) |
| 43 |
#define printf(x...) printk(BIOS_ERR, x) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#define TIS_REG(LOCALITY, REG) \ |
| 49 |
(void *)(uintptr_t)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG) |
| 50 |
|
| 51 |
|
| 52 |
#define TIS_REG_ACCESS 0x0 |
| 53 |
#define TIS_REG_INT_ENABLE 0x8 |
| 54 |
#define TIS_REG_INT_VECTOR 0xc |
| 55 |
#define TIS_REG_INT_STATUS 0x10 |
| 56 |
#define TIS_REG_INTF_CAPABILITY 0x14 |
| 57 |
#define TIS_REG_STS 0x18 |
| 58 |
#define TIS_REG_BURST_COUNT 0x19 |
| 59 |
#define TIS_REG_DATA_FIFO 0x24 |
| 60 |
#define TIS_REG_INTF_ID 0x30 |
| 61 |
#define TIS_REG_DID_VID 0xf00 |
| 62 |
#define TIS_REG_RID 0xf04 |
| 63 |
|
| 64 |
|
| 65 |
#define TIS_STS_VALID (1 << 7) |
| 66 |
#define TIS_STS_COMMAND_READY (1 << 6) |
| 67 |
#define TIS_STS_TPM_GO (1 << 5) |
| 68 |
#define TIS_STS_DATA_AVAILABLE (1 << 4) |
| 69 |
#define TIS_STS_EXPECT (1 << 3) |
| 70 |
#define TIS_STS_RESPONSE_RETRY (1 << 1) |
| 71 |
|
| 72 |
#define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) |
| 73 |
#define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) |
| 74 |
#define TIS_ACCESS_BEEN_SEIZED (1 << 4) |
| 75 |
#define TIS_ACCESS_SEIZE (1 << 3) |
| 76 |
#define TIS_ACCESS_PENDING_REQUEST (1 << 2) |
| 77 |
#define TIS_ACCESS_REQUEST_USE (1 << 1) |
| 78 |
#define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) |
| 79 |
|
| 80 |
|
| 81 |
#define MAX_DELAY_US USECS_PER_SEC |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
struct device_name { |
| 88 |
u16 dev_id; |
| 89 |
enum tpm_family family; |
| 90 |
const char *const dev_name; |
| 91 |
}; |
| 92 |
|
| 93 |
struct vendor_name { |
| 94 |
u16 vendor_id; |
| 95 |
const char *vendor_name; |
| 96 |
const struct device_name *dev_names; |
| 97 |
}; |
| 98 |
|
| 99 |
static const struct device_name atmel_devices[] = { |
| 100 |
{0x3204, TPM_1, "AT97SC3204"}, |
| 101 |
{0xffff} |
| 102 |
}; |
| 103 |
|
| 104 |
static const struct device_name infineon_devices[] = { |
| 105 |
{0x000b, TPM_1, "SLB9635 TT 1.2"}, |
| 106 |
{0x001a, TPM_1, "SLB9660 TT 1.2"}, |
| 107 |
{0x001b, TPM_1, "SLB9670 TT 1.2"}, |
| 108 |
{0x001a, TPM_2, "SLB9665 TT 2.0"}, |
| 109 |
{0x001b, TPM_2, "SLB9670 TT 2.0"}, |
| 110 |
{0x001d, TPM_2, "SLB9672 TT 2.0"}, |
| 111 |
{0xffff} |
| 112 |
}; |
| 113 |
|
| 114 |
static const struct device_name nuvoton_devices[] = { |
| 115 |
{0x00fe, TPM_1, "NPCT420AA V2"}, |
| 116 |
{0xffff} |
| 117 |
}; |
| 118 |
|
| 119 |
static const struct device_name stmicro_devices[] = { |
| 120 |
{0x0000, TPM_1, "ST33ZP24" }, |
| 121 |
{0xffff} |
| 122 |
}; |
| 123 |
|
| 124 |
static const struct device_name swtpm_devices[] = { |
| 125 |
{0x0001, TPM_1, "SwTPM 1.2" }, |
| 126 |
{0x0001, TPM_2, "SwTPM 2.0" }, |
| 127 |
{0xffff} |
| 128 |
}; |
| 129 |
|
| 130 |
static const struct vendor_name vendor_names[] = { |
| 131 |
{0x1114, "Atmel", atmel_devices}, |
| 132 |
{0x15d1, "Infineon", infineon_devices}, |
| 133 |
{0x1050, "Nuvoton", nuvoton_devices}, |
| 134 |
{0x1014, "TPM Emulator", swtpm_devices}, |
| 135 |
{0x104a, "ST Microelectronics", stmicro_devices}, |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
static u32 vendor_dev_id; |
| 143 |
|
| 144 |
static inline u8 tpm_read_status(int locality) |
| 145 |
{ |
| 146 |
u8 value = read8(TIS_REG(locality, TIS_REG_STS)); |
| 147 |
TPM_DEBUG_IO_READ(TIS_REG_STS, value); |
| 148 |
return value; |
| 149 |
} |
| 150 |
|
| 151 |
static inline void tpm_write_status(u8 sts, int locality) |
| 152 |
{ |
| 153 |
TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts); |
| 154 |
write8(TIS_REG(locality, TIS_REG_STS), sts); |
| 155 |
} |
| 156 |
|
| 157 |
static inline u8 tpm_read_data(int locality) |
| 158 |
{ |
| 159 |
u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO)); |
| 160 |
TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value); |
| 161 |
return value; |
| 162 |
} |
| 163 |
|
| 164 |
static inline void tpm_write_data(u8 data, int locality) |
| 165 |
{ |
| 166 |
TPM_DEBUG_IO_WRITE(TIS_REG_DATA_FIFO, data); |
| 167 |
write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data); |
| 168 |
} |
| 169 |
|
| 170 |
static inline u16 tpm_read_burst_count(int locality) |
| 171 |
{ |
| 172 |
u16 count; |
| 173 |
count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT)); |
| 174 |
count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8; |
| 175 |
TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count); |
| 176 |
return count; |
| 177 |
} |
| 178 |
|
| 179 |
static inline u8 tpm_read_access(int locality) |
| 180 |
{ |
| 181 |
u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS)); |
| 182 |
TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value); |
| 183 |
return value; |
| 184 |
} |
| 185 |
|
| 186 |
static inline void tpm_write_access(u8 data, int locality) |
| 187 |
{ |
| 188 |
TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data); |
| 189 |
write8(TIS_REG(locality, TIS_REG_ACCESS), data); |
| 190 |
} |
| 191 |
|
| 192 |
static inline u32 tpm_read_intf_cap(int locality) |
| 193 |
{ |
| 194 |
u32 value = read32(TIS_REG(locality, TIS_REG_INTF_CAPABILITY)); |
| 195 |
TPM_DEBUG_IO_READ(TIS_REG_INTF_CAPABILITY, value); |
| 196 |
return value; |
| 197 |
} |
| 198 |
|
| 199 |
static inline u32 tpm_read_intf_id(int locality) |
| 200 |
{ |
| 201 |
u32 value = read32(TIS_REG(locality, TIS_REG_INTF_ID)); |
| 202 |
TPM_DEBUG_IO_READ(TIS_REG_INTF_ID, value); |
| 203 |
return value; |
| 204 |
} |
| 205 |
|
| 206 |
static inline u32 tpm_read_did_vid(int locality) |
| 207 |
{ |
| 208 |
u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID)); |
| 209 |
TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value); |
| 210 |
return value; |
| 211 |
} |
| 212 |
|
| 213 |
static inline void tpm_write_int_vector(int vector, int locality) |
| 214 |
{ |
| 215 |
TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector); |
| 216 |
write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf); |
| 217 |
} |
| 218 |
|
| 219 |
static inline u8 tpm_read_int_vector(int locality) |
| 220 |
{ |
| 221 |
u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR)); |
| 222 |
TPM_DEBUG_IO_READ(TIS_REG_INT_VECTOR, value); |
| 223 |
return value; |
| 224 |
} |
| 225 |
|
| 226 |
static inline void tpm_write_int_polarity(int polarity, int locality) |
| 227 |
{ |
| 228 |
|
| 229 |
u32 value = (polarity & 0x3) << 3; |
| 230 |
TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value); |
| 231 |
write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value); |
| 232 |
} |
| 233 |
|
| 234 |
static inline u32 tpm_read_int_polarity(int locality) |
| 235 |
{ |
| 236 |
|
| 237 |
u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE)); |
| 238 |
value = (value >> 3) & 0x3; |
| 239 |
TPM_DEBUG_IO_READ(TIS_REG_INT_ENABLE, value); |
| 240 |
return value; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static tpm_result_t tis_wait_sts(int locality, u8 mask, u8 expected) |
| 256 |
{ |
| 257 |
struct stopwatch sw; |
| 258 |
|
| 259 |
stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 260 |
do { |
| 261 |
u8 value = tpm_read_status(locality); |
| 262 |
if ((value & mask) == expected) |
| 263 |
return TPM_SUCCESS; |
| 264 |
udelay(1); |
| 265 |
} while (!stopwatch_expired(&sw)); |
| 266 |
return TPM_CB_TIMEOUT; |
| 267 |
} |
| 268 |
|
| 269 |
static inline tpm_result_t tis_wait_ready(int locality) |
| 270 |
{ |
| 271 |
return tis_wait_sts(locality, TIS_STS_COMMAND_READY, |
| 272 |
TIS_STS_COMMAND_READY); |
| 273 |
} |
| 274 |
|
| 275 |
static inline tpm_result_t tis_wait_valid(int locality) |
| 276 |
{ |
| 277 |
return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID); |
| 278 |
} |
| 279 |
|
| 280 |
static inline tpm_result_t tis_wait_valid_data(int locality) |
| 281 |
{ |
| 282 |
const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 283 |
return tis_wait_sts(locality, has_data, has_data); |
| 284 |
} |
| 285 |
|
| 286 |
static inline int tis_has_valid_data(int locality) |
| 287 |
{ |
| 288 |
const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if ((tpm_read_status(locality) & has_data) == has_data) |
| 295 |
return 1; |
| 296 |
|
| 297 |
return (tpm_read_status(locality) & has_data) == has_data; |
| 298 |
} |
| 299 |
|
| 300 |
static inline int tis_expect_data(int locality) |
| 301 |
{ |
| 302 |
return !!(tpm_read_status(locality) & TIS_STS_EXPECT); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static tpm_result_t tis_wait_access(int locality, u8 mask, u8 expected) |
| 318 |
{ |
| 319 |
struct stopwatch sw; |
| 320 |
|
| 321 |
stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 322 |
do { |
| 323 |
u8 value = tpm_read_access(locality); |
| 324 |
if ((value & mask) == expected) |
| 325 |
return TPM_SUCCESS; |
| 326 |
udelay(1); |
| 327 |
} while (!stopwatch_expired(&sw)); |
| 328 |
return TPM_CB_TIMEOUT; |
| 329 |
} |
| 330 |
|
| 331 |
static inline tpm_result_t tis_wait_received_access(int locality) |
| 332 |
{ |
| 333 |
return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, |
| 334 |
TIS_ACCESS_ACTIVE_LOCALITY); |
| 335 |
} |
| 336 |
|
| 337 |
static inline int tis_has_access(int locality) |
| 338 |
{ |
| 339 |
return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY); |
| 340 |
} |
| 341 |
|
| 342 |
static inline void tis_request_access(int locality) |
| 343 |
{ |
| 344 |
tpm_write_access(TIS_ACCESS_REQUEST_USE, locality); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
static tpm_result_t tis_command_ready(u8 locality) |
| 360 |
{ |
| 361 |
u32 status; |
| 362 |
|
| 363 |
|
| 364 |
tpm_write_status(TIS_STS_COMMAND_READY, locality); |
| 365 |
|
| 366 |
|
| 367 |
status = tpm_read_status(locality); |
| 368 |
|
| 369 |
|
| 370 |
if (status & TIS_STS_COMMAND_READY) |
| 371 |
return TPM_SUCCESS; |
| 372 |
|
| 373 |
|
| 374 |
tpm_write_status(TIS_STS_COMMAND_READY, locality); |
| 375 |
|
| 376 |
return tis_wait_ready(locality); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
static tpm_result_t pc80_tpm_probe(enum tpm_family *family) |
| 388 |
{ |
| 389 |
static enum tpm_family tpm_family; |
| 390 |
|
| 391 |
const char *device_name = NULL; |
| 392 |
const char *vendor_name = NULL; |
| 393 |
const struct device_name *dev; |
| 394 |
u32 didvid, intf_id; |
| 395 |
u16 vid, did; |
| 396 |
u8 locality = 0, intf_type; |
| 397 |
int i; |
| 398 |
const char *family_str; |
| 399 |
|
| 400 |
if (vendor_dev_id) { |
| 401 |
if (family != NULL) |
| 402 |
*family = tpm_family; |
| 403 |
return TPM_SUCCESS; |
| 404 |
} |
| 405 |
|
| 406 |
didvid = tpm_read_did_vid(0); |
| 407 |
if (!didvid || (didvid == 0xffffffff)) { |
| 408 |
printf("%s: No TPM device found\n", __func__); |
| 409 |
return TPM_CB_FAIL; |
| 410 |
} |
| 411 |
|
| 412 |
intf_id = tpm_read_intf_id(locality); |
| 413 |
intf_type = (intf_id & 0xf); |
| 414 |
if (intf_type == 0xf) { |
| 415 |
u32 intf_cap = tpm_read_intf_cap(locality); |
| 416 |
u8 intf_version = (intf_cap >> 28) & 0x7; |
| 417 |
switch (intf_version) { |
| 418 |
case 0: |
| 419 |
case 2: |
| 420 |
tpm_family = TPM_1; |
| 421 |
break; |
| 422 |
case 3: |
| 423 |
tpm_family = TPM_2; |
| 424 |
break; |
| 425 |
default: |
| 426 |
printf("%s: Unexpected TPM interface version: %d\n", __func__, |
| 427 |
intf_version); |
| 428 |
return TPM_CB_PROBE_FAILURE; |
| 429 |
} |
| 430 |
} else if (intf_type == 0) { |
| 431 |
tpm_family = TPM_2; |
| 432 |
} else { |
| 433 |
printf("%s: Unexpected TPM interface type: %d\n", __func__, intf_type); |
| 434 |
return TPM_CB_PROBE_FAILURE; |
| 435 |
} |
| 436 |
|
| 437 |
vendor_dev_id = didvid; |
| 438 |
|
| 439 |
vid = didvid & 0xffff; |
| 440 |
did = (didvid >> 16) & 0xffff; |
| 441 |
for (i = 0; i < ARRAY_SIZE(vendor_names); i++) { |
| 442 |
int j = 0; |
| 443 |
if (vid == vendor_names[i].vendor_id) { |
| 444 |
vendor_name = vendor_names[i].vendor_name; |
| 445 |
} else { |
| 446 |
continue; |
| 447 |
} |
| 448 |
dev = &vendor_names[i].dev_names[j]; |
| 449 |
while (dev->dev_id != 0xffff) { |
| 450 |
if (dev->dev_id == did && dev->family == tpm_family) { |
| 451 |
device_name = dev->dev_name; |
| 452 |
break; |
| 453 |
} |
| 454 |
j++; |
| 455 |
dev = &vendor_names[i].dev_names[j]; |
| 456 |
} |
| 457 |
break; |
| 458 |
} |
| 459 |
|
| 460 |
family_str = (tpm_family == TPM_1 ? "TPM 1.2" : "TPM 2.0"); |
| 461 |
if (vendor_name == NULL) { |
| 462 |
printk(BIOS_INFO, "Found %s 0x%04x by 0x%04x\n", family_str, did, vid); |
| 463 |
} else if (device_name == NULL) { |
| 464 |
printk(BIOS_INFO, "Found %s 0x%04x by %s (0x%04x)\n", family_str, did, |
| 465 |
vendor_name, vid); |
| 466 |
} else { |
| 467 |
printk(BIOS_INFO, "Found %s %s (0x%04x) by %s (0x%04x)\n", family_str, |
| 468 |
device_name, did, vendor_name, vid); |
| 469 |
} |
| 470 |
|
| 471 |
if (family != NULL) |
| 472 |
*family = tpm_family; |
| 473 |
return TPM_SUCCESS; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static tpm_result_t tis_senddata(const u8 *const data, u32 len) |
| 488 |
{ |
| 489 |
u32 offset = 0; |
| 490 |
u16 burst = 0; |
| 491 |
u8 locality = 0; |
| 492 |
tpm_result_t rc = TPM_SUCCESS; |
| 493 |
|
| 494 |
rc = tis_wait_ready(locality); |
| 495 |
if (rc) { |
| 496 |
printf("%s:%d - failed to get 'command_ready' status with error %#x\n", |
| 497 |
__FILE__, __LINE__, rc); |
| 498 |
return rc; |
| 499 |
} |
| 500 |
burst = tpm_read_burst_count(locality); |
| 501 |
|
| 502 |
while (1) { |
| 503 |
unsigned int count; |
| 504 |
struct stopwatch sw; |
| 505 |
|
| 506 |
|
| 507 |
stopwatch_init_usecs_expire(&sw, MAX_DELAY_US); |
| 508 |
while (!burst) { |
| 509 |
if (stopwatch_expired(&sw)) { |
| 510 |
printf("%s:%d failed to feed %u bytes of %u\n", |
| 511 |
__FILE__, __LINE__, len - offset, len); |
| 512 |
return TPM_CB_TIMEOUT; |
| 513 |
} |
| 514 |
udelay(1); |
| 515 |
burst = tpm_read_burst_count(locality); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
count = MIN(burst, len - offset - 1); |
| 528 |
while (count--) |
| 529 |
tpm_write_data(data[offset++], locality); |
| 530 |
|
| 531 |
rc = tis_wait_valid(locality); |
| 532 |
if (rc || !tis_expect_data(locality)) { |
| 533 |
printf("%s:%d TPM command feed overflow with error %#x\n", |
| 534 |
__FILE__, __LINE__, rc); |
| 535 |
return rc ? rc : TPM_CB_FAIL; |
| 536 |
} |
| 537 |
|
| 538 |
burst = tpm_read_burst_count(locality); |
| 539 |
if ((offset == (len - 1)) && burst) |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
break; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
tpm_write_data(data[offset++], locality); |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
rc = tis_wait_valid(locality); |
| 556 |
if (rc || tis_expect_data(locality)) { |
| 557 |
printf("%s:%d unexpected TPM error %#x with status %#x\n", |
| 558 |
__FILE__, __LINE__, rc, tpm_read_status(locality)); |
| 559 |
return rc ? rc : TPM_CB_FAIL; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
tpm_write_status(TIS_STS_TPM_GO, locality); |
| 564 |
|
| 565 |
return TPM_SUCCESS; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
static tpm_result_t tis_readresponse(u8 *buffer, size_t *len) |
| 581 |
{ |
| 582 |
u16 burst_count; |
| 583 |
u32 offset = 0; |
| 584 |
u8 locality = 0; |
| 585 |
u32 expected_count = *len; |
| 586 |
int max_cycles = 0; |
| 587 |
tpm_result_t rc = TPM_SUCCESS; |
| 588 |
|
| 589 |
|
| 590 |
rc = tis_wait_valid_data(locality); |
| 591 |
if (rc) { |
| 592 |
printf("%s:%d failed processing command with error %#x\n", |
| 593 |
__FILE__, __LINE__, rc); |
| 594 |
return rc; |
| 595 |
} |
| 596 |
|
| 597 |
do { |
| 598 |
while ((burst_count = tpm_read_burst_count(locality)) == 0) { |
| 599 |
if (max_cycles++ == MAX_DELAY_US) { |
| 600 |
printf("%s:%d TPM stuck on read\n", |
| 601 |
__FILE__, __LINE__); |
| 602 |
return TPM_CB_FAIL; |
| 603 |
} |
| 604 |
udelay(1); |
| 605 |
} |
| 606 |
|
| 607 |
max_cycles = 0; |
| 608 |
|
| 609 |
while (burst_count-- && (offset < expected_count)) { |
| 610 |
buffer[offset++] = tpm_read_data(locality); |
| 611 |
if (offset == 6) { |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
u32 real_length; |
| 620 |
memcpy(&real_length, |
| 621 |
buffer + 2, |
| 622 |
sizeof(real_length)); |
| 623 |
expected_count = be32_to_cpu(real_length); |
| 624 |
|
| 625 |
if ((expected_count < offset) || |
| 626 |
(expected_count > *len)) { |
| 627 |
printf("%s:%d bad response size %u\n", |
| 628 |
__FILE__, __LINE__, |
| 629 |
expected_count); |
| 630 |
return TPM_CB_FAIL; |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
rc = tis_wait_valid(locality); |
| 637 |
if (rc) { |
| 638 |
printf("%s:%d failed to read response with error %#x\n", |
| 639 |
__FILE__, __LINE__, rc); |
| 640 |
return rc; |
| 641 |
} |
| 642 |
|
| 643 |
if (offset == expected_count) |
| 644 |
break; |
| 645 |
|
| 646 |
} while (tis_has_valid_data(locality)); |
| 647 |
|
| 648 |
|
| 649 |
if (tis_has_valid_data(locality)) { |
| 650 |
printf("%s:%d wrong receive status: %#x %u bytes left\n", |
| 651 |
__FILE__, __LINE__, tpm_read_status(locality), |
| 652 |
tpm_read_burst_count(locality)); |
| 653 |
return TPM_CB_FAIL; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
rc = tis_command_ready(locality); |
| 658 |
if (rc) |
| 659 |
return rc; |
| 660 |
|
| 661 |
*len = offset; |
| 662 |
return TPM_SUCCESS; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
static tpm_result_t pc80_tis_open(void) |
| 673 |
{ |
| 674 |
u8 locality = 0; |
| 675 |
tpm_result_t rc = TPM_SUCCESS; |
| 676 |
|
| 677 |
if (!tis_has_access(locality)) { |
| 678 |
|
| 679 |
tis_request_access(locality); |
| 680 |
|
| 681 |
|
| 682 |
rc = tis_wait_received_access(locality); |
| 683 |
if (rc) { |
| 684 |
printf("%s:%d - failed to lock locality %u with error %#x\n", |
| 685 |
__FILE__, __LINE__, locality, rc); |
| 686 |
return rc; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
udelay(10); |
| 691 |
} |
| 692 |
|
| 693 |
return tis_command_ready(locality); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
static tpm_result_t pc80_tpm_sendrecv(const uint8_t *sendbuf, size_t send_size, |
| 710 |
uint8_t *recvbuf, size_t *recv_len) |
| 711 |
{ |
| 712 |
tpm_result_t rc = tis_senddata(sendbuf, send_size); |
| 713 |
if (rc) { |
| 714 |
printf("%s:%d failed sending data to TPM with error %#x\n", |
| 715 |
__FILE__, __LINE__, rc); |
| 716 |
return rc; |
| 717 |
} |
| 718 |
|
| 719 |
return tis_readresponse(recvbuf, recv_len); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
tis_sendrecv_fn pc80_tis_probe(enum tpm_family *family) |
| 732 |
{ |
| 733 |
if (pc80_tpm_probe(family)) |
| 734 |
return NULL; |
| 735 |
|
| 736 |
if (pc80_tis_open()) |
| 737 |
return NULL; |
| 738 |
|
| 739 |
return &pc80_tpm_sendrecv; |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
static tpm_result_t tis_setup_interrupt(int vector, int polarity) |
| 758 |
{ |
| 759 |
u8 locality = 0; |
| 760 |
tpm_result_t rc = tlcl_lib_init(); |
| 761 |
|
| 762 |
if (rc) |
| 763 |
return rc; |
| 764 |
|
| 765 |
|
| 766 |
tpm_write_int_vector(vector, locality); |
| 767 |
|
| 768 |
|
| 769 |
tpm_write_int_polarity(polarity, locality); |
| 770 |
|
| 771 |
return TPM_SUCCESS; |
| 772 |
} |
| 773 |
|
| 774 |
static void lpc_tpm_read_resources(struct device *dev) |
| 775 |
{ |
| 776 |
|
| 777 |
mmio_range(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
| 778 |
} |
| 779 |
|
| 780 |
static void lpc_tpm_set_resources(struct device *dev) |
| 781 |
{ |
| 782 |
struct drivers_pc80_tpm_config *config; |
| 783 |
DEVTREE_CONST struct resource *res; |
| 784 |
|
| 785 |
config = (struct drivers_pc80_tpm_config *)dev->chip_info; |
| 786 |
|
| 787 |
for (res = dev->resource_list; res; res = res->next) { |
| 788 |
if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 789 |
continue; |
| 790 |
|
| 791 |
if (res->flags & IORESOURCE_IRQ) { |
| 792 |
|
| 793 |
tis_setup_interrupt((int)res->base, |
| 794 |
config->irq_polarity); |
| 795 |
} else { |
| 796 |
continue; |
| 797 |
} |
| 798 |
|
| 799 |
#if !DEVTREE_EARLY |
| 800 |
res->flags |= IORESOURCE_STORED; |
| 801 |
report_resource_stored(dev, res, " <tpm>"); |
| 802 |
#endif |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
#if CONFIG(HAVE_ACPI_TABLES) |
| 807 |
static void lpc_tpm_fill_ssdt(const struct device *dev) |
| 808 |
{ |
| 809 |
|
| 810 |
const char *path = "\\_SB_.PCI0"; |
| 811 |
|
| 812 |
|
| 813 |
acpigen_write_scope(path); |
| 814 |
acpigen_write_device(acpi_device_name(dev)); |
| 815 |
|
| 816 |
if (tlcl_get_family() == TPM_2) { |
| 817 |
acpigen_write_name_string("_HID", "MSFT0101"); |
| 818 |
acpigen_write_name_string("_CID", "MSFT0101"); |
| 819 |
} else { |
| 820 |
acpigen_write_name("_HID"); |
| 821 |
acpigen_emit_eisaid("PNP0C31"); |
| 822 |
|
| 823 |
acpigen_write_name("_CID"); |
| 824 |
acpigen_emit_eisaid("PNP0C31"); |
| 825 |
} |
| 826 |
|
| 827 |
acpi_device_write_uid(dev); |
| 828 |
|
| 829 |
u32 did_vid = tpm_read_did_vid(0); |
| 830 |
if (did_vid > 0 && did_vid < 0xffffffff) |
| 831 |
acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 832 |
else |
| 833 |
acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF); |
| 834 |
|
| 835 |
u16 port = dev->path.pnp.port; |
| 836 |
|
| 837 |
|
| 838 |
acpigen_write_name("_CRS"); |
| 839 |
acpigen_write_resourcetemplate_header(); |
| 840 |
acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000); |
| 841 |
if (port) |
| 842 |
acpigen_write_io16(port, port, 1, 2, 1); |
| 843 |
|
| 844 |
if (CONFIG_TPM_PIRQ) { |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
struct acpi_irq tpm_irq_a = ACPI_IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ); |
| 850 |
acpi_device_write_interrupt(&tpm_irq_a); |
| 851 |
} else if (tpm_read_int_vector(0) > 0) { |
| 852 |
u8 int_vec = tpm_read_int_vector(0); |
| 853 |
u8 int_pol = tpm_read_int_polarity(0); |
| 854 |
struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec); |
| 855 |
|
| 856 |
if (int_pol & 1) |
| 857 |
tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW; |
| 858 |
else |
| 859 |
tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH; |
| 860 |
|
| 861 |
if (int_pol & 2) |
| 862 |
tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED; |
| 863 |
else |
| 864 |
tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED; |
| 865 |
|
| 866 |
acpi_device_write_interrupt(&tpm_irq); |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
acpigen_write_resourcetemplate_footer(); |
| 871 |
|
| 872 |
if (!CONFIG(CHROMEOS)) |
| 873 |
tpm_ppi_acpi_fill_ssdt(dev); |
| 874 |
|
| 875 |
acpigen_pop_len(); |
| 876 |
acpigen_pop_len(); |
| 877 |
|
| 878 |
#if !DEVTREE_EARLY |
| 879 |
printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev), |
| 880 |
dev->chip_ops->name, dev_path(dev)); |
| 881 |
#endif |
| 882 |
} |
| 883 |
|
| 884 |
static const char *lpc_tpm_acpi_name(const struct device *dev) |
| 885 |
{ |
| 886 |
return "TPM"; |
| 887 |
} |
| 888 |
#endif |
| 889 |
|
| 890 |
static struct device_operations lpc_tpm_ops = { |
| 891 |
.read_resources = lpc_tpm_read_resources, |
| 892 |
.set_resources = lpc_tpm_set_resources, |
| 893 |
#if CONFIG(HAVE_ACPI_TABLES) |
| 894 |
.acpi_name = lpc_tpm_acpi_name, |
| 895 |
.acpi_fill_ssdt = lpc_tpm_fill_ssdt, |
| 896 |
#endif |
| 897 |
}; |
| 898 |
|
| 899 |
static struct device_operations noop_tpm_ops = { |
| 900 |
.read_resources = noop_read_resources, |
| 901 |
.set_resources = noop_set_resources, |
| 902 |
}; |
| 903 |
|
| 904 |
static struct pnp_info pnp_dev_info[] = { |
| 905 |
{ .flags = PNP_IRQ0 } |
| 906 |
}; |
| 907 |
|
| 908 |
static void enable_dev(struct device *dev) |
| 909 |
{ |
| 910 |
if (CONFIG(TPM)) { |
| 911 |
if (pc80_tis_probe(NULL) == NULL) { |
| 912 |
dev->enabled = 0; |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
pnp_enable_devices(dev, &lpc_tpm_ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 917 |
} else { |
| 918 |
pnp_enable_devices(dev, &noop_tpm_ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
struct chip_operations drivers_pc80_tpm_ops = { |
| 923 |
.name = "LPC TPM", |
| 924 |
.enable_dev = enable_dev |
| 925 |
}; |