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