2 * Copyright (C) 2012,2013 Infineon Technologies
5 * Peter Huewe <peter.huewe@infineon.com>
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_9635 0xd1150b00L
60 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
68 /* Structure to store I2C TPM specific stuff */
70 struct i2c_client *client;
71 u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
72 struct tpm_chip *chip;
73 enum i2c_chip_type chip_type;
76 static struct tpm_inf_dev tpm_dev;
77 static struct i2c_driver tpm_tis_i2c_driver;
80 * iic_tpm_read() - read from TPM register
81 * @addr: register address to read from
82 * @buffer: provided by caller
83 * @len: number of bytes to read
85 * Read len bytes from TPM register and put them into
86 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
88 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
89 * values have to be swapped.
91 * NOTE: We can't unfortunately use the combined read/write functions
92 * provided by the i2c core as the TPM currently does not support the
93 * repeated start condition and due to it's special requirements.
94 * The i2c_smbus* functions do not work for this chip.
96 * Return -EIO on error, 0 on success.
98 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
101 struct i2c_msg msg1 = {
102 .addr = tpm_dev.client->addr,
106 struct i2c_msg msg2 = {
107 .addr = tpm_dev.client->addr,
112 struct i2c_msg msgs[] = {msg1, msg2};
117 /* Lock the adapter for the duration of the whole sequence. */
118 if (!tpm_dev.client->adapter->algo->master_xfer)
120 i2c_lock_adapter(tpm_dev.client->adapter);
122 if (tpm_dev.chip_type == SLB9645) {
123 /* use a combined read for newer chips
124 * unfortunately the smbus functions are not suitable due to
125 * the 32 byte limit of the smbus.
126 * retries should usually not be needed, but are kept just to
127 * be on the safe side.
129 for (count = 0; count < MAX_COUNT; count++) {
130 rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
132 break; /* break here to skip sleep */
133 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
136 /* slb9635 protocol should work in all cases */
137 for (count = 0; count < MAX_COUNT; count++) {
138 rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
140 break; /* break here to skip sleep */
142 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
148 /* After the TPM has successfully received the register address
149 * it needs some time, thus we're sleeping here again, before
150 * retrieving the data
152 for (count = 0; count < MAX_COUNT; count++) {
153 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
154 rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1);
161 i2c_unlock_adapter(tpm_dev.client->adapter);
162 /* take care of 'guard time' */
163 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
165 /* __i2c_transfer returns the number of successfully transferred
167 * So rc should be greater than 0 here otherwise we have an error.
175 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
176 unsigned int sleep_low,
177 unsigned int sleep_hi, u8 max_count)
182 struct i2c_msg msg1 = {
183 .addr = tpm_dev.client->addr,
188 if (len > TPM_BUFSIZE)
191 if (!tpm_dev.client->adapter->algo->master_xfer)
193 i2c_lock_adapter(tpm_dev.client->adapter);
195 /* prepend the 'register address' to the buffer */
196 tpm_dev.buf[0] = addr;
197 memcpy(&(tpm_dev.buf[1]), buffer, len);
200 * NOTE: We have to use these special mechanisms here and unfortunately
201 * cannot rely on the standard behavior of i2c_transfer.
202 * Even for newer chips the smbus functions are not
203 * suitable due to the 32 byte limit of the smbus.
205 for (count = 0; count < max_count; count++) {
206 rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
209 usleep_range(sleep_low, sleep_hi);
212 i2c_unlock_adapter(tpm_dev.client->adapter);
213 /* take care of 'guard time' */
214 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
216 /* __i2c_transfer returns the number of successfully transferred
218 * So rc should be greater than 0 here otherwise we have an error.
227 * iic_tpm_write() - write to TPM register
228 * @addr: register address to write to
229 * @buffer: containing data to be written
230 * @len: number of bytes to write
232 * Write len bytes from provided buffer to TPM register (little
233 * endian format, i.e. buffer[0] is written as first byte).
235 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
236 * values have to be swapped.
238 * NOTE: use this function instead of the iic_tpm_write_generic function.
240 * Return -EIO on error, 0 on success
242 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
244 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
245 SLEEP_DURATION_HI, MAX_COUNT);
249 * This function is needed especially for the cleanup situation after
252 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
254 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
255 SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
259 TPM_ACCESS_VALID = 0x80,
260 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
261 TPM_ACCESS_REQUEST_PENDING = 0x04,
262 TPM_ACCESS_REQUEST_USE = 0x02,
266 TPM_STS_VALID = 0x80,
267 TPM_STS_COMMAND_READY = 0x40,
269 TPM_STS_DATA_AVAIL = 0x10,
270 TPM_STS_DATA_EXPECT = 0x08,
274 TIS_SHORT_TIMEOUT = 750, /* ms */
275 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
278 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
279 #define TPM_STS(l) (0x0001 | ((l) << 4))
280 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
281 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
283 static int check_locality(struct tpm_chip *chip, int loc)
288 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
292 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
293 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
294 chip->vendor.locality = loc;
301 /* implementation similar to tpm_tis */
302 static void release_locality(struct tpm_chip *chip, int loc, int force)
305 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
308 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
309 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
310 buf = TPM_ACCESS_ACTIVE_LOCALITY;
311 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
315 static int request_locality(struct tpm_chip *chip, int loc)
318 u8 buf = TPM_ACCESS_REQUEST_USE;
320 if (check_locality(chip, loc) >= 0)
323 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
325 /* wait for burstcount */
326 stop = jiffies + chip->vendor.timeout_a;
328 if (check_locality(chip, loc) >= 0)
330 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
331 } while (time_before(jiffies, stop));
336 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
338 /* NOTE: since I2C read may fail, return 0 in this case --> time-out */
343 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
347 /* if locallity is set STS should not be 0xFF */
348 } while ((buf == 0xFF) && i < 10);
353 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
355 /* this causes the current command to be aborted */
356 u8 buf = TPM_STS_COMMAND_READY;
357 iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
360 static ssize_t get_burstcount(struct tpm_chip *chip)
366 /* wait for burstcount */
367 /* which timeout value, spec has 2 answers (c & d) */
368 stop = jiffies + chip->vendor.timeout_d;
370 /* Note: STS is little endian */
371 if (iic_tpm_read(TPM_STS(chip->vendor.locality)+1, buf, 3) < 0)
374 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
379 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
380 } while (time_before(jiffies, stop));
384 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
389 /* check current status */
390 *status = tpm_tis_i2c_status(chip);
391 if ((*status != 0xFF) && (*status & mask) == mask)
394 stop = jiffies + timeout;
396 /* since we just checked the status, give the TPM some time */
397 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
398 *status = tpm_tis_i2c_status(chip);
399 if ((*status & mask) == mask)
402 } while (time_before(jiffies, stop));
407 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
414 while (size < count) {
415 burstcnt = get_burstcount(chip);
417 /* burstcnt < 0 = TPM is busy */
421 /* limit received data to max. left */
422 if (burstcnt > (count - size))
423 burstcnt = count - size;
425 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
426 &(buf[size]), burstcnt);
432 /* avoid endless loop in case of broken HW */
433 if (retries > MAX_COUNT_LONG)
439 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
442 int expected, status;
444 if (count < TPM_HEADER_SIZE) {
449 /* read first 10 bytes, including tag, paramsize, and result */
450 size = recv_data(chip, buf, TPM_HEADER_SIZE);
451 if (size < TPM_HEADER_SIZE) {
452 dev_err(chip->dev, "Unable to read header\n");
456 expected = be32_to_cpu(*(__be32 *)(buf + 2));
457 if ((size_t) expected > count) {
462 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
463 expected - TPM_HEADER_SIZE);
464 if (size < expected) {
465 dev_err(chip->dev, "Unable to read remainder of result\n");
470 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
471 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
472 dev_err(chip->dev, "Error left over data\n");
478 tpm_tis_i2c_ready(chip);
479 /* The TPM needs some time to clean up here,
480 * so we sleep rather than keeping the bus busy
482 usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
483 release_locality(chip, chip->vendor.locality, 0);
487 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
495 if (len > TPM_BUFSIZE)
496 return -E2BIG; /* command is too long for our tpm, sorry */
498 if (request_locality(chip, 0) < 0)
501 status = tpm_tis_i2c_status(chip);
502 if ((status & TPM_STS_COMMAND_READY) == 0) {
503 tpm_tis_i2c_ready(chip);
505 (chip, TPM_STS_COMMAND_READY,
506 chip->vendor.timeout_b, &status) < 0) {
512 while (count < len - 1) {
513 burstcnt = get_burstcount(chip);
515 /* burstcnt < 0 = TPM is busy */
519 if (burstcnt > (len - 1 - count))
520 burstcnt = len - 1 - count;
522 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
523 &(buf[count]), burstcnt);
529 /* avoid endless loop in case of broken HW */
530 if (retries > MAX_COUNT_LONG) {
535 wait_for_stat(chip, TPM_STS_VALID,
536 chip->vendor.timeout_c, &status);
538 if ((status & TPM_STS_DATA_EXPECT) == 0) {
544 /* write last byte */
545 iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
546 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
547 if ((status & TPM_STS_DATA_EXPECT) != 0) {
553 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
557 tpm_tis_i2c_ready(chip);
558 /* The TPM needs some time to clean up here,
559 * so we sleep rather than keeping the bus busy
561 usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
562 release_locality(chip, chip->vendor.locality, 0);
566 static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
568 return (status == TPM_STS_COMMAND_READY);
571 static const struct file_operations tis_ops = {
572 .owner = THIS_MODULE,
577 .release = tpm_release,
580 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
581 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
582 static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
583 static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
584 static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
585 static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
586 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
587 static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
588 static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
589 static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
591 static struct attribute *tis_attrs[] = {
592 &dev_attr_pubek.attr,
594 &dev_attr_enabled.attr,
595 &dev_attr_active.attr,
596 &dev_attr_owned.attr,
597 &dev_attr_temp_deactivated.attr,
599 &dev_attr_cancel.attr,
600 &dev_attr_durations.attr,
601 &dev_attr_timeouts.attr,
605 static struct attribute_group tis_attr_grp = {
609 static struct tpm_vendor_specific tpm_tis_i2c = {
610 .status = tpm_tis_i2c_status,
611 .recv = tpm_tis_i2c_recv,
612 .send = tpm_tis_i2c_send,
613 .cancel = tpm_tis_i2c_ready,
614 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
615 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
616 .req_canceled = tpm_tis_i2c_req_canceled,
617 .attr_group = &tis_attr_grp,
618 .miscdev.fops = &tis_ops,
621 static int tpm_tis_i2c_init(struct device *dev)
625 struct tpm_chip *chip;
627 chip = tpm_register_hardware(dev, &tpm_tis_i2c);
629 dev_err(dev, "could not register hardware\n");
634 /* Disable interrupts */
635 chip->vendor.irq = 0;
637 /* Default timeouts */
638 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
639 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
640 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
641 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
643 if (request_locality(chip, 0) != 0) {
644 dev_err(dev, "could not request locality\n");
649 /* read four bytes from DID_VID register */
650 if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
651 dev_err(dev, "could not read vendor id\n");
656 if (vendor == TPM_TIS_I2C_DID_VID_9645) {
657 tpm_dev.chip_type = SLB9645;
658 } else if (vendor == TPM_TIS_I2C_DID_VID_9635) {
659 tpm_dev.chip_type = SLB9635;
661 dev_err(dev, "vendor id did not match! ID was %08x\n", vendor);
666 dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
668 INIT_LIST_HEAD(&chip->vendor.list);
671 tpm_get_timeouts(chip);
672 tpm_do_selftest(chip);
677 release_locality(chip, chip->vendor.locality, 1);
680 /* close file handles */
681 tpm_dev_vendor_release(chip);
683 /* remove hardware */
684 tpm_remove_hardware(chip->dev);
686 /* reset these pointers, otherwise we oops */
687 chip->dev->release = NULL;
688 chip->release = NULL;
689 tpm_dev.client = NULL;
690 dev_set_drvdata(chip->dev, chip);
695 static const struct i2c_device_id tpm_tis_i2c_table[] = {
696 {"tpm_i2c_infineon", 0},
702 MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
705 static const struct of_device_id tpm_tis_i2c_of_match[] = {
707 .name = "tpm_i2c_infineon",
709 .compatible = "infineon,tpm_i2c_infineon",
715 .compatible = "infineon,slb9635tt",
721 .compatible = "infineon,slb9645tt",
726 MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match);
729 static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
731 static int tpm_tis_i2c_probe(struct i2c_client *client,
732 const struct i2c_device_id *id)
735 struct device *dev = &(client->dev);
737 if (tpm_dev.client != NULL) {
738 dev_err(dev, "This driver only supports one client at a time\n");
739 return -EBUSY; /* We only support one client */
742 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
743 dev_err(dev, "no algorithms associated to the i2c bus\n");
747 client->driver = &tpm_tis_i2c_driver;
748 tpm_dev.client = client;
749 rc = tpm_tis_i2c_init(&client->dev);
751 client->driver = NULL;
752 tpm_dev.client = NULL;
758 static int tpm_tis_i2c_remove(struct i2c_client *client)
760 struct tpm_chip *chip = tpm_dev.chip;
761 release_locality(chip, chip->vendor.locality, 1);
763 /* close file handles */
764 tpm_dev_vendor_release(chip);
766 /* remove hardware */
767 tpm_remove_hardware(chip->dev);
769 /* reset these pointers, otherwise we oops */
770 chip->dev->release = NULL;
771 chip->release = NULL;
772 tpm_dev.client = NULL;
773 dev_set_drvdata(chip->dev, chip);
778 static struct i2c_driver tpm_tis_i2c_driver = {
779 .id_table = tpm_tis_i2c_table,
780 .probe = tpm_tis_i2c_probe,
781 .remove = tpm_tis_i2c_remove,
783 .name = "tpm_i2c_infineon",
784 .owner = THIS_MODULE,
785 .pm = &tpm_tis_i2c_ops,
786 .of_match_table = of_match_ptr(tpm_tis_i2c_of_match),
790 module_i2c_driver(tpm_tis_i2c_driver);
791 MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
792 MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
793 MODULE_VERSION("2.2.0");
794 MODULE_LICENSE("GPL");