Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Infineon Technologies |
| 3 | * |
| 4 | * Authors: |
| 5 | * Peter Huewe <huewe.external@infineon.com> |
| 6 | * |
| 7 | * Description: |
| 8 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 9 | * Specifications at www.trustedcomputinggroup.org |
| 10 | * |
| 11 | * This device driver implements the TPM interface as defined in |
| 12 | * the TCG TPM Interface Spec version 1.2, revision 1.0 and the |
| 13 | * Infineon I2C Protocol Stack Specification v0.20. |
| 14 | * |
| 15 | * It is based on the Linux kernel driver tpm.c from Leendert van |
| 16 | * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. |
| 17 | * |
| 18 | * Version: 2.1.1 |
| 19 | * |
Simon Glass | c98f7fe | 2015-08-22 18:31:21 -0600 | [diff] [blame] | 20 | * SPDX-License-Identifier: GPL-2.0 |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <common.h> |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 24 | #include <dm.h> |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 25 | #include <fdtdec.h> |
Masahiro Yamada | e722852 | 2014-11-26 16:00:58 +0900 | [diff] [blame] | 26 | #include <linux/compiler.h> |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 27 | #include <i2c.h> |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 28 | #include <tpm.h> |
| 29 | #include <asm-generic/errno.h> |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 30 | #include <linux/types.h> |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 31 | #include <linux/unaligned/be_byteshift.h> |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 32 | |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 33 | #include "tpm_tis_i2c.h" |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 34 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 37 | /* Max buffer size supported by our tpm */ |
| 38 | #define TPM_DEV_BUFSIZE 1260 |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 39 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 40 | /* Max number of iterations after i2c NAK */ |
| 41 | #define MAX_COUNT 3 |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 42 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Max number of iterations after i2c NAK for 'long' commands |
| 45 | * |
| 46 | * We need this especially for sending TPM_READY, since the cleanup after the |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 47 | * transtion to the ready state may take some time, but it is unpredictable |
| 48 | * how long it will take. |
| 49 | */ |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 50 | #define MAX_COUNT_LONG 50 |
| 51 | |
| 52 | #define SLEEP_DURATION 60 /* in usec */ |
| 53 | #define SLEEP_DURATION_LONG 210 /* in usec */ |
| 54 | |
| 55 | #define TPM_HEADER_SIZE 10 |
| 56 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 57 | enum tis_access { |
| 58 | TPM_ACCESS_VALID = 0x80, |
| 59 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, |
| 60 | TPM_ACCESS_REQUEST_PENDING = 0x04, |
| 61 | TPM_ACCESS_REQUEST_USE = 0x02, |
| 62 | }; |
| 63 | |
| 64 | enum tis_status { |
| 65 | TPM_STS_VALID = 0x80, |
| 66 | TPM_STS_COMMAND_READY = 0x40, |
| 67 | TPM_STS_GO = 0x20, |
| 68 | TPM_STS_DATA_AVAIL = 0x10, |
| 69 | TPM_STS_DATA_EXPECT = 0x08, |
| 70 | }; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 71 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 72 | enum tis_defaults { |
| 73 | TIS_SHORT_TIMEOUT = 750, /* ms */ |
| 74 | TIS_LONG_TIMEOUT = 2000, /* ms */ |
| 75 | }; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 76 | |
| 77 | /* expected value for DIDVID register */ |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 78 | #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L |
| 79 | #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L |
| 80 | |
| 81 | enum i2c_chip_type { |
| 82 | SLB9635, |
| 83 | SLB9645, |
| 84 | UNKNOWN, |
| 85 | }; |
| 86 | |
| 87 | static const char * const chip_name[] = { |
| 88 | [SLB9635] = "slb9635tt", |
| 89 | [SLB9645] = "slb9645tt", |
| 90 | [UNKNOWN] = "unknown/fallback to slb9635", |
| 91 | }; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 92 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 93 | #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) |
| 94 | #define TPM_STS(l) (0x0001 | ((l) << 4)) |
| 95 | #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) |
| 96 | #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) |
| 97 | |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 98 | enum tpm_duration { |
| 99 | TPM_SHORT = 0, |
| 100 | TPM_MEDIUM = 1, |
| 101 | TPM_LONG = 2, |
| 102 | TPM_UNDEFINED, |
| 103 | }; |
| 104 | |
| 105 | /* Extended error numbers from linux (see errno.h) */ |
| 106 | #define ECANCELED 125 /* Operation Canceled */ |
| 107 | |
| 108 | /* Timer frequency. Corresponds to msec timer resolution*/ |
| 109 | #define HZ 1000 |
| 110 | |
| 111 | #define TPM_MAX_ORDINAL 243 |
| 112 | #define TPM_MAX_PROTECTED_ORDINAL 12 |
| 113 | #define TPM_PROTECTED_ORDINAL_MASK 0xFF |
| 114 | |
| 115 | #define TPM_CMD_COUNT_BYTE 2 |
| 116 | #define TPM_CMD_ORDINAL_BYTE 6 |
| 117 | |
| 118 | /* |
| 119 | * Array with one entry per ordinal defining the maximum amount |
| 120 | * of time the chip could take to return the result. The ordinal |
| 121 | * designation of short, medium or long is defined in a table in |
| 122 | * TCG Specification TPM Main Part 2 TPM Structures Section 17. The |
| 123 | * values of the SHORT, MEDIUM, and LONG durations are retrieved |
| 124 | * from the chip during initialization with a call to tpm_get_timeouts. |
| 125 | */ |
| 126 | static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = { |
| 127 | TPM_UNDEFINED, /* 0 */ |
| 128 | TPM_UNDEFINED, |
| 129 | TPM_UNDEFINED, |
| 130 | TPM_UNDEFINED, |
| 131 | TPM_UNDEFINED, |
| 132 | TPM_UNDEFINED, /* 5 */ |
| 133 | TPM_UNDEFINED, |
| 134 | TPM_UNDEFINED, |
| 135 | TPM_UNDEFINED, |
| 136 | TPM_UNDEFINED, |
| 137 | TPM_SHORT, /* 10 */ |
| 138 | TPM_SHORT, |
| 139 | }; |
| 140 | |
| 141 | static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { |
| 142 | TPM_UNDEFINED, /* 0 */ |
| 143 | TPM_UNDEFINED, |
| 144 | TPM_UNDEFINED, |
| 145 | TPM_UNDEFINED, |
| 146 | TPM_UNDEFINED, |
| 147 | TPM_UNDEFINED, /* 5 */ |
| 148 | TPM_UNDEFINED, |
| 149 | TPM_UNDEFINED, |
| 150 | TPM_UNDEFINED, |
| 151 | TPM_UNDEFINED, |
| 152 | TPM_SHORT, /* 10 */ |
| 153 | TPM_SHORT, |
| 154 | TPM_MEDIUM, |
| 155 | TPM_LONG, |
| 156 | TPM_LONG, |
| 157 | TPM_MEDIUM, /* 15 */ |
| 158 | TPM_SHORT, |
| 159 | TPM_SHORT, |
| 160 | TPM_MEDIUM, |
| 161 | TPM_LONG, |
| 162 | TPM_SHORT, /* 20 */ |
| 163 | TPM_SHORT, |
| 164 | TPM_MEDIUM, |
| 165 | TPM_MEDIUM, |
| 166 | TPM_MEDIUM, |
| 167 | TPM_SHORT, /* 25 */ |
| 168 | TPM_SHORT, |
| 169 | TPM_MEDIUM, |
| 170 | TPM_SHORT, |
| 171 | TPM_SHORT, |
| 172 | TPM_MEDIUM, /* 30 */ |
| 173 | TPM_LONG, |
| 174 | TPM_MEDIUM, |
| 175 | TPM_SHORT, |
| 176 | TPM_SHORT, |
| 177 | TPM_SHORT, /* 35 */ |
| 178 | TPM_MEDIUM, |
| 179 | TPM_MEDIUM, |
| 180 | TPM_UNDEFINED, |
| 181 | TPM_UNDEFINED, |
| 182 | TPM_MEDIUM, /* 40 */ |
| 183 | TPM_LONG, |
| 184 | TPM_MEDIUM, |
| 185 | TPM_SHORT, |
| 186 | TPM_SHORT, |
| 187 | TPM_SHORT, /* 45 */ |
| 188 | TPM_SHORT, |
| 189 | TPM_SHORT, |
| 190 | TPM_SHORT, |
| 191 | TPM_LONG, |
| 192 | TPM_MEDIUM, /* 50 */ |
| 193 | TPM_MEDIUM, |
| 194 | TPM_UNDEFINED, |
| 195 | TPM_UNDEFINED, |
| 196 | TPM_UNDEFINED, |
| 197 | TPM_UNDEFINED, /* 55 */ |
| 198 | TPM_UNDEFINED, |
| 199 | TPM_UNDEFINED, |
| 200 | TPM_UNDEFINED, |
| 201 | TPM_UNDEFINED, |
| 202 | TPM_MEDIUM, /* 60 */ |
| 203 | TPM_MEDIUM, |
| 204 | TPM_MEDIUM, |
| 205 | TPM_SHORT, |
| 206 | TPM_SHORT, |
| 207 | TPM_MEDIUM, /* 65 */ |
| 208 | TPM_UNDEFINED, |
| 209 | TPM_UNDEFINED, |
| 210 | TPM_UNDEFINED, |
| 211 | TPM_UNDEFINED, |
| 212 | TPM_SHORT, /* 70 */ |
| 213 | TPM_SHORT, |
| 214 | TPM_UNDEFINED, |
| 215 | TPM_UNDEFINED, |
| 216 | TPM_UNDEFINED, |
| 217 | TPM_UNDEFINED, /* 75 */ |
| 218 | TPM_UNDEFINED, |
| 219 | TPM_UNDEFINED, |
| 220 | TPM_UNDEFINED, |
| 221 | TPM_UNDEFINED, |
| 222 | TPM_LONG, /* 80 */ |
| 223 | TPM_UNDEFINED, |
| 224 | TPM_MEDIUM, |
| 225 | TPM_LONG, |
| 226 | TPM_SHORT, |
| 227 | TPM_UNDEFINED, /* 85 */ |
| 228 | TPM_UNDEFINED, |
| 229 | TPM_UNDEFINED, |
| 230 | TPM_UNDEFINED, |
| 231 | TPM_UNDEFINED, |
| 232 | TPM_SHORT, /* 90 */ |
| 233 | TPM_SHORT, |
| 234 | TPM_SHORT, |
| 235 | TPM_SHORT, |
| 236 | TPM_SHORT, |
| 237 | TPM_UNDEFINED, /* 95 */ |
| 238 | TPM_UNDEFINED, |
| 239 | TPM_UNDEFINED, |
| 240 | TPM_UNDEFINED, |
| 241 | TPM_UNDEFINED, |
| 242 | TPM_MEDIUM, /* 100 */ |
| 243 | TPM_SHORT, |
| 244 | TPM_SHORT, |
| 245 | TPM_UNDEFINED, |
| 246 | TPM_UNDEFINED, |
| 247 | TPM_UNDEFINED, /* 105 */ |
| 248 | TPM_UNDEFINED, |
| 249 | TPM_UNDEFINED, |
| 250 | TPM_UNDEFINED, |
| 251 | TPM_UNDEFINED, |
| 252 | TPM_SHORT, /* 110 */ |
| 253 | TPM_SHORT, |
| 254 | TPM_SHORT, |
| 255 | TPM_SHORT, |
| 256 | TPM_SHORT, |
| 257 | TPM_SHORT, /* 115 */ |
| 258 | TPM_SHORT, |
| 259 | TPM_SHORT, |
| 260 | TPM_UNDEFINED, |
| 261 | TPM_UNDEFINED, |
| 262 | TPM_LONG, /* 120 */ |
| 263 | TPM_LONG, |
| 264 | TPM_MEDIUM, |
| 265 | TPM_UNDEFINED, |
| 266 | TPM_SHORT, |
| 267 | TPM_SHORT, /* 125 */ |
| 268 | TPM_SHORT, |
| 269 | TPM_LONG, |
| 270 | TPM_SHORT, |
| 271 | TPM_SHORT, |
| 272 | TPM_SHORT, /* 130 */ |
| 273 | TPM_MEDIUM, |
| 274 | TPM_UNDEFINED, |
| 275 | TPM_SHORT, |
| 276 | TPM_MEDIUM, |
| 277 | TPM_UNDEFINED, /* 135 */ |
| 278 | TPM_UNDEFINED, |
| 279 | TPM_UNDEFINED, |
| 280 | TPM_UNDEFINED, |
| 281 | TPM_UNDEFINED, |
| 282 | TPM_SHORT, /* 140 */ |
| 283 | TPM_SHORT, |
| 284 | TPM_UNDEFINED, |
| 285 | TPM_UNDEFINED, |
| 286 | TPM_UNDEFINED, |
| 287 | TPM_UNDEFINED, /* 145 */ |
| 288 | TPM_UNDEFINED, |
| 289 | TPM_UNDEFINED, |
| 290 | TPM_UNDEFINED, |
| 291 | TPM_UNDEFINED, |
| 292 | TPM_SHORT, /* 150 */ |
| 293 | TPM_MEDIUM, |
| 294 | TPM_MEDIUM, |
| 295 | TPM_SHORT, |
| 296 | TPM_SHORT, |
| 297 | TPM_UNDEFINED, /* 155 */ |
| 298 | TPM_UNDEFINED, |
| 299 | TPM_UNDEFINED, |
| 300 | TPM_UNDEFINED, |
| 301 | TPM_UNDEFINED, |
| 302 | TPM_SHORT, /* 160 */ |
| 303 | TPM_SHORT, |
| 304 | TPM_SHORT, |
| 305 | TPM_SHORT, |
| 306 | TPM_UNDEFINED, |
| 307 | TPM_UNDEFINED, /* 165 */ |
| 308 | TPM_UNDEFINED, |
| 309 | TPM_UNDEFINED, |
| 310 | TPM_UNDEFINED, |
| 311 | TPM_UNDEFINED, |
| 312 | TPM_LONG, /* 170 */ |
| 313 | TPM_UNDEFINED, |
| 314 | TPM_UNDEFINED, |
| 315 | TPM_UNDEFINED, |
| 316 | TPM_UNDEFINED, |
| 317 | TPM_UNDEFINED, /* 175 */ |
| 318 | TPM_UNDEFINED, |
| 319 | TPM_UNDEFINED, |
| 320 | TPM_UNDEFINED, |
| 321 | TPM_UNDEFINED, |
| 322 | TPM_MEDIUM, /* 180 */ |
| 323 | TPM_SHORT, |
| 324 | TPM_MEDIUM, |
| 325 | TPM_MEDIUM, |
| 326 | TPM_MEDIUM, |
| 327 | TPM_MEDIUM, /* 185 */ |
| 328 | TPM_SHORT, |
| 329 | TPM_UNDEFINED, |
| 330 | TPM_UNDEFINED, |
| 331 | TPM_UNDEFINED, |
| 332 | TPM_UNDEFINED, /* 190 */ |
| 333 | TPM_UNDEFINED, |
| 334 | TPM_UNDEFINED, |
| 335 | TPM_UNDEFINED, |
| 336 | TPM_UNDEFINED, |
| 337 | TPM_UNDEFINED, /* 195 */ |
| 338 | TPM_UNDEFINED, |
| 339 | TPM_UNDEFINED, |
| 340 | TPM_UNDEFINED, |
| 341 | TPM_UNDEFINED, |
| 342 | TPM_SHORT, /* 200 */ |
| 343 | TPM_UNDEFINED, |
| 344 | TPM_UNDEFINED, |
| 345 | TPM_UNDEFINED, |
| 346 | TPM_SHORT, |
| 347 | TPM_SHORT, /* 205 */ |
| 348 | TPM_SHORT, |
| 349 | TPM_SHORT, |
| 350 | TPM_SHORT, |
| 351 | TPM_SHORT, |
| 352 | TPM_MEDIUM, /* 210 */ |
| 353 | TPM_UNDEFINED, |
| 354 | TPM_MEDIUM, |
| 355 | TPM_MEDIUM, |
| 356 | TPM_MEDIUM, |
| 357 | TPM_UNDEFINED, /* 215 */ |
| 358 | TPM_MEDIUM, |
| 359 | TPM_UNDEFINED, |
| 360 | TPM_UNDEFINED, |
| 361 | TPM_SHORT, |
| 362 | TPM_SHORT, /* 220 */ |
| 363 | TPM_SHORT, |
| 364 | TPM_SHORT, |
| 365 | TPM_SHORT, |
| 366 | TPM_SHORT, |
| 367 | TPM_UNDEFINED, /* 225 */ |
| 368 | TPM_UNDEFINED, |
| 369 | TPM_UNDEFINED, |
| 370 | TPM_UNDEFINED, |
| 371 | TPM_UNDEFINED, |
| 372 | TPM_SHORT, /* 230 */ |
| 373 | TPM_LONG, |
| 374 | TPM_MEDIUM, |
| 375 | TPM_UNDEFINED, |
| 376 | TPM_UNDEFINED, |
| 377 | TPM_UNDEFINED, /* 235 */ |
| 378 | TPM_UNDEFINED, |
| 379 | TPM_UNDEFINED, |
| 380 | TPM_UNDEFINED, |
| 381 | TPM_UNDEFINED, |
| 382 | TPM_SHORT, /* 240 */ |
| 383 | TPM_UNDEFINED, |
| 384 | TPM_MEDIUM, |
| 385 | }; |
| 386 | |
| 387 | /* TPM configuration */ |
| 388 | struct tpm { |
| 389 | struct udevice *dev; |
| 390 | char inited; |
| 391 | } tpm; |
| 392 | |
| 393 | /* Global structure for tpm chip data */ |
| 394 | static struct tpm_chip g_chip; |
| 395 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 396 | /* Structure to store I2C TPM specific stuff */ |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 397 | struct tpm_dev { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 398 | struct udevice *dev; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 399 | u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */ |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 400 | enum i2c_chip_type chip_type; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 403 | static struct tpm_dev tpm_dev; |
| 404 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 405 | /* |
| 406 | * iic_tpm_read() - read from TPM register |
| 407 | * @addr: register address to read from |
| 408 | * @buffer: provided by caller |
| 409 | * @len: number of bytes to read |
| 410 | * |
| 411 | * Read len bytes from TPM register and put them into |
| 412 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 413 | * |
| 414 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 415 | * values have to be swapped. |
| 416 | * |
| 417 | * Return -EIO on error, 0 on success. |
| 418 | */ |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 419 | static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 420 | { |
| 421 | int rc; |
| 422 | int count; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 423 | uint32_t addrbuf = addr; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 424 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 425 | if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) { |
| 426 | /* slb9635 protocol should work in both cases */ |
| 427 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 428 | rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1); |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 429 | if (rc == 0) |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 430 | break; /* Success, break to skip sleep */ |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 431 | udelay(SLEEP_DURATION); |
| 432 | } |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 433 | if (rc) |
| 434 | return -rc; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 435 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 436 | /* After the TPM has successfully received the register address |
| 437 | * it needs some time, thus we're sleeping here again, before |
| 438 | * retrieving the data |
| 439 | */ |
| 440 | for (count = 0; count < MAX_COUNT; count++) { |
| 441 | udelay(SLEEP_DURATION); |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 442 | rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len); |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 443 | if (rc == 0) |
| 444 | break; /* success, break to skip sleep */ |
| 445 | } |
| 446 | } else { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 447 | /* |
| 448 | * Use a combined read for newer chips. |
| 449 | * Unfortunately the smbus functions are not suitable due to |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 450 | * the 32 byte limit of the smbus. |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 451 | * Retries should usually not be needed, but are kept just to |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 452 | * be safe on the safe side. |
| 453 | */ |
| 454 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 455 | rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len); |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 456 | if (rc == 0) |
| 457 | break; /* break here to skip sleep */ |
| 458 | udelay(SLEEP_DURATION); |
| 459 | } |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 462 | /* Take care of 'guard time' */ |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 463 | udelay(SLEEP_DURATION); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 464 | if (rc) |
| 465 | return -rc; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 471 | unsigned int sleep_time, u8 max_count) |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 472 | { |
| 473 | int rc = 0; |
| 474 | int count; |
| 475 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 476 | for (count = 0; count < max_count; count++) { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 477 | rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 478 | if (rc == 0) |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 479 | break; /* Success, break to skip sleep */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 480 | udelay(sleep_time); |
| 481 | } |
| 482 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 483 | /* take care of 'guard time' */ |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 484 | udelay(sleep_time); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 485 | if (rc) |
| 486 | return -rc; |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * iic_tpm_write() - write to TPM register |
| 493 | * @addr: register address to write to |
| 494 | * @buffer: containing data to be written |
| 495 | * @len: number of bytes to write |
| 496 | * |
| 497 | * Write len bytes from provided buffer to TPM register (little |
| 498 | * endian format, i.e. buffer[0] is written as first byte). |
| 499 | * |
| 500 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 501 | * values have to be swapped. |
| 502 | * |
| 503 | * NOTE: use this function instead of the iic_tpm_write_generic function. |
| 504 | * |
| 505 | * Return -EIO on error, 0 on success |
| 506 | */ |
| 507 | static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) |
| 508 | { |
| 509 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION, |
| 510 | MAX_COUNT); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * This function is needed especially for the cleanup situation after |
| 515 | * sending TPM_READY |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 516 | */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 517 | static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) |
| 518 | { |
| 519 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG, |
| 520 | MAX_COUNT_LONG); |
| 521 | } |
| 522 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 523 | static int check_locality(struct tpm_chip *chip, int loc) |
| 524 | { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 525 | const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 526 | u8 buf; |
| 527 | int rc; |
| 528 | |
| 529 | rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); |
| 530 | if (rc < 0) |
| 531 | return rc; |
| 532 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 533 | if ((buf & mask) == mask) { |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 534 | chip->vendor.locality = loc; |
| 535 | return loc; |
| 536 | } |
| 537 | |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | static void release_locality(struct tpm_chip *chip, int loc, int force) |
| 542 | { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 543 | const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 544 | u8 buf; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 545 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 546 | if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) |
| 547 | return; |
| 548 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 549 | if (force || (buf & mask) == mask) { |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 550 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
| 551 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | static int request_locality(struct tpm_chip *chip, int loc) |
| 556 | { |
| 557 | unsigned long start, stop; |
| 558 | u8 buf = TPM_ACCESS_REQUEST_USE; |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 559 | int rc; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 560 | |
| 561 | if (check_locality(chip, loc) >= 0) |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 562 | return loc; /* We already have the locality */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 563 | |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 564 | rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 565 | if (rc) |
| 566 | return rc; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 567 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 568 | /* Wait for burstcount */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 569 | start = get_timer(0); |
| 570 | stop = chip->vendor.timeout_a; |
| 571 | do { |
| 572 | if (check_locality(chip, loc) >= 0) |
| 573 | return loc; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 574 | udelay(TPM_TIMEOUT * 1000); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 575 | } while (get_timer(start) < stop); |
| 576 | |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | static u8 tpm_tis_i2c_status(struct tpm_chip *chip) |
| 581 | { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 582 | /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 583 | u8 buf; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 584 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 585 | if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0) |
| 586 | return 0; |
| 587 | else |
| 588 | return buf; |
| 589 | } |
| 590 | |
| 591 | static void tpm_tis_i2c_ready(struct tpm_chip *chip) |
| 592 | { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 593 | int rc; |
| 594 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 595 | /* This causes the current command to be aborted */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 596 | u8 buf = TPM_STS_COMMAND_READY; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 597 | |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 598 | debug("%s\n", __func__); |
| 599 | rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1); |
| 600 | if (rc) |
| 601 | debug("%s: rc=%d\n", __func__, rc); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static ssize_t get_burstcount(struct tpm_chip *chip) |
| 605 | { |
| 606 | unsigned long start, stop; |
| 607 | ssize_t burstcnt; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 608 | u8 addr, buf[3]; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 609 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 610 | /* Wait for burstcount */ |
| 611 | /* XXX: Which timeout value? Spec has 2 answers (c & d) */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 612 | start = get_timer(0); |
| 613 | stop = chip->vendor.timeout_d; |
| 614 | do { |
| 615 | /* Note: STS is little endian */ |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 616 | addr = TPM_STS(chip->vendor.locality) + 1; |
| 617 | if (iic_tpm_read(addr, buf, 3) < 0) |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 618 | burstcnt = 0; |
| 619 | else |
| 620 | burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 621 | |
| 622 | if (burstcnt) |
| 623 | return burstcnt; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 624 | udelay(TPM_TIMEOUT * 1000); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 625 | } while (get_timer(start) < stop); |
| 626 | |
| 627 | return -EBUSY; |
| 628 | } |
| 629 | |
| 630 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 631 | int *status) |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 632 | { |
| 633 | unsigned long start, stop; |
| 634 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 635 | /* Check current status */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 636 | *status = tpm_tis_i2c_status(chip); |
| 637 | if ((*status & mask) == mask) |
| 638 | return 0; |
| 639 | |
| 640 | start = get_timer(0); |
| 641 | stop = timeout; |
| 642 | do { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 643 | udelay(TPM_TIMEOUT * 1000); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 644 | *status = tpm_tis_i2c_status(chip); |
| 645 | if ((*status & mask) == mask) |
| 646 | return 0; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 647 | } while (get_timer(start) < stop); |
| 648 | |
| 649 | return -ETIME; |
| 650 | } |
| 651 | |
| 652 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
| 653 | { |
| 654 | size_t size = 0; |
| 655 | ssize_t burstcnt; |
| 656 | int rc; |
| 657 | |
| 658 | while (size < count) { |
| 659 | burstcnt = get_burstcount(chip); |
| 660 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 661 | /* burstcount < 0 -> tpm is busy */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 662 | if (burstcnt < 0) |
| 663 | return burstcnt; |
| 664 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 665 | /* Limit received data to max left */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 666 | if (burstcnt > (count - size)) |
| 667 | burstcnt = count - size; |
| 668 | |
| 669 | rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality), |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 670 | &(buf[size]), burstcnt); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 671 | if (rc == 0) |
| 672 | size += burstcnt; |
| 673 | } |
| 674 | |
| 675 | return size; |
| 676 | } |
| 677 | |
| 678 | static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 679 | { |
| 680 | int size = 0; |
| 681 | int expected, status; |
| 682 | |
| 683 | if (count < TPM_HEADER_SIZE) { |
| 684 | size = -EIO; |
| 685 | goto out; |
| 686 | } |
| 687 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 688 | /* Read first 10 bytes, including tag, paramsize, and result */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 689 | size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 690 | if (size < TPM_HEADER_SIZE) { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 691 | error("Unable to read header\n"); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 692 | goto out; |
| 693 | } |
| 694 | |
| 695 | expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE); |
| 696 | if ((size_t)expected > count) { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 697 | error("Error size=%x, expected=%x, count=%x\n", size, expected, |
| 698 | count); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 699 | size = -EIO; |
| 700 | goto out; |
| 701 | } |
| 702 | |
| 703 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 704 | expected - TPM_HEADER_SIZE); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 705 | if (size < expected) { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 706 | error("Unable to read remainder of result\n"); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 707 | size = -ETIME; |
| 708 | goto out; |
| 709 | } |
| 710 | |
| 711 | wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 712 | if (status & TPM_STS_DATA_AVAIL) { /* Retry? */ |
| 713 | error("Error left over data\n"); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 714 | size = -EIO; |
| 715 | goto out; |
| 716 | } |
| 717 | |
| 718 | out: |
| 719 | tpm_tis_i2c_ready(chip); |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 720 | /* |
| 721 | * The TPM needs some time to clean up here, |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 722 | * so we sleep rather than keeping the bus busy |
| 723 | */ |
| 724 | udelay(2000); |
| 725 | release_locality(chip, chip->vendor.locality, 0); |
| 726 | |
| 727 | return size; |
| 728 | } |
| 729 | |
| 730 | static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 731 | { |
| 732 | int rc, status; |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 733 | size_t burstcnt; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 734 | size_t count = 0; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 735 | int retry = 0; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 736 | u8 sts = TPM_STS_GO; |
| 737 | |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 738 | debug("%s: len=%d\n", __func__, len); |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 739 | if (len > TPM_DEV_BUFSIZE) |
| 740 | return -E2BIG; /* Command is too long for our tpm, sorry */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 741 | |
| 742 | if (request_locality(chip, 0) < 0) |
| 743 | return -EBUSY; |
| 744 | |
| 745 | status = tpm_tis_i2c_status(chip); |
| 746 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 747 | tpm_tis_i2c_ready(chip); |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 748 | if (wait_for_stat(chip, TPM_STS_COMMAND_READY, |
| 749 | chip->vendor.timeout_b, &status) < 0) { |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 750 | rc = -ETIME; |
| 751 | goto out_err; |
| 752 | } |
| 753 | } |
| 754 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 755 | burstcnt = get_burstcount(chip); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 756 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 757 | /* burstcount < 0 -> tpm is busy */ |
| 758 | if (burstcnt < 0) |
| 759 | return burstcnt; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 760 | |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 761 | while (count < len) { |
| 762 | udelay(300); |
| 763 | if (burstcnt > len - count) |
| 764 | burstcnt = len - count; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 765 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 766 | #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION |
| 767 | if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION) |
| 768 | burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION; |
| 769 | #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 770 | |
| 771 | rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 772 | &(buf[count]), burstcnt); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 773 | if (rc == 0) |
| 774 | count += burstcnt; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 775 | else { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 776 | debug("%s: error\n", __func__); |
| 777 | if (retry++ > 10) { |
| 778 | rc = -EIO; |
| 779 | goto out_err; |
| 780 | } |
| 781 | rc = wait_for_stat(chip, TPM_STS_VALID, |
| 782 | chip->vendor.timeout_c, &status); |
| 783 | if (rc) |
| 784 | goto out_err; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 785 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 786 | if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 787 | rc = -EIO; |
| 788 | goto out_err; |
| 789 | } |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 793 | /* Go and do it */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 794 | iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1); |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 795 | debug("done\n"); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 796 | |
| 797 | return len; |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 798 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 799 | out_err: |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 800 | debug("%s: out_err\n", __func__); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 801 | tpm_tis_i2c_ready(chip); |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 802 | /* |
| 803 | * The TPM needs some time to clean up here, |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 804 | * so we sleep rather than keeping the bus busy |
| 805 | */ |
| 806 | udelay(2000); |
| 807 | release_locality(chip, chip->vendor.locality, 0); |
| 808 | |
| 809 | return rc; |
| 810 | } |
| 811 | |
| 812 | static struct tpm_vendor_specific tpm_tis_i2c = { |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 813 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 814 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 815 | .req_canceled = TPM_STS_COMMAND_READY, |
| 816 | }; |
| 817 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 818 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 819 | static enum i2c_chip_type tpm_vendor_chip_type(void) |
| 820 | { |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 821 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 822 | const void *blob = gd->fdt_blob; |
| 823 | |
| 824 | if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0) |
| 825 | return SLB9645; |
| 826 | |
| 827 | if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0) |
| 828 | return SLB9635; |
| 829 | #endif |
| 830 | return UNKNOWN; |
| 831 | } |
| 832 | |
Simon Glass | ac1f857 | 2015-08-22 18:31:17 -0600 | [diff] [blame] | 833 | int tpm_vendor_init(struct udevice *dev) |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 834 | { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 835 | struct tpm_chip *chip; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 836 | u32 vendor; |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 837 | u32 expected_did_vid; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 838 | |
Simon Glass | ac1f857 | 2015-08-22 18:31:17 -0600 | [diff] [blame] | 839 | tpm_dev.dev = dev; |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 840 | tpm_dev.chip_type = tpm_vendor_chip_type(); |
| 841 | |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 842 | chip = tpm_register_hardware(&tpm_tis_i2c); |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 843 | if (chip < 0) |
| 844 | return -ENODEV; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 845 | |
| 846 | /* Disable interrupts (not supported) */ |
| 847 | chip->vendor.irq = 0; |
| 848 | |
| 849 | /* Default timeouts */ |
| 850 | chip->vendor.timeout_a = TIS_SHORT_TIMEOUT; |
| 851 | chip->vendor.timeout_b = TIS_LONG_TIMEOUT; |
| 852 | chip->vendor.timeout_c = TIS_SHORT_TIMEOUT; |
| 853 | chip->vendor.timeout_d = TIS_SHORT_TIMEOUT; |
| 854 | |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 855 | if (request_locality(chip, 0) < 0) |
| 856 | return -ENODEV; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 857 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 858 | /* Read four bytes from DID_VID register */ |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 859 | if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) { |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 860 | release_locality(chip, 0, 1); |
| 861 | return -EIO; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 864 | if (tpm_dev.chip_type == SLB9635) { |
| 865 | vendor = be32_to_cpu(vendor); |
| 866 | expected_did_vid = TPM_TIS_I2C_DID_VID_9635; |
| 867 | } else { |
| 868 | /* device id and byte order has changed for newer i2c tpms */ |
| 869 | expected_did_vid = TPM_TIS_I2C_DID_VID_9645; |
| 870 | } |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 871 | |
Vincent Palatin | 06883eb | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 872 | if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) { |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 873 | error("Vendor id did not match! ID was %08x\n", vendor); |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 874 | return -ENODEV; |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Tom Wai-Hong Tam | e49fed5 | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 877 | debug("1.2 TPM (chip type %s device-id 0x%X)\n", |
| 878 | chip_name[tpm_dev.chip_type], vendor >> 16); |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 879 | |
| 880 | /* |
| 881 | * A timeout query to TPM can be placed here. |
| 882 | * Standard timeout values are used so far |
| 883 | */ |
| 884 | |
| 885 | return 0; |
Simon Glass | c9a3e84 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 886 | } |
Rong Chang | 8faa945 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 887 | |
| 888 | void tpm_vendor_cleanup(struct tpm_chip *chip) |
| 889 | { |
| 890 | release_locality(chip, chip->vendor.locality, 1); |
| 891 | } |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 892 | |
| 893 | /* Returns max number of milliseconds to wait */ |
| 894 | static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, |
| 895 | u32 ordinal) |
| 896 | { |
| 897 | int duration_idx = TPM_UNDEFINED; |
| 898 | int duration = 0; |
| 899 | |
| 900 | if (ordinal < TPM_MAX_ORDINAL) { |
| 901 | duration_idx = tpm_ordinal_duration[ordinal]; |
| 902 | } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < |
| 903 | TPM_MAX_PROTECTED_ORDINAL) { |
| 904 | duration_idx = tpm_protected_ordinal_duration[ |
| 905 | ordinal & TPM_PROTECTED_ORDINAL_MASK]; |
| 906 | } |
| 907 | |
| 908 | if (duration_idx != TPM_UNDEFINED) |
| 909 | duration = chip->vendor.duration[duration_idx]; |
| 910 | |
| 911 | if (duration <= 0) |
| 912 | return 2 * 60 * HZ; /* Two minutes timeout */ |
| 913 | else |
| 914 | return duration; |
| 915 | } |
| 916 | |
| 917 | static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz) |
| 918 | { |
| 919 | int rc; |
| 920 | u32 count, ordinal; |
| 921 | unsigned long start, stop; |
| 922 | |
| 923 | struct tpm_chip *chip = &g_chip; |
| 924 | |
| 925 | /* switch endianess: big->little */ |
| 926 | count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE); |
| 927 | ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE); |
| 928 | |
| 929 | if (count == 0) { |
| 930 | error("no data\n"); |
| 931 | return -ENODATA; |
| 932 | } |
| 933 | if (count > bufsiz) { |
| 934 | error("invalid count value %x %zx\n", count, bufsiz); |
| 935 | return -E2BIG; |
| 936 | } |
| 937 | |
| 938 | debug("Calling send\n"); |
Simon Glass | 651d3d1 | 2015-08-22 18:31:23 -0600 | [diff] [blame^] | 939 | rc = tpm_tis_i2c_send(chip, (u8 *)buf, count); |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 940 | debug(" ... done calling send\n"); |
| 941 | if (rc < 0) { |
| 942 | error("tpm_transmit: tpm_send: error %d\n", rc); |
| 943 | goto out; |
| 944 | } |
| 945 | |
| 946 | if (chip->vendor.irq) |
| 947 | goto out_recv; |
| 948 | |
| 949 | start = get_timer(0); |
| 950 | stop = tpm_calc_ordinal_duration(chip, ordinal); |
| 951 | do { |
| 952 | debug("waiting for status... %ld %ld\n", start, stop); |
Simon Glass | 651d3d1 | 2015-08-22 18:31:23 -0600 | [diff] [blame^] | 953 | u8 status = tpm_tis_i2c_status(chip); |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 954 | if ((status & chip->vendor.req_complete_mask) == |
| 955 | chip->vendor.req_complete_val) { |
| 956 | debug("...got it;\n"); |
| 957 | goto out_recv; |
| 958 | } |
| 959 | |
| 960 | if (status == chip->vendor.req_canceled) { |
| 961 | error("Operation Canceled\n"); |
| 962 | rc = -ECANCELED; |
| 963 | goto out; |
| 964 | } |
| 965 | udelay(TPM_TIMEOUT * 1000); |
| 966 | } while (get_timer(start) < stop); |
| 967 | |
Simon Glass | 651d3d1 | 2015-08-22 18:31:23 -0600 | [diff] [blame^] | 968 | tpm_tis_i2c_ready(chip); |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 969 | error("Operation Timed out\n"); |
| 970 | rc = -ETIME; |
| 971 | goto out; |
| 972 | |
| 973 | out_recv: |
| 974 | debug("out_recv: reading response...\n"); |
Simon Glass | 651d3d1 | 2015-08-22 18:31:23 -0600 | [diff] [blame^] | 975 | rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE); |
Simon Glass | 9457627 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 976 | if (rc < 0) |
| 977 | error("tpm_transmit: tpm_recv: error %d\n", rc); |
| 978 | |
| 979 | out: |
| 980 | return rc; |
| 981 | } |
| 982 | |
| 983 | static int tpm_open_dev(struct udevice *dev) |
| 984 | { |
| 985 | int rc; |
| 986 | |
| 987 | debug("%s: start\n", __func__); |
| 988 | if (g_chip.is_open) |
| 989 | return -EBUSY; |
| 990 | rc = tpm_vendor_init(dev); |
| 991 | if (rc < 0) |
| 992 | g_chip.is_open = 0; |
| 993 | return rc; |
| 994 | } |
| 995 | |
| 996 | static void tpm_close(void) |
| 997 | { |
| 998 | if (g_chip.is_open) { |
| 999 | tpm_vendor_cleanup(&g_chip); |
| 1000 | g_chip.is_open = 0; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Decode TPM configuration. |
| 1006 | * |
| 1007 | * @param dev Returns a configuration of TPM device |
| 1008 | * @return 0 if ok, -1 on error |
| 1009 | */ |
| 1010 | static int tpm_decode_config(struct tpm *dev) |
| 1011 | { |
| 1012 | const void *blob = gd->fdt_blob; |
| 1013 | struct udevice *bus; |
| 1014 | int chip_addr; |
| 1015 | int parent; |
| 1016 | int node; |
| 1017 | int ret; |
| 1018 | |
| 1019 | node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM); |
| 1020 | if (node < 0) { |
| 1021 | node = fdtdec_next_compatible(blob, 0, |
| 1022 | COMPAT_INFINEON_SLB9645_TPM); |
| 1023 | } |
| 1024 | if (node < 0) { |
| 1025 | debug("%s: Node not found\n", __func__); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | parent = fdt_parent_offset(blob, node); |
| 1029 | if (parent < 0) { |
| 1030 | debug("%s: Cannot find node parent\n", __func__); |
| 1031 | return -1; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * TODO(sjg@chromium.org): Remove this when driver model supports |
| 1036 | * TPMs |
| 1037 | */ |
| 1038 | ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus); |
| 1039 | if (ret) { |
| 1040 | debug("Cannot find bus for node '%s: ret=%d'\n", |
| 1041 | fdt_get_name(blob, parent, NULL), ret); |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
| 1045 | chip_addr = fdtdec_get_int(blob, node, "reg", -1); |
| 1046 | if (chip_addr == -1) { |
| 1047 | debug("Cannot find reg property for node '%s: ret=%d'\n", |
| 1048 | fdt_get_name(blob, node, NULL), ret); |
| 1049 | return ret; |
| 1050 | } |
| 1051 | /* |
| 1052 | * TODO(sjg@chromium.org): Older TPMs will need to use the older method |
| 1053 | * in iic_tpm_read() so the offset length needs to be 0 here. |
| 1054 | */ |
| 1055 | ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev); |
| 1056 | if (ret) { |
| 1057 | debug("Cannot find device for node '%s: ret=%d'\n", |
| 1058 | fdt_get_name(blob, node, NULL), ret); |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry) |
| 1066 | { |
| 1067 | struct tpm_chip *chip; |
| 1068 | |
| 1069 | /* Driver specific per-device data */ |
| 1070 | chip = &g_chip; |
| 1071 | memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); |
| 1072 | chip->is_open = 1; |
| 1073 | |
| 1074 | return chip; |
| 1075 | } |
| 1076 | |
| 1077 | int tis_init(void) |
| 1078 | { |
| 1079 | if (tpm.inited) |
| 1080 | return 0; |
| 1081 | |
| 1082 | if (tpm_decode_config(&tpm)) |
| 1083 | return -1; |
| 1084 | |
| 1085 | debug("%s: done\n", __func__); |
| 1086 | |
| 1087 | tpm.inited = 1; |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | int tis_open(void) |
| 1093 | { |
| 1094 | int rc; |
| 1095 | |
| 1096 | if (!tpm.inited) |
| 1097 | return -1; |
| 1098 | |
| 1099 | rc = tpm_open_dev(tpm.dev); |
| 1100 | |
| 1101 | return rc; |
| 1102 | } |
| 1103 | |
| 1104 | int tis_close(void) |
| 1105 | { |
| 1106 | if (!tpm.inited) |
| 1107 | return -1; |
| 1108 | |
| 1109 | tpm_close(); |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, |
| 1115 | uint8_t *recvbuf, size_t *rbuf_len) |
| 1116 | { |
| 1117 | int len; |
| 1118 | uint8_t buf[4096]; |
| 1119 | |
| 1120 | if (!tpm.inited) |
| 1121 | return -1; |
| 1122 | |
| 1123 | if (sizeof(buf) < sbuf_size) |
| 1124 | return -1; |
| 1125 | |
| 1126 | memcpy(buf, sendbuf, sbuf_size); |
| 1127 | |
| 1128 | len = tpm_transmit(buf, sbuf_size); |
| 1129 | |
| 1130 | if (len < 10) { |
| 1131 | *rbuf_len = 0; |
| 1132 | return -1; |
| 1133 | } |
| 1134 | |
| 1135 | memcpy(recvbuf, buf, len); |
| 1136 | *rbuf_len = len; |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |