1*aad628c1SPeter Huewe /* 2*aad628c1SPeter Huewe * Copyright (C) 2012 Infineon Technologies 3*aad628c1SPeter Huewe * 4*aad628c1SPeter Huewe * Authors: 5*aad628c1SPeter Huewe * Peter Huewe <peter.huewe@infineon.com> 6*aad628c1SPeter Huewe * 7*aad628c1SPeter Huewe * Device driver for TCG/TCPA TPM (trusted platform module). 8*aad628c1SPeter Huewe * Specifications at www.trustedcomputinggroup.org 9*aad628c1SPeter Huewe * 10*aad628c1SPeter Huewe * This device driver implements the TPM interface as defined in 11*aad628c1SPeter Huewe * the TCG TPM Interface Spec version 1.2, revision 1.0 and the 12*aad628c1SPeter Huewe * Infineon I2C Protocol Stack Specification v0.20. 13*aad628c1SPeter Huewe * 14*aad628c1SPeter Huewe * It is based on the original tpm_tis device driver from Leendert van 15*aad628c1SPeter Huewe * Dorn and Kyleen Hall. 16*aad628c1SPeter Huewe * 17*aad628c1SPeter Huewe * This program is free software; you can redistribute it and/or 18*aad628c1SPeter Huewe * modify it under the terms of the GNU General Public License as 19*aad628c1SPeter Huewe * published by the Free Software Foundation, version 2 of the 20*aad628c1SPeter Huewe * License. 21*aad628c1SPeter Huewe * 22*aad628c1SPeter Huewe * 23*aad628c1SPeter Huewe */ 24*aad628c1SPeter Huewe #include <linux/init.h> 25*aad628c1SPeter Huewe #include <linux/i2c.h> 26*aad628c1SPeter Huewe #include <linux/module.h> 27*aad628c1SPeter Huewe #include <linux/moduleparam.h> 28*aad628c1SPeter Huewe #include <linux/wait.h> 29*aad628c1SPeter Huewe #include "tpm.h" 30*aad628c1SPeter Huewe 31*aad628c1SPeter Huewe /* max. buffer size supported by our TPM */ 32*aad628c1SPeter Huewe #define TPM_BUFSIZE 1260 33*aad628c1SPeter Huewe 34*aad628c1SPeter Huewe /* max. number of iterations after I2C NAK */ 35*aad628c1SPeter Huewe #define MAX_COUNT 3 36*aad628c1SPeter Huewe 37*aad628c1SPeter Huewe #define SLEEP_DURATION_LOW 55 38*aad628c1SPeter Huewe #define SLEEP_DURATION_HI 65 39*aad628c1SPeter Huewe 40*aad628c1SPeter Huewe /* max. number of iterations after I2C NAK for 'long' commands 41*aad628c1SPeter Huewe * we need this especially for sending TPM_READY, since the cleanup after the 42*aad628c1SPeter Huewe * transtion to the ready state may take some time, but it is unpredictable 43*aad628c1SPeter Huewe * how long it will take. 44*aad628c1SPeter Huewe */ 45*aad628c1SPeter Huewe #define MAX_COUNT_LONG 50 46*aad628c1SPeter Huewe 47*aad628c1SPeter Huewe #define SLEEP_DURATION_LONG_LOW 200 48*aad628c1SPeter Huewe #define SLEEP_DURATION_LONG_HI 220 49*aad628c1SPeter Huewe 50*aad628c1SPeter Huewe /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */ 51*aad628c1SPeter Huewe #define SLEEP_DURATION_RESET_LOW 2400 52*aad628c1SPeter Huewe #define SLEEP_DURATION_RESET_HI 2600 53*aad628c1SPeter Huewe 54*aad628c1SPeter Huewe /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */ 55*aad628c1SPeter Huewe #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000) 56*aad628c1SPeter Huewe #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000) 57*aad628c1SPeter Huewe 58*aad628c1SPeter Huewe /* expected value for DIDVID register */ 59*aad628c1SPeter Huewe #define TPM_TIS_I2C_DID_VID 0x000b15d1L 60*aad628c1SPeter Huewe 61*aad628c1SPeter Huewe /* Structure to store I2C TPM specific stuff */ 62*aad628c1SPeter Huewe struct tpm_inf_dev { 63*aad628c1SPeter Huewe struct i2c_client *client; 64*aad628c1SPeter Huewe u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */ 65*aad628c1SPeter Huewe struct tpm_chip *chip; 66*aad628c1SPeter Huewe }; 67*aad628c1SPeter Huewe 68*aad628c1SPeter Huewe static struct tpm_inf_dev tpm_dev; 69*aad628c1SPeter Huewe static struct i2c_driver tpm_tis_i2c_driver; 70*aad628c1SPeter Huewe 71*aad628c1SPeter Huewe /* 72*aad628c1SPeter Huewe * iic_tpm_read() - read from TPM register 73*aad628c1SPeter Huewe * @addr: register address to read from 74*aad628c1SPeter Huewe * @buffer: provided by caller 75*aad628c1SPeter Huewe * @len: number of bytes to read 76*aad628c1SPeter Huewe * 77*aad628c1SPeter Huewe * Read len bytes from TPM register and put them into 78*aad628c1SPeter Huewe * buffer (little-endian format, i.e. first byte is put into buffer[0]). 79*aad628c1SPeter Huewe * 80*aad628c1SPeter Huewe * NOTE: TPM is big-endian for multi-byte values. Multi-byte 81*aad628c1SPeter Huewe * values have to be swapped. 82*aad628c1SPeter Huewe * 83*aad628c1SPeter Huewe * NOTE: We can't unfortunately use the combined read/write functions 84*aad628c1SPeter Huewe * provided by the i2c core as the TPM currently does not support the 85*aad628c1SPeter Huewe * repeated start condition and due to it's special requirements. 86*aad628c1SPeter Huewe * The i2c_smbus* functions do not work for this chip. 87*aad628c1SPeter Huewe * 88*aad628c1SPeter Huewe * Return -EIO on error, 0 on success. 89*aad628c1SPeter Huewe */ 90*aad628c1SPeter Huewe static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) 91*aad628c1SPeter Huewe { 92*aad628c1SPeter Huewe 93*aad628c1SPeter Huewe struct i2c_msg msg1 = { tpm_dev.client->addr, 0, 1, &addr }; 94*aad628c1SPeter Huewe struct i2c_msg msg2 = { tpm_dev.client->addr, I2C_M_RD, len, buffer }; 95*aad628c1SPeter Huewe 96*aad628c1SPeter Huewe int rc; 97*aad628c1SPeter Huewe int count; 98*aad628c1SPeter Huewe 99*aad628c1SPeter Huewe /* Lock the adapter for the duration of the whole sequence. */ 100*aad628c1SPeter Huewe if (!tpm_dev.client->adapter->algo->master_xfer) 101*aad628c1SPeter Huewe return -EOPNOTSUPP; 102*aad628c1SPeter Huewe i2c_lock_adapter(tpm_dev.client->adapter); 103*aad628c1SPeter Huewe 104*aad628c1SPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 105*aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); 106*aad628c1SPeter Huewe if (rc > 0) 107*aad628c1SPeter Huewe break; /* break here to skip sleep */ 108*aad628c1SPeter Huewe 109*aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 110*aad628c1SPeter Huewe } 111*aad628c1SPeter Huewe 112*aad628c1SPeter Huewe if (rc <= 0) 113*aad628c1SPeter Huewe goto out; 114*aad628c1SPeter Huewe 115*aad628c1SPeter Huewe /* After the TPM has successfully received the register address it needs 116*aad628c1SPeter Huewe * some time, thus we're sleeping here again, before retrieving the data 117*aad628c1SPeter Huewe */ 118*aad628c1SPeter Huewe for (count = 0; count < MAX_COUNT; count++) { 119*aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 120*aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1); 121*aad628c1SPeter Huewe if (rc > 0) 122*aad628c1SPeter Huewe break; 123*aad628c1SPeter Huewe 124*aad628c1SPeter Huewe } 125*aad628c1SPeter Huewe 126*aad628c1SPeter Huewe out: 127*aad628c1SPeter Huewe i2c_unlock_adapter(tpm_dev.client->adapter); 128*aad628c1SPeter Huewe if (rc <= 0) 129*aad628c1SPeter Huewe return -EIO; 130*aad628c1SPeter Huewe 131*aad628c1SPeter Huewe return 0; 132*aad628c1SPeter Huewe } 133*aad628c1SPeter Huewe 134*aad628c1SPeter Huewe static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, 135*aad628c1SPeter Huewe unsigned int sleep_low, 136*aad628c1SPeter Huewe unsigned int sleep_hi, u8 max_count) 137*aad628c1SPeter Huewe { 138*aad628c1SPeter Huewe int rc = -EIO; 139*aad628c1SPeter Huewe int count; 140*aad628c1SPeter Huewe 141*aad628c1SPeter Huewe struct i2c_msg msg1 = { tpm_dev.client->addr, 0, len + 1, tpm_dev.buf }; 142*aad628c1SPeter Huewe 143*aad628c1SPeter Huewe if (len > TPM_BUFSIZE) 144*aad628c1SPeter Huewe return -EINVAL; 145*aad628c1SPeter Huewe 146*aad628c1SPeter Huewe if (!tpm_dev.client->adapter->algo->master_xfer) 147*aad628c1SPeter Huewe return -EOPNOTSUPP; 148*aad628c1SPeter Huewe i2c_lock_adapter(tpm_dev.client->adapter); 149*aad628c1SPeter Huewe 150*aad628c1SPeter Huewe /* prepend the 'register address' to the buffer */ 151*aad628c1SPeter Huewe tpm_dev.buf[0] = addr; 152*aad628c1SPeter Huewe memcpy(&(tpm_dev.buf[1]), buffer, len); 153*aad628c1SPeter Huewe 154*aad628c1SPeter Huewe /* 155*aad628c1SPeter Huewe * NOTE: We have to use these special mechanisms here and unfortunately 156*aad628c1SPeter Huewe * cannot rely on the standard behavior of i2c_transfer. 157*aad628c1SPeter Huewe */ 158*aad628c1SPeter Huewe for (count = 0; count < max_count; count++) { 159*aad628c1SPeter Huewe rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); 160*aad628c1SPeter Huewe if (rc > 0) 161*aad628c1SPeter Huewe break; 162*aad628c1SPeter Huewe 163*aad628c1SPeter Huewe usleep_range(sleep_low, sleep_hi); 164*aad628c1SPeter Huewe } 165*aad628c1SPeter Huewe 166*aad628c1SPeter Huewe i2c_unlock_adapter(tpm_dev.client->adapter); 167*aad628c1SPeter Huewe if (rc <= 0) 168*aad628c1SPeter Huewe return -EIO; 169*aad628c1SPeter Huewe 170*aad628c1SPeter Huewe return 0; 171*aad628c1SPeter Huewe } 172*aad628c1SPeter Huewe 173*aad628c1SPeter Huewe /* 174*aad628c1SPeter Huewe * iic_tpm_write() - write to TPM register 175*aad628c1SPeter Huewe * @addr: register address to write to 176*aad628c1SPeter Huewe * @buffer: containing data to be written 177*aad628c1SPeter Huewe * @len: number of bytes to write 178*aad628c1SPeter Huewe * 179*aad628c1SPeter Huewe * Write len bytes from provided buffer to TPM register (little 180*aad628c1SPeter Huewe * endian format, i.e. buffer[0] is written as first byte). 181*aad628c1SPeter Huewe * 182*aad628c1SPeter Huewe * NOTE: TPM is big-endian for multi-byte values. Multi-byte 183*aad628c1SPeter Huewe * values have to be swapped. 184*aad628c1SPeter Huewe * 185*aad628c1SPeter Huewe * NOTE: use this function instead of the iic_tpm_write_generic function. 186*aad628c1SPeter Huewe * 187*aad628c1SPeter Huewe * Return -EIO on error, 0 on success 188*aad628c1SPeter Huewe */ 189*aad628c1SPeter Huewe static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) 190*aad628c1SPeter Huewe { 191*aad628c1SPeter Huewe return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW, 192*aad628c1SPeter Huewe SLEEP_DURATION_HI, MAX_COUNT); 193*aad628c1SPeter Huewe } 194*aad628c1SPeter Huewe 195*aad628c1SPeter Huewe /* 196*aad628c1SPeter Huewe * This function is needed especially for the cleanup situation after 197*aad628c1SPeter Huewe * sending TPM_READY 198*aad628c1SPeter Huewe * */ 199*aad628c1SPeter Huewe static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) 200*aad628c1SPeter Huewe { 201*aad628c1SPeter Huewe return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW, 202*aad628c1SPeter Huewe SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG); 203*aad628c1SPeter Huewe } 204*aad628c1SPeter Huewe 205*aad628c1SPeter Huewe enum tis_access { 206*aad628c1SPeter Huewe TPM_ACCESS_VALID = 0x80, 207*aad628c1SPeter Huewe TPM_ACCESS_ACTIVE_LOCALITY = 0x20, 208*aad628c1SPeter Huewe TPM_ACCESS_REQUEST_PENDING = 0x04, 209*aad628c1SPeter Huewe TPM_ACCESS_REQUEST_USE = 0x02, 210*aad628c1SPeter Huewe }; 211*aad628c1SPeter Huewe 212*aad628c1SPeter Huewe enum tis_status { 213*aad628c1SPeter Huewe TPM_STS_VALID = 0x80, 214*aad628c1SPeter Huewe TPM_STS_COMMAND_READY = 0x40, 215*aad628c1SPeter Huewe TPM_STS_GO = 0x20, 216*aad628c1SPeter Huewe TPM_STS_DATA_AVAIL = 0x10, 217*aad628c1SPeter Huewe TPM_STS_DATA_EXPECT = 0x08, 218*aad628c1SPeter Huewe }; 219*aad628c1SPeter Huewe 220*aad628c1SPeter Huewe enum tis_defaults { 221*aad628c1SPeter Huewe TIS_SHORT_TIMEOUT = 750, /* ms */ 222*aad628c1SPeter Huewe TIS_LONG_TIMEOUT = 2000, /* 2 sec */ 223*aad628c1SPeter Huewe }; 224*aad628c1SPeter Huewe 225*aad628c1SPeter Huewe #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) 226*aad628c1SPeter Huewe #define TPM_STS(l) (0x0001 | ((l) << 4)) 227*aad628c1SPeter Huewe #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) 228*aad628c1SPeter Huewe #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) 229*aad628c1SPeter Huewe 230*aad628c1SPeter Huewe static int check_locality(struct tpm_chip *chip, int loc) 231*aad628c1SPeter Huewe { 232*aad628c1SPeter Huewe u8 buf; 233*aad628c1SPeter Huewe int rc; 234*aad628c1SPeter Huewe 235*aad628c1SPeter Huewe rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); 236*aad628c1SPeter Huewe if (rc < 0) 237*aad628c1SPeter Huewe return rc; 238*aad628c1SPeter Huewe 239*aad628c1SPeter Huewe if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == 240*aad628c1SPeter Huewe (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { 241*aad628c1SPeter Huewe chip->vendor.locality = loc; 242*aad628c1SPeter Huewe return loc; 243*aad628c1SPeter Huewe } 244*aad628c1SPeter Huewe 245*aad628c1SPeter Huewe return -EIO; 246*aad628c1SPeter Huewe } 247*aad628c1SPeter Huewe 248*aad628c1SPeter Huewe /* implementation similar to tpm_tis */ 249*aad628c1SPeter Huewe static void release_locality(struct tpm_chip *chip, int loc, int force) 250*aad628c1SPeter Huewe { 251*aad628c1SPeter Huewe u8 buf; 252*aad628c1SPeter Huewe if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) 253*aad628c1SPeter Huewe return; 254*aad628c1SPeter Huewe 255*aad628c1SPeter Huewe if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == 256*aad628c1SPeter Huewe (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { 257*aad628c1SPeter Huewe buf = TPM_ACCESS_ACTIVE_LOCALITY; 258*aad628c1SPeter Huewe iic_tpm_write(TPM_ACCESS(loc), &buf, 1); 259*aad628c1SPeter Huewe } 260*aad628c1SPeter Huewe } 261*aad628c1SPeter Huewe 262*aad628c1SPeter Huewe static int request_locality(struct tpm_chip *chip, int loc) 263*aad628c1SPeter Huewe { 264*aad628c1SPeter Huewe unsigned long stop; 265*aad628c1SPeter Huewe u8 buf = TPM_ACCESS_REQUEST_USE; 266*aad628c1SPeter Huewe 267*aad628c1SPeter Huewe if (check_locality(chip, loc) >= 0) 268*aad628c1SPeter Huewe return loc; 269*aad628c1SPeter Huewe 270*aad628c1SPeter Huewe iic_tpm_write(TPM_ACCESS(loc), &buf, 1); 271*aad628c1SPeter Huewe 272*aad628c1SPeter Huewe /* wait for burstcount */ 273*aad628c1SPeter Huewe stop = jiffies + chip->vendor.timeout_a; 274*aad628c1SPeter Huewe do { 275*aad628c1SPeter Huewe if (check_locality(chip, loc) >= 0) 276*aad628c1SPeter Huewe return loc; 277*aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 278*aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 279*aad628c1SPeter Huewe 280*aad628c1SPeter Huewe return -ETIME; 281*aad628c1SPeter Huewe } 282*aad628c1SPeter Huewe 283*aad628c1SPeter Huewe static u8 tpm_tis_i2c_status(struct tpm_chip *chip) 284*aad628c1SPeter Huewe { 285*aad628c1SPeter Huewe /* NOTE: since I2C read may fail, return 0 in this case --> time-out */ 286*aad628c1SPeter Huewe u8 buf; 287*aad628c1SPeter Huewe if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0) 288*aad628c1SPeter Huewe return 0; 289*aad628c1SPeter Huewe else 290*aad628c1SPeter Huewe return buf; 291*aad628c1SPeter Huewe } 292*aad628c1SPeter Huewe 293*aad628c1SPeter Huewe static void tpm_tis_i2c_ready(struct tpm_chip *chip) 294*aad628c1SPeter Huewe { 295*aad628c1SPeter Huewe /* this causes the current command to be aborted */ 296*aad628c1SPeter Huewe u8 buf = TPM_STS_COMMAND_READY; 297*aad628c1SPeter Huewe iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1); 298*aad628c1SPeter Huewe } 299*aad628c1SPeter Huewe 300*aad628c1SPeter Huewe static ssize_t get_burstcount(struct tpm_chip *chip) 301*aad628c1SPeter Huewe { 302*aad628c1SPeter Huewe unsigned long stop; 303*aad628c1SPeter Huewe ssize_t burstcnt; 304*aad628c1SPeter Huewe u8 buf[3]; 305*aad628c1SPeter Huewe 306*aad628c1SPeter Huewe /* wait for burstcount */ 307*aad628c1SPeter Huewe /* which timeout value, spec has 2 answers (c & d) */ 308*aad628c1SPeter Huewe stop = jiffies + chip->vendor.timeout_d; 309*aad628c1SPeter Huewe do { 310*aad628c1SPeter Huewe /* Note: STS is little endian */ 311*aad628c1SPeter Huewe if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0) 312*aad628c1SPeter Huewe burstcnt = 0; 313*aad628c1SPeter Huewe else 314*aad628c1SPeter Huewe burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; 315*aad628c1SPeter Huewe 316*aad628c1SPeter Huewe if (burstcnt) 317*aad628c1SPeter Huewe return burstcnt; 318*aad628c1SPeter Huewe 319*aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 320*aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 321*aad628c1SPeter Huewe return -EBUSY; 322*aad628c1SPeter Huewe } 323*aad628c1SPeter Huewe 324*aad628c1SPeter Huewe static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 325*aad628c1SPeter Huewe int *status) 326*aad628c1SPeter Huewe { 327*aad628c1SPeter Huewe unsigned long stop; 328*aad628c1SPeter Huewe 329*aad628c1SPeter Huewe /* check current status */ 330*aad628c1SPeter Huewe *status = tpm_tis_i2c_status(chip); 331*aad628c1SPeter Huewe if ((*status & mask) == mask) 332*aad628c1SPeter Huewe return 0; 333*aad628c1SPeter Huewe 334*aad628c1SPeter Huewe stop = jiffies + timeout; 335*aad628c1SPeter Huewe do { 336*aad628c1SPeter Huewe /* since we just checked the status, give the TPM some time */ 337*aad628c1SPeter Huewe usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); 338*aad628c1SPeter Huewe *status = tpm_tis_i2c_status(chip); 339*aad628c1SPeter Huewe if ((*status & mask) == mask) 340*aad628c1SPeter Huewe return 0; 341*aad628c1SPeter Huewe 342*aad628c1SPeter Huewe } while (time_before(jiffies, stop)); 343*aad628c1SPeter Huewe 344*aad628c1SPeter Huewe return -ETIME; 345*aad628c1SPeter Huewe } 346*aad628c1SPeter Huewe 347*aad628c1SPeter Huewe static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) 348*aad628c1SPeter Huewe { 349*aad628c1SPeter Huewe size_t size = 0; 350*aad628c1SPeter Huewe ssize_t burstcnt; 351*aad628c1SPeter Huewe u8 retries = 0; 352*aad628c1SPeter Huewe int rc; 353*aad628c1SPeter Huewe 354*aad628c1SPeter Huewe while (size < count) { 355*aad628c1SPeter Huewe burstcnt = get_burstcount(chip); 356*aad628c1SPeter Huewe 357*aad628c1SPeter Huewe /* burstcnt < 0 = TPM is busy */ 358*aad628c1SPeter Huewe if (burstcnt < 0) 359*aad628c1SPeter Huewe return burstcnt; 360*aad628c1SPeter Huewe 361*aad628c1SPeter Huewe /* limit received data to max. left */ 362*aad628c1SPeter Huewe if (burstcnt > (count - size)) 363*aad628c1SPeter Huewe burstcnt = count - size; 364*aad628c1SPeter Huewe 365*aad628c1SPeter Huewe rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality), 366*aad628c1SPeter Huewe &(buf[size]), burstcnt); 367*aad628c1SPeter Huewe if (rc == 0) 368*aad628c1SPeter Huewe size += burstcnt; 369*aad628c1SPeter Huewe else if (rc < 0) 370*aad628c1SPeter Huewe retries++; 371*aad628c1SPeter Huewe 372*aad628c1SPeter Huewe /* avoid endless loop in case of broken HW */ 373*aad628c1SPeter Huewe if (retries > MAX_COUNT_LONG) 374*aad628c1SPeter Huewe return -EIO; 375*aad628c1SPeter Huewe 376*aad628c1SPeter Huewe } 377*aad628c1SPeter Huewe return size; 378*aad628c1SPeter Huewe } 379*aad628c1SPeter Huewe 380*aad628c1SPeter Huewe static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) 381*aad628c1SPeter Huewe { 382*aad628c1SPeter Huewe int size = 0; 383*aad628c1SPeter Huewe int expected, status; 384*aad628c1SPeter Huewe 385*aad628c1SPeter Huewe if (count < TPM_HEADER_SIZE) { 386*aad628c1SPeter Huewe size = -EIO; 387*aad628c1SPeter Huewe goto out; 388*aad628c1SPeter Huewe } 389*aad628c1SPeter Huewe 390*aad628c1SPeter Huewe /* read first 10 bytes, including tag, paramsize, and result */ 391*aad628c1SPeter Huewe size = recv_data(chip, buf, TPM_HEADER_SIZE); 392*aad628c1SPeter Huewe if (size < TPM_HEADER_SIZE) { 393*aad628c1SPeter Huewe dev_err(chip->dev, "Unable to read header\n"); 394*aad628c1SPeter Huewe goto out; 395*aad628c1SPeter Huewe } 396*aad628c1SPeter Huewe 397*aad628c1SPeter Huewe expected = be32_to_cpu(*(__be32 *)(buf + 2)); 398*aad628c1SPeter Huewe if ((size_t) expected > count) { 399*aad628c1SPeter Huewe size = -EIO; 400*aad628c1SPeter Huewe goto out; 401*aad628c1SPeter Huewe } 402*aad628c1SPeter Huewe 403*aad628c1SPeter Huewe size += recv_data(chip, &buf[TPM_HEADER_SIZE], 404*aad628c1SPeter Huewe expected - TPM_HEADER_SIZE); 405*aad628c1SPeter Huewe if (size < expected) { 406*aad628c1SPeter Huewe dev_err(chip->dev, "Unable to read remainder of result\n"); 407*aad628c1SPeter Huewe size = -ETIME; 408*aad628c1SPeter Huewe goto out; 409*aad628c1SPeter Huewe } 410*aad628c1SPeter Huewe 411*aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); 412*aad628c1SPeter Huewe if (status & TPM_STS_DATA_AVAIL) { /* retry? */ 413*aad628c1SPeter Huewe dev_err(chip->dev, "Error left over data\n"); 414*aad628c1SPeter Huewe size = -EIO; 415*aad628c1SPeter Huewe goto out; 416*aad628c1SPeter Huewe } 417*aad628c1SPeter Huewe 418*aad628c1SPeter Huewe out: 419*aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 420*aad628c1SPeter Huewe /* The TPM needs some time to clean up here, 421*aad628c1SPeter Huewe * so we sleep rather than keeping the bus busy 422*aad628c1SPeter Huewe */ 423*aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); 424*aad628c1SPeter Huewe release_locality(chip, chip->vendor.locality, 0); 425*aad628c1SPeter Huewe return size; 426*aad628c1SPeter Huewe } 427*aad628c1SPeter Huewe 428*aad628c1SPeter Huewe static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) 429*aad628c1SPeter Huewe { 430*aad628c1SPeter Huewe int rc, status; 431*aad628c1SPeter Huewe ssize_t burstcnt; 432*aad628c1SPeter Huewe size_t count = 0; 433*aad628c1SPeter Huewe u8 retries = 0; 434*aad628c1SPeter Huewe u8 sts = TPM_STS_GO; 435*aad628c1SPeter Huewe 436*aad628c1SPeter Huewe if (len > TPM_BUFSIZE) 437*aad628c1SPeter Huewe return -E2BIG; /* command is too long for our tpm, sorry */ 438*aad628c1SPeter Huewe 439*aad628c1SPeter Huewe if (request_locality(chip, 0) < 0) 440*aad628c1SPeter Huewe return -EBUSY; 441*aad628c1SPeter Huewe 442*aad628c1SPeter Huewe status = tpm_tis_i2c_status(chip); 443*aad628c1SPeter Huewe if ((status & TPM_STS_COMMAND_READY) == 0) { 444*aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 445*aad628c1SPeter Huewe if (wait_for_stat 446*aad628c1SPeter Huewe (chip, TPM_STS_COMMAND_READY, 447*aad628c1SPeter Huewe chip->vendor.timeout_b, &status) < 0) { 448*aad628c1SPeter Huewe rc = -ETIME; 449*aad628c1SPeter Huewe goto out_err; 450*aad628c1SPeter Huewe } 451*aad628c1SPeter Huewe } 452*aad628c1SPeter Huewe 453*aad628c1SPeter Huewe while (count < len - 1) { 454*aad628c1SPeter Huewe burstcnt = get_burstcount(chip); 455*aad628c1SPeter Huewe 456*aad628c1SPeter Huewe /* burstcnt < 0 = TPM is busy */ 457*aad628c1SPeter Huewe if (burstcnt < 0) 458*aad628c1SPeter Huewe return burstcnt; 459*aad628c1SPeter Huewe 460*aad628c1SPeter Huewe if (burstcnt > (len - 1 - count)) 461*aad628c1SPeter Huewe burstcnt = len - 1 - count; 462*aad628c1SPeter Huewe 463*aad628c1SPeter Huewe rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), 464*aad628c1SPeter Huewe &(buf[count]), burstcnt); 465*aad628c1SPeter Huewe if (rc == 0) 466*aad628c1SPeter Huewe count += burstcnt; 467*aad628c1SPeter Huewe else if (rc < 0) 468*aad628c1SPeter Huewe retries++; 469*aad628c1SPeter Huewe 470*aad628c1SPeter Huewe /* avoid endless loop in case of broken HW */ 471*aad628c1SPeter Huewe if (retries > MAX_COUNT_LONG) { 472*aad628c1SPeter Huewe rc = -EIO; 473*aad628c1SPeter Huewe goto out_err; 474*aad628c1SPeter Huewe } 475*aad628c1SPeter Huewe 476*aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, 477*aad628c1SPeter Huewe chip->vendor.timeout_c, &status); 478*aad628c1SPeter Huewe 479*aad628c1SPeter Huewe if ((status & TPM_STS_DATA_EXPECT) == 0) { 480*aad628c1SPeter Huewe rc = -EIO; 481*aad628c1SPeter Huewe goto out_err; 482*aad628c1SPeter Huewe } 483*aad628c1SPeter Huewe 484*aad628c1SPeter Huewe } 485*aad628c1SPeter Huewe 486*aad628c1SPeter Huewe /* write last byte */ 487*aad628c1SPeter Huewe iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1); 488*aad628c1SPeter Huewe wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); 489*aad628c1SPeter Huewe if ((status & TPM_STS_DATA_EXPECT) != 0) { 490*aad628c1SPeter Huewe rc = -EIO; 491*aad628c1SPeter Huewe goto out_err; 492*aad628c1SPeter Huewe } 493*aad628c1SPeter Huewe 494*aad628c1SPeter Huewe /* go and do it */ 495*aad628c1SPeter Huewe iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1); 496*aad628c1SPeter Huewe 497*aad628c1SPeter Huewe return len; 498*aad628c1SPeter Huewe out_err: 499*aad628c1SPeter Huewe tpm_tis_i2c_ready(chip); 500*aad628c1SPeter Huewe /* The TPM needs some time to clean up here, 501*aad628c1SPeter Huewe * so we sleep rather than keeping the bus busy 502*aad628c1SPeter Huewe */ 503*aad628c1SPeter Huewe usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); 504*aad628c1SPeter Huewe release_locality(chip, chip->vendor.locality, 0); 505*aad628c1SPeter Huewe return rc; 506*aad628c1SPeter Huewe } 507*aad628c1SPeter Huewe 508*aad628c1SPeter Huewe static const struct file_operations tis_ops = { 509*aad628c1SPeter Huewe .owner = THIS_MODULE, 510*aad628c1SPeter Huewe .llseek = no_llseek, 511*aad628c1SPeter Huewe .open = tpm_open, 512*aad628c1SPeter Huewe .read = tpm_read, 513*aad628c1SPeter Huewe .write = tpm_write, 514*aad628c1SPeter Huewe .release = tpm_release, 515*aad628c1SPeter Huewe }; 516*aad628c1SPeter Huewe 517*aad628c1SPeter Huewe static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); 518*aad628c1SPeter Huewe static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); 519*aad628c1SPeter Huewe static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); 520*aad628c1SPeter Huewe static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); 521*aad628c1SPeter Huewe static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); 522*aad628c1SPeter Huewe static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL); 523*aad628c1SPeter Huewe static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL); 524*aad628c1SPeter Huewe static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel); 525*aad628c1SPeter Huewe static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL); 526*aad628c1SPeter Huewe static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL); 527*aad628c1SPeter Huewe 528*aad628c1SPeter Huewe static struct attribute *tis_attrs[] = { 529*aad628c1SPeter Huewe &dev_attr_pubek.attr, 530*aad628c1SPeter Huewe &dev_attr_pcrs.attr, 531*aad628c1SPeter Huewe &dev_attr_enabled.attr, 532*aad628c1SPeter Huewe &dev_attr_active.attr, 533*aad628c1SPeter Huewe &dev_attr_owned.attr, 534*aad628c1SPeter Huewe &dev_attr_temp_deactivated.attr, 535*aad628c1SPeter Huewe &dev_attr_caps.attr, 536*aad628c1SPeter Huewe &dev_attr_cancel.attr, 537*aad628c1SPeter Huewe &dev_attr_durations.attr, 538*aad628c1SPeter Huewe &dev_attr_timeouts.attr, 539*aad628c1SPeter Huewe NULL, 540*aad628c1SPeter Huewe }; 541*aad628c1SPeter Huewe 542*aad628c1SPeter Huewe static struct attribute_group tis_attr_grp = { 543*aad628c1SPeter Huewe .attrs = tis_attrs 544*aad628c1SPeter Huewe }; 545*aad628c1SPeter Huewe 546*aad628c1SPeter Huewe static struct tpm_vendor_specific tpm_tis_i2c = { 547*aad628c1SPeter Huewe .status = tpm_tis_i2c_status, 548*aad628c1SPeter Huewe .recv = tpm_tis_i2c_recv, 549*aad628c1SPeter Huewe .send = tpm_tis_i2c_send, 550*aad628c1SPeter Huewe .cancel = tpm_tis_i2c_ready, 551*aad628c1SPeter Huewe .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 552*aad628c1SPeter Huewe .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 553*aad628c1SPeter Huewe .req_canceled = TPM_STS_COMMAND_READY, 554*aad628c1SPeter Huewe .attr_group = &tis_attr_grp, 555*aad628c1SPeter Huewe .miscdev.fops = &tis_ops, 556*aad628c1SPeter Huewe }; 557*aad628c1SPeter Huewe 558*aad628c1SPeter Huewe static int __devinit tpm_tis_i2c_init(struct device *dev) 559*aad628c1SPeter Huewe { 560*aad628c1SPeter Huewe u32 vendor; 561*aad628c1SPeter Huewe int rc = 0; 562*aad628c1SPeter Huewe struct tpm_chip *chip; 563*aad628c1SPeter Huewe 564*aad628c1SPeter Huewe chip = tpm_register_hardware(dev, &tpm_tis_i2c); 565*aad628c1SPeter Huewe if (!chip) { 566*aad628c1SPeter Huewe rc = -ENODEV; 567*aad628c1SPeter Huewe goto out_err; 568*aad628c1SPeter Huewe } 569*aad628c1SPeter Huewe 570*aad628c1SPeter Huewe /* Disable interrupts */ 571*aad628c1SPeter Huewe chip->vendor.irq = 0; 572*aad628c1SPeter Huewe 573*aad628c1SPeter Huewe /* Default timeouts */ 574*aad628c1SPeter Huewe chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 575*aad628c1SPeter Huewe chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); 576*aad628c1SPeter Huewe chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 577*aad628c1SPeter Huewe chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 578*aad628c1SPeter Huewe 579*aad628c1SPeter Huewe if (request_locality(chip, 0) != 0) { 580*aad628c1SPeter Huewe rc = -ENODEV; 581*aad628c1SPeter Huewe goto out_vendor; 582*aad628c1SPeter Huewe } 583*aad628c1SPeter Huewe 584*aad628c1SPeter Huewe /* read four bytes from DID_VID register */ 585*aad628c1SPeter Huewe if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) { 586*aad628c1SPeter Huewe rc = -EIO; 587*aad628c1SPeter Huewe goto out_release; 588*aad628c1SPeter Huewe } 589*aad628c1SPeter Huewe 590*aad628c1SPeter Huewe /* create DID_VID register value, after swapping to little-endian */ 591*aad628c1SPeter Huewe vendor = be32_to_cpu((__be32) vendor); 592*aad628c1SPeter Huewe 593*aad628c1SPeter Huewe if (vendor != TPM_TIS_I2C_DID_VID) { 594*aad628c1SPeter Huewe rc = -ENODEV; 595*aad628c1SPeter Huewe goto out_release; 596*aad628c1SPeter Huewe } 597*aad628c1SPeter Huewe 598*aad628c1SPeter Huewe dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); 599*aad628c1SPeter Huewe 600*aad628c1SPeter Huewe INIT_LIST_HEAD(&chip->vendor.list); 601*aad628c1SPeter Huewe tpm_dev.chip = chip; 602*aad628c1SPeter Huewe 603*aad628c1SPeter Huewe tpm_get_timeouts(chip); 604*aad628c1SPeter Huewe tpm_do_selftest(chip); 605*aad628c1SPeter Huewe 606*aad628c1SPeter Huewe return 0; 607*aad628c1SPeter Huewe 608*aad628c1SPeter Huewe out_release: 609*aad628c1SPeter Huewe release_locality(chip, chip->vendor.locality, 1); 610*aad628c1SPeter Huewe 611*aad628c1SPeter Huewe out_vendor: 612*aad628c1SPeter Huewe /* close file handles */ 613*aad628c1SPeter Huewe tpm_dev_vendor_release(chip); 614*aad628c1SPeter Huewe 615*aad628c1SPeter Huewe /* remove hardware */ 616*aad628c1SPeter Huewe tpm_remove_hardware(chip->dev); 617*aad628c1SPeter Huewe 618*aad628c1SPeter Huewe /* reset these pointers, otherwise we oops */ 619*aad628c1SPeter Huewe chip->dev->release = NULL; 620*aad628c1SPeter Huewe chip->release = NULL; 621*aad628c1SPeter Huewe tpm_dev.client = NULL; 622*aad628c1SPeter Huewe dev_set_drvdata(chip->dev, chip); 623*aad628c1SPeter Huewe out_err: 624*aad628c1SPeter Huewe return rc; 625*aad628c1SPeter Huewe } 626*aad628c1SPeter Huewe 627*aad628c1SPeter Huewe static const struct i2c_device_id tpm_tis_i2c_table[] = { 628*aad628c1SPeter Huewe {"tpm_i2c_infineon", 0}, 629*aad628c1SPeter Huewe {}, 630*aad628c1SPeter Huewe }; 631*aad628c1SPeter Huewe 632*aad628c1SPeter Huewe MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table); 633*aad628c1SPeter Huewe static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume); 634*aad628c1SPeter Huewe 635*aad628c1SPeter Huewe static int __devinit tpm_tis_i2c_probe(struct i2c_client *client, 636*aad628c1SPeter Huewe const struct i2c_device_id *id) 637*aad628c1SPeter Huewe { 638*aad628c1SPeter Huewe int rc; 639*aad628c1SPeter Huewe if (tpm_dev.client != NULL) 640*aad628c1SPeter Huewe return -EBUSY; /* We only support one client */ 641*aad628c1SPeter Huewe 642*aad628c1SPeter Huewe if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 643*aad628c1SPeter Huewe dev_err(&client->dev, 644*aad628c1SPeter Huewe "no algorithms associated to the i2c bus\n"); 645*aad628c1SPeter Huewe return -ENODEV; 646*aad628c1SPeter Huewe } 647*aad628c1SPeter Huewe 648*aad628c1SPeter Huewe client->driver = &tpm_tis_i2c_driver; 649*aad628c1SPeter Huewe tpm_dev.client = client; 650*aad628c1SPeter Huewe rc = tpm_tis_i2c_init(&client->dev); 651*aad628c1SPeter Huewe if (rc != 0) { 652*aad628c1SPeter Huewe client->driver = NULL; 653*aad628c1SPeter Huewe tpm_dev.client = NULL; 654*aad628c1SPeter Huewe rc = -ENODEV; 655*aad628c1SPeter Huewe } 656*aad628c1SPeter Huewe return rc; 657*aad628c1SPeter Huewe } 658*aad628c1SPeter Huewe 659*aad628c1SPeter Huewe static int __devexit tpm_tis_i2c_remove(struct i2c_client *client) 660*aad628c1SPeter Huewe { 661*aad628c1SPeter Huewe struct tpm_chip *chip = tpm_dev.chip; 662*aad628c1SPeter Huewe release_locality(chip, chip->vendor.locality, 1); 663*aad628c1SPeter Huewe 664*aad628c1SPeter Huewe /* close file handles */ 665*aad628c1SPeter Huewe tpm_dev_vendor_release(chip); 666*aad628c1SPeter Huewe 667*aad628c1SPeter Huewe /* remove hardware */ 668*aad628c1SPeter Huewe tpm_remove_hardware(chip->dev); 669*aad628c1SPeter Huewe 670*aad628c1SPeter Huewe /* reset these pointers, otherwise we oops */ 671*aad628c1SPeter Huewe chip->dev->release = NULL; 672*aad628c1SPeter Huewe chip->release = NULL; 673*aad628c1SPeter Huewe tpm_dev.client = NULL; 674*aad628c1SPeter Huewe dev_set_drvdata(chip->dev, chip); 675*aad628c1SPeter Huewe 676*aad628c1SPeter Huewe return 0; 677*aad628c1SPeter Huewe } 678*aad628c1SPeter Huewe 679*aad628c1SPeter Huewe static struct i2c_driver tpm_tis_i2c_driver = { 680*aad628c1SPeter Huewe 681*aad628c1SPeter Huewe .id_table = tpm_tis_i2c_table, 682*aad628c1SPeter Huewe .probe = tpm_tis_i2c_probe, 683*aad628c1SPeter Huewe .remove = tpm_tis_i2c_remove, 684*aad628c1SPeter Huewe .driver = { 685*aad628c1SPeter Huewe .name = "tpm_i2c_infineon", 686*aad628c1SPeter Huewe .owner = THIS_MODULE, 687*aad628c1SPeter Huewe .pm = &tpm_tis_i2c_ops, 688*aad628c1SPeter Huewe }, 689*aad628c1SPeter Huewe }; 690*aad628c1SPeter Huewe 691*aad628c1SPeter Huewe module_i2c_driver(tpm_tis_i2c_driver); 692*aad628c1SPeter Huewe MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>"); 693*aad628c1SPeter Huewe MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver"); 694*aad628c1SPeter Huewe MODULE_VERSION("2.1.5"); 695*aad628c1SPeter Huewe MODULE_LICENSE("GPL"); 696