Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [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 | * |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 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 | * |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 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 | * |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 20 | * This program is free software; you can redistribute it and/or |
| 21 | * modify it under the terms of the GNU General Public License as |
| 22 | * published by the Free Software Foundation, version 2 of the |
| 23 | * License. |
| 24 | * |
| 25 | * This program is distributed in the hope that it will be useful, |
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 | * GNU General Public License for more details. |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 29 | */ |
| 30 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 31 | #include <arch/early_variables.h> |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 32 | #include <commonlib/endian.h> |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | #include <string.h> |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 35 | #include <types.h> |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 36 | #include <delay.h> |
| 37 | #include <console/console.h> |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 38 | #include <device/i2c.h> |
Julius Werner | 9ff8f6f | 2015-02-23 14:31:09 -0800 | [diff] [blame] | 39 | #include <endian.h> |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 40 | #include <timer.h> |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 41 | #include "tpm.h" |
| 42 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 43 | /* max. number of iterations after I2C NAK */ |
| 44 | #define MAX_COUNT 3 |
| 45 | |
| 46 | #define SLEEP_DURATION 60 /* in usec */ |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 47 | #define SLEEP_DURATION_LONG 210 /* in usec */ |
Duncan Laurie | 1ca1968 | 2016-09-07 10:52:12 -0700 | [diff] [blame] | 48 | #define SLEEP_DURATION_PROBE_MS 1000 /* in msec */ |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 49 | |
| 50 | /* max. number of iterations after I2C NAK for 'long' commands |
| 51 | * we need this especially for sending TPM_READY, since the cleanup after the |
| 52 | * transtion to the ready state may take some time, but it is unpredictable |
| 53 | * how long it will take. |
| 54 | */ |
| 55 | #define MAX_COUNT_LONG 50 |
| 56 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 57 | /* expected value for DIDVID register */ |
| 58 | #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L |
| 59 | #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L |
| 60 | |
| 61 | enum i2c_chip_type { |
| 62 | SLB9635, |
| 63 | SLB9645, |
| 64 | UNKNOWN, |
| 65 | }; |
| 66 | |
| 67 | static const char * const chip_name[] = { |
| 68 | [SLB9635] = "slb9635tt", |
| 69 | [SLB9645] = "slb9645tt", |
| 70 | [UNKNOWN] = "unknown/fallback to slb9635", |
| 71 | }; |
| 72 | |
| 73 | /* Structure to store I2C TPM specific stuff */ |
| 74 | struct tpm_inf_dev { |
Daisuke Nojiri | 1fd91a1 | 2014-10-29 11:14:53 -0700 | [diff] [blame] | 75 | int bus; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 76 | unsigned int addr; |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 77 | unsigned int sleep_short; /* Short sleep duration in usec */ |
| 78 | unsigned int sleep_long; /* Long sleep duration in usec */ |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 79 | uint8_t buf[TPM_BUFSIZE + sizeof(uint8_t)]; // max. buffer size + addr |
| 80 | enum i2c_chip_type chip_type; |
| 81 | }; |
| 82 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 83 | static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * iic_tpm_read() - read from TPM register |
| 87 | * @addr: register address to read from |
| 88 | * @buffer: provided by caller |
| 89 | * @len: number of bytes to read |
| 90 | * |
| 91 | * Read len bytes from TPM register and put them into |
| 92 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). |
| 93 | * |
| 94 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 95 | * values have to be swapped. |
| 96 | * |
| 97 | * Return -1 on error, 0 on success. |
| 98 | */ |
| 99 | static int iic_tpm_read(uint8_t addr, uint8_t *buffer, size_t len) |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 100 | { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 101 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 102 | int rc; |
| 103 | int count; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 104 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 105 | if (tpm_dev->addr == 0) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 106 | return -1; |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 107 | |
| 108 | switch (tpm_dev->chip_type) { |
| 109 | case SLB9635: |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 110 | case UNKNOWN: |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 111 | /* slb9635 protocol should work in both cases */ |
| 112 | for (count = 0; count < MAX_COUNT; count++) { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 113 | rc = i2c_write_raw(tpm_dev->bus, tpm_dev->addr, |
| 114 | &addr, 1); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 115 | if (rc == 0) |
| 116 | break; /* success, break to skip sleep */ |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 117 | |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 118 | udelay(tpm_dev->sleep_short); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 119 | } |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 120 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 121 | if (rc) |
| 122 | return -1; |
| 123 | |
| 124 | /* After the TPM has successfully received the register address |
| 125 | * it needs some time, thus we're sleeping here again, before |
| 126 | * retrieving the data |
| 127 | */ |
| 128 | for (count = 0; count < MAX_COUNT; count++) { |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 129 | udelay(tpm_dev->sleep_short); |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 130 | rc = i2c_read_raw(tpm_dev->bus, tpm_dev->addr, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 131 | buffer, len); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 132 | if (rc == 0) |
| 133 | break; /* success, break to skip sleep */ |
| 134 | |
| 135 | } |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 136 | break; |
| 137 | |
| 138 | default: |
| 139 | { |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 140 | /* use a combined read for newer chips |
| 141 | * unfortunately the smbus functions are not suitable due to |
| 142 | * the 32 byte limit of the smbus. |
| 143 | * retries should usually not be needed, but are kept just to |
| 144 | * be safe on the safe side. |
| 145 | */ |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 146 | struct i2c_seg aseg = { .read = 0, .chip = tpm_dev->addr, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 147 | .buf = &addr, .len = 1 }; |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 148 | struct i2c_seg dseg = { .read = 1, .chip = tpm_dev->addr, |
Gabe Black | cdb61a6 | 2014-04-07 18:45:14 -0700 | [diff] [blame] | 149 | .buf = buffer, .len = len }; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 150 | for (count = 0; count < MAX_COUNT; count++) { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 151 | rc = i2c_transfer(tpm_dev->bus, &aseg, 1) || |
| 152 | i2c_transfer(tpm_dev->bus, &dseg, 1); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 153 | if (rc == 0) |
| 154 | break; /* break here to skip sleep */ |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 155 | udelay(tpm_dev->sleep_short); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 156 | } |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 157 | } |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 158 | } |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 159 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 160 | /* take care of 'guard time' */ |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 161 | udelay(tpm_dev->sleep_short); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 162 | if (rc) |
| 163 | return -1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int iic_tpm_write_generic(uint8_t addr, uint8_t *buffer, size_t len, |
| 169 | unsigned int sleep_time, |
| 170 | uint8_t max_count) |
| 171 | { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 172 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 173 | int rc = 0; |
| 174 | int count; |
| 175 | |
| 176 | if (len > TPM_BUFSIZE) { |
Furquan Shaikh | 251eef1 | 2014-07-22 11:12:15 -0700 | [diff] [blame] | 177 | printk(BIOS_DEBUG, "%s: Length %zd is too large\n", __func__, len); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 178 | return -1; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 181 | /* prepare send buffer */ |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 182 | tpm_dev->buf[0] = addr; |
| 183 | memcpy(&(tpm_dev->buf[1]), buffer, len); |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 184 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 185 | if (tpm_dev->addr == 0) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 186 | return -1; |
| 187 | for (count = 0; count < max_count; count++) { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 188 | rc = i2c_write_raw(tpm_dev->bus, tpm_dev->addr, |
| 189 | tpm_dev->buf, len + 1); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 190 | if (rc == 0) |
| 191 | break; /* success, break to skip sleep */ |
| 192 | |
| 193 | udelay(sleep_time); |
| 194 | } |
| 195 | |
| 196 | /* take care of 'guard time' */ |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 197 | udelay(tpm_dev->sleep_short); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 198 | if (rc) |
| 199 | return -1; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * iic_tpm_write() - write to TPM register |
| 206 | * @addr: register address to write to |
| 207 | * @buffer: containing data to be written |
| 208 | * @len: number of bytes to write |
| 209 | * |
| 210 | * Write len bytes from provided buffer to TPM register (little |
| 211 | * endian format, i.e. buffer[0] is written as first byte). |
| 212 | * |
| 213 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte |
| 214 | * values have to be swapped. |
| 215 | * |
| 216 | * NOTE: use this function instead of the iic_tpm_write_generic function. |
| 217 | * |
| 218 | * Return -EIO on error, 0 on success |
| 219 | */ |
| 220 | static int iic_tpm_write(uint8_t addr, uint8_t *buffer, size_t len) |
| 221 | { |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 222 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 223 | return iic_tpm_write_generic(addr, buffer, len, tpm_dev->sleep_short, |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 224 | MAX_COUNT); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * This function is needed especially for the cleanup situation after |
| 229 | * sending TPM_READY |
| 230 | * */ |
| 231 | static int iic_tpm_write_long(uint8_t addr, uint8_t *buffer, size_t len) |
| 232 | { |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 233 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 234 | return iic_tpm_write_generic(addr, buffer, len, tpm_dev->sleep_long, |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 235 | MAX_COUNT_LONG); |
| 236 | } |
| 237 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 238 | static int check_locality(struct tpm_chip *chip, int loc) |
| 239 | { |
| 240 | uint8_t buf; |
| 241 | |
| 242 | if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) |
| 243 | return -1; |
| 244 | |
| 245 | if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == |
| 246 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { |
| 247 | chip->vendor.locality = loc; |
| 248 | return loc; |
| 249 | } |
| 250 | |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | static void release_locality(struct tpm_chip *chip, int loc, int force) |
| 255 | { |
| 256 | uint8_t buf; |
| 257 | if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) |
| 258 | return; |
| 259 | |
| 260 | if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == |
| 261 | (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { |
| 262 | buf = TPM_ACCESS_ACTIVE_LOCALITY; |
| 263 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static int request_locality(struct tpm_chip *chip, int loc) |
| 268 | { |
| 269 | uint8_t buf = TPM_ACCESS_REQUEST_USE; |
| 270 | |
| 271 | if (check_locality(chip, loc) >= 0) |
| 272 | return loc; /* we already have the locality */ |
| 273 | |
| 274 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); |
| 275 | |
| 276 | /* wait for burstcount */ |
| 277 | int timeout = 2 * 1000; /* 2s timeout */ |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 278 | while (timeout) { |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 279 | if (check_locality(chip, loc) >= 0) |
| 280 | return loc; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 281 | mdelay(TPM_TIMEOUT); |
| 282 | timeout--; |
| 283 | } |
| 284 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 285 | return -1; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 288 | static uint8_t tpm_tis_i2c_status(struct tpm_chip *chip) |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 289 | { |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 290 | /* NOTE: Since I2C read may fail, return 0 in this case --> time-out */ |
| 291 | uint8_t buf; |
| 292 | if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0) |
| 293 | return 0; |
Julius Werner | a69ac78 | 2016-11-30 17:46:17 -0800 | [diff] [blame] | 294 | else if (buf == 0xff) /* Some TPMs sometimes randomly return 0xff. */ |
| 295 | return 0; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 296 | else |
| 297 | return buf; |
| 298 | } |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 299 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 300 | static void tpm_tis_i2c_ready(struct tpm_chip *chip) |
| 301 | { |
| 302 | /* this causes the current command to be aborted */ |
| 303 | uint8_t buf = TPM_STS_COMMAND_READY; |
| 304 | iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1); |
| 305 | } |
| 306 | |
| 307 | static ssize_t get_burstcount(struct tpm_chip *chip) |
| 308 | { |
| 309 | ssize_t burstcnt; |
| 310 | uint8_t buf[3]; |
| 311 | |
| 312 | /* wait for burstcount */ |
| 313 | int timeout = 2 * 1000; /* 2s timeout */ |
| 314 | while (timeout) { |
| 315 | /* Note: STS is little endian */ |
| 316 | if (iic_tpm_read(TPM_STS(chip->vendor.locality) + 1, buf, 3) < 0) |
| 317 | burstcnt = 0; |
| 318 | else |
| 319 | burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; |
| 320 | |
Julius Werner | a69ac78 | 2016-11-30 17:46:17 -0800 | [diff] [blame] | 321 | if (burstcnt && burstcnt != 0xffffff) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 322 | return burstcnt; |
| 323 | mdelay(TPM_TIMEOUT); |
| 324 | timeout--; |
| 325 | } |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | static int wait_for_stat(struct tpm_chip *chip, uint8_t mask, int *status) |
| 330 | { |
| 331 | unsigned long timeout = 2 * 1024; |
| 332 | while (timeout) { |
| 333 | *status = tpm_tis_i2c_status(chip); |
| 334 | if ((*status & mask) == mask) |
| 335 | return 0; |
| 336 | mdelay(TPM_TIMEOUT); |
| 337 | timeout--; |
| 338 | } |
| 339 | |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | static int recv_data(struct tpm_chip *chip, uint8_t *buf, size_t count) |
| 344 | { |
| 345 | size_t size = 0; |
| 346 | |
| 347 | while (size < count) { |
| 348 | ssize_t burstcnt = get_burstcount(chip); |
| 349 | int rc; |
| 350 | |
| 351 | /* burstcount < 0 = TPM is busy */ |
| 352 | if (burstcnt < 0) |
| 353 | return burstcnt; |
| 354 | |
| 355 | /* limit received data to max. left */ |
| 356 | if (burstcnt > (count - size)) |
| 357 | burstcnt = count - size; |
| 358 | |
| 359 | rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality), |
| 360 | &(buf[size]), |
| 361 | burstcnt); |
| 362 | if (rc == 0) |
| 363 | size += burstcnt; |
| 364 | |
| 365 | } |
| 366 | return size; |
| 367 | } |
| 368 | |
| 369 | static int tpm_tis_i2c_recv(struct tpm_chip *chip, uint8_t *buf, size_t count) |
| 370 | { |
| 371 | int size = 0; |
| 372 | uint32_t expected; |
| 373 | int status; |
| 374 | |
| 375 | if (count < TPM_HEADER_SIZE) { |
| 376 | size = -1; |
| 377 | goto out; |
| 378 | } |
| 379 | |
| 380 | /* read first 10 bytes, including tag, paramsize, and result */ |
| 381 | size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 382 | if (size < TPM_HEADER_SIZE) { |
| 383 | printk(BIOS_DEBUG, "tpm_tis_i2c_recv: Unable to read header\n"); |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | memcpy(&expected, buf + TPM_RSP_SIZE_BYTE, sizeof(expected)); |
| 388 | expected = be32_to_cpu(expected); |
| 389 | if ((size_t)expected > count) { |
| 390 | size = -1; |
| 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
| 395 | expected - TPM_HEADER_SIZE); |
| 396 | if (size < expected) { |
| 397 | printk(BIOS_DEBUG, "tpm_tis_i2c_recv: Unable to " |
| 398 | "read remainder of result\n"); |
| 399 | size = -1; |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | wait_for_stat(chip, TPM_STS_VALID, &status); |
| 404 | if (status & TPM_STS_DATA_AVAIL) { /* retry? */ |
| 405 | printk(BIOS_DEBUG, "tpm_tis_i2c_recv: Error left over data\n"); |
| 406 | size = -1; |
| 407 | goto out; |
| 408 | } |
| 409 | |
| 410 | out: |
| 411 | tpm_tis_i2c_ready(chip); |
| 412 | |
| 413 | return size; |
| 414 | } |
| 415 | |
| 416 | static int tpm_tis_i2c_send(struct tpm_chip *chip, uint8_t *buf, size_t len) |
| 417 | { |
| 418 | int status; |
| 419 | size_t count = 0; |
| 420 | uint8_t sts = TPM_STS_GO; |
| 421 | |
| 422 | if (len > TPM_BUFSIZE) |
| 423 | return -1; /* command is too long for our TPM, sorry */ |
| 424 | |
| 425 | status = tpm_tis_i2c_status(chip); |
| 426 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 427 | tpm_tis_i2c_ready(chip); |
| 428 | if (wait_for_stat(chip, TPM_STS_COMMAND_READY, &status) < 0) |
| 429 | goto out_err; |
| 430 | } |
| 431 | |
| 432 | while (count < len - 1) { |
| 433 | ssize_t burstcnt = get_burstcount(chip); |
| 434 | |
| 435 | /* burstcount < 0 = TPM is busy */ |
| 436 | if (burstcnt < 0) |
| 437 | return burstcnt; |
| 438 | |
| 439 | if (burstcnt > (len-1-count)) |
| 440 | burstcnt = len-1-count; |
| 441 | |
| 442 | #ifdef CONFIG_TPM_I2C_BURST_LIMITATION |
| 443 | if (burstcnt > CONFIG_TPM_I2C_BURST_LIMITATION) |
| 444 | burstcnt = CONFIG_TPM_I2C_BURST_LIMITATION; |
| 445 | #endif /* CONFIG_TPM_I2C_BURST_LIMITATION */ |
| 446 | |
| 447 | if (iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), |
| 448 | &(buf[count]), burstcnt) == 0) |
| 449 | count += burstcnt; |
| 450 | |
| 451 | wait_for_stat(chip, TPM_STS_VALID, &status); |
| 452 | if ((status & TPM_STS_DATA_EXPECT) == 0) |
| 453 | goto out_err; |
| 454 | } |
| 455 | |
| 456 | /* write last byte */ |
| 457 | iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1); |
| 458 | |
| 459 | wait_for_stat(chip, TPM_STS_VALID, &status); |
| 460 | if ((status & TPM_STS_DATA_EXPECT) != 0) |
| 461 | goto out_err; |
| 462 | |
| 463 | /* go and do it */ |
| 464 | iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1); |
| 465 | |
| 466 | return len; |
| 467 | |
| 468 | out_err: |
| 469 | tpm_tis_i2c_ready(chip); |
| 470 | |
| 471 | return -1; |
| 472 | } |
| 473 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 474 | /* Initialization of I2C TPM */ |
| 475 | |
Duncan Laurie | 1ca1968 | 2016-09-07 10:52:12 -0700 | [diff] [blame] | 476 | int tpm_vendor_probe(unsigned bus, uint32_t addr) |
| 477 | { |
| 478 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
| 479 | struct stopwatch sw; |
| 480 | uint8_t buf = 0; |
| 481 | int ret; |
| 482 | long sw_run_duration = SLEEP_DURATION_PROBE_MS; |
| 483 | |
| 484 | tpm_dev->chip_type = UNKNOWN; |
| 485 | tpm_dev->bus = bus; |
| 486 | tpm_dev->addr = addr; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 487 | tpm_dev->sleep_short = SLEEP_DURATION; |
| 488 | tpm_dev->sleep_long = SLEEP_DURATION_LONG; |
Duncan Laurie | 1ca1968 | 2016-09-07 10:52:12 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Probe TPM. Check if the TPM_ACCESS register's ValidSts bit is set(1) |
| 492 | * If the bit remains clear(0) then claim that init has failed. |
| 493 | */ |
| 494 | stopwatch_init_msecs_expire(&sw, sw_run_duration); |
| 495 | do { |
| 496 | ret = iic_tpm_read(TPM_ACCESS(0), &buf, 1); |
| 497 | if (!ret && (buf & TPM_STS_VALID)) { |
| 498 | sw_run_duration = stopwatch_duration_msecs(&sw); |
| 499 | break; |
| 500 | } |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 501 | udelay(SLEEP_DURATION); |
Duncan Laurie | 1ca1968 | 2016-09-07 10:52:12 -0700 | [diff] [blame] | 502 | } while (!stopwatch_expired(&sw)); |
| 503 | |
| 504 | printk(BIOS_INFO, |
| 505 | "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n", |
| 506 | __func__, (buf & TPM_STS_VALID) ? "set" : "clear", |
| 507 | (buf & TPM_STS_VALID) >> 7, sw_run_duration); |
| 508 | |
| 509 | /* |
| 510 | * Claim failure if the ValidSts (bit 7) is clear. |
| 511 | */ |
| 512 | if (!(buf & TPM_STS_VALID)) |
| 513 | return -1; |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 518 | int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr) |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 519 | { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 520 | struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 521 | uint32_t vendor; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 522 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 523 | if (dev_addr == 0) { |
| 524 | printk(BIOS_ERR, "%s: missing device address\n", __func__); |
| 525 | return -1; |
| 526 | } |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 527 | |
Duncan Laurie | fbce31a | 2016-09-12 11:26:45 -0700 | [diff] [blame] | 528 | tpm_dev->chip_type = UNKNOWN; |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 529 | tpm_dev->bus = bus; |
| 530 | tpm_dev->addr = dev_addr; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 531 | tpm_dev->sleep_short = SLEEP_DURATION; |
| 532 | tpm_dev->sleep_long = SLEEP_DURATION_LONG; |
Duncan Laurie | efa579f | 2016-08-31 14:48:12 -0700 | [diff] [blame] | 533 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 534 | memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific)); |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 535 | chip->is_open = 1; |
| 536 | |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 537 | chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 538 | chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID; |
| 539 | chip->vendor.req_canceled = TPM_STS_COMMAND_READY; |
Duncan Laurie | 2ea13c8 | 2016-09-19 16:04:39 -0700 | [diff] [blame] | 540 | chip->vendor.status = &tpm_tis_i2c_status; |
| 541 | chip->vendor.recv = &tpm_tis_i2c_recv; |
| 542 | chip->vendor.send = &tpm_tis_i2c_send; |
| 543 | chip->vendor.cancel = &tpm_tis_i2c_ready; |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 544 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 545 | /* Disable interrupts (not supported) */ |
| 546 | chip->vendor.irq = 0; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 547 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 548 | if (request_locality(chip, 0) != 0) |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 549 | return -1; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 550 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 551 | /* Read four bytes from DID_VID register */ |
| 552 | if (iic_tpm_read(TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0) |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 553 | goto out_err; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 554 | |
| 555 | if (vendor == TPM_TIS_I2C_DID_VID_9645) { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 556 | tpm_dev->chip_type = SLB9645; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 557 | } else if (be32_to_cpu(vendor) == TPM_TIS_I2C_DID_VID_9635) { |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 558 | tpm_dev->chip_type = SLB9635; |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 559 | } else { |
| 560 | printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized.\n", vendor); |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 561 | goto out_err; |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 562 | } |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 563 | |
Duncan Laurie | ffa765f | 2016-09-01 15:50:22 -0700 | [diff] [blame] | 564 | printk(BIOS_DEBUG, "I2C TPM %u:%02x (chip type %s device-id 0x%X)\n", |
| 565 | tpm_dev->bus, tpm_dev->addr, |
| 566 | chip_name[tpm_dev->chip_type], vendor >> 16); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * A timeout query to TPM can be placed here. |
| 570 | * Standard timeout values are used so far |
| 571 | */ |
| 572 | |
| 573 | return 0; |
| 574 | |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 575 | out_err: |
Duncan Laurie | 40ae170 | 2016-08-31 13:51:14 -0700 | [diff] [blame] | 576 | release_locality(chip, 0, 1); |
Stefan Reinauer | d518c7a | 2013-11-04 17:38:32 -0800 | [diff] [blame] | 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | void tpm_vendor_cleanup(struct tpm_chip *chip) |
| 581 | { |
| 582 | release_locality(chip, chip->vendor.locality, 1); |
Stefan Reinauer | 7cb01e0 | 2013-08-29 16:05:02 -0700 | [diff] [blame] | 583 | } |