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