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; 69*56671c89SChristophe 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; 73aad628c1SPeter Huewe }; 74aad628c1SPeter Huewe 75aad628c1SPeter Huewe static struct tpm_inf_dev tpm_dev; 76aad628c1SPeter Huewe 77aad628c1SPeter Huewe /* 78aad628c1SPeter Huewe * iic_tpm_read() - read from TPM register 79aad628c1SPeter Huewe * @addr: register address to read from 80aad628c1SPeter Huewe * @buffer: provided by caller 81aad628c1SPeter Huewe * @len: number of bytes to read 82aad628c1SPeter Huewe * 83aad628c1SPeter Huewe * Read len bytes from TPM register and put them into 84aad628c1SPeter Huewe * buffer (little-endian format, i.e. first byte is put into buffer[0]). 85aad628c1SPeter Huewe * 86aad628c1SPeter Huewe * NOTE: TPM is big-endian for multi-byte values. Multi-byte 87aad628c1SPeter Huewe * values have to be swapped. 88aad628c1SPeter Huewe * 89aad628c1SPeter Huewe * NOTE: We can't unfortunately use the combined read/write functions 90aad628c1SPeter Huewe * provided by the i2c core as the TPM currently does not support the 91aad628c1SPeter Huewe * repeated start condition and due to it's special requirements. 92aad628c1SPeter Huewe * The i2c_smbus* functions do not work for this chip. 93aad628c1SPeter Huewe * 94aad628c1SPeter Huewe * Return -EIO on error, 0 on success. 95aad628c1SPeter Huewe */ 96aad628c1SPeter Huewe static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) 97aad628c1SPeter Huewe { 98aad628c1SPeter Huewe 99eef8b629SShubhrajyoti Datta struct i2c_msg msg1 = { 100eef8b629SShubhrajyoti Datta .addr = tpm_dev.client->addr, 101eef8b629SShubhrajyoti Datta .len = 1, 102eef8b629SShubhrajyoti Datta .buf = &addr 103eef8b629SShubhrajyoti Datta }; 104eef8b629SShubhrajyoti Datta struct i2c_msg msg2 = { 105eef8b629SShubhrajyoti Datta .addr = tpm_dev.client->addr, 106eef8b629SShubhrajyoti Datta .flags = I2C_M_RD, 107eef8b629SShubhrajyoti Datta .len = len, 108eef8b629SShubhrajyoti Datta .buf = buffer 109eef8b629SShubhrajyoti Datta }; 110c61c86ddSPeter Huewe struct i2c_msg msgs[] = {msg1, msg2}; 111aad628c1SPeter Huewe 112c61c86ddSPeter Huewe int rc = 0; 113aad628c1SPeter Huewe int count; 114aad628c1SPeter Huewe 115aad628c1SPeter Huewe /* Lock the adapter for the duration of the whole sequence. */ 116aad628c1SPeter Huewe if (!tpm_dev.client->adapter->algo->master_xfer) 117aad628c1SPeter Huewe return -EOPNOTSUPP; 118aad628c1SPeter Huewe i2c_lock_adapter(tpm_dev.client->adapter); 119aad628c1SPeter Huewe 120c61c86ddSPeter Huewe if (tpm_dev.chip_type == SLB9645) { 121c61c86ddSPeter Huewe /* use a combined read for newer chips 122c61c86ddSPeter Huewe * unfortunately the smbus functions are not suitable due to 123c61c86ddSPeter Huewe * the 32 byte limit of the smbus. 124c61c86ddSPeter Huewe * retries should usually not be needed, but are kept just to 125c61c86ddSPeter Huewe * be on the safe side. 126c61c86ddSPeter Huewe */ 127c61c86ddSPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 128c61c86ddSPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2); 129c61c86ddSPeter Huewe if (rc > 0) 130c61c86ddSPeter Huewe break; /* break here to skip sleep */ 131c61c86ddSPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 132c61c86ddSPeter Huewe } 133c61c86ddSPeter Huewe } else { 134c61c86ddSPeter Huewe /* slb9635 protocol should work in all cases */ 135aad628c1SPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 136aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); 137aad628c1SPeter Huewe if (rc > 0) 138aad628c1SPeter Huewe break; /* break here to skip sleep */ 139aad628c1SPeter Huewe 140aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 141aad628c1SPeter Huewe } 142aad628c1SPeter Huewe 143aad628c1SPeter Huewe if (rc <= 0) 144aad628c1SPeter Huewe goto out; 145aad628c1SPeter Huewe 146c61c86ddSPeter Huewe /* After the TPM has successfully received the register address 147c61c86ddSPeter Huewe * it needs some time, thus we're sleeping here again, before 148c61c86ddSPeter Huewe * retrieving the data 149aad628c1SPeter Huewe */ 150aad628c1SPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 151aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 152aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1); 153aad628c1SPeter Huewe if (rc > 0) 154aad628c1SPeter Huewe break; 155c61c86ddSPeter Huewe } 156aad628c1SPeter Huewe } 157aad628c1SPeter Huewe 158aad628c1SPeter Huewe out: 159aad628c1SPeter Huewe i2c_unlock_adapter(tpm_dev.client->adapter); 160c61c86ddSPeter Huewe /* take care of 'guard time' */ 161c61c86ddSPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 162c61c86ddSPeter Huewe 1636aa4ef4dSPeter Huewe /* __i2c_transfer returns the number of successfully transferred 1646aa4ef4dSPeter Huewe * messages. 1656aa4ef4dSPeter Huewe * So rc should be greater than 0 here otherwise we have an error. 1666aa4ef4dSPeter Huewe */ 167aad628c1SPeter Huewe if (rc <= 0) 168aad628c1SPeter Huewe return -EIO; 169aad628c1SPeter Huewe 170aad628c1SPeter Huewe return 0; 171aad628c1SPeter Huewe } 172aad628c1SPeter Huewe 173aad628c1SPeter Huewe static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, 174aad628c1SPeter Huewe unsigned int sleep_low, 175aad628c1SPeter Huewe unsigned int sleep_hi, u8 max_count) 176aad628c1SPeter Huewe { 177aad628c1SPeter Huewe int rc = -EIO; 178aad628c1SPeter Huewe int count; 179aad628c1SPeter Huewe 180eef8b629SShubhrajyoti Datta struct i2c_msg msg1 = { 181eef8b629SShubhrajyoti Datta .addr = tpm_dev.client->addr, 182eef8b629SShubhrajyoti Datta .len = len + 1, 183eef8b629SShubhrajyoti Datta .buf = tpm_dev.buf 184eef8b629SShubhrajyoti Datta }; 185aad628c1SPeter Huewe 186aad628c1SPeter Huewe if (len > TPM_BUFSIZE) 187aad628c1SPeter Huewe return -EINVAL; 188aad628c1SPeter Huewe 189aad628c1SPeter Huewe if (!tpm_dev.client->adapter->algo->master_xfer) 190aad628c1SPeter Huewe return -EOPNOTSUPP; 191aad628c1SPeter Huewe i2c_lock_adapter(tpm_dev.client->adapter); 192aad628c1SPeter Huewe 193aad628c1SPeter Huewe /* prepend the 'register address' to the buffer */ 194aad628c1SPeter Huewe tpm_dev.buf[0] = addr; 195aad628c1SPeter Huewe memcpy(&(tpm_dev.buf[1]), buffer, len); 196aad628c1SPeter Huewe 197aad628c1SPeter Huewe /* 198aad628c1SPeter Huewe * NOTE: We have to use these special mechanisms here and unfortunately 199aad628c1SPeter Huewe * cannot rely on the standard behavior of i2c_transfer. 200c61c86ddSPeter Huewe * Even for newer chips the smbus functions are not 201c61c86ddSPeter Huewe * suitable due to the 32 byte limit of the smbus. 202aad628c1SPeter Huewe */ 203aad628c1SPeter Huewe for (count = 0; count < max_count; count++) { 204aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); 205aad628c1SPeter Huewe if (rc > 0) 206aad628c1SPeter Huewe break; 207aad628c1SPeter Huewe usleep_range(sleep_low, sleep_hi); 208aad628c1SPeter Huewe } 209aad628c1SPeter Huewe 210aad628c1SPeter Huewe i2c_unlock_adapter(tpm_dev.client->adapter); 211c61c86ddSPeter Huewe /* take care of 'guard time' */ 212c61c86ddSPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 2136aa4ef4dSPeter Huewe 2146aa4ef4dSPeter Huewe /* __i2c_transfer returns the number of successfully transferred 2156aa4ef4dSPeter Huewe * messages. 2166aa4ef4dSPeter Huewe * So rc should be greater than 0 here otherwise we have an error. 2176aa4ef4dSPeter Huewe */ 218aad628c1SPeter Huewe if (rc <= 0) 219aad628c1SPeter Huewe return -EIO; 220aad628c1SPeter Huewe 221aad628c1SPeter Huewe return 0; 222aad628c1SPeter Huewe } 223aad628c1SPeter Huewe 224aad628c1SPeter Huewe /* 225aad628c1SPeter Huewe * iic_tpm_write() - write to TPM register 226aad628c1SPeter Huewe * @addr: register address to write to 227aad628c1SPeter Huewe * @buffer: containing data to be written 228aad628c1SPeter Huewe * @len: number of bytes to write 229aad628c1SPeter Huewe * 230aad628c1SPeter Huewe * Write len bytes from provided buffer to TPM register (little 231aad628c1SPeter Huewe * endian format, i.e. buffer[0] is written as first byte). 232aad628c1SPeter Huewe * 233aad628c1SPeter Huewe * NOTE: TPM is big-endian for multi-byte values. Multi-byte 234aad628c1SPeter Huewe * values have to be swapped. 235aad628c1SPeter Huewe * 236aad628c1SPeter Huewe * NOTE: use this function instead of the iic_tpm_write_generic function. 237aad628c1SPeter Huewe * 238aad628c1SPeter Huewe * Return -EIO on error, 0 on success 239aad628c1SPeter Huewe */ 240aad628c1SPeter Huewe static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) 241aad628c1SPeter Huewe { 242aad628c1SPeter Huewe return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW, 243aad628c1SPeter Huewe SLEEP_DURATION_HI, MAX_COUNT); 244aad628c1SPeter Huewe } 245aad628c1SPeter Huewe 246aad628c1SPeter Huewe /* 247aad628c1SPeter Huewe * This function is needed especially for the cleanup situation after 248aad628c1SPeter Huewe * sending TPM_READY 249aad628c1SPeter Huewe * */ 250aad628c1SPeter Huewe static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) 251aad628c1SPeter Huewe { 252aad628c1SPeter Huewe return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW, 253aad628c1SPeter Huewe SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG); 254aad628c1SPeter Huewe } 255aad628c1SPeter Huewe 256aad628c1SPeter Huewe enum tis_access { 257aad628c1SPeter Huewe TPM_ACCESS_VALID = 0x80, 258aad628c1SPeter Huewe TPM_ACCESS_ACTIVE_LOCALITY = 0x20, 259aad628c1SPeter Huewe TPM_ACCESS_REQUEST_PENDING = 0x04, 260aad628c1SPeter Huewe TPM_ACCESS_REQUEST_USE = 0x02, 261aad628c1SPeter Huewe }; 262aad628c1SPeter Huewe 263aad628c1SPeter Huewe enum tis_status { 264aad628c1SPeter Huewe TPM_STS_VALID = 0x80, 265aad628c1SPeter Huewe TPM_STS_COMMAND_READY = 0x40, 266aad628c1SPeter Huewe TPM_STS_GO = 0x20, 267aad628c1SPeter Huewe TPM_STS_DATA_AVAIL = 0x10, 268aad628c1SPeter Huewe TPM_STS_DATA_EXPECT = 0x08, 269aad628c1SPeter Huewe }; 270aad628c1SPeter Huewe 271aad628c1SPeter Huewe enum tis_defaults { 272aad628c1SPeter Huewe TIS_SHORT_TIMEOUT = 750, /* ms */ 273aad628c1SPeter Huewe TIS_LONG_TIMEOUT = 2000, /* 2 sec */ 274aad628c1SPeter Huewe }; 275aad628c1SPeter Huewe 276aad628c1SPeter Huewe #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) 277aad628c1SPeter Huewe #define TPM_STS(l) (0x0001 | ((l) << 4)) 278aad628c1SPeter Huewe #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) 279aad628c1SPeter Huewe #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) 280aad628c1SPeter Huewe 281aad628c1SPeter Huewe static int check_locality(struct tpm_chip *chip, int loc) 282aad628c1SPeter Huewe { 283aad628c1SPeter Huewe u8 buf; 284aad628c1SPeter Huewe int rc; 285aad628c1SPeter Huewe 286aad628c1SPeter Huewe rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); 287aad628c1SPeter Huewe if (rc < 0) 288aad628c1SPeter Huewe return rc; 289aad628c1SPeter Huewe 290aad628c1SPeter Huewe if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == 291aad628c1SPeter Huewe (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { 292*56671c89SChristophe Ricard tpm_dev.locality = loc; 293aad628c1SPeter Huewe return loc; 294aad628c1SPeter Huewe } 295aad628c1SPeter Huewe 296aad628c1SPeter Huewe return -EIO; 297aad628c1SPeter Huewe } 298aad628c1SPeter Huewe 299aad628c1SPeter Huewe /* implementation similar to tpm_tis */ 300aad628c1SPeter Huewe static void release_locality(struct tpm_chip *chip, int loc, int force) 301aad628c1SPeter Huewe { 302aad628c1SPeter Huewe u8 buf; 303aad628c1SPeter Huewe if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) 304aad628c1SPeter Huewe return; 305aad628c1SPeter Huewe 306aad628c1SPeter Huewe if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == 307aad628c1SPeter Huewe (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { 308aad628c1SPeter Huewe buf = TPM_ACCESS_ACTIVE_LOCALITY; 309aad628c1SPeter Huewe iic_tpm_write(TPM_ACCESS(loc), &buf, 1); 310aad628c1SPeter Huewe } 311aad628c1SPeter Huewe } 312aad628c1SPeter Huewe 313aad628c1SPeter Huewe static int request_locality(struct tpm_chip *chip, int loc) 314aad628c1SPeter Huewe { 315aad628c1SPeter Huewe unsigned long stop; 316aad628c1SPeter Huewe u8 buf = TPM_ACCESS_REQUEST_USE; 317aad628c1SPeter Huewe 318aad628c1SPeter Huewe if (check_locality(chip, loc) >= 0) 319aad628c1SPeter Huewe return loc; 320aad628c1SPeter Huewe 321aad628c1SPeter Huewe iic_tpm_write(TPM_ACCESS(loc), &buf, 1); 322aad628c1SPeter Huewe 323aad628c1SPeter Huewe /* wait for burstcount */ 324aad628c1SPeter Huewe stop = jiffies + chip->vendor.timeout_a; 325aad628c1SPeter Huewe do { 326aad628c1SPeter Huewe if (check_locality(chip, loc) >= 0) 327aad628c1SPeter Huewe return loc; 328aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 329aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 330aad628c1SPeter Huewe 331aad628c1SPeter Huewe return -ETIME; 332aad628c1SPeter Huewe } 333aad628c1SPeter Huewe 334aad628c1SPeter Huewe static u8 tpm_tis_i2c_status(struct tpm_chip *chip) 335aad628c1SPeter Huewe { 336aad628c1SPeter Huewe /* NOTE: since I2C read may fail, return 0 in this case --> time-out */ 337c61c86ddSPeter Huewe u8 buf = 0xFF; 338c61c86ddSPeter Huewe u8 i = 0; 339c61c86ddSPeter Huewe 340c61c86ddSPeter Huewe do { 341*56671c89SChristophe Ricard if (iic_tpm_read(TPM_STS(tpm_dev.locality), &buf, 1) < 0) 342aad628c1SPeter Huewe return 0; 343c61c86ddSPeter Huewe 344c61c86ddSPeter Huewe i++; 345c61c86ddSPeter Huewe /* if locallity is set STS should not be 0xFF */ 346c61c86ddSPeter Huewe } while ((buf == 0xFF) && i < 10); 347c61c86ddSPeter Huewe 348aad628c1SPeter Huewe return buf; 349aad628c1SPeter Huewe } 350aad628c1SPeter Huewe 351aad628c1SPeter Huewe static void tpm_tis_i2c_ready(struct tpm_chip *chip) 352aad628c1SPeter Huewe { 353aad628c1SPeter Huewe /* this causes the current command to be aborted */ 354aad628c1SPeter Huewe u8 buf = TPM_STS_COMMAND_READY; 355*56671c89SChristophe Ricard iic_tpm_write_long(TPM_STS(tpm_dev.locality), &buf, 1); 356aad628c1SPeter Huewe } 357aad628c1SPeter Huewe 358aad628c1SPeter Huewe static ssize_t get_burstcount(struct tpm_chip *chip) 359aad628c1SPeter Huewe { 360aad628c1SPeter Huewe unsigned long stop; 361aad628c1SPeter Huewe ssize_t burstcnt; 362aad628c1SPeter Huewe u8 buf[3]; 363aad628c1SPeter Huewe 364aad628c1SPeter Huewe /* wait for burstcount */ 365aad628c1SPeter Huewe /* which timeout value, spec has 2 answers (c & d) */ 366aad628c1SPeter Huewe stop = jiffies + chip->vendor.timeout_d; 367aad628c1SPeter Huewe do { 368aad628c1SPeter Huewe /* Note: STS is little endian */ 369*56671c89SChristophe Ricard if (iic_tpm_read(TPM_STS(tpm_dev.locality)+1, buf, 3) < 0) 370aad628c1SPeter Huewe burstcnt = 0; 371aad628c1SPeter Huewe else 372aad628c1SPeter Huewe burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; 373aad628c1SPeter Huewe 374aad628c1SPeter Huewe if (burstcnt) 375aad628c1SPeter Huewe return burstcnt; 376aad628c1SPeter Huewe 377aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 378aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 379aad628c1SPeter Huewe return -EBUSY; 380aad628c1SPeter Huewe } 381aad628c1SPeter Huewe 382aad628c1SPeter Huewe static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 383aad628c1SPeter Huewe int *status) 384aad628c1SPeter Huewe { 385aad628c1SPeter Huewe unsigned long stop; 386aad628c1SPeter Huewe 387aad628c1SPeter Huewe /* check current status */ 388aad628c1SPeter Huewe *status = tpm_tis_i2c_status(chip); 389c61c86ddSPeter Huewe if ((*status != 0xFF) && (*status & mask) == mask) 390aad628c1SPeter Huewe return 0; 391aad628c1SPeter Huewe 392aad628c1SPeter Huewe stop = jiffies + timeout; 393aad628c1SPeter Huewe do { 394aad628c1SPeter Huewe /* since we just checked the status, give the TPM some time */ 395aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 396aad628c1SPeter Huewe *status = tpm_tis_i2c_status(chip); 397aad628c1SPeter Huewe if ((*status & mask) == mask) 398aad628c1SPeter Huewe return 0; 399aad628c1SPeter Huewe 400aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 401aad628c1SPeter Huewe 402aad628c1SPeter Huewe return -ETIME; 403aad628c1SPeter Huewe } 404aad628c1SPeter Huewe 405aad628c1SPeter Huewe static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) 406aad628c1SPeter Huewe { 407aad628c1SPeter Huewe size_t size = 0; 408aad628c1SPeter Huewe ssize_t burstcnt; 409aad628c1SPeter Huewe u8 retries = 0; 410aad628c1SPeter Huewe int rc; 411aad628c1SPeter Huewe 412aad628c1SPeter Huewe while (size < count) { 413aad628c1SPeter Huewe burstcnt = get_burstcount(chip); 414aad628c1SPeter Huewe 415aad628c1SPeter Huewe /* burstcnt < 0 = TPM is busy */ 416aad628c1SPeter Huewe if (burstcnt < 0) 417aad628c1SPeter Huewe return burstcnt; 418aad628c1SPeter Huewe 419aad628c1SPeter Huewe /* limit received data to max. left */ 420aad628c1SPeter Huewe if (burstcnt > (count - size)) 421aad628c1SPeter Huewe burstcnt = count - size; 422aad628c1SPeter Huewe 423*56671c89SChristophe Ricard rc = iic_tpm_read(TPM_DATA_FIFO(tpm_dev.locality), 424aad628c1SPeter Huewe &(buf[size]), burstcnt); 425aad628c1SPeter Huewe if (rc == 0) 426aad628c1SPeter Huewe size += burstcnt; 427aad628c1SPeter Huewe else if (rc < 0) 428aad628c1SPeter Huewe retries++; 429aad628c1SPeter Huewe 430aad628c1SPeter Huewe /* avoid endless loop in case of broken HW */ 431aad628c1SPeter Huewe if (retries > MAX_COUNT_LONG) 432aad628c1SPeter Huewe return -EIO; 433aad628c1SPeter Huewe } 434aad628c1SPeter Huewe return size; 435aad628c1SPeter Huewe } 436aad628c1SPeter Huewe 437aad628c1SPeter Huewe static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) 438aad628c1SPeter Huewe { 439aad628c1SPeter Huewe int size = 0; 440aad628c1SPeter Huewe int expected, status; 441aad628c1SPeter Huewe 442aad628c1SPeter Huewe if (count < TPM_HEADER_SIZE) { 443aad628c1SPeter Huewe size = -EIO; 444aad628c1SPeter Huewe goto out; 445aad628c1SPeter Huewe } 446aad628c1SPeter Huewe 447aad628c1SPeter Huewe /* read first 10 bytes, including tag, paramsize, and result */ 448aad628c1SPeter Huewe size = recv_data(chip, buf, TPM_HEADER_SIZE); 449aad628c1SPeter Huewe if (size < TPM_HEADER_SIZE) { 4508cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Unable to read header\n"); 451aad628c1SPeter Huewe goto out; 452aad628c1SPeter Huewe } 453aad628c1SPeter Huewe 454aad628c1SPeter Huewe expected = be32_to_cpu(*(__be32 *)(buf + 2)); 455aad628c1SPeter Huewe if ((size_t) expected > count) { 456aad628c1SPeter Huewe size = -EIO; 457aad628c1SPeter Huewe goto out; 458aad628c1SPeter Huewe } 459aad628c1SPeter Huewe 460aad628c1SPeter Huewe size += recv_data(chip, &buf[TPM_HEADER_SIZE], 461aad628c1SPeter Huewe expected - TPM_HEADER_SIZE); 462aad628c1SPeter Huewe if (size < expected) { 4638cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Unable to read remainder of result\n"); 464aad628c1SPeter Huewe size = -ETIME; 465aad628c1SPeter Huewe goto out; 466aad628c1SPeter Huewe } 467aad628c1SPeter Huewe 468aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); 469aad628c1SPeter Huewe if (status & TPM_STS_DATA_AVAIL) { /* retry? */ 4708cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Error left over data\n"); 471aad628c1SPeter Huewe size = -EIO; 472aad628c1SPeter Huewe goto out; 473aad628c1SPeter Huewe } 474aad628c1SPeter Huewe 475aad628c1SPeter Huewe out: 476aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 477aad628c1SPeter Huewe /* The TPM needs some time to clean up here, 478aad628c1SPeter Huewe * so we sleep rather than keeping the bus busy 479aad628c1SPeter Huewe */ 480aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); 481*56671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 0); 482aad628c1SPeter Huewe return size; 483aad628c1SPeter Huewe } 484aad628c1SPeter Huewe 485aad628c1SPeter Huewe static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) 486aad628c1SPeter Huewe { 487aad628c1SPeter Huewe int rc, status; 488aad628c1SPeter Huewe ssize_t burstcnt; 489aad628c1SPeter Huewe size_t count = 0; 490aad628c1SPeter Huewe u8 retries = 0; 491aad628c1SPeter Huewe u8 sts = TPM_STS_GO; 492aad628c1SPeter Huewe 493aad628c1SPeter Huewe if (len > TPM_BUFSIZE) 494aad628c1SPeter Huewe return -E2BIG; /* command is too long for our tpm, sorry */ 495aad628c1SPeter Huewe 496aad628c1SPeter Huewe if (request_locality(chip, 0) < 0) 497aad628c1SPeter Huewe return -EBUSY; 498aad628c1SPeter Huewe 499aad628c1SPeter Huewe status = tpm_tis_i2c_status(chip); 500aad628c1SPeter Huewe if ((status & TPM_STS_COMMAND_READY) == 0) { 501aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 502aad628c1SPeter Huewe if (wait_for_stat 503aad628c1SPeter Huewe (chip, TPM_STS_COMMAND_READY, 504aad628c1SPeter Huewe chip->vendor.timeout_b, &status) < 0) { 505aad628c1SPeter Huewe rc = -ETIME; 506aad628c1SPeter Huewe goto out_err; 507aad628c1SPeter Huewe } 508aad628c1SPeter Huewe } 509aad628c1SPeter Huewe 510aad628c1SPeter Huewe while (count < len - 1) { 511aad628c1SPeter Huewe burstcnt = get_burstcount(chip); 512aad628c1SPeter Huewe 513aad628c1SPeter Huewe /* burstcnt < 0 = TPM is busy */ 514aad628c1SPeter Huewe if (burstcnt < 0) 515aad628c1SPeter Huewe return burstcnt; 516aad628c1SPeter Huewe 517aad628c1SPeter Huewe if (burstcnt > (len - 1 - count)) 518aad628c1SPeter Huewe burstcnt = len - 1 - count; 519aad628c1SPeter Huewe 520*56671c89SChristophe Ricard rc = iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), 521aad628c1SPeter Huewe &(buf[count]), burstcnt); 522aad628c1SPeter Huewe if (rc == 0) 523aad628c1SPeter Huewe count += burstcnt; 524aad628c1SPeter Huewe else if (rc < 0) 525aad628c1SPeter Huewe retries++; 526aad628c1SPeter Huewe 527aad628c1SPeter Huewe /* avoid endless loop in case of broken HW */ 528aad628c1SPeter Huewe if (retries > MAX_COUNT_LONG) { 529aad628c1SPeter Huewe rc = -EIO; 530aad628c1SPeter Huewe goto out_err; 531aad628c1SPeter Huewe } 532aad628c1SPeter Huewe 533aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, 534aad628c1SPeter Huewe chip->vendor.timeout_c, &status); 535aad628c1SPeter Huewe 536aad628c1SPeter Huewe if ((status & TPM_STS_DATA_EXPECT) == 0) { 537aad628c1SPeter Huewe rc = -EIO; 538aad628c1SPeter Huewe goto out_err; 539aad628c1SPeter Huewe } 540aad628c1SPeter Huewe } 541aad628c1SPeter Huewe 542aad628c1SPeter Huewe /* write last byte */ 543*56671c89SChristophe Ricard iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), &(buf[count]), 1); 544aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); 545aad628c1SPeter Huewe if ((status & TPM_STS_DATA_EXPECT) != 0) { 546aad628c1SPeter Huewe rc = -EIO; 547aad628c1SPeter Huewe goto out_err; 548aad628c1SPeter Huewe } 549aad628c1SPeter Huewe 550aad628c1SPeter Huewe /* go and do it */ 551*56671c89SChristophe Ricard iic_tpm_write(TPM_STS(tpm_dev.locality), &sts, 1); 552aad628c1SPeter Huewe 553aad628c1SPeter Huewe return len; 554aad628c1SPeter Huewe out_err: 555aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 556aad628c1SPeter Huewe /* The TPM needs some time to clean up here, 557aad628c1SPeter Huewe * so we sleep rather than keeping the bus busy 558aad628c1SPeter Huewe */ 559aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); 560*56671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 0); 561aad628c1SPeter Huewe return rc; 562aad628c1SPeter Huewe } 563aad628c1SPeter Huewe 5641f866057SStefan Berger static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status) 5651f866057SStefan Berger { 5661f866057SStefan Berger return (status == TPM_STS_COMMAND_READY); 5671f866057SStefan Berger } 5681f866057SStefan Berger 56901ad1fa7SJason Gunthorpe static const struct tpm_class_ops tpm_tis_i2c = { 570aad628c1SPeter Huewe .status = tpm_tis_i2c_status, 571aad628c1SPeter Huewe .recv = tpm_tis_i2c_recv, 572aad628c1SPeter Huewe .send = tpm_tis_i2c_send, 573aad628c1SPeter Huewe .cancel = tpm_tis_i2c_ready, 574aad628c1SPeter Huewe .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 575aad628c1SPeter Huewe .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 5761f866057SStefan Berger .req_canceled = tpm_tis_i2c_req_canceled, 577aad628c1SPeter Huewe }; 578aad628c1SPeter Huewe 579afc6d369SBill Pemberton static int tpm_tis_i2c_init(struct device *dev) 580aad628c1SPeter Huewe { 581aad628c1SPeter Huewe u32 vendor; 582aad628c1SPeter Huewe int rc = 0; 583aad628c1SPeter Huewe struct tpm_chip *chip; 584aad628c1SPeter Huewe 585afb5abc2SJarkko Sakkinen chip = tpmm_chip_alloc(dev, &tpm_tis_i2c); 586afb5abc2SJarkko Sakkinen if (IS_ERR(chip)) 587afb5abc2SJarkko Sakkinen return PTR_ERR(chip); 588aad628c1SPeter Huewe 589aad628c1SPeter Huewe /* Default timeouts */ 590aad628c1SPeter Huewe chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 591aad628c1SPeter Huewe chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); 592aad628c1SPeter Huewe chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 593aad628c1SPeter Huewe chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 594aad628c1SPeter Huewe 595aad628c1SPeter Huewe if (request_locality(chip, 0) != 0) { 596c61c86ddSPeter Huewe dev_err(dev, "could not request locality\n"); 597aad628c1SPeter Huewe rc = -ENODEV; 598afb5abc2SJarkko Sakkinen goto out_err; 599aad628c1SPeter Huewe } 600aad628c1SPeter Huewe 601aad628c1SPeter Huewe /* read four bytes from DID_VID register */ 602aad628c1SPeter Huewe if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) { 603c61c86ddSPeter Huewe dev_err(dev, "could not read vendor id\n"); 604aad628c1SPeter Huewe rc = -EIO; 605aad628c1SPeter Huewe goto out_release; 606aad628c1SPeter Huewe } 607aad628c1SPeter Huewe 608c61c86ddSPeter Huewe if (vendor == TPM_TIS_I2C_DID_VID_9645) { 609c61c86ddSPeter Huewe tpm_dev.chip_type = SLB9645; 610c61c86ddSPeter Huewe } else if (vendor == TPM_TIS_I2C_DID_VID_9635) { 611c61c86ddSPeter Huewe tpm_dev.chip_type = SLB9635; 612c61c86ddSPeter Huewe } else { 613c61c86ddSPeter Huewe dev_err(dev, "vendor id did not match! ID was %08x\n", vendor); 614aad628c1SPeter Huewe rc = -ENODEV; 615aad628c1SPeter Huewe goto out_release; 616aad628c1SPeter Huewe } 617aad628c1SPeter Huewe 618aad628c1SPeter Huewe dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); 619aad628c1SPeter Huewe 620aad628c1SPeter Huewe tpm_dev.chip = chip; 621aad628c1SPeter Huewe 622aad628c1SPeter Huewe tpm_get_timeouts(chip); 623aad628c1SPeter Huewe tpm_do_selftest(chip); 624aad628c1SPeter Huewe 625afb5abc2SJarkko Sakkinen return tpm_chip_register(chip); 626aad628c1SPeter Huewe out_release: 627*56671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 1); 628aad628c1SPeter Huewe tpm_dev.client = NULL; 629aad628c1SPeter Huewe out_err: 630aad628c1SPeter Huewe return rc; 631aad628c1SPeter Huewe } 632aad628c1SPeter Huewe 633aad628c1SPeter Huewe static const struct i2c_device_id tpm_tis_i2c_table[] = { 634aad628c1SPeter Huewe {"tpm_i2c_infineon", 0}, 635c61c86ddSPeter Huewe {"slb9635tt", 0}, 636c61c86ddSPeter Huewe {"slb9645tt", 1}, 637aad628c1SPeter Huewe {}, 638aad628c1SPeter Huewe }; 639aad628c1SPeter Huewe 640aad628c1SPeter Huewe MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table); 641c61c86ddSPeter Huewe 642c61c86ddSPeter Huewe #ifdef CONFIG_OF 643c61c86ddSPeter Huewe static const struct of_device_id tpm_tis_i2c_of_match[] = { 64421dc02eaSPeter Huewe { 64521dc02eaSPeter Huewe .name = "tpm_i2c_infineon", 64621dc02eaSPeter Huewe .type = "tpm", 64721dc02eaSPeter Huewe .compatible = "infineon,tpm_i2c_infineon", 64821dc02eaSPeter Huewe .data = (void *)0 64921dc02eaSPeter Huewe }, 65021dc02eaSPeter Huewe { 65121dc02eaSPeter Huewe .name = "slb9635tt", 65221dc02eaSPeter Huewe .type = "tpm", 65321dc02eaSPeter Huewe .compatible = "infineon,slb9635tt", 65421dc02eaSPeter Huewe .data = (void *)0 65521dc02eaSPeter Huewe }, 65621dc02eaSPeter Huewe { 65721dc02eaSPeter Huewe .name = "slb9645tt", 65821dc02eaSPeter Huewe .type = "tpm", 65921dc02eaSPeter Huewe .compatible = "infineon,slb9645tt", 66021dc02eaSPeter Huewe .data = (void *)1 66121dc02eaSPeter Huewe }, 662c61c86ddSPeter Huewe {}, 663c61c86ddSPeter Huewe }; 664c61c86ddSPeter Huewe MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match); 665c61c86ddSPeter Huewe #endif 666c61c86ddSPeter Huewe 667aad628c1SPeter Huewe static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume); 668aad628c1SPeter Huewe 669afc6d369SBill Pemberton static int tpm_tis_i2c_probe(struct i2c_client *client, 670aad628c1SPeter Huewe const struct i2c_device_id *id) 671aad628c1SPeter Huewe { 672aad628c1SPeter Huewe int rc; 673c61c86ddSPeter Huewe struct device *dev = &(client->dev); 674c61c86ddSPeter Huewe 675c61c86ddSPeter Huewe if (tpm_dev.client != NULL) { 676c61c86ddSPeter Huewe dev_err(dev, "This driver only supports one client at a time\n"); 677aad628c1SPeter Huewe return -EBUSY; /* We only support one client */ 678c61c86ddSPeter Huewe } 679aad628c1SPeter Huewe 680aad628c1SPeter Huewe if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 681c61c86ddSPeter Huewe dev_err(dev, "no algorithms associated to the i2c bus\n"); 682aad628c1SPeter Huewe return -ENODEV; 683aad628c1SPeter Huewe } 684aad628c1SPeter Huewe 685aad628c1SPeter Huewe tpm_dev.client = client; 686aad628c1SPeter Huewe rc = tpm_tis_i2c_init(&client->dev); 687aad628c1SPeter Huewe if (rc != 0) { 688aad628c1SPeter Huewe tpm_dev.client = NULL; 689aad628c1SPeter Huewe rc = -ENODEV; 690aad628c1SPeter Huewe } 691aad628c1SPeter Huewe return rc; 692aad628c1SPeter Huewe } 693aad628c1SPeter Huewe 69439af33fcSBill Pemberton static int tpm_tis_i2c_remove(struct i2c_client *client) 695aad628c1SPeter Huewe { 696aad628c1SPeter Huewe struct tpm_chip *chip = tpm_dev.chip; 697afb5abc2SJarkko Sakkinen 698afb5abc2SJarkko Sakkinen tpm_chip_unregister(chip); 699*56671c89SChristophe Ricard release_locality(chip, tpm_dev.locality, 1); 700aad628c1SPeter Huewe tpm_dev.client = NULL; 701aad628c1SPeter Huewe 702aad628c1SPeter Huewe return 0; 703aad628c1SPeter Huewe } 704aad628c1SPeter Huewe 705aad628c1SPeter Huewe static struct i2c_driver tpm_tis_i2c_driver = { 706aad628c1SPeter Huewe .id_table = tpm_tis_i2c_table, 707aad628c1SPeter Huewe .probe = tpm_tis_i2c_probe, 708aad628c1SPeter Huewe .remove = tpm_tis_i2c_remove, 709aad628c1SPeter Huewe .driver = { 710aad628c1SPeter Huewe .name = "tpm_i2c_infineon", 711aad628c1SPeter Huewe .pm = &tpm_tis_i2c_ops, 712c61c86ddSPeter Huewe .of_match_table = of_match_ptr(tpm_tis_i2c_of_match), 713aad628c1SPeter Huewe }, 714aad628c1SPeter Huewe }; 715aad628c1SPeter Huewe 716aad628c1SPeter Huewe module_i2c_driver(tpm_tis_i2c_driver); 717aad628c1SPeter Huewe MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>"); 718aad628c1SPeter Huewe MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver"); 719c61c86ddSPeter Huewe MODULE_VERSION("2.2.0"); 720aad628c1SPeter Huewe MODULE_LICENSE("GPL"); 721