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