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> |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 26 | #include <i2c.h> |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 27 | #include <tpm.h> |
Masahiro Yamada | 5d97dff | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 28 | #include <linux/errno.h> |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 29 | #include <linux/compiler.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 | |
Christophe Ricard | 1259dcd | 2016-01-21 23:27:12 +0100 | [diff] [blame] | 33 | #include "tpm_tis.h" |
Simon Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 34 | #include "tpm_internal.h" |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 35 | |
Christophe Ricard | 1259dcd | 2016-01-21 23:27:12 +0100 | [diff] [blame] | 36 | enum i2c_chip_type { |
| 37 | SLB9635, |
| 38 | SLB9645, |
| 39 | UNKNOWN, |
| 40 | }; |
| 41 | |
| 42 | /* expected value for DIDVID register */ |
| 43 | #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L |
| 44 | #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L |
| 45 | |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 46 | static const char * const chip_name[] = { |
| 47 | [SLB9635] = "slb9635tt", |
| 48 | [SLB9645] = "slb9645tt", |
| 49 | [UNKNOWN] = "unknown/fallback to slb9635", |
| 50 | }; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 51 | |
Christophe Ricard | 1259dcd | 2016-01-21 23:27:12 +0100 | [diff] [blame] | 52 | #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) |
| 53 | #define TPM_STS(l) (0x0001 | ((l) << 4)) |
| 54 | #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) |
| 55 | #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) |
| 56 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 57 | /* |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 58 | * tpm_tis_i2c_read() - read from TPM register |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 59 | * @addr: register address to read from |
| 60 | * @buffer: provided by caller |
| 61 | * @len: number of bytes to read |
| 62 | * |
| 63 | * Read len bytes from TPM register and put them into |
| 64 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 65 | * |
| 66 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 67 | * values have to be swapped. |
| 68 | * |
| 69 | * Return -EIO on error, 0 on success. |
| 70 | */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 71 | static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer, |
| 72 | size_t len) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 73 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 74 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 75 | int rc; |
| 76 | int count; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 77 | uint32_t addrbuf = addr; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 78 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 79 | if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) { |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 80 | /* slb9635 protocol should work in both cases */ |
| 81 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 82 | rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 83 | if (rc == 0) |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 84 | break; /* Success, break to skip sleep */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 85 | udelay(SLEEP_DURATION_US); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 86 | } |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 87 | if (rc) |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 88 | return rc; |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 89 | |
| 90 | /* After the TPM has successfully received the register address |
| 91 | * it needs some time, thus we're sleeping here again, before |
| 92 | * retrieving the data |
| 93 | */ |
| 94 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 95 | udelay(SLEEP_DURATION_US); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 96 | rc = dm_i2c_read(dev, 0, buffer, len); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 97 | if (rc == 0) |
| 98 | break; /* success, break to skip sleep */ |
| 99 | } |
| 100 | } else { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Use a combined read for newer chips. |
| 103 | * Unfortunately the smbus functions are not suitable due to |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 104 | * the 32 byte limit of the smbus. |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 105 | * Retries should usually not be needed, but are kept just to |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 106 | * be safe on the safe side. |
| 107 | */ |
| 108 | for (count = 0; count < MAX_COUNT; count++) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 109 | rc = dm_i2c_read(dev, addr, buffer, len); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 110 | if (rc == 0) |
| 111 | break; /* break here to skip sleep */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 112 | udelay(SLEEP_DURATION_US); |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 113 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 116 | /* Take care of 'guard time' */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 117 | udelay(SLEEP_DURATION_US); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 118 | if (rc) |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 119 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 124 | static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr, |
| 125 | const u8 *buffer, size_t len, |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 126 | unsigned int sleep_time_us, u8 max_count) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 127 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 128 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
| 129 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 130 | int rc = 0; |
| 131 | int count; |
| 132 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 133 | if (chip->chip_type == SLB9635) { |
| 134 | /* Prepare send buffer to include the address */ |
| 135 | priv->buf[0] = addr; |
| 136 | memcpy(&(priv->buf[1]), buffer, len); |
| 137 | buffer = priv->buf; |
| 138 | len++; |
| 139 | addr = 0; |
| 140 | } |
| 141 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 142 | for (count = 0; count < max_count; count++) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 143 | rc = dm_i2c_write(dev, addr, buffer, len); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 144 | if (rc == 0) |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 145 | break; /* Success, break to skip sleep */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 146 | udelay(sleep_time_us); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 149 | /* take care of 'guard time' */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 150 | udelay(sleep_time_us); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 151 | if (rc) |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 152 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 158 | * tpm_tis_i2c_write() - write to TPM register |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 159 | * @addr: register address to write to |
| 160 | * @buffer: containing data to be written |
| 161 | * @len: number of bytes to write |
| 162 | * |
| 163 | * Write len bytes from provided buffer to TPM register (little |
| 164 | * endian format, i.e. buffer[0] is written as first byte). |
| 165 | * |
| 166 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 167 | * values have to be swapped. |
| 168 | * |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 169 | * 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] | 170 | * |
| 171 | * Return -EIO on error, 0 on success |
| 172 | */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 173 | static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer, |
| 174 | size_t len) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 175 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 176 | return tpm_tis_i2c_write_generic(dev, addr, buffer, len, |
| 177 | SLEEP_DURATION_US, MAX_COUNT); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * This function is needed especially for the cleanup situation after |
| 182 | * sending TPM_READY |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 183 | */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 184 | static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer, |
| 185 | size_t len) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 186 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 187 | return tpm_tis_i2c_write_generic(dev, addr, buffer, len, |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 188 | SLEEP_DURATION_LONG_US, |
| 189 | MAX_COUNT_LONG); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 192 | static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 193 | { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 194 | const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 195 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 196 | u8 buf; |
| 197 | int rc; |
| 198 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 199 | rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 200 | if (rc < 0) |
| 201 | return rc; |
| 202 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 203 | if ((buf & mask) == mask) { |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 204 | chip->locality = loc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 205 | return loc; |
| 206 | } |
| 207 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 208 | return -ENOENT; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 211 | static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc, |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 212 | int force) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 213 | { |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 214 | const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 215 | u8 buf; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 216 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 217 | if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 218 | return; |
| 219 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 220 | if (force || (buf & mask) == mask) { |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 221 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 222 | tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 226 | static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 227 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 228 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 229 | unsigned long start, stop; |
| 230 | u8 buf = TPM_ACCESS_REQUEST_USE; |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 231 | int rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 232 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 233 | rc = tpm_tis_i2c_check_locality(dev, loc); |
| 234 | if (rc >= 0) { |
| 235 | debug("%s: Already have locality\n", __func__); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 236 | return loc; /* We already have the locality */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 237 | } else if (rc != -ENOENT) { |
| 238 | debug("%s: Failed to get locality: %d\n", __func__, rc); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 239 | return rc; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1); |
| 243 | if (rc) { |
| 244 | debug("%s: Failed to write to TPM: %d\n", __func__, rc); |
| 245 | return rc; |
| 246 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 247 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 248 | /* Wait for burstcount */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 249 | start = get_timer(0); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 250 | stop = chip->timeout_a; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 251 | do { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 252 | rc = tpm_tis_i2c_check_locality(dev, loc); |
| 253 | if (rc >= 0) { |
| 254 | debug("%s: Have locality\n", __func__); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 255 | return loc; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 256 | } else if (rc != -ENOENT) { |
| 257 | debug("%s: Failed to get locality: %d\n", __func__, rc); |
| 258 | return rc; |
| 259 | } |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 260 | mdelay(TPM_TIMEOUT_MS); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 261 | } while (get_timer(start) < stop); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 262 | debug("%s: Timeout getting locality: %d\n", __func__, rc); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 263 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 264 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 267 | static u8 tpm_tis_i2c_status(struct udevice *dev) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 268 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 269 | struct tpm_chip *chip = dev_get_priv(dev); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 270 | /* 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] | 271 | u8 buf; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 272 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 273 | if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | else |
| 276 | return buf; |
| 277 | } |
| 278 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 279 | static int tpm_tis_i2c_ready(struct udevice *dev) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 280 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 281 | struct tpm_chip *chip = dev_get_priv(dev); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 282 | int rc; |
| 283 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 284 | /* This causes the current command to be aborted */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 285 | u8 buf = TPM_STS_COMMAND_READY; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 286 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 287 | debug("%s\n", __func__); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 288 | rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 289 | if (rc) |
| 290 | debug("%s: rc=%d\n", __func__, rc); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 291 | |
| 292 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 295 | static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 296 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 297 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 298 | unsigned long start, stop; |
| 299 | ssize_t burstcnt; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 300 | u8 addr, buf[3]; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 301 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 302 | /* Wait for burstcount */ |
| 303 | /* XXX: Which timeout value? Spec has 2 answers (c & d) */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 304 | start = get_timer(0); |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 305 | stop = chip->timeout_d; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 306 | do { |
| 307 | /* Note: STS is little endian */ |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 308 | addr = TPM_STS(chip->locality) + 1; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 309 | if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 310 | burstcnt = 0; |
| 311 | else |
| 312 | burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 313 | |
| 314 | if (burstcnt) |
| 315 | return burstcnt; |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 316 | mdelay(TPM_TIMEOUT_MS); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 317 | } while (get_timer(start) < stop); |
| 318 | |
| 319 | return -EBUSY; |
| 320 | } |
| 321 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 322 | static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask, |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 323 | unsigned long timeout, int *status) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 324 | { |
| 325 | unsigned long start, stop; |
| 326 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 327 | /* Check current status */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 328 | *status = tpm_tis_i2c_status(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 329 | if ((*status & mask) == mask) |
| 330 | return 0; |
| 331 | |
| 332 | start = get_timer(0); |
| 333 | stop = timeout; |
| 334 | do { |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 335 | mdelay(TPM_TIMEOUT_MS); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 336 | *status = tpm_tis_i2c_status(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 337 | if ((*status & mask) == mask) |
| 338 | return 0; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 339 | } while (get_timer(start) < stop); |
| 340 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 341 | return -ETIMEDOUT; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 344 | static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 345 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 346 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 347 | size_t size = 0; |
| 348 | ssize_t burstcnt; |
| 349 | int rc; |
| 350 | |
| 351 | while (size < count) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 352 | burstcnt = tpm_tis_i2c_get_burstcount(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 353 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 354 | /* burstcount < 0 -> tpm is busy */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 355 | if (burstcnt < 0) |
| 356 | return burstcnt; |
| 357 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 358 | /* Limit received data to max left */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 359 | if (burstcnt > (count - size)) |
| 360 | burstcnt = count - size; |
| 361 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 362 | rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality), |
| 363 | &(buf[size]), burstcnt); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 364 | if (rc == 0) |
| 365 | size += burstcnt; |
| 366 | } |
| 367 | |
| 368 | return size; |
| 369 | } |
| 370 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 371 | static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 372 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 373 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 374 | int size = 0; |
Jeremy Boone | afe0e6b | 2018-02-12 17:56:36 -0500 | [diff] [blame] | 375 | int status; |
| 376 | unsigned int expected; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 377 | int rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 378 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 379 | status = tpm_tis_i2c_status(dev); |
| 380 | if (status == TPM_STS_COMMAND_READY) |
| 381 | return -EINTR; |
| 382 | if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) != |
| 383 | (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) |
| 384 | return -EAGAIN; |
| 385 | |
| 386 | debug("...got it;\n"); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 387 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 388 | /* Read first 10 bytes, including tag, paramsize, and result */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 389 | size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 390 | if (size < TPM_HEADER_SIZE) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 391 | debug("Unable to read header\n"); |
| 392 | return size < 0 ? size : -EIO; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE); |
Jeremy Boone | afe0e6b | 2018-02-12 17:56:36 -0500 | [diff] [blame] | 396 | if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 397 | debug("Error size=%x, expected=%x, count=%x\n", size, expected, |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 398 | count); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 399 | return -ENOSPC; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 402 | size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE], |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 403 | expected - TPM_HEADER_SIZE); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 404 | if (size < expected) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 405 | debug("Unable to read remainder of result\n"); |
| 406 | return -ETIMEDOUT; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 409 | rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c, |
| 410 | &status); |
| 411 | if (rc) |
| 412 | return rc; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 413 | if (status & TPM_STS_DATA_AVAIL) { /* Retry? */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 414 | debug("Error left over data\n"); |
| 415 | return -EIO; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 418 | return size; |
| 419 | } |
| 420 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 421 | static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 422 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 423 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 424 | int rc, status; |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 425 | size_t burstcnt; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 426 | size_t count = 0; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 427 | int retry = 0; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 428 | u8 sts = TPM_STS_GO; |
| 429 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 430 | debug("%s: len=%d\n", __func__, len); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 431 | if (len > TPM_DEV_BUFSIZE) |
| 432 | return -E2BIG; /* Command is too long for our tpm, sorry */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 433 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 434 | if (tpm_tis_i2c_request_locality(dev, 0) < 0) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 435 | return -EBUSY; |
| 436 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 437 | status = tpm_tis_i2c_status(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 438 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 439 | rc = tpm_tis_i2c_ready(dev); |
| 440 | if (rc) |
| 441 | return rc; |
| 442 | rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY, |
| 443 | chip->timeout_b, &status); |
| 444 | if (rc) |
| 445 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 448 | burstcnt = tpm_tis_i2c_get_burstcount(dev); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 449 | |
| 450 | /* burstcount < 0 -> tpm is busy */ |
| 451 | if (burstcnt < 0) |
| 452 | return burstcnt; |
| 453 | |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 454 | while (count < len) { |
| 455 | udelay(300); |
| 456 | if (burstcnt > len - count) |
| 457 | burstcnt = len - count; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 458 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 459 | #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 460 | if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN) |
| 461 | burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 462 | #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 463 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 464 | rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality), |
| 465 | &(buf[count]), burstcnt); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 466 | if (rc == 0) |
| 467 | count += burstcnt; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 468 | else { |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 469 | debug("%s: error\n", __func__); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 470 | if (retry++ > 10) |
| 471 | return -EIO; |
| 472 | rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 473 | chip->timeout_c, |
| 474 | &status); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 475 | if (rc) |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 476 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 477 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 478 | if ((status & TPM_STS_DATA_EXPECT) == 0) |
| 479 | return -EIO; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 483 | /* Go and do it */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 484 | rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1); |
| 485 | if (rc < 0) |
| 486 | return rc; |
| 487 | debug("%s: done, rc=%d\n", __func__, rc); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 488 | |
| 489 | return len; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 490 | } |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 491 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 492 | static int tpm_tis_i2c_cleanup(struct udevice *dev) |
| 493 | { |
| 494 | struct tpm_chip *chip = dev_get_priv(dev); |
| 495 | |
| 496 | tpm_tis_i2c_ready(dev); |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 497 | /* |
| 498 | * The TPM needs some time to clean up here, |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 499 | * so we sleep rather than keeping the bus busy |
| 500 | */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 501 | mdelay(2); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 502 | tpm_tis_i2c_release_locality(dev, chip->locality, 0); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 503 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 504 | return 0; |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Simon Glass | 13932b0 | 2015-08-22 18:31:25 -0600 | [diff] [blame] | 507 | static int tpm_tis_i2c_init(struct udevice *dev) |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 508 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 509 | struct tpm_chip *chip = dev_get_priv(dev); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 510 | u32 vendor; |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 511 | u32 expected_did_vid; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 512 | int rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 513 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 514 | chip->is_open = 1; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 515 | |
Simon Glass | e56e20e | 2015-08-22 18:31:29 -0600 | [diff] [blame] | 516 | /* Default timeouts - these could move to the device tree */ |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 517 | chip->timeout_a = TIS_SHORT_TIMEOUT_MS; |
| 518 | chip->timeout_b = TIS_LONG_TIMEOUT_MS; |
| 519 | chip->timeout_c = TIS_SHORT_TIMEOUT_MS; |
| 520 | chip->timeout_d = TIS_SHORT_TIMEOUT_MS; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 521 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 522 | rc = tpm_tis_i2c_request_locality(dev, 0); |
| 523 | if (rc < 0) |
| 524 | return rc; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 525 | |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 526 | /* Read four bytes from DID_VID register */ |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 527 | if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) { |
| 528 | tpm_tis_i2c_release_locality(dev, 0, 1); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 529 | return -EIO; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 532 | if (chip->chip_type == SLB9635) { |
Vincent Palatin | ec34fa5 | 2013-04-12 11:04:36 +0000 | [diff] [blame] | 533 | vendor = be32_to_cpu(vendor); |
| 534 | expected_did_vid = TPM_TIS_I2C_DID_VID_9635; |
| 535 | } else { |
| 536 | /* device id and byte order has changed for newer i2c tpms */ |
| 537 | expected_did_vid = TPM_TIS_I2C_DID_VID_9645; |
| 538 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 539 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 540 | if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 541 | pr_err("Vendor id did not match! ID was %08x\n", vendor); |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 542 | return -ENODEV; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 545 | chip->vend_dev = vendor; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 546 | debug("1.2 TPM (chip type %s device-id 0x%X)\n", |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 547 | chip_name[chip->chip_type], vendor >> 16); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * A timeout query to TPM can be placed here. |
| 551 | * Standard timeout values are used so far |
| 552 | */ |
| 553 | |
| 554 | return 0; |
Simon Glass | f90acf1 | 2015-05-04 11:30:59 -0600 | [diff] [blame] | 555 | } |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 556 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 557 | static int tpm_tis_i2c_open(struct udevice *dev) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 558 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 559 | struct tpm_chip *chip = dev_get_priv(dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 560 | int rc; |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 561 | |
Simon Glass | a53b79a | 2015-08-22 18:31:28 -0600 | [diff] [blame] | 562 | debug("%s: start\n", __func__); |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 563 | if (chip->is_open) |
Simon Glass | a53b79a | 2015-08-22 18:31:28 -0600 | [diff] [blame] | 564 | return -EBUSY; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 565 | rc = tpm_tis_i2c_init(dev); |
Simon Glass | a53b79a | 2015-08-22 18:31:28 -0600 | [diff] [blame] | 566 | if (rc < 0) |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 567 | chip->is_open = 0; |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 568 | |
| 569 | return rc; |
| 570 | } |
| 571 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 572 | static int tpm_tis_i2c_close(struct udevice *dev) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 573 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 574 | struct tpm_chip *chip = dev_get_priv(dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 575 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 576 | if (chip->is_open) { |
| 577 | tpm_tis_i2c_release_locality(dev, chip->locality, 1); |
| 578 | chip->is_open = 0; |
| 579 | chip->vend_dev = 0; |
Simon Glass | a53b79a | 2015-08-22 18:31:28 -0600 | [diff] [blame] | 580 | } |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 585 | static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size) |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 586 | { |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 587 | struct tpm_chip *chip = dev_get_priv(dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 588 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 589 | if (size < 50) |
| 590 | return -ENOSPC; |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 591 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 592 | return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)", |
| 593 | chip->is_open ? "open" : "closed", |
| 594 | chip_name[chip->chip_type], |
| 595 | chip->vend_dev >> 16); |
| 596 | } |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 597 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 598 | static int tpm_tis_i2c_probe(struct udevice *dev) |
| 599 | { |
| 600 | struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev); |
| 601 | struct tpm_chip *chip = dev_get_priv(dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 602 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 603 | chip->chip_type = dev_get_driver_data(dev); |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 604 | |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 605 | /* TODO: These need to be checked and tuned */ |
| 606 | uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS; |
| 607 | uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS; |
| 608 | uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS; |
| 609 | uc_priv->retry_time_ms = TPM_TIMEOUT_MS; |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 610 | |
| 611 | return 0; |
| 612 | } |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 613 | |
| 614 | static const struct tpm_ops tpm_tis_i2c_ops = { |
| 615 | .open = tpm_tis_i2c_open, |
| 616 | .close = tpm_tis_i2c_close, |
| 617 | .get_desc = tpm_tis_get_desc, |
| 618 | .send = tpm_tis_i2c_send, |
| 619 | .recv = tpm_tis_i2c_recv, |
| 620 | .cleanup = tpm_tis_i2c_cleanup, |
| 621 | }; |
| 622 | |
| 623 | static const struct udevice_id tpm_tis_i2c_ids[] = { |
| 624 | { .compatible = "infineon,slb9635tt", .data = SLB9635 }, |
| 625 | { .compatible = "infineon,slb9645tt", .data = SLB9645 }, |
| 626 | { } |
| 627 | }; |
| 628 | |
| 629 | U_BOOT_DRIVER(tpm_tis_i2c) = { |
Christophe Ricard | 0766ad2 | 2015-10-06 22:54:41 +0200 | [diff] [blame] | 630 | .name = "tpm_tis_infineon", |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 631 | .id = UCLASS_TPM, |
| 632 | .of_match = tpm_tis_i2c_ids, |
| 633 | .ops = &tpm_tis_i2c_ops, |
| 634 | .probe = tpm_tis_i2c_probe, |
| 635 | .priv_auto_alloc_size = sizeof(struct tpm_chip), |
| 636 | }; |