1aad628c1SPeter Huewe /* 2c61c86ddSPeter Huewe * Copyright (C) 2012,2013 Infineon Technologies 3aad628c1SPeter Huewe * 4aad628c1SPeter Huewe * Authors: 5aad628c1SPeter Huewe * Peter Huewe <peter.huewe@infineon.com> 6aad628c1SPeter Huewe * 7aad628c1SPeter Huewe * Device driver for TCG/TCPA TPM (trusted platform module). 8aad628c1SPeter Huewe * Specifications at www.trustedcomputinggroup.org 9aad628c1SPeter Huewe * 10aad628c1SPeter Huewe * This device driver implements the TPM interface as defined in 11aad628c1SPeter Huewe * the TCG TPM Interface Spec version 1.2, revision 1.0 and the 12aad628c1SPeter Huewe * Infineon I2C Protocol Stack Specification v0.20. 13aad628c1SPeter Huewe * 14aad628c1SPeter Huewe * It is based on the original tpm_tis device driver from Leendert van 15aad628c1SPeter Huewe * Dorn and Kyleen Hall. 16aad628c1SPeter Huewe * 17aad628c1SPeter Huewe * This program is free software; you can redistribute it and/or 18aad628c1SPeter Huewe * modify it under the terms of the GNU General Public License as 19aad628c1SPeter Huewe * published by the Free Software Foundation, version 2 of the 20aad628c1SPeter Huewe * License. 21aad628c1SPeter Huewe * 22aad628c1SPeter Huewe * 23aad628c1SPeter Huewe */ 24aad628c1SPeter Huewe #include <linux/i2c.h> 25aad628c1SPeter Huewe #include <linux/module.h> 26aad628c1SPeter Huewe #include <linux/wait.h> 27aad628c1SPeter Huewe #include "tpm.h" 28aad628c1SPeter Huewe 29aad628c1SPeter Huewe /* max. buffer size supported by our TPM */ 30aad628c1SPeter Huewe #define TPM_BUFSIZE 1260 31aad628c1SPeter Huewe 32aad628c1SPeter Huewe /* max. number of iterations after I2C NAK */ 33aad628c1SPeter Huewe #define MAX_COUNT 3 34aad628c1SPeter Huewe 35aad628c1SPeter Huewe #define SLEEP_DURATION_LOW 55 36aad628c1SPeter Huewe #define SLEEP_DURATION_HI 65 37aad628c1SPeter Huewe 38aad628c1SPeter Huewe /* max. number of iterations after I2C NAK for 'long' commands 39aad628c1SPeter Huewe * we need this especially for sending TPM_READY, since the cleanup after the 40aad628c1SPeter Huewe * transtion to the ready state may take some time, but it is unpredictable 41aad628c1SPeter Huewe * how long it will take. 42aad628c1SPeter Huewe */ 43aad628c1SPeter Huewe #define MAX_COUNT_LONG 50 44aad628c1SPeter Huewe 45aad628c1SPeter Huewe #define SLEEP_DURATION_LONG_LOW 200 46aad628c1SPeter Huewe #define SLEEP_DURATION_LONG_HI 220 47aad628c1SPeter Huewe 48aad628c1SPeter Huewe /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */ 49aad628c1SPeter Huewe #define SLEEP_DURATION_RESET_LOW 2400 50aad628c1SPeter Huewe #define SLEEP_DURATION_RESET_HI 2600 51aad628c1SPeter Huewe 52aad628c1SPeter Huewe /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */ 53aad628c1SPeter Huewe #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000) 54aad628c1SPeter Huewe #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000) 55aad628c1SPeter Huewe 56aad628c1SPeter Huewe /* expected value for DIDVID register */ 57c61c86ddSPeter Huewe #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L 58c61c86ddSPeter Huewe #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L 59c61c86ddSPeter Huewe 60c61c86ddSPeter Huewe enum i2c_chip_type { 61c61c86ddSPeter Huewe SLB9635, 62c61c86ddSPeter Huewe SLB9645, 63c61c86ddSPeter Huewe UNKNOWN, 64c61c86ddSPeter Huewe }; 65aad628c1SPeter Huewe 66aad628c1SPeter Huewe /* Structure to store I2C TPM specific stuff */ 67aad628c1SPeter Huewe struct tpm_inf_dev { 68aad628c1SPeter Huewe struct i2c_client *client; 6956671c89SChristophe Ricard int locality; 70aad628c1SPeter Huewe u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */ 71aad628c1SPeter Huewe struct tpm_chip *chip; 72c61c86ddSPeter Huewe enum i2c_chip_type chip_type; 73d8c3eab5SBryan Freed unsigned int adapterlimit; 74aad628c1SPeter Huewe }; 75aad628c1SPeter Huewe 76aad628c1SPeter Huewe static struct tpm_inf_dev tpm_dev; 77aad628c1SPeter Huewe 78aad628c1SPeter Huewe /* 79aad628c1SPeter Huewe * iic_tpm_read() - read from TPM register 80aad628c1SPeter Huewe * @addr: register address to read from 81aad628c1SPeter Huewe * @buffer: provided by caller 82aad628c1SPeter Huewe * @len: number of bytes to read 83aad628c1SPeter Huewe * 84aad628c1SPeter Huewe * Read len bytes from TPM register and put them into 85aad628c1SPeter Huewe * buffer (little-endian format, i.e. first byte is put into buffer[0]). 86aad628c1SPeter Huewe * 87aad628c1SPeter Huewe * NOTE: TPM is big-endian for multi-byte values. Multi-byte 88aad628c1SPeter Huewe * values have to be swapped. 89aad628c1SPeter Huewe * 90aad628c1SPeter Huewe * NOTE: We can't unfortunately use the combined read/write functions 91aad628c1SPeter Huewe * provided by the i2c core as the TPM currently does not support the 92aad628c1SPeter Huewe * repeated start condition and due to it's special requirements. 93aad628c1SPeter Huewe * The i2c_smbus* functions do not work for this chip. 94aad628c1SPeter Huewe * 95aad628c1SPeter Huewe * Return -EIO on error, 0 on success. 96aad628c1SPeter Huewe */ 97aad628c1SPeter Huewe static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) 98aad628c1SPeter Huewe { 99aad628c1SPeter Huewe 100eef8b629SShubhrajyoti Datta struct i2c_msg msg1 = { 101eef8b629SShubhrajyoti Datta .addr = tpm_dev.client->addr, 102eef8b629SShubhrajyoti Datta .len = 1, 103eef8b629SShubhrajyoti Datta .buf = &addr 104eef8b629SShubhrajyoti Datta }; 105eef8b629SShubhrajyoti Datta struct i2c_msg msg2 = { 106eef8b629SShubhrajyoti Datta .addr = tpm_dev.client->addr, 107eef8b629SShubhrajyoti Datta .flags = I2C_M_RD, 108eef8b629SShubhrajyoti Datta .len = len, 109eef8b629SShubhrajyoti Datta .buf = buffer 110eef8b629SShubhrajyoti Datta }; 111c61c86ddSPeter Huewe struct i2c_msg msgs[] = {msg1, msg2}; 112aad628c1SPeter Huewe 113c61c86ddSPeter Huewe int rc = 0; 114aad628c1SPeter Huewe int count; 115d8c3eab5SBryan Freed unsigned int msglen = len; 116aad628c1SPeter Huewe 117aad628c1SPeter Huewe /* Lock the adapter for the duration of the whole sequence. */ 118aad628c1SPeter Huewe if (!tpm_dev.client->adapter->algo->master_xfer) 119aad628c1SPeter Huewe return -EOPNOTSUPP; 120*79e2472fSPeter Rosin i2c_lock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT); 121aad628c1SPeter Huewe 122c61c86ddSPeter Huewe if (tpm_dev.chip_type == SLB9645) { 123c61c86ddSPeter Huewe /* use a combined read for newer chips 124c61c86ddSPeter Huewe * unfortunately the smbus functions are not suitable due to 125c61c86ddSPeter Huewe * the 32 byte limit of the smbus. 126c61c86ddSPeter Huewe * retries should usually not be needed, but are kept just to 127c61c86ddSPeter Huewe * be on the safe side. 128c61c86ddSPeter Huewe */ 129c61c86ddSPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 130c61c86ddSPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2); 131c61c86ddSPeter Huewe if (rc > 0) 132c61c86ddSPeter Huewe break; /* break here to skip sleep */ 133c61c86ddSPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 134c61c86ddSPeter Huewe } 135c61c86ddSPeter Huewe } else { 136d8c3eab5SBryan Freed /* Expect to send one command message and one data message, but 137d8c3eab5SBryan Freed * support looping over each or both if necessary. 138d8c3eab5SBryan Freed */ 139d8c3eab5SBryan Freed while (len > 0) { 140c61c86ddSPeter Huewe /* slb9635 protocol should work in all cases */ 141aad628c1SPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 142d8c3eab5SBryan Freed rc = __i2c_transfer(tpm_dev.client->adapter, 143d8c3eab5SBryan Freed &msg1, 1); 144aad628c1SPeter Huewe if (rc > 0) 145aad628c1SPeter Huewe break; /* break here to skip sleep */ 146aad628c1SPeter Huewe 147d8c3eab5SBryan Freed usleep_range(SLEEP_DURATION_LOW, 148d8c3eab5SBryan Freed SLEEP_DURATION_HI); 149aad628c1SPeter Huewe } 150aad628c1SPeter Huewe 151aad628c1SPeter Huewe if (rc <= 0) 152aad628c1SPeter Huewe goto out; 153aad628c1SPeter Huewe 154d8c3eab5SBryan Freed /* After the TPM has successfully received the register 155d8c3eab5SBryan Freed * address it needs some time, thus we're sleeping here 156d8c3eab5SBryan Freed * again, before retrieving the data 157aad628c1SPeter Huewe */ 158aad628c1SPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 159d8c3eab5SBryan Freed if (tpm_dev.adapterlimit) { 160d8c3eab5SBryan Freed msglen = min_t(unsigned int, 161d8c3eab5SBryan Freed tpm_dev.adapterlimit, 162d8c3eab5SBryan Freed len); 163d8c3eab5SBryan Freed msg2.len = msglen; 164d8c3eab5SBryan Freed } 165d8c3eab5SBryan Freed usleep_range(SLEEP_DURATION_LOW, 166d8c3eab5SBryan Freed SLEEP_DURATION_HI); 167d8c3eab5SBryan Freed rc = __i2c_transfer(tpm_dev.client->adapter, 168d8c3eab5SBryan Freed &msg2, 1); 169d8c3eab5SBryan Freed if (rc > 0) { 170d8c3eab5SBryan Freed /* Since len is unsigned, make doubly 171d8c3eab5SBryan Freed * sure we do not underflow it. 172d8c3eab5SBryan Freed */ 173d8c3eab5SBryan Freed if (msglen > len) 174d8c3eab5SBryan Freed len = 0; 175d8c3eab5SBryan Freed else 176d8c3eab5SBryan Freed len -= msglen; 177d8c3eab5SBryan Freed msg2.buf += msglen; 178aad628c1SPeter Huewe break; 179c61c86ddSPeter Huewe } 180d8c3eab5SBryan Freed /* If the I2C adapter rejected the request (e.g 181d8c3eab5SBryan Freed * when the quirk read_max_len < len) fall back 182d8c3eab5SBryan Freed * to a sane minimum value and try again. 183d8c3eab5SBryan Freed */ 184d8c3eab5SBryan Freed if (rc == -EOPNOTSUPP) 185d8c3eab5SBryan Freed tpm_dev.adapterlimit = 186d8c3eab5SBryan Freed I2C_SMBUS_BLOCK_MAX; 187d8c3eab5SBryan Freed } 188d8c3eab5SBryan Freed 189d8c3eab5SBryan Freed if (rc <= 0) 190d8c3eab5SBryan Freed goto out; 191d8c3eab5SBryan Freed } 192aad628c1SPeter Huewe } 193aad628c1SPeter Huewe 194aad628c1SPeter Huewe out: 195*79e2472fSPeter Rosin i2c_unlock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT); 196c61c86ddSPeter Huewe /* take care of 'guard time' */ 197c61c86ddSPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 198c61c86ddSPeter Huewe 1996aa4ef4dSPeter Huewe /* __i2c_transfer returns the number of successfully transferred 2006aa4ef4dSPeter Huewe * messages. 2016aa4ef4dSPeter Huewe * So rc should be greater than 0 here otherwise we have an error. 2026aa4ef4dSPeter Huewe */ 203aad628c1SPeter Huewe if (rc <= 0) 204aad628c1SPeter Huewe return -EIO; 205aad628c1SPeter Huewe 206aad628c1SPeter Huewe return 0; 207aad628c1SPeter Huewe } 208aad628c1SPeter Huewe 209aad628c1SPeter Huewe static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, 210aad628c1SPeter Huewe unsigned int sleep_low, 211aad628c1SPeter Huewe unsigned int sleep_hi, u8 max_count) 212aad628c1SPeter Huewe { 213aad628c1SPeter Huewe int rc = -EIO; 214aad628c1SPeter Huewe int count; 215aad628c1SPeter Huewe 216eef8b629SShubhrajyoti Datta struct i2c_msg msg1 = { 217eef8b629SShubhrajyoti Datta .addr = tpm_dev.client->addr, 218eef8b629SShubhrajyoti Datta .len = len + 1, 219eef8b629SShubhrajyoti Datta .buf = tpm_dev.buf 220eef8b629SShubhrajyoti Datta }; 221aad628c1SPeter Huewe 222aad628c1SPeter Huewe if (len > TPM_BUFSIZE) 223aad628c1SPeter Huewe return -EINVAL; 224aad628c1SPeter Huewe 225aad628c1SPeter Huewe if (!tpm_dev.client->adapter->algo->master_xfer) 226aad628c1SPeter Huewe return -EOPNOTSUPP; 227*79e2472fSPeter Rosin i2c_lock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT); 228aad628c1SPeter Huewe 229aad628c1SPeter Huewe /* prepend the 'register address' to the buffer */ 230aad628c1SPeter Huewe tpm_dev.buf[0] = addr; 231aad628c1SPeter Huewe memcpy(&(tpm_dev.buf[1]), buffer, len); 232aad628c1SPeter Huewe 233aad628c1SPeter Huewe /* 234aad628c1SPeter Huewe * NOTE: We have to use these special mechanisms here and unfortunately 235aad628c1SPeter Huewe * cannot rely on the standard behavior of i2c_transfer. 236c61c86ddSPeter Huewe * Even for newer chips the smbus functions are not 237c61c86ddSPeter Huewe * suitable due to the 32 byte limit of the smbus. 238aad628c1SPeter Huewe */ 239aad628c1SPeter Huewe for (count = 0; count < max_count; count++) { 240aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); 241aad628c1SPeter Huewe if (rc > 0) 242aad628c1SPeter Huewe break; 243aad628c1SPeter Huewe usleep_range(sleep_low, sleep_hi); 244aad628c1SPeter Huewe } 245aad628c1SPeter Huewe 246*79e2472fSPeter Rosin i2c_unlock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT); 247c61c86ddSPeter Huewe /* take care of 'guard time' */ 248c61c86ddSPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 2496aa4ef4dSPeter Huewe 2506aa4ef4dSPeter Huewe /* __i2c_transfer returns the number of successfully transferred 2516aa4ef4dSPeter Huewe * messages. 2526aa4ef4dSPeter Huewe * So rc should be greater than 0 here otherwise we have an error. 2536aa4ef4dSPeter Huewe */ 254aad628c1SPeter Huewe if (rc <= 0) 255aad628c1SPeter Huewe return -EIO; 256aad628c1SPeter Huewe 257aad628c1SPeter Huewe return 0; 258aad628c1SPeter Huewe } 259aad628c1SPeter Huewe 260aad628c1SPeter Huewe /* 261aad628c1SPeter Huewe * iic_tpm_write() - write to TPM register 262aad628c1SPeter Huewe * @addr: register address to write to 263aad628c1SPeter Huewe * @buffer: containing data to be written 264aad628c1SPeter Huewe * @len: number of bytes to write 265aad628c1SPeter Huewe * 266aad628c1SPeter Huewe * Write len bytes from provided buffer to TPM register (little 267aad628c1SPeter Huewe * endian format, i.e. buffer[0] is written as first byte). 268aad628c1SPeter Huewe * 269aad628c1SPeter Huewe * NOTE: TPM is big-endian for multi-byte values. Multi-byte 270aad628c1SPeter Huewe * values have to be swapped. 271aad628c1SPeter Huewe * 272aad628c1SPeter Huewe * NOTE: use this function instead of the iic_tpm_write_generic function. 273aad628c1SPeter Huewe * 274aad628c1SPeter Huewe * Return -EIO on error, 0 on success 275aad628c1SPeter Huewe */ 276aad628c1SPeter Huewe static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) 277aad628c1SPeter Huewe { 278aad628c1SPeter Huewe return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW, 279aad628c1SPeter Huewe SLEEP_DURATION_HI, MAX_COUNT); 280aad628c1SPeter Huewe } 281aad628c1SPeter Huewe 282aad628c1SPeter Huewe /* 283aad628c1SPeter Huewe * This function is needed especially for the cleanup situation after 284aad628c1SPeter Huewe * sending TPM_READY 285aad628c1SPeter Huewe * */ 286aad628c1SPeter Huewe static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) 287aad628c1SPeter Huewe { 288aad628c1SPeter Huewe return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW, 289aad628c1SPeter Huewe SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG); 290aad628c1SPeter Huewe } 291aad628c1SPeter Huewe 292aad628c1SPeter Huewe enum tis_access { 293aad628c1SPeter Huewe TPM_ACCESS_VALID = 0x80, 294aad628c1SPeter Huewe TPM_ACCESS_ACTIVE_LOCALITY = 0x20, 295aad628c1SPeter Huewe TPM_ACCESS_REQUEST_PENDING = 0x04, 296aad628c1SPeter Huewe TPM_ACCESS_REQUEST_USE = 0x02, 297aad628c1SPeter Huewe }; 298aad628c1SPeter Huewe 299aad628c1SPeter Huewe enum tis_status { 300aad628c1SPeter Huewe TPM_STS_VALID = 0x80, 301aad628c1SPeter Huewe TPM_STS_COMMAND_READY = 0x40, 302aad628c1SPeter Huewe TPM_STS_GO = 0x20, 303aad628c1SPeter Huewe TPM_STS_DATA_AVAIL = 0x10, 304aad628c1SPeter Huewe TPM_STS_DATA_EXPECT = 0x08, 305aad628c1SPeter Huewe }; 306aad628c1SPeter Huewe 307aad628c1SPeter Huewe enum tis_defaults { 308aad628c1SPeter Huewe TIS_SHORT_TIMEOUT = 750, /* ms */ 309aad628c1SPeter Huewe TIS_LONG_TIMEOUT = 2000, /* 2 sec */ 310aad628c1SPeter Huewe }; 311aad628c1SPeter Huewe 312aad628c1SPeter Huewe #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) 313aad628c1SPeter Huewe #define TPM_STS(l) (0x0001 | ((l) << 4)) 314aad628c1SPeter Huewe #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) 315aad628c1SPeter Huewe #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) 316aad628c1SPeter Huewe 31784d25940SJerry Snitselaar static bool check_locality(struct tpm_chip *chip, int loc) 318aad628c1SPeter Huewe { 319aad628c1SPeter Huewe u8 buf; 320aad628c1SPeter Huewe int rc; 321aad628c1SPeter Huewe 322aad628c1SPeter Huewe rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); 323aad628c1SPeter Huewe if (rc < 0) 32484d25940SJerry Snitselaar return false; 325aad628c1SPeter Huewe 326aad628c1SPeter Huewe if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == 327aad628c1SPeter Huewe (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { 32856671c89SChristophe Ricard tpm_dev.locality = loc; 32984d25940SJerry Snitselaar return true; 330aad628c1SPeter Huewe } 331aad628c1SPeter Huewe 33284d25940SJerry Snitselaar return false; 333aad628c1SPeter Huewe } 334aad628c1SPeter Huewe 335aad628c1SPeter Huewe /* implementation similar to tpm_tis */ 336aad628c1SPeter Huewe static void release_locality(struct tpm_chip *chip, int loc, int force) 337aad628c1SPeter Huewe { 338aad628c1SPeter Huewe u8 buf; 339aad628c1SPeter Huewe if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) 340aad628c1SPeter Huewe return; 341aad628c1SPeter Huewe 342aad628c1SPeter Huewe if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == 343aad628c1SPeter Huewe (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { 344aad628c1SPeter Huewe buf = TPM_ACCESS_ACTIVE_LOCALITY; 345aad628c1SPeter Huewe iic_tpm_write(TPM_ACCESS(loc), &buf, 1); 346aad628c1SPeter Huewe } 347aad628c1SPeter Huewe } 348aad628c1SPeter Huewe 349aad628c1SPeter Huewe static int request_locality(struct tpm_chip *chip, int loc) 350aad628c1SPeter Huewe { 351aad628c1SPeter Huewe unsigned long stop; 352aad628c1SPeter Huewe u8 buf = TPM_ACCESS_REQUEST_USE; 353aad628c1SPeter Huewe 35484d25940SJerry Snitselaar if (check_locality(chip, loc)) 355aad628c1SPeter Huewe return loc; 356aad628c1SPeter Huewe 357aad628c1SPeter Huewe iic_tpm_write(TPM_ACCESS(loc), &buf, 1); 358aad628c1SPeter Huewe 359aad628c1SPeter Huewe /* wait for burstcount */ 360af782f33SChristophe Ricard stop = jiffies + chip->timeout_a; 361aad628c1SPeter Huewe do { 36284d25940SJerry Snitselaar if (check_locality(chip, loc)) 363aad628c1SPeter Huewe return loc; 364aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 365aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 366aad628c1SPeter Huewe 367aad628c1SPeter Huewe return -ETIME; 368aad628c1SPeter Huewe } 369aad628c1SPeter Huewe 370aad628c1SPeter Huewe static u8 tpm_tis_i2c_status(struct tpm_chip *chip) 371aad628c1SPeter Huewe { 372aad628c1SPeter Huewe /* NOTE: since I2C read may fail, return 0 in this case --> time-out */ 373c61c86ddSPeter Huewe u8 buf = 0xFF; 374c61c86ddSPeter Huewe u8 i = 0; 375c61c86ddSPeter Huewe 376c61c86ddSPeter Huewe do { 37756671c89SChristophe Ricard if (iic_tpm_read(TPM_STS(tpm_dev.locality), &buf, 1) < 0) 378aad628c1SPeter Huewe return 0; 379c61c86ddSPeter Huewe 380c61c86ddSPeter Huewe i++; 381c61c86ddSPeter Huewe /* if locallity is set STS should not be 0xFF */ 382c61c86ddSPeter Huewe } while ((buf == 0xFF) && i < 10); 383c61c86ddSPeter Huewe 384aad628c1SPeter Huewe return buf; 385aad628c1SPeter Huewe } 386aad628c1SPeter Huewe 387aad628c1SPeter Huewe static void tpm_tis_i2c_ready(struct tpm_chip *chip) 388aad628c1SPeter Huewe { 389aad628c1SPeter Huewe /* this causes the current command to be aborted */ 390aad628c1SPeter Huewe u8 buf = TPM_STS_COMMAND_READY; 39156671c89SChristophe Ricard iic_tpm_write_long(TPM_STS(tpm_dev.locality), &buf, 1); 392aad628c1SPeter Huewe } 393aad628c1SPeter Huewe 394aad628c1SPeter Huewe static ssize_t get_burstcount(struct tpm_chip *chip) 395aad628c1SPeter Huewe { 396aad628c1SPeter Huewe unsigned long stop; 397aad628c1SPeter Huewe ssize_t burstcnt; 398aad628c1SPeter Huewe u8 buf[3]; 399aad628c1SPeter Huewe 400aad628c1SPeter Huewe /* wait for burstcount */ 401aad628c1SPeter Huewe /* which timeout value, spec has 2 answers (c & d) */ 402af782f33SChristophe Ricard stop = jiffies + chip->timeout_d; 403aad628c1SPeter Huewe do { 404aad628c1SPeter Huewe /* Note: STS is little endian */ 40556671c89SChristophe Ricard if (iic_tpm_read(TPM_STS(tpm_dev.locality)+1, buf, 3) < 0) 406aad628c1SPeter Huewe burstcnt = 0; 407aad628c1SPeter Huewe else 408aad628c1SPeter Huewe burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; 409aad628c1SPeter Huewe 410aad628c1SPeter Huewe if (burstcnt) 411aad628c1SPeter Huewe return burstcnt; 412aad628c1SPeter Huewe 413aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 414aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 415aad628c1SPeter Huewe return -EBUSY; 416aad628c1SPeter Huewe } 417aad628c1SPeter Huewe 418aad628c1SPeter Huewe static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 419aad628c1SPeter Huewe int *status) 420aad628c1SPeter Huewe { 421aad628c1SPeter Huewe unsigned long stop; 422aad628c1SPeter Huewe 423aad628c1SPeter Huewe /* check current status */ 424aad628c1SPeter Huewe *status = tpm_tis_i2c_status(chip); 425c61c86ddSPeter Huewe if ((*status != 0xFF) && (*status & mask) == mask) 426aad628c1SPeter Huewe return 0; 427aad628c1SPeter Huewe 428aad628c1SPeter Huewe stop = jiffies + timeout; 429aad628c1SPeter Huewe do { 430aad628c1SPeter Huewe /* since we just checked the status, give the TPM some time */ 431aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 432aad628c1SPeter Huewe *status = tpm_tis_i2c_status(chip); 433aad628c1SPeter Huewe if ((*status & mask) == mask) 434aad628c1SPeter Huewe return 0; 435aad628c1SPeter Huewe 436aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 437aad628c1SPeter Huewe 438aad628c1SPeter Huewe return -ETIME; 439aad628c1SPeter Huewe } 440aad628c1SPeter Huewe 441aad628c1SPeter Huewe static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) 442aad628c1SPeter Huewe { 443aad628c1SPeter Huewe size_t size = 0; 444aad628c1SPeter Huewe ssize_t burstcnt; 445aad628c1SPeter Huewe u8 retries = 0; 446aad628c1SPeter Huewe int rc; 447aad628c1SPeter Huewe 448aad628c1SPeter Huewe while (size < count) { 449aad628c1SPeter Huewe burstcnt = get_burstcount(chip); 450aad628c1SPeter Huewe 451aad628c1SPeter Huewe /* burstcnt < 0 = TPM is busy */ 452aad628c1SPeter Huewe if (burstcnt < 0) 453aad628c1SPeter Huewe return burstcnt; 454aad628c1SPeter Huewe 455aad628c1SPeter Huewe /* limit received data to max. left */ 456aad628c1SPeter Huewe if (burstcnt > (count - size)) 457aad628c1SPeter Huewe burstcnt = count - size; 458aad628c1SPeter Huewe 45956671c89SChristophe Ricard rc = iic_tpm_read(TPM_DATA_FIFO(tpm_dev.locality), 460aad628c1SPeter Huewe &(buf[size]), burstcnt); 461aad628c1SPeter Huewe if (rc == 0) 462aad628c1SPeter Huewe size += burstcnt; 463aad628c1SPeter Huewe else if (rc < 0) 464aad628c1SPeter Huewe retries++; 465aad628c1SPeter Huewe 466aad628c1SPeter Huewe /* avoid endless loop in case of broken HW */ 467aad628c1SPeter Huewe if (retries > MAX_COUNT_LONG) 468aad628c1SPeter Huewe return -EIO; 469aad628c1SPeter Huewe } 470aad628c1SPeter Huewe return size; 471aad628c1SPeter Huewe } 472aad628c1SPeter Huewe 473aad628c1SPeter Huewe static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) 474aad628c1SPeter Huewe { 475aad628c1SPeter Huewe int size = 0; 4769b8cb28dSJeremy Boone int status; 4779b8cb28dSJeremy Boone u32 expected; 478aad628c1SPeter Huewe 479aad628c1SPeter Huewe if (count < TPM_HEADER_SIZE) { 480aad628c1SPeter Huewe size = -EIO; 481aad628c1SPeter Huewe goto out; 482aad628c1SPeter Huewe } 483aad628c1SPeter Huewe 484aad628c1SPeter Huewe /* read first 10 bytes, including tag, paramsize, and result */ 485aad628c1SPeter Huewe size = recv_data(chip, buf, TPM_HEADER_SIZE); 486aad628c1SPeter Huewe if (size < TPM_HEADER_SIZE) { 4878cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Unable to read header\n"); 488aad628c1SPeter Huewe goto out; 489aad628c1SPeter Huewe } 490aad628c1SPeter Huewe 491aad628c1SPeter Huewe expected = be32_to_cpu(*(__be32 *)(buf + 2)); 4929b8cb28dSJeremy Boone if (((size_t) expected > count) || (expected < TPM_HEADER_SIZE)) { 493aad628c1SPeter Huewe size = -EIO; 494aad628c1SPeter Huewe goto out; 495aad628c1SPeter Huewe } 496aad628c1SPeter Huewe 497aad628c1SPeter Huewe size += recv_data(chip, &buf[TPM_HEADER_SIZE], 498aad628c1SPeter Huewe expected - TPM_HEADER_SIZE); 499aad628c1SPeter Huewe if (size < expected) { 5008cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Unable to read remainder of result\n"); 501aad628c1SPeter Huewe size = -ETIME; 502aad628c1SPeter Huewe goto out; 503aad628c1SPeter Huewe } 504aad628c1SPeter Huewe 505af782f33SChristophe Ricard wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status); 506aad628c1SPeter Huewe if (status & TPM_STS_DATA_AVAIL) { /* retry? */ 5078cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Error left over data\n"); 508aad628c1SPeter Huewe size = -EIO; 509aad628c1SPeter Huewe goto out; 510aad628c1SPeter Huewe } 511aad628c1SPeter Huewe 512aad628c1SPeter Huewe out: 513aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 514aad628c1SPeter Huewe /* The TPM needs some time to clean up here, 515aad628c1SPeter Huewe * so we sleep rather than keeping the bus busy 516aad628c1SPeter Huewe */ 517aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); 51856671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 0); 519aad628c1SPeter Huewe return size; 520aad628c1SPeter Huewe } 521aad628c1SPeter Huewe 522aad628c1SPeter Huewe static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) 523aad628c1SPeter Huewe { 524aad628c1SPeter Huewe int rc, status; 525aad628c1SPeter Huewe ssize_t burstcnt; 526aad628c1SPeter Huewe size_t count = 0; 527aad628c1SPeter Huewe u8 retries = 0; 528aad628c1SPeter Huewe u8 sts = TPM_STS_GO; 529aad628c1SPeter Huewe 530aad628c1SPeter Huewe if (len > TPM_BUFSIZE) 531aad628c1SPeter Huewe return -E2BIG; /* command is too long for our tpm, sorry */ 532aad628c1SPeter Huewe 533aad628c1SPeter Huewe if (request_locality(chip, 0) < 0) 534aad628c1SPeter Huewe return -EBUSY; 535aad628c1SPeter Huewe 536aad628c1SPeter Huewe status = tpm_tis_i2c_status(chip); 537aad628c1SPeter Huewe if ((status & TPM_STS_COMMAND_READY) == 0) { 538aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 539aad628c1SPeter Huewe if (wait_for_stat 540aad628c1SPeter Huewe (chip, TPM_STS_COMMAND_READY, 541af782f33SChristophe Ricard chip->timeout_b, &status) < 0) { 542aad628c1SPeter Huewe rc = -ETIME; 543aad628c1SPeter Huewe goto out_err; 544aad628c1SPeter Huewe } 545aad628c1SPeter Huewe } 546aad628c1SPeter Huewe 547aad628c1SPeter Huewe while (count < len - 1) { 548aad628c1SPeter Huewe burstcnt = get_burstcount(chip); 549aad628c1SPeter Huewe 550aad628c1SPeter Huewe /* burstcnt < 0 = TPM is busy */ 551aad628c1SPeter Huewe if (burstcnt < 0) 552aad628c1SPeter Huewe return burstcnt; 553aad628c1SPeter Huewe 554aad628c1SPeter Huewe if (burstcnt > (len - 1 - count)) 555aad628c1SPeter Huewe burstcnt = len - 1 - count; 556aad628c1SPeter Huewe 55756671c89SChristophe Ricard rc = iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), 558aad628c1SPeter Huewe &(buf[count]), burstcnt); 559aad628c1SPeter Huewe if (rc == 0) 560aad628c1SPeter Huewe count += burstcnt; 561aad628c1SPeter Huewe else if (rc < 0) 562aad628c1SPeter Huewe retries++; 563aad628c1SPeter Huewe 564aad628c1SPeter Huewe /* avoid endless loop in case of broken HW */ 565aad628c1SPeter Huewe if (retries > MAX_COUNT_LONG) { 566aad628c1SPeter Huewe rc = -EIO; 567aad628c1SPeter Huewe goto out_err; 568aad628c1SPeter Huewe } 569aad628c1SPeter Huewe 570aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, 571af782f33SChristophe Ricard chip->timeout_c, &status); 572aad628c1SPeter Huewe 573aad628c1SPeter Huewe if ((status & TPM_STS_DATA_EXPECT) == 0) { 574aad628c1SPeter Huewe rc = -EIO; 575aad628c1SPeter Huewe goto out_err; 576aad628c1SPeter Huewe } 577aad628c1SPeter Huewe } 578aad628c1SPeter Huewe 579aad628c1SPeter Huewe /* write last byte */ 58056671c89SChristophe Ricard iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), &(buf[count]), 1); 581af782f33SChristophe Ricard wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status); 582aad628c1SPeter Huewe if ((status & TPM_STS_DATA_EXPECT) != 0) { 583aad628c1SPeter Huewe rc = -EIO; 584aad628c1SPeter Huewe goto out_err; 585aad628c1SPeter Huewe } 586aad628c1SPeter Huewe 587aad628c1SPeter Huewe /* go and do it */ 58856671c89SChristophe Ricard iic_tpm_write(TPM_STS(tpm_dev.locality), &sts, 1); 589aad628c1SPeter Huewe 590aad628c1SPeter Huewe return len; 591aad628c1SPeter Huewe out_err: 592aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 593aad628c1SPeter Huewe /* The TPM needs some time to clean up here, 594aad628c1SPeter Huewe * so we sleep rather than keeping the bus busy 595aad628c1SPeter Huewe */ 596aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); 59756671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 0); 598aad628c1SPeter Huewe return rc; 599aad628c1SPeter Huewe } 600aad628c1SPeter Huewe 6011f866057SStefan Berger static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status) 6021f866057SStefan Berger { 6031f866057SStefan Berger return (status == TPM_STS_COMMAND_READY); 6041f866057SStefan Berger } 6051f866057SStefan Berger 60601ad1fa7SJason Gunthorpe static const struct tpm_class_ops tpm_tis_i2c = { 607cae8b441SJason Gunthorpe .flags = TPM_OPS_AUTO_STARTUP, 608aad628c1SPeter Huewe .status = tpm_tis_i2c_status, 609aad628c1SPeter Huewe .recv = tpm_tis_i2c_recv, 610aad628c1SPeter Huewe .send = tpm_tis_i2c_send, 611aad628c1SPeter Huewe .cancel = tpm_tis_i2c_ready, 612aad628c1SPeter Huewe .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 613aad628c1SPeter Huewe .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 6141f866057SStefan Berger .req_canceled = tpm_tis_i2c_req_canceled, 615aad628c1SPeter Huewe }; 616aad628c1SPeter Huewe 617afc6d369SBill Pemberton static int tpm_tis_i2c_init(struct device *dev) 618aad628c1SPeter Huewe { 619aad628c1SPeter Huewe u32 vendor; 620aad628c1SPeter Huewe int rc = 0; 621aad628c1SPeter Huewe struct tpm_chip *chip; 622aad628c1SPeter Huewe 623afb5abc2SJarkko Sakkinen chip = tpmm_chip_alloc(dev, &tpm_tis_i2c); 624afb5abc2SJarkko Sakkinen if (IS_ERR(chip)) 625afb5abc2SJarkko Sakkinen return PTR_ERR(chip); 626aad628c1SPeter Huewe 627aad628c1SPeter Huewe /* Default timeouts */ 628af782f33SChristophe Ricard chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 629af782f33SChristophe Ricard chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); 630af782f33SChristophe Ricard chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 631af782f33SChristophe Ricard chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 632aad628c1SPeter Huewe 633aad628c1SPeter Huewe if (request_locality(chip, 0) != 0) { 634c61c86ddSPeter Huewe dev_err(dev, "could not request locality\n"); 635aad628c1SPeter Huewe rc = -ENODEV; 636afb5abc2SJarkko Sakkinen goto out_err; 637aad628c1SPeter Huewe } 638aad628c1SPeter Huewe 639aad628c1SPeter Huewe /* read four bytes from DID_VID register */ 640aad628c1SPeter Huewe if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) { 641c61c86ddSPeter Huewe dev_err(dev, "could not read vendor id\n"); 642aad628c1SPeter Huewe rc = -EIO; 643aad628c1SPeter Huewe goto out_release; 644aad628c1SPeter Huewe } 645aad628c1SPeter Huewe 646c61c86ddSPeter Huewe if (vendor == TPM_TIS_I2C_DID_VID_9645) { 647c61c86ddSPeter Huewe tpm_dev.chip_type = SLB9645; 648c61c86ddSPeter Huewe } else if (vendor == TPM_TIS_I2C_DID_VID_9635) { 649c61c86ddSPeter Huewe tpm_dev.chip_type = SLB9635; 650c61c86ddSPeter Huewe } else { 651c61c86ddSPeter Huewe dev_err(dev, "vendor id did not match! ID was %08x\n", vendor); 652aad628c1SPeter Huewe rc = -ENODEV; 653aad628c1SPeter Huewe goto out_release; 654aad628c1SPeter Huewe } 655aad628c1SPeter Huewe 656aad628c1SPeter Huewe dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); 657aad628c1SPeter Huewe 658aad628c1SPeter Huewe tpm_dev.chip = chip; 659aad628c1SPeter Huewe 660afb5abc2SJarkko Sakkinen return tpm_chip_register(chip); 661aad628c1SPeter Huewe out_release: 66256671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 1); 663aad628c1SPeter Huewe tpm_dev.client = NULL; 664aad628c1SPeter Huewe out_err: 665aad628c1SPeter Huewe return rc; 666aad628c1SPeter Huewe } 667aad628c1SPeter Huewe 668aad628c1SPeter Huewe static const struct i2c_device_id tpm_tis_i2c_table[] = { 669b4f20826SJavier Martinez Canillas {"tpm_i2c_infineon"}, 670b4f20826SJavier Martinez Canillas {"slb9635tt"}, 671b4f20826SJavier Martinez Canillas {"slb9645tt"}, 672aad628c1SPeter Huewe {}, 673aad628c1SPeter Huewe }; 674aad628c1SPeter Huewe 675aad628c1SPeter Huewe MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table); 676c61c86ddSPeter Huewe 677c61c86ddSPeter Huewe #ifdef CONFIG_OF 678c61c86ddSPeter Huewe static const struct of_device_id tpm_tis_i2c_of_match[] = { 679b4f20826SJavier Martinez Canillas {.compatible = "infineon,tpm_i2c_infineon"}, 680b4f20826SJavier Martinez Canillas {.compatible = "infineon,slb9635tt"}, 681b4f20826SJavier Martinez Canillas {.compatible = "infineon,slb9645tt"}, 682c61c86ddSPeter Huewe {}, 683c61c86ddSPeter Huewe }; 684c61c86ddSPeter Huewe MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match); 685c61c86ddSPeter Huewe #endif 686c61c86ddSPeter Huewe 687aad628c1SPeter Huewe static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume); 688aad628c1SPeter Huewe 689afc6d369SBill Pemberton static int tpm_tis_i2c_probe(struct i2c_client *client, 690aad628c1SPeter Huewe const struct i2c_device_id *id) 691aad628c1SPeter Huewe { 692aad628c1SPeter Huewe int rc; 693c61c86ddSPeter Huewe struct device *dev = &(client->dev); 694c61c86ddSPeter Huewe 695c61c86ddSPeter Huewe if (tpm_dev.client != NULL) { 696c61c86ddSPeter Huewe dev_err(dev, "This driver only supports one client at a time\n"); 697aad628c1SPeter Huewe return -EBUSY; /* We only support one client */ 698c61c86ddSPeter Huewe } 699aad628c1SPeter Huewe 700aad628c1SPeter Huewe if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 701c61c86ddSPeter Huewe dev_err(dev, "no algorithms associated to the i2c bus\n"); 702aad628c1SPeter Huewe return -ENODEV; 703aad628c1SPeter Huewe } 704aad628c1SPeter Huewe 705aad628c1SPeter Huewe tpm_dev.client = client; 706aad628c1SPeter Huewe rc = tpm_tis_i2c_init(&client->dev); 707aad628c1SPeter Huewe if (rc != 0) { 708aad628c1SPeter Huewe tpm_dev.client = NULL; 709aad628c1SPeter Huewe rc = -ENODEV; 710aad628c1SPeter Huewe } 711aad628c1SPeter Huewe return rc; 712aad628c1SPeter Huewe } 713aad628c1SPeter Huewe 71439af33fcSBill Pemberton static int tpm_tis_i2c_remove(struct i2c_client *client) 715aad628c1SPeter Huewe { 716aad628c1SPeter Huewe struct tpm_chip *chip = tpm_dev.chip; 717afb5abc2SJarkko Sakkinen 718afb5abc2SJarkko Sakkinen tpm_chip_unregister(chip); 71956671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 1); 720aad628c1SPeter Huewe tpm_dev.client = NULL; 721aad628c1SPeter Huewe 722aad628c1SPeter Huewe return 0; 723aad628c1SPeter Huewe } 724aad628c1SPeter Huewe 725aad628c1SPeter Huewe static struct i2c_driver tpm_tis_i2c_driver = { 726aad628c1SPeter Huewe .id_table = tpm_tis_i2c_table, 727aad628c1SPeter Huewe .probe = tpm_tis_i2c_probe, 728aad628c1SPeter Huewe .remove = tpm_tis_i2c_remove, 729aad628c1SPeter Huewe .driver = { 730aad628c1SPeter Huewe .name = "tpm_i2c_infineon", 731aad628c1SPeter Huewe .pm = &tpm_tis_i2c_ops, 732c61c86ddSPeter Huewe .of_match_table = of_match_ptr(tpm_tis_i2c_of_match), 733aad628c1SPeter Huewe }, 734aad628c1SPeter Huewe }; 735aad628c1SPeter Huewe 736aad628c1SPeter Huewe module_i2c_driver(tpm_tis_i2c_driver); 737aad628c1SPeter Huewe MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>"); 738aad628c1SPeter Huewe MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver"); 739c61c86ddSPeter Huewe MODULE_VERSION("2.2.0"); 740aad628c1SPeter Huewe MODULE_LICENSE("GPL"); 741