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