2 * Copyright (C) 2012 Infineon Technologies
7 * Device driver for TCG/TCPA TPM (trusted platform module).
8 * Specifications at www.trustedcomputinggroup.org
10 * This device driver implements the TPM interface as defined in
11 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
12 * Infineon I2C Protocol Stack Specification v0.20.
14 * It is based on the original tpm_tis device driver from Leendert van
15 * Dorn and Kyleen Hall.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation, version 2 of the
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/wait.h>
31 /* max. buffer size supported by our TPM */
32 #define TPM_BUFSIZE 1260
34 /* max. number of iterations after I2C NAK */
37 #define SLEEP_DURATION_LOW 55
38 #define SLEEP_DURATION_HI 65
40 /* max. number of iterations after I2C NAK for 'long' commands
41 * we need this especially for sending TPM_READY, since the cleanup after the
42 * transtion to the ready state may take some time, but it is unpredictable
43 * how long it will take.
45 #define MAX_COUNT_LONG 50
47 #define SLEEP_DURATION_LONG_LOW 200
48 #define SLEEP_DURATION_LONG_HI 220
50 /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */
51 #define SLEEP_DURATION_RESET_LOW 2400
52 #define SLEEP_DURATION_RESET_HI 2600
54 /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */
55 #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
56 #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000)
58 /* expected value for DIDVID register */
59 #define TPM_TIS_I2C_DID_VID 0x000b15d1L
61 /* Structure to store I2C TPM specific stuff */
63 struct i2c_client *client;
64 u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
65 struct tpm_chip *chip;
68 static struct tpm_inf_dev tpm_dev;
69 static struct i2c_driver tpm_tis_i2c_driver;
72 * iic_tpm_read() - read from TPM register
73 * @addr: register address to read from
74 * @buffer: provided by caller
75 * @len: number of bytes to read
77 * Read len bytes from TPM register and put them into
78 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
80 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
81 * values have to be swapped.
83 * NOTE: We can't unfortunately use the combined read/write functions
84 * provided by the i2c core as the TPM currently does not support the
85 * repeated start condition and due to it's special requirements.
86 * The i2c_smbus* functions do not work for this chip.
88 * Return -EIO on error, 0 on success.
90 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
93 struct i2c_msg msg1 = { tpm_dev.client->addr, 0, 1, &addr };
94 struct i2c_msg msg2 = { tpm_dev.client->addr, I2C_M_RD, len, buffer };
99 /* Lock the adapter for the duration of the whole sequence. */
100 if (!tpm_dev.client->adapter->algo->master_xfer)
102 i2c_lock_adapter(tpm_dev.client->adapter);
104 for (count = 0; count < MAX_COUNT; count++) {
105 rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
107 break; /* break here to skip sleep */
109 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
115 /* After the TPM has successfully received the register address it needs
116 * some time, thus we're sleeping here again, before retrieving the data
118 for (count = 0; count < MAX_COUNT; count++) {
119 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
120 rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1);
127 i2c_unlock_adapter(tpm_dev.client->adapter);
134 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
135 unsigned int sleep_low,
136 unsigned int sleep_hi, u8 max_count)
141 struct i2c_msg msg1 = { tpm_dev.client->addr, 0, len + 1, tpm_dev.buf };
143 if (len > TPM_BUFSIZE)
146 if (!tpm_dev.client->adapter->algo->master_xfer)
148 i2c_lock_adapter(tpm_dev.client->adapter);
150 /* prepend the 'register address' to the buffer */
151 tpm_dev.buf[0] = addr;
152 memcpy(&(tpm_dev.buf[1]), buffer, len);
155 * NOTE: We have to use these special mechanisms here and unfortunately
156 * cannot rely on the standard behavior of i2c_transfer.
158 for (count = 0; count < max_count; count++) {
159 rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
163 usleep_range(sleep_low, sleep_hi);
166 i2c_unlock_adapter(tpm_dev.client->adapter);
174 * iic_tpm_write() - write to TPM register
175 * @addr: register address to write to
176 * @buffer: containing data to be written
177 * @len: number of bytes to write
179 * Write len bytes from provided buffer to TPM register (little
180 * endian format, i.e. buffer[0] is written as first byte).
182 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
183 * values have to be swapped.
185 * NOTE: use this function instead of the iic_tpm_write_generic function.
187 * Return -EIO on error, 0 on success
189 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
191 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
192 SLEEP_DURATION_HI, MAX_COUNT);
196 * This function is needed especially for the cleanup situation after
199 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
201 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
202 SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
206 TPM_ACCESS_VALID = 0x80,
207 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
208 TPM_ACCESS_REQUEST_PENDING = 0x04,
209 TPM_ACCESS_REQUEST_USE = 0x02,
213 TPM_STS_VALID = 0x80,
214 TPM_STS_COMMAND_READY = 0x40,
216 TPM_STS_DATA_AVAIL = 0x10,
217 TPM_STS_DATA_EXPECT = 0x08,
221 TIS_SHORT_TIMEOUT = 750, /* ms */
222 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
225 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
226 #define TPM_STS(l) (0x0001 | ((l) << 4))
227 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
228 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
230 static int check_locality(struct tpm_chip *chip, int loc)
235 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
239 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
240 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
241 chip->vendor.locality = loc;
248 /* implementation similar to tpm_tis */
249 static void release_locality(struct tpm_chip *chip, int loc, int force)
252 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
255 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
256 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
257 buf = TPM_ACCESS_ACTIVE_LOCALITY;
258 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
262 static int request_locality(struct tpm_chip *chip, int loc)
265 u8 buf = TPM_ACCESS_REQUEST_USE;
267 if (check_locality(chip, loc) >= 0)
270 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
272 /* wait for burstcount */
273 stop = jiffies + chip->vendor.timeout_a;
275 if (check_locality(chip, loc) >= 0)
277 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
278 } while (time_before(jiffies, stop));
283 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
285 /* NOTE: since I2C read may fail, return 0 in this case --> time-out */
287 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
293 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
295 /* this causes the current command to be aborted */
296 u8 buf = TPM_STS_COMMAND_READY;
297 iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
300 static ssize_t get_burstcount(struct tpm_chip *chip)
306 /* wait for burstcount */
307 /* which timeout value, spec has 2 answers (c & d) */
308 stop = jiffies + chip->vendor.timeout_d;
310 /* Note: STS is little endian */
311 if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0)
314 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
319 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
320 } while (time_before(jiffies, stop));
324 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
329 /* check current status */
330 *status = tpm_tis_i2c_status(chip);
331 if ((*status & mask) == mask)
334 stop = jiffies + timeout;
336 /* since we just checked the status, give the TPM some time */
337 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
338 *status = tpm_tis_i2c_status(chip);
339 if ((*status & mask) == mask)
342 } while (time_before(jiffies, stop));
347 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
354 while (size < count) {
355 burstcnt = get_burstcount(chip);
357 /* burstcnt < 0 = TPM is busy */
361 /* limit received data to max. left */
362 if (burstcnt > (count - size))
363 burstcnt = count - size;
365 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
366 &(buf[size]), burstcnt);
372 /* avoid endless loop in case of broken HW */
373 if (retries > MAX_COUNT_LONG)
380 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
383 int expected, status;
385 if (count < TPM_HEADER_SIZE) {
390 /* read first 10 bytes, including tag, paramsize, and result */
391 size = recv_data(chip, buf, TPM_HEADER_SIZE);
392 if (size < TPM_HEADER_SIZE) {
393 dev_err(chip->dev, "Unable to read header\n");
397 expected = be32_to_cpu(*(__be32 *)(buf + 2));
398 if ((size_t) expected > count) {
403 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
404 expected - TPM_HEADER_SIZE);
405 if (size < expected) {
406 dev_err(chip->dev, "Unable to read remainder of result\n");
411 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
412 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
413 dev_err(chip->dev, "Error left over data\n");
419 tpm_tis_i2c_ready(chip);
420 /* The TPM needs some time to clean up here,
421 * so we sleep rather than keeping the bus busy
423 usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
424 release_locality(chip, chip->vendor.locality, 0);
428 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
436 if (len > TPM_BUFSIZE)
437 return -E2BIG; /* command is too long for our tpm, sorry */
439 if (request_locality(chip, 0) < 0)
442 status = tpm_tis_i2c_status(chip);
443 if ((status & TPM_STS_COMMAND_READY) == 0) {
444 tpm_tis_i2c_ready(chip);
446 (chip, TPM_STS_COMMAND_READY,
447 chip->vendor.timeout_b, &status) < 0) {
453 while (count < len - 1) {
454 burstcnt = get_burstcount(chip);
456 /* burstcnt < 0 = TPM is busy */
460 if (burstcnt > (len - 1 - count))
461 burstcnt = len - 1 - count;
463 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
464 &(buf[count]), burstcnt);
470 /* avoid endless loop in case of broken HW */
471 if (retries > MAX_COUNT_LONG) {
476 wait_for_stat(chip, TPM_STS_VALID,
477 chip->vendor.timeout_c, &status);
479 if ((status & TPM_STS_DATA_EXPECT) == 0) {
486 /* write last byte */
487 iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
488 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
489 if ((status & TPM_STS_DATA_EXPECT) != 0) {
495 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
499 tpm_tis_i2c_ready(chip);
500 /* The TPM needs some time to clean up here,
501 * so we sleep rather than keeping the bus busy
503 usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
504 release_locality(chip, chip->vendor.locality, 0);
508 static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
510 return (status == TPM_STS_COMMAND_READY);
513 static const struct file_operations tis_ops = {
514 .owner = THIS_MODULE,
519 .release = tpm_release,
522 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
523 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
524 static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
525 static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
526 static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
527 static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
528 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
529 static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
530 static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
531 static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
533 static struct attribute *tis_attrs[] = {
534 &dev_attr_pubek.attr,
536 &dev_attr_enabled.attr,
537 &dev_attr_active.attr,
538 &dev_attr_owned.attr,
539 &dev_attr_temp_deactivated.attr,
541 &dev_attr_cancel.attr,
542 &dev_attr_durations.attr,
543 &dev_attr_timeouts.attr,
547 static struct attribute_group tis_attr_grp = {
551 static struct tpm_vendor_specific tpm_tis_i2c = {
552 .status = tpm_tis_i2c_status,
553 .recv = tpm_tis_i2c_recv,
554 .send = tpm_tis_i2c_send,
555 .cancel = tpm_tis_i2c_ready,
556 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
557 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
558 .req_canceled = tpm_tis_i2c_req_canceled,
559 .attr_group = &tis_attr_grp,
560 .miscdev.fops = &tis_ops,
563 static int tpm_tis_i2c_init(struct device *dev)
567 struct tpm_chip *chip;
569 chip = tpm_register_hardware(dev, &tpm_tis_i2c);
575 /* Disable interrupts */
576 chip->vendor.irq = 0;
578 /* Default timeouts */
579 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
580 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
581 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
582 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
584 if (request_locality(chip, 0) != 0) {
589 /* read four bytes from DID_VID register */
590 if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
595 /* create DID_VID register value, after swapping to little-endian */
596 vendor = be32_to_cpu((__be32) vendor);
598 if (vendor != TPM_TIS_I2C_DID_VID) {
603 dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
605 INIT_LIST_HEAD(&chip->vendor.list);
608 tpm_get_timeouts(chip);
609 tpm_do_selftest(chip);
614 release_locality(chip, chip->vendor.locality, 1);
617 /* close file handles */
618 tpm_dev_vendor_release(chip);
620 /* remove hardware */
621 tpm_remove_hardware(chip->dev);
623 /* reset these pointers, otherwise we oops */
624 chip->dev->release = NULL;
625 chip->release = NULL;
626 tpm_dev.client = NULL;
627 dev_set_drvdata(chip->dev, chip);
632 static const struct i2c_device_id tpm_tis_i2c_table[] = {
633 {"tpm_i2c_infineon", 0},
637 MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
638 static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
640 static int tpm_tis_i2c_probe(struct i2c_client *client,
641 const struct i2c_device_id *id)
644 if (tpm_dev.client != NULL)
645 return -EBUSY; /* We only support one client */
647 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
648 dev_err(&client->dev,
649 "no algorithms associated to the i2c bus\n");
653 client->driver = &tpm_tis_i2c_driver;
654 tpm_dev.client = client;
655 rc = tpm_tis_i2c_init(&client->dev);
657 client->driver = NULL;
658 tpm_dev.client = NULL;
664 static int tpm_tis_i2c_remove(struct i2c_client *client)
666 struct tpm_chip *chip = tpm_dev.chip;
667 release_locality(chip, chip->vendor.locality, 1);
669 /* close file handles */
670 tpm_dev_vendor_release(chip);
672 /* remove hardware */
673 tpm_remove_hardware(chip->dev);
675 /* reset these pointers, otherwise we oops */
676 chip->dev->release = NULL;
677 chip->release = NULL;
678 tpm_dev.client = NULL;
679 dev_set_drvdata(chip->dev, chip);
684 static struct i2c_driver tpm_tis_i2c_driver = {
686 .id_table = tpm_tis_i2c_table,
687 .probe = tpm_tis_i2c_probe,
688 .remove = tpm_tis_i2c_remove,
690 .name = "tpm_i2c_infineon",
691 .owner = THIS_MODULE,
692 .pm = &tpm_tis_i2c_ops,
696 module_i2c_driver(tpm_tis_i2c_driver);
698 MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
699 MODULE_VERSION("2.1.5");
700 MODULE_LICENSE("GPL");