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