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