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 | |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 37 | static const char * const chip_name[] = { |
| 38 | [SLB9635] = "slb9635tt", |
| 39 | [SLB9645] = "slb9645tt", |
| 40 | [UNKNOWN] = "unknown/fallback to slb9635", |
| 41 | }; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 42 | |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 43 | static struct tpm_chip g_chip; |
| 44 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 45 | /* |
| 46 | * iic_tpm_read() - read from TPM register |
| 47 | * @addr: register address to read from |
| 48 | * @buffer: provided by caller |
| 49 | * @len: number of bytes to read |
| 50 | * |
| 51 | * Read len bytes from TPM register and put them into |
| 52 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 53 | * |
| 54 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 55 | * values have to be swapped. |
| 56 | * |
| 57 | * Return -EIO on error, 0 on success. |
| 58 | */ |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 59 | static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 60 | { |
| 61 | int rc; |
| 62 | int count; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 63 | uint32_t addrbuf = addr; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 64 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 65 | if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) { |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 66 | /* slb9635 protocol should work in both cases */ |
| 67 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 68 | rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 69 | if (rc == 0) |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 70 | break; /* Success, break to skip sleep */ |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 71 | udelay(SLEEP_DURATION); |
| 72 | } |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 73 | if (rc) |
| 74 | return -rc; |
| 75 | |
| 76 | /* After the TPM has successfully received the register address |
| 77 | * it needs some time, thus we're sleeping here again, before |
| 78 | * retrieving the data |
| 79 | */ |
| 80 | for (count = 0; count < MAX_COUNT; count++) { |
| 81 | udelay(SLEEP_DURATION); |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 82 | rc = dm_i2c_read(g_chip.dev, 0, buffer, len); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 83 | if (rc == 0) |
| 84 | break; /* success, break to skip sleep */ |
| 85 | } |
| 86 | } else { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 87 | /* |
| 88 | * Use a combined read for newer chips. |
| 89 | * Unfortunately the smbus functions are not suitable due to |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 90 | * the 32 byte limit of the smbus. |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 91 | * Retries should usually not be needed, but are kept just to |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 92 | * be safe on the safe side. |
| 93 | */ |
| 94 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 95 | rc = dm_i2c_read(g_chip.dev, addr, buffer, len); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 96 | if (rc == 0) |
| 97 | break; /* break here to skip sleep */ |
| 98 | udelay(SLEEP_DURATION); |
| 99 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 102 | /* Take care of 'guard time' */ |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 103 | udelay(SLEEP_DURATION); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 104 | if (rc) |
| 105 | return -rc; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | 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] | 111 | unsigned int sleep_time, u8 max_count) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 112 | { |
| 113 | int rc = 0; |
| 114 | int count; |
| 115 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 116 | for (count = 0; count < max_count; count++) { |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 117 | rc = dm_i2c_write(g_chip.dev, addr, buffer, len); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 118 | if (rc == 0) |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 119 | break; /* Success, break to skip sleep */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 120 | udelay(sleep_time); |
| 121 | } |
| 122 | |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 123 | /* take care of 'guard time' */ |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 124 | udelay(sleep_time); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 125 | if (rc) |
| 126 | return -rc; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * iic_tpm_write() - write to TPM register |
| 133 | * @addr: register address to write to |
| 134 | * @buffer: containing data to be written |
| 135 | * @len: number of bytes to write |
| 136 | * |
| 137 | * Write len bytes from provided buffer to TPM register (little |
| 138 | * endian format, i.e. buffer[0] is written as first byte). |
| 139 | * |
| 140 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 141 | * values have to be swapped. |
| 142 | * |
| 143 | * NOTE: use this function instead of the iic_tpm_write_generic function. |
| 144 | * |
| 145 | * Return -EIO on error, 0 on success |
| 146 | */ |
| 147 | static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) |
| 148 | { |
| 149 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION, |
| 150 | MAX_COUNT); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * This function is needed especially for the cleanup situation after |
| 155 | * sending TPM_READY |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 156 | */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 157 | static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) |
| 158 | { |
| 159 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG, |
| 160 | MAX_COUNT_LONG); |
| 161 | } |
| 162 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 163 | static int check_locality(struct tpm_chip *chip, int loc) |
| 164 | { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 165 | const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 166 | u8 buf; |
| 167 | int rc; |
| 168 | |
| 169 | rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); |
| 170 | if (rc < 0) |
| 171 | return rc; |
| 172 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 173 | if ((buf & mask) == mask) { |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 174 | chip->locality = loc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 175 | return loc; |
| 176 | } |
| 177 | |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | static void release_locality(struct tpm_chip *chip, int loc, int force) |
| 182 | { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 183 | const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 184 | u8 buf; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 185 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 186 | if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) |
| 187 | return; |
| 188 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 189 | if (force || (buf & mask) == mask) { |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 190 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
| 191 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static int request_locality(struct tpm_chip *chip, int loc) |
| 196 | { |
| 197 | unsigned long start, stop; |
| 198 | u8 buf = TPM_ACCESS_REQUEST_USE; |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 199 | int rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 200 | |
| 201 | if (check_locality(chip, loc) >= 0) |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 202 | return loc; /* We already have the locality */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 203 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 204 | rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 205 | if (rc) |
| 206 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 207 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 208 | /* Wait for burstcount */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 209 | start = get_timer(0); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 210 | stop = chip->timeout_a; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 211 | do { |
| 212 | if (check_locality(chip, loc) >= 0) |
| 213 | return loc; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 214 | udelay(TPM_TIMEOUT * 1000); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 215 | } while (get_timer(start) < stop); |
| 216 | |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | static u8 tpm_tis_i2c_status(struct tpm_chip *chip) |
| 221 | { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 222 | /* 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] | 223 | u8 buf; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 224 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 225 | if (iic_tpm_read(TPM_STS(chip->locality), &buf, 1) < 0) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | else |
| 228 | return buf; |
| 229 | } |
| 230 | |
| 231 | static void tpm_tis_i2c_ready(struct tpm_chip *chip) |
| 232 | { |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 233 | int rc; |
| 234 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 235 | /* This causes the current command to be aborted */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 236 | u8 buf = TPM_STS_COMMAND_READY; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 237 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 238 | debug("%s\n", __func__); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 239 | rc = iic_tpm_write_long(TPM_STS(chip->locality), &buf, 1); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 240 | if (rc) |
| 241 | debug("%s: rc=%d\n", __func__, rc); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static ssize_t get_burstcount(struct tpm_chip *chip) |
| 245 | { |
| 246 | unsigned long start, stop; |
| 247 | ssize_t burstcnt; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 248 | u8 addr, buf[3]; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 249 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 250 | /* Wait for burstcount */ |
| 251 | /* XXX: Which timeout value? Spec has 2 answers (c & d) */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 252 | start = get_timer(0); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 253 | stop = chip->timeout_d; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 254 | do { |
| 255 | /* Note: STS is little endian */ |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 256 | addr = TPM_STS(chip->locality) + 1; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 257 | if (iic_tpm_read(addr, buf, 3) < 0) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 258 | burstcnt = 0; |
| 259 | else |
| 260 | burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 261 | |
| 262 | if (burstcnt) |
| 263 | return burstcnt; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 264 | udelay(TPM_TIMEOUT * 1000); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 265 | } while (get_timer(start) < stop); |
| 266 | |
| 267 | return -EBUSY; |
| 268 | } |
| 269 | |
| 270 | 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] | 271 | int *status) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 272 | { |
| 273 | unsigned long start, stop; |
| 274 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 275 | /* Check current status */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 276 | *status = tpm_tis_i2c_status(chip); |
| 277 | if ((*status & mask) == mask) |
| 278 | return 0; |
| 279 | |
| 280 | start = get_timer(0); |
| 281 | stop = timeout; |
| 282 | do { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 283 | udelay(TPM_TIMEOUT * 1000); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 284 | *status = tpm_tis_i2c_status(chip); |
| 285 | if ((*status & mask) == mask) |
| 286 | return 0; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 287 | } while (get_timer(start) < stop); |
| 288 | |
| 289 | return -ETIME; |
| 290 | } |
| 291 | |
| 292 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
| 293 | { |
| 294 | size_t size = 0; |
| 295 | ssize_t burstcnt; |
| 296 | int rc; |
| 297 | |
| 298 | while (size < count) { |
| 299 | burstcnt = get_burstcount(chip); |
| 300 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 301 | /* burstcount < 0 -> tpm is busy */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 302 | if (burstcnt < 0) |
| 303 | return burstcnt; |
| 304 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 305 | /* Limit received data to max left */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 306 | if (burstcnt > (count - size)) |
| 307 | burstcnt = count - size; |
| 308 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 309 | rc = iic_tpm_read(TPM_DATA_FIFO(chip->locality), |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 310 | &(buf[size]), burstcnt); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 311 | if (rc == 0) |
| 312 | size += burstcnt; |
| 313 | } |
| 314 | |
| 315 | return size; |
| 316 | } |
| 317 | |
| 318 | static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) |
| 319 | { |
| 320 | int size = 0; |
| 321 | int expected, status; |
| 322 | |
| 323 | if (count < TPM_HEADER_SIZE) { |
| 324 | size = -EIO; |
| 325 | goto out; |
| 326 | } |
| 327 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 328 | /* Read first 10 bytes, including tag, paramsize, and result */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 329 | size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 330 | if (size < TPM_HEADER_SIZE) { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 331 | error("Unable to read header\n"); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE); |
| 336 | if ((size_t)expected > count) { |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 337 | error("Error size=%x, expected=%x, count=%x\n", size, expected, |
| 338 | count); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 339 | size = -EIO; |
| 340 | goto out; |
| 341 | } |
| 342 | |
| 343 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 344 | expected - TPM_HEADER_SIZE); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 345 | if (size < expected) { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 346 | error("Unable to read remainder of result\n"); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 347 | size = -ETIME; |
| 348 | goto out; |
| 349 | } |
| 350 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 351 | 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] | 352 | if (status & TPM_STS_DATA_AVAIL) { /* Retry? */ |
| 353 | error("Error left over data\n"); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 354 | size = -EIO; |
| 355 | goto out; |
| 356 | } |
| 357 | |
| 358 | out: |
| 359 | tpm_tis_i2c_ready(chip); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 360 | /* |
| 361 | * The TPM needs some time to clean up here, |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 362 | * so we sleep rather than keeping the bus busy |
| 363 | */ |
| 364 | udelay(2000); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 365 | release_locality(chip, chip->locality, 0); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 366 | |
| 367 | return size; |
| 368 | } |
| 369 | |
| 370 | static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) |
| 371 | { |
| 372 | int rc, status; |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 373 | size_t burstcnt; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 374 | size_t count = 0; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 375 | int retry = 0; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 376 | u8 sts = TPM_STS_GO; |
| 377 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 378 | debug("%s: len=%d\n", __func__, len); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 379 | if (len > TPM_DEV_BUFSIZE) |
| 380 | return -E2BIG; /* Command is too long for our tpm, sorry */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 381 | |
| 382 | if (request_locality(chip, 0) < 0) |
| 383 | return -EBUSY; |
| 384 | |
| 385 | status = tpm_tis_i2c_status(chip); |
| 386 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 387 | tpm_tis_i2c_ready(chip); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 388 | if (wait_for_stat(chip, TPM_STS_COMMAND_READY, |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 389 | chip->timeout_b, &status) < 0) { |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 390 | rc = -ETIME; |
| 391 | goto out_err; |
| 392 | } |
| 393 | } |
| 394 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 395 | burstcnt = get_burstcount(chip); |
| 396 | |
| 397 | /* burstcount < 0 -> tpm is busy */ |
| 398 | if (burstcnt < 0) |
| 399 | return burstcnt; |
| 400 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 401 | while (count < len) { |
| 402 | udelay(300); |
| 403 | if (burstcnt > len - count) |
| 404 | burstcnt = len - count; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 405 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 406 | #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION |
| 407 | if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION) |
| 408 | burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION; |
| 409 | #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 410 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 411 | rc = iic_tpm_write(TPM_DATA_FIFO(chip->locality), |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 412 | &(buf[count]), burstcnt); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 413 | if (rc == 0) |
| 414 | count += burstcnt; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 415 | else { |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 416 | debug("%s: error\n", __func__); |
| 417 | if (retry++ > 10) { |
| 418 | rc = -EIO; |
| 419 | goto out_err; |
| 420 | } |
| 421 | rc = wait_for_stat(chip, TPM_STS_VALID, |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 422 | chip->timeout_c, &status); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 423 | if (rc) |
| 424 | goto out_err; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 425 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 426 | if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 427 | rc = -EIO; |
| 428 | goto out_err; |
| 429 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 433 | /* Go and do it */ |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 434 | iic_tpm_write(TPM_STS(chip->locality), &sts, 1); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 435 | debug("done\n"); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 436 | |
| 437 | return len; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 438 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 439 | out_err: |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 440 | debug("%s: out_err\n", __func__); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 441 | tpm_tis_i2c_ready(chip); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 442 | /* |
| 443 | * The TPM needs some time to clean up here, |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 444 | * so we sleep rather than keeping the bus busy |
| 445 | */ |
| 446 | udelay(2000); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 447 | release_locality(chip, chip->locality, 0); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 448 | |
| 449 | return rc; |
| 450 | } |
| 451 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 452 | static enum i2c_chip_type tpm_tis_i2c_chip_type(void) |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 453 | { |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 454 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 455 | const void *blob = gd->fdt_blob; |
| 456 | |
| 457 | if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0) |
| 458 | return SLB9645; |
| 459 | |
| 460 | if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0) |
| 461 | return SLB9635; |
| 462 | #endif |
| 463 | return UNKNOWN; |
| 464 | } |
| 465 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 466 | static int tpm_tis_i2c_init(struct udevice *dev) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 467 | { |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 468 | struct tpm_chip *chip = &g_chip; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 469 | u32 vendor; |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 470 | u32 expected_did_vid; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 471 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 472 | g_chip.dev = dev; |
| 473 | g_chip.chip_type = tpm_tis_i2c_chip_type(); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 474 | chip->is_open = 1; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 475 | |
| 476 | /* Disable interrupts (not supported) */ |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 477 | chip->irq = 0; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 478 | |
| 479 | /* Default timeouts */ |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 480 | chip->timeout_a = TIS_SHORT_TIMEOUT; |
| 481 | chip->timeout_b = TIS_LONG_TIMEOUT; |
| 482 | chip->timeout_c = TIS_SHORT_TIMEOUT; |
| 483 | chip->timeout_d = TIS_SHORT_TIMEOUT; |
| 484 | chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 485 | chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 486 | chip->req_canceled = TPM_STS_COMMAND_READY; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 487 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 488 | if (request_locality(chip, 0) < 0) |
| 489 | return -ENODEV; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 490 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 491 | /* Read four bytes from DID_VID register */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 492 | if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) { |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 493 | release_locality(chip, 0, 1); |
| 494 | return -EIO; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 497 | if (g_chip.chip_type == SLB9635) { |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 498 | vendor = be32_to_cpu(vendor); |
| 499 | expected_did_vid = TPM_TIS_I2C_DID_VID_9635; |
| 500 | } else { |
| 501 | /* device id and byte order has changed for newer i2c tpms */ |
| 502 | expected_did_vid = TPM_TIS_I2C_DID_VID_9645; |
| 503 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 504 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 505 | if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 506 | error("Vendor id did not match! ID was %08x\n", vendor); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 507 | return -ENODEV; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 510 | debug("1.2 TPM (chip type %s device-id 0x%X)\n", |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 511 | chip_name[g_chip.chip_type], vendor >> 16); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * A timeout query to TPM can be placed here. |
| 515 | * Standard timeout values are used so far |
| 516 | */ |
| 517 | |
| 518 | return 0; |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 519 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 520 | |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 521 | /* Returns max number of milliseconds to wait */ |
| 522 | static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, |
| 523 | u32 ordinal) |
| 524 | { |
| 525 | int duration_idx = TPM_UNDEFINED; |
| 526 | int duration = 0; |
| 527 | |
| 528 | if (ordinal < TPM_MAX_ORDINAL) { |
| 529 | duration_idx = tpm_ordinal_duration[ordinal]; |
| 530 | } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < |
| 531 | TPM_MAX_PROTECTED_ORDINAL) { |
| 532 | duration_idx = tpm_protected_ordinal_duration[ |
| 533 | ordinal & TPM_PROTECTED_ORDINAL_MASK]; |
| 534 | } |
| 535 | |
| 536 | if (duration_idx != TPM_UNDEFINED) |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 537 | duration = chip->duration[duration_idx]; |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 538 | |
| 539 | if (duration <= 0) |
| 540 | return 2 * 60 * HZ; /* Two minutes timeout */ |
| 541 | else |
| 542 | return duration; |
| 543 | } |
| 544 | |
| 545 | static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz) |
| 546 | { |
| 547 | int rc; |
| 548 | u32 count, ordinal; |
| 549 | unsigned long start, stop; |
| 550 | |
| 551 | struct tpm_chip *chip = &g_chip; |
| 552 | |
| 553 | /* switch endianess: big->little */ |
| 554 | count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE); |
| 555 | ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE); |
| 556 | |
| 557 | if (count == 0) { |
| 558 | error("no data\n"); |
| 559 | return -ENODATA; |
| 560 | } |
| 561 | if (count > bufsiz) { |
| 562 | error("invalid count value %x %zx\n", count, bufsiz); |
| 563 | return -E2BIG; |
| 564 | } |
| 565 | |
| 566 | debug("Calling send\n"); |
Simon Glass | b382e02 | 2015-08-22 18:31:23 -0600 | [diff] [blame] | 567 | rc = tpm_tis_i2c_send(chip, (u8 *)buf, count); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 568 | debug(" ... done calling send\n"); |
| 569 | if (rc < 0) { |
| 570 | error("tpm_transmit: tpm_send: error %d\n", rc); |
| 571 | goto out; |
| 572 | } |
| 573 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 574 | if (chip->irq) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 575 | goto out_recv; |
| 576 | |
| 577 | start = get_timer(0); |
| 578 | stop = tpm_calc_ordinal_duration(chip, ordinal); |
| 579 | do { |
| 580 | debug("waiting for status... %ld %ld\n", start, stop); |
Simon Glass | b382e02 | 2015-08-22 18:31:23 -0600 | [diff] [blame] | 581 | u8 status = tpm_tis_i2c_status(chip); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 582 | if ((status & chip->req_complete_mask) == |
| 583 | chip->req_complete_val) { |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 584 | debug("...got it;\n"); |
| 585 | goto out_recv; |
| 586 | } |
| 587 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 588 | if (status == chip->req_canceled) { |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 589 | error("Operation Canceled\n"); |
| 590 | rc = -ECANCELED; |
| 591 | goto out; |
| 592 | } |
| 593 | udelay(TPM_TIMEOUT * 1000); |
| 594 | } while (get_timer(start) < stop); |
| 595 | |
Simon Glass | b382e02 | 2015-08-22 18:31:23 -0600 | [diff] [blame] | 596 | tpm_tis_i2c_ready(chip); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 597 | error("Operation Timed out\n"); |
| 598 | rc = -ETIME; |
| 599 | goto out; |
| 600 | |
| 601 | out_recv: |
| 602 | debug("out_recv: reading response...\n"); |
Simon Glass | b382e02 | 2015-08-22 18:31:23 -0600 | [diff] [blame] | 603 | rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 604 | if (rc < 0) |
| 605 | error("tpm_transmit: tpm_recv: error %d\n", rc); |
| 606 | |
| 607 | out: |
| 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | static int tpm_open_dev(struct udevice *dev) |
| 612 | { |
| 613 | int rc; |
| 614 | |
| 615 | debug("%s: start\n", __func__); |
| 616 | if (g_chip.is_open) |
| 617 | return -EBUSY; |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 618 | rc = tpm_tis_i2c_init(dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 619 | if (rc < 0) |
| 620 | g_chip.is_open = 0; |
| 621 | return rc; |
| 622 | } |
| 623 | |
| 624 | static void tpm_close(void) |
| 625 | { |
| 626 | if (g_chip.is_open) { |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 627 | release_locality(&g_chip, g_chip.locality, 1); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 628 | g_chip.is_open = 0; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Decode TPM configuration. |
| 634 | * |
| 635 | * @param dev Returns a configuration of TPM device |
| 636 | * @return 0 if ok, -1 on error |
| 637 | */ |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 638 | static int tpm_decode_config(struct tpm_chip *chip) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 639 | { |
| 640 | const void *blob = gd->fdt_blob; |
| 641 | struct udevice *bus; |
| 642 | int chip_addr; |
| 643 | int parent; |
| 644 | int node; |
| 645 | int ret; |
| 646 | |
| 647 | node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM); |
| 648 | if (node < 0) { |
| 649 | node = fdtdec_next_compatible(blob, 0, |
| 650 | COMPAT_INFINEON_SLB9645_TPM); |
| 651 | } |
| 652 | if (node < 0) { |
| 653 | debug("%s: Node not found\n", __func__); |
| 654 | return -1; |
| 655 | } |
| 656 | parent = fdt_parent_offset(blob, node); |
| 657 | if (parent < 0) { |
| 658 | debug("%s: Cannot find node parent\n", __func__); |
| 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * TODO(sjg@chromium.org): Remove this when driver model supports |
| 664 | * TPMs |
| 665 | */ |
| 666 | ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus); |
| 667 | if (ret) { |
| 668 | debug("Cannot find bus for node '%s: ret=%d'\n", |
| 669 | fdt_get_name(blob, parent, NULL), ret); |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | chip_addr = fdtdec_get_int(blob, node, "reg", -1); |
| 674 | if (chip_addr == -1) { |
| 675 | debug("Cannot find reg property for node '%s: ret=%d'\n", |
| 676 | fdt_get_name(blob, node, NULL), ret); |
| 677 | return ret; |
| 678 | } |
| 679 | /* |
| 680 | * TODO(sjg@chromium.org): Older TPMs will need to use the older method |
| 681 | * in iic_tpm_read() so the offset length needs to be 0 here. |
| 682 | */ |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 683 | ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 684 | if (ret) { |
| 685 | debug("Cannot find device for node '%s: ret=%d'\n", |
| 686 | fdt_get_name(blob, node, NULL), ret); |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 693 | int tis_init(void) |
| 694 | { |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 695 | if (g_chip.inited) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 696 | return 0; |
| 697 | |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 698 | if (tpm_decode_config(&g_chip)) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 699 | return -1; |
| 700 | |
| 701 | debug("%s: done\n", __func__); |
| 702 | |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 703 | g_chip.inited = 1; |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | int tis_open(void) |
| 709 | { |
| 710 | int rc; |
| 711 | |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 712 | if (!g_chip.inited) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 713 | return -1; |
| 714 | |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 715 | rc = tpm_open_dev(g_chip.dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 716 | |
| 717 | return rc; |
| 718 | } |
| 719 | |
| 720 | int tis_close(void) |
| 721 | { |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 722 | if (!g_chip.inited) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 723 | return -1; |
| 724 | |
| 725 | tpm_close(); |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, |
| 731 | uint8_t *recvbuf, size_t *rbuf_len) |
| 732 | { |
| 733 | int len; |
| 734 | uint8_t buf[4096]; |
| 735 | |
Simon Glass | 605152a | 2015-08-22 18:31:26 -0600 | [diff] [blame] | 736 | if (!g_chip.inited) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 737 | return -1; |
| 738 | |
| 739 | if (sizeof(buf) < sbuf_size) |
| 740 | return -1; |
| 741 | |
| 742 | memcpy(buf, sendbuf, sbuf_size); |
| 743 | |
| 744 | len = tpm_transmit(buf, sbuf_size); |
| 745 | |
| 746 | if (len < 10) { |
| 747 | *rbuf_len = 0; |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | memcpy(recvbuf, buf, len); |
| 752 | *rbuf_len = len; |
| 753 | |
| 754 | return 0; |
| 755 | } |