Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 The Chromium OS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * The code in this file has been heavily based on the article "Writing a TPM |
| 22 | * Device Driver" published on http://ptgmedia.pearsoncmg.com and the |
| 23 | * submission by Stefan Berger on Qemu-devel mailing list. |
| 24 | * |
| 25 | * One principal difference is that in the simplest config the other than 0 |
| 26 | * TPM localities do not get mapped by some devices (for instance, by |
| 27 | * Infineon slb9635), so this driver provides access to locality 0 only. |
| 28 | */ |
| 29 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <delay.h> |
| 33 | #include <arch/io.h> |
| 34 | #include <arch/byteorder.h> |
| 35 | #include <console/console.h> |
| 36 | #include <pc80/tpm.h> |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 37 | #include <cpu/x86/car.h> |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 38 | |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 39 | #define PREFIX "lpc_tpm: " |
| 40 | |
| 41 | /* coreboot wrapper for TPM driver (start) */ |
| 42 | #define TPM_DEBUG(fmt, args...) \ |
Stefan Reinauer | dfb098d | 2011-11-17 12:50:54 -0800 | [diff] [blame] | 43 | if (CONFIG_DEBUG_TPM) { \ |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 44 | printk(BIOS_DEBUG, PREFIX); \ |
| 45 | printk(BIOS_DEBUG, fmt , ##args); \ |
| 46 | } |
| 47 | #define printf(x...) printk(BIOS_ERR, x) |
| 48 | |
| 49 | #define min(a,b) MIN(a,b) |
| 50 | #define max(a,b) MAX(a,b) |
| 51 | #define readb(_a) (*(volatile unsigned char *) (_a)) |
| 52 | #define writeb(_v, _a) (*(volatile unsigned char *) (_a) = (_v)) |
| 53 | #define readl(_a) (*(volatile unsigned long *) (_a)) |
| 54 | #define writel(_v, _a) (*(volatile unsigned long *) (_a) = (_v)) |
| 55 | /* coreboot wrapper for TPM driver (end) */ |
| 56 | |
| 57 | #ifndef CONFIG_TPM_TIS_BASE_ADDRESS |
| 58 | /* Base TPM address standard for x86 systems */ |
| 59 | #define CONFIG_TPM_TIS_BASE_ADDRESS 0xfed40000 |
| 60 | #endif |
| 61 | |
| 62 | /* the macro accepts the locality value, but only locality 0 is operational */ |
| 63 | #define TIS_REG(LOCALITY, REG) \ |
| 64 | (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG) |
| 65 | |
| 66 | /* hardware registers' offsets */ |
| 67 | #define TIS_REG_ACCESS 0x0 |
| 68 | #define TIS_REG_INT_ENABLE 0x8 |
| 69 | #define TIS_REG_INT_VECTOR 0xc |
| 70 | #define TIS_REG_INT_STATUS 0x10 |
| 71 | #define TIS_REG_INTF_CAPABILITY 0x14 |
| 72 | #define TIS_REG_STS 0x18 |
| 73 | #define TIS_REG_DATA_FIFO 0x24 |
| 74 | #define TIS_REG_DID_VID 0xf00 |
| 75 | #define TIS_REG_RID 0xf04 |
| 76 | |
| 77 | /* Some registers' bit field definitions */ |
| 78 | #define TIS_STS_VALID (1 << 7) /* 0x80 */ |
| 79 | #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */ |
| 80 | #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */ |
| 81 | #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */ |
| 82 | #define TIS_STS_EXPECT (1 << 3) /* 0x08 */ |
| 83 | #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */ |
| 84 | |
| 85 | #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */ |
| 86 | #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */ |
| 87 | #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */ |
| 88 | #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */ |
| 89 | #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */ |
| 90 | #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */ |
| 91 | #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */ |
| 92 | |
| 93 | #define TIS_STS_BURST_COUNT_MASK (0xffff) |
| 94 | #define TIS_STS_BURST_COUNT_SHIFT (8) |
| 95 | |
| 96 | /* |
| 97 | * Error value returned if a tpm register does not enter the expected state |
| 98 | * after continuous polling. No actual TPM register reading ever returns ~0, |
| 99 | * so this value is a safe error indication to be mixed with possible status |
| 100 | * register values. |
| 101 | */ |
| 102 | #define TPM_TIMEOUT_ERR (~0) |
| 103 | |
| 104 | /* Error value returned on various TPM driver errors */ |
| 105 | #define TPM_DRIVER_ERR (~0) |
| 106 | |
| 107 | /* 1 second is plenty for anything TPM does.*/ |
| 108 | #define MAX_DELAY_US (1000 * 1000) |
| 109 | |
| 110 | /* Retrieve burst count value out of the status register contents. */ |
| 111 | #define BURST_COUNT(status) ((u16)(((status) >> TIS_STS_BURST_COUNT_SHIFT) & \ |
| 112 | TIS_STS_BURST_COUNT_MASK)) |
| 113 | |
| 114 | /* |
| 115 | * Structures defined below allow creating descriptions of TPM vendor/device |
| 116 | * ID information for run time discovery. The only device the system knows |
| 117 | * about at this time is Infineon slb9635 |
| 118 | */ |
| 119 | struct device_name { |
| 120 | u16 dev_id; |
| 121 | const char * const dev_name; |
| 122 | }; |
| 123 | |
| 124 | struct vendor_name { |
| 125 | u16 vendor_id; |
| 126 | const char * vendor_name; |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 127 | const struct device_name* dev_names; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 130 | static const struct device_name atmel_devices[] = { |
| 131 | {0x3204, "AT97SC3204"}, |
| 132 | {0xffff} |
| 133 | }; |
| 134 | |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 135 | static const struct device_name infineon_devices[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 136 | {0x000b, "SLB9635 TT 1.2"}, |
| 137 | {0xffff} |
| 138 | }; |
| 139 | |
| 140 | static const struct device_name nuvoton_devices[] = { |
| 141 | {0x00fe, "NPCT420AA V2"}, |
| 142 | {0xffff} |
| 143 | }; |
| 144 | |
| 145 | static const struct device_name stmicro_devices[] = { |
| 146 | {0x0000, "ST33ZP24" }, |
| 147 | {0xffff} |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static const struct vendor_name vendor_names[] = { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 151 | {0x1114, "Atmel", atmel_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 152 | {0x15d1, "Infineon", infineon_devices}, |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 153 | {0x1050, "Nuvoton", nuvoton_devices}, |
| 154 | {0x104a, "ST Microelectronics", stmicro_devices}, |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /* |
| 158 | * Cached vendor/device ID pair to indicate that the device has been already |
| 159 | * discovered |
| 160 | */ |
Stefan Reinauer | c668af7 | 2011-10-27 21:28:25 +0000 | [diff] [blame] | 161 | static u32 vendor_dev_id CAR_GLOBAL; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 162 | |
| 163 | static int is_byte_reg(u32 reg) |
| 164 | { |
| 165 | /* |
| 166 | * These TPM registers are 8 bits wide and as such require byte access |
| 167 | * on writes and truncated value on reads. |
| 168 | */ |
| 169 | return ((reg == TIS_REG_ACCESS) || |
| 170 | (reg == TIS_REG_INT_VECTOR) || |
| 171 | (reg == TIS_REG_DATA_FIFO)); |
| 172 | } |
| 173 | |
| 174 | /* TPM access functions are carved out to make tracing easier. */ |
| 175 | static u32 tpm_read(int locality, u32 reg) |
| 176 | { |
| 177 | u32 value; |
| 178 | /* |
| 179 | * Data FIFO register must be read and written in byte access mode, |
| 180 | * otherwise the FIFO values are returned 4 bytes at a time. |
| 181 | */ |
| 182 | if (is_byte_reg(reg)) |
| 183 | value = readb(TIS_REG(locality, reg)); |
| 184 | else |
| 185 | value = readl(TIS_REG(locality, reg)); |
| 186 | |
| 187 | TPM_DEBUG("Read reg 0x%x returns 0x%x\n", reg, value); |
| 188 | return value; |
| 189 | } |
| 190 | |
| 191 | static void tpm_write(u32 value, int locality, u32 reg) |
| 192 | { |
| 193 | TPM_DEBUG("Write reg 0x%x with 0x%x\n", reg, value); |
| 194 | |
| 195 | if (is_byte_reg(reg)) |
| 196 | writeb(value & 0xff, TIS_REG(locality, reg)); |
| 197 | else |
| 198 | writel(value, TIS_REG(locality, reg)); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * tis_wait_reg() |
| 203 | * |
| 204 | * Wait for at least a second for a register to change its state to match the |
| 205 | * expected state. Normally the transition happens within microseconds. |
| 206 | * |
| 207 | * @reg - the TPM register offset |
| 208 | * @locality - locality |
| 209 | * @mask - bitmask for the bitfield(s) to watch |
| 210 | * @expected - value the field(s) are supposed to be set to |
| 211 | * |
| 212 | * Returns the register contents in case the expected value was found in the |
| 213 | * appropriate register bits, or TPM_TIMEOUT_ERR on timeout. |
| 214 | */ |
| 215 | static u32 tis_wait_reg(u8 reg, u8 locality, u8 mask, u8 expected) |
| 216 | { |
| 217 | u32 time_us = MAX_DELAY_US; |
| 218 | while (time_us > 0) { |
| 219 | u32 value = tpm_read(locality, reg); |
| 220 | if ((value & mask) == expected) |
| 221 | return value; |
| 222 | udelay(1); /* 1 us */ |
| 223 | time_us--; |
| 224 | } |
| 225 | return TPM_TIMEOUT_ERR; |
| 226 | } |
| 227 | |
| 228 | /* |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 229 | * PC Client Specific TPM Interface Specification section 11.2.12: |
| 230 | * |
| 231 | * Software must be prepared to send two writes of a "1" to command ready |
| 232 | * field: the first to indicate successful read of all the data, thus |
| 233 | * clearing the data from the ReadFIFO and freeing the TPM's resources, |
| 234 | * and the second to indicate to the TPM it is about to send a new command. |
| 235 | * |
| 236 | * In practice not all TPMs behave the same so it is necessary to be |
| 237 | * flexible when trying to set command ready. |
| 238 | * |
| 239 | * Returns 0 on success if the TPM is ready for transactions. |
| 240 | * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set. |
| 241 | */ |
| 242 | static int tis_command_ready(u8 locality) |
| 243 | { |
| 244 | u32 status; |
| 245 | |
| 246 | /* 1st attempt to set command ready */ |
| 247 | tpm_write(TIS_STS_COMMAND_READY, locality, TIS_REG_STS); |
| 248 | |
| 249 | /* Wait for response */ |
| 250 | status = tpm_read(locality, TIS_REG_STS); |
| 251 | |
| 252 | /* Check if command ready is set yet */ |
| 253 | if (status & TIS_STS_COMMAND_READY) |
| 254 | return 0; |
| 255 | |
| 256 | /* 2nd attempt to set command ready */ |
| 257 | tpm_write(TIS_STS_COMMAND_READY, locality, TIS_REG_STS); |
| 258 | |
| 259 | /* Wait for command ready to get set */ |
| 260 | status = tis_wait_reg(TIS_REG_STS, locality, |
| 261 | TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY); |
| 262 | |
| 263 | return (status == TPM_TIMEOUT_ERR) ? TPM_TIMEOUT_ERR : 0; |
| 264 | } |
| 265 | |
| 266 | /* |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 267 | * Probe the TPM device and try determining its manufacturer/device name. |
| 268 | * |
| 269 | * Returns 0 on success (the device is found or was found during an earlier |
| 270 | * invocation) or TPM_DRIVER_ERR if the device is not found. |
| 271 | */ |
| 272 | static u32 tis_probe(void) |
| 273 | { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 274 | const char *device_name = "unknown"; |
| 275 | const char *vendor_name = device_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 276 | const struct device_name *dev; |
| 277 | u32 didvid; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 278 | u16 vid, did; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 279 | int i; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 280 | |
| 281 | if (vendor_dev_id) |
| 282 | return 0; /* Already probed. */ |
| 283 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 284 | didvid = tpm_read(0, TIS_REG_DID_VID); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 285 | if (!didvid || (didvid == 0xffffffff)) { |
| 286 | printf("%s: No TPM device found\n", __FUNCTION__); |
| 287 | return TPM_DRIVER_ERR; |
| 288 | } |
| 289 | |
| 290 | vendor_dev_id = didvid; |
| 291 | |
| 292 | vid = didvid & 0xffff; |
| 293 | did = (didvid >> 16) & 0xffff; |
| 294 | for (i = 0; i < ARRAY_SIZE(vendor_names); i++) { |
| 295 | int j = 0; |
| 296 | u16 known_did; |
| 297 | if (vid == vendor_names[i].vendor_id) { |
| 298 | vendor_name = vendor_names[i].vendor_name; |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 299 | } else { |
| 300 | continue; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 301 | } |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 302 | dev = &vendor_names[i].dev_names[j]; |
| 303 | while ((known_did = dev->dev_id) != 0xffff) { |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 304 | if (known_did == did) { |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 305 | device_name = dev->dev_name; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | j++; |
| 309 | } |
| 310 | break; |
| 311 | } |
| 312 | /* this will have to be converted into debug printout */ |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 313 | printf("Found TPM %s by %s\n", device_name, vendor_name); |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * tis_senddata() |
| 319 | * |
| 320 | * send the passed in data to the TPM device. |
| 321 | * |
| 322 | * @data - address of the data to send, byte by byte |
| 323 | * @len - length of the data to send |
| 324 | * |
| 325 | * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does |
| 326 | * not accept the entire command). |
| 327 | */ |
| 328 | static u32 tis_senddata(const u8 * const data, u32 len) |
| 329 | { |
| 330 | u32 offset = 0; |
| 331 | u16 burst = 0; |
| 332 | u32 max_cycles = 0; |
| 333 | u8 locality = 0; |
| 334 | u32 value; |
| 335 | |
| 336 | value = tis_wait_reg(TIS_REG_STS, locality, TIS_STS_COMMAND_READY, |
| 337 | TIS_STS_COMMAND_READY); |
| 338 | if (value == TPM_TIMEOUT_ERR) { |
| 339 | printf("%s:%d - failed to get 'command_ready' status\n", |
| 340 | __FILE__, __LINE__); |
| 341 | return TPM_DRIVER_ERR; |
| 342 | } |
| 343 | burst = BURST_COUNT(value); |
| 344 | |
| 345 | while (1) { |
| 346 | unsigned count; |
| 347 | |
| 348 | /* Wait till the device is ready to accept more data. */ |
| 349 | while (!burst) { |
| 350 | if (max_cycles++ == MAX_DELAY_US) { |
| 351 | printf("%s:%d failed to feed %d bytes of %d\n", |
| 352 | __FILE__, __LINE__, len - offset, len); |
| 353 | return TPM_DRIVER_ERR; |
| 354 | } |
| 355 | udelay(1); |
| 356 | burst = BURST_COUNT(tpm_read(locality, TIS_REG_STS)); |
| 357 | } |
| 358 | |
| 359 | max_cycles = 0; |
| 360 | |
| 361 | /* |
| 362 | * Calculate number of bytes the TPM is ready to accept in one |
| 363 | * shot. |
| 364 | * |
| 365 | * We want to send the last byte outside of the loop (hence |
| 366 | * the -1 below) to make sure that the 'expected' status bit |
| 367 | * changes to zero exactly after the last byte is fed into the |
| 368 | * FIFO. |
| 369 | */ |
| 370 | count = min(burst, len - offset - 1); |
| 371 | while (count--) |
| 372 | tpm_write(data[offset++], locality, TIS_REG_DATA_FIFO); |
| 373 | |
| 374 | value = tis_wait_reg(TIS_REG_STS, locality, |
| 375 | TIS_STS_VALID, TIS_STS_VALID); |
| 376 | |
| 377 | if ((value == TPM_TIMEOUT_ERR) || !(value & TIS_STS_EXPECT)) { |
| 378 | printf("%s:%d TPM command feed overflow\n", |
| 379 | __FILE__, __LINE__); |
| 380 | return TPM_DRIVER_ERR; |
| 381 | } |
| 382 | |
| 383 | burst = BURST_COUNT(value); |
| 384 | if ((offset == (len - 1)) && burst) |
| 385 | /* |
| 386 | * We need to be able to send the last byte to the |
| 387 | * device, so burst size must be nonzero before we |
| 388 | * break out. |
| 389 | */ |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | /* Send the last byte. */ |
| 394 | tpm_write(data[offset++], locality, TIS_REG_DATA_FIFO); |
| 395 | |
| 396 | /* |
| 397 | * Verify that TPM does not expect any more data as part of this |
| 398 | * command. |
| 399 | */ |
| 400 | value = tis_wait_reg(TIS_REG_STS, locality, |
| 401 | TIS_STS_VALID, TIS_STS_VALID); |
| 402 | if ((value == TPM_TIMEOUT_ERR) || (value & TIS_STS_EXPECT)) { |
| 403 | printf("%s:%d unexpected TPM status 0x%x\n", |
| 404 | __FILE__, __LINE__, value); |
| 405 | return TPM_DRIVER_ERR; |
| 406 | } |
| 407 | |
| 408 | /* OK, sitting pretty, let's start the command execution. */ |
| 409 | tpm_write(TIS_STS_TPM_GO, locality, TIS_REG_STS); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * tis_readresponse() |
| 416 | * |
| 417 | * read the TPM device response after a command was issued. |
| 418 | * |
| 419 | * @buffer - address where to read the response, byte by byte. |
| 420 | * @len - pointer to the size of buffer |
| 421 | * |
| 422 | * On success stores the number of received bytes to len and returns 0. On |
| 423 | * errors (misformatted TPM data or synchronization problems) returns |
| 424 | * TPM_DRIVER_ERR. |
| 425 | */ |
| 426 | static u32 tis_readresponse(u8 *buffer, size_t *len) |
| 427 | { |
| 428 | u16 burst_count; |
| 429 | u32 status; |
| 430 | u32 offset = 0; |
| 431 | u8 locality = 0; |
| 432 | const u32 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID; |
| 433 | u32 expected_count = *len; |
| 434 | int max_cycles = 0; |
| 435 | |
| 436 | /* Wait for the TPM to process the command */ |
| 437 | status = tis_wait_reg(TIS_REG_STS, locality, has_data, has_data); |
| 438 | if (status == TPM_TIMEOUT_ERR) { |
| 439 | printf("%s:%d failed processing command\n", |
| 440 | __FILE__, __LINE__); |
| 441 | return TPM_DRIVER_ERR; |
| 442 | } |
| 443 | |
| 444 | do { |
| 445 | while ((burst_count = BURST_COUNT(status)) == 0) { |
| 446 | if (max_cycles++ == MAX_DELAY_US) { |
| 447 | printf("%s:%d TPM stuck on read\n", |
| 448 | __FILE__, __LINE__); |
| 449 | return TPM_DRIVER_ERR; |
| 450 | } |
| 451 | udelay(1); |
| 452 | status = tpm_read(locality, TIS_REG_STS); |
| 453 | } |
| 454 | |
| 455 | max_cycles = 0; |
| 456 | |
| 457 | while (burst_count-- && (offset < expected_count)) { |
| 458 | buffer[offset++] = (u8) tpm_read(locality, |
| 459 | TIS_REG_DATA_FIFO); |
| 460 | if (offset == 6) { |
| 461 | /* |
| 462 | * We got the first six bytes of the reply, |
| 463 | * let's figure out how many bytes to expect |
| 464 | * total - it is stored as a 4 byte number in |
| 465 | * network order, starting with offset 2 into |
| 466 | * the body of the reply. |
| 467 | */ |
| 468 | u32 real_length; |
| 469 | memcpy(&real_length, |
| 470 | buffer + 2, |
| 471 | sizeof(real_length)); |
| 472 | expected_count = be32_to_cpu(real_length); |
| 473 | |
| 474 | if ((expected_count < offset) || |
| 475 | (expected_count > *len)) { |
| 476 | printf("%s:%d bad response size %d\n", |
| 477 | __FILE__, __LINE__, |
| 478 | expected_count); |
| 479 | return TPM_DRIVER_ERR; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* Wait for the next portion */ |
| 485 | status = tis_wait_reg(TIS_REG_STS, locality, |
| 486 | TIS_STS_VALID, TIS_STS_VALID); |
| 487 | if (status == TPM_TIMEOUT_ERR) { |
| 488 | printf("%s:%d failed to read response\n", |
| 489 | __FILE__, __LINE__); |
| 490 | return TPM_DRIVER_ERR; |
| 491 | } |
| 492 | |
| 493 | if (offset == expected_count) |
| 494 | break; /* We got all we need */ |
| 495 | |
| 496 | } while ((status & has_data) == has_data); |
| 497 | |
| 498 | /* |
| 499 | * Make sure we indeed read all there was. The TIS_STS_VALID bit is |
| 500 | * known to be set. |
| 501 | */ |
| 502 | if (status & TIS_STS_DATA_AVAILABLE) { |
| 503 | printf("%s:%d wrong receive status %x\n", |
| 504 | __FILE__, __LINE__, status); |
| 505 | return TPM_DRIVER_ERR; |
| 506 | } |
| 507 | |
| 508 | /* Tell the TPM that we are done. */ |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 509 | if (tis_command_ready(locality) == TPM_TIMEOUT_ERR) |
| 510 | return TPM_DRIVER_ERR; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 511 | |
| 512 | *len = offset; |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * tis_init() |
| 518 | * |
| 519 | * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on |
| 520 | * failure (in case device probing did not succeed). |
| 521 | */ |
| 522 | int tis_init(void) |
| 523 | { |
| 524 | if (tis_probe()) |
| 525 | return TPM_DRIVER_ERR; |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * tis_open() |
| 531 | * |
| 532 | * Requests access to locality 0 for the caller. After all commands have been |
| 533 | * completed the caller is supposed to call tis_close(). |
| 534 | * |
| 535 | * Returns 0 on success, TPM_DRIVER_ERR on failure. |
| 536 | */ |
| 537 | int tis_open(void) |
| 538 | { |
| 539 | u8 locality = 0; /* we use locality zero for everything */ |
| 540 | |
| 541 | if (tis_close()) |
| 542 | return TPM_DRIVER_ERR; |
| 543 | |
| 544 | /* now request access to locality */ |
| 545 | tpm_write(TIS_ACCESS_REQUEST_USE, locality, TIS_REG_ACCESS); |
| 546 | |
| 547 | /* did we get a lock? */ |
| 548 | if (tis_wait_reg(TIS_REG_ACCESS, locality, |
| 549 | TIS_ACCESS_ACTIVE_LOCALITY, |
| 550 | TIS_ACCESS_ACTIVE_LOCALITY) == TPM_TIMEOUT_ERR) { |
| 551 | printf("%s:%d - failed to lock locality %d\n", |
| 552 | __FILE__, __LINE__, locality); |
| 553 | return TPM_DRIVER_ERR; |
| 554 | } |
| 555 | |
Stefan Reinauer | c908fc7 | 2012-04-30 16:33:44 -0700 | [diff] [blame^] | 556 | /* Certain TPMs seem to need some delay here or they hang... */ |
| 557 | udelay(10); |
| 558 | |
| 559 | if (tis_command_ready(locality) == TPM_TIMEOUT_ERR) |
| 560 | return TPM_DRIVER_ERR; |
Stefan Reinauer | 3008bbad | 2011-10-11 14:46:25 -0700 | [diff] [blame] | 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * tis_close() |
| 567 | * |
| 568 | * terminate the currect session with the TPM by releasing the locked |
| 569 | * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock |
| 570 | * removal did not succeed). |
| 571 | */ |
| 572 | int tis_close(void) |
| 573 | { |
| 574 | u8 locality = 0; |
| 575 | if (tpm_read(locality, TIS_REG_ACCESS) & |
| 576 | TIS_ACCESS_ACTIVE_LOCALITY) { |
| 577 | tpm_write(TIS_ACCESS_ACTIVE_LOCALITY, locality, TIS_REG_ACCESS); |
| 578 | |
| 579 | if (tis_wait_reg(TIS_REG_ACCESS, locality, |
| 580 | TIS_ACCESS_ACTIVE_LOCALITY, 0) == |
| 581 | TPM_TIMEOUT_ERR) { |
| 582 | printf("%s:%d - failed to release locality %d\n", |
| 583 | __FILE__, __LINE__, locality); |
| 584 | return TPM_DRIVER_ERR; |
| 585 | } |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * tis_sendrecv() |
| 592 | * |
| 593 | * Send the requested data to the TPM and then try to get its response |
| 594 | * |
| 595 | * @sendbuf - buffer of the data to send |
| 596 | * @send_size size of the data to send |
| 597 | * @recvbuf - memory to save the response to |
| 598 | * @recv_len - pointer to the size of the response buffer |
| 599 | * |
| 600 | * Returns 0 on success (and places the number of response bytes at recv_len) |
| 601 | * or TPM_DRIVER_ERR on failure. |
| 602 | */ |
| 603 | int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, |
| 604 | uint8_t *recvbuf, size_t *recv_len) |
| 605 | { |
| 606 | if (tis_senddata(sendbuf, send_size)) { |
| 607 | printf("%s:%d failed sending data to TPM\n", |
| 608 | __FILE__, __LINE__); |
| 609 | return TPM_DRIVER_ERR; |
| 610 | } |
| 611 | |
| 612 | return tis_readresponse(recvbuf, recv_len); |
| 613 | } |