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/i2c.h>
25 #include <linux/module.h>
26 #include <linux/wait.h>
29 #define TPM_I2C_INFINEON_BUFSIZE 1260
31 /* max. number of iterations after I2C NAK */
34 #define SLEEP_DURATION_LOW 55
35 #define SLEEP_DURATION_HI 65
37 /* max. number of iterations after I2C NAK for 'long' commands
38 * we need this especially for sending TPM_READY, since the cleanup after the
39 * transtion to the ready state may take some time, but it is unpredictable
40 * how long it will take.
42 #define MAX_COUNT_LONG 50
44 #define SLEEP_DURATION_LONG_LOW 200
45 #define SLEEP_DURATION_LONG_HI 220
47 /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */
48 #define SLEEP_DURATION_RESET_LOW 2400
49 #define SLEEP_DURATION_RESET_HI 2600
51 /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */
52 #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
53 #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000)
55 /* expected value for DIDVID register */
56 #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L
57 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
66 struct i2c_client
*client
;
68 /* In addition to the data itself, the buffer must fit the 7-bit I2C
69 * address and the direction bit.
71 u8 buf
[TPM_I2C_INFINEON_BUFSIZE
+ 1];
72 struct tpm_chip
*chip
;
73 enum i2c_chip_type chip_type
;
74 unsigned int adapterlimit
;
77 static struct tpm_inf_dev tpm_dev
;
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
};
116 unsigned int msglen
= len
;
118 /* Lock the adapter for the duration of the whole sequence. */
119 if (!tpm_dev
.client
->adapter
->algo
->master_xfer
)
121 i2c_lock_bus(tpm_dev
.client
->adapter
, I2C_LOCK_SEGMENT
);
123 if (tpm_dev
.chip_type
== SLB9645
) {
124 /* use a combined read for newer chips
125 * unfortunately the smbus functions are not suitable due to
126 * the 32 byte limit of the smbus.
127 * retries should usually not be needed, but are kept just to
128 * be on the safe side.
130 for (count
= 0; count
< MAX_COUNT
; count
++) {
131 rc
= __i2c_transfer(tpm_dev
.client
->adapter
, msgs
, 2);
133 break; /* break here to skip sleep */
134 usleep_range(SLEEP_DURATION_LOW
, SLEEP_DURATION_HI
);
137 /* Expect to send one command message and one data message, but
138 * support looping over each or both if necessary.
141 /* slb9635 protocol should work in all cases */
142 for (count
= 0; count
< MAX_COUNT
; count
++) {
143 rc
= __i2c_transfer(tpm_dev
.client
->adapter
,
146 break; /* break here to skip sleep */
148 usleep_range(SLEEP_DURATION_LOW
,
155 /* After the TPM has successfully received the register
156 * address it needs some time, thus we're sleeping here
157 * again, before retrieving the data
159 for (count
= 0; count
< MAX_COUNT
; count
++) {
160 if (tpm_dev
.adapterlimit
) {
161 msglen
= min_t(unsigned int,
162 tpm_dev
.adapterlimit
,
166 usleep_range(SLEEP_DURATION_LOW
,
168 rc
= __i2c_transfer(tpm_dev
.client
->adapter
,
171 /* Since len is unsigned, make doubly
172 * sure we do not underflow it.
181 /* If the I2C adapter rejected the request (e.g
182 * when the quirk read_max_len < len) fall back
183 * to a sane minimum value and try again.
185 if (rc
== -EOPNOTSUPP
)
186 tpm_dev
.adapterlimit
=
196 i2c_unlock_bus(tpm_dev
.client
->adapter
, I2C_LOCK_SEGMENT
);
197 /* take care of 'guard time' */
198 usleep_range(SLEEP_DURATION_LOW
, SLEEP_DURATION_HI
);
200 /* __i2c_transfer returns the number of successfully transferred
202 * So rc should be greater than 0 here otherwise we have an error.
210 static int iic_tpm_write_generic(u8 addr
, u8
*buffer
, size_t len
,
211 unsigned int sleep_low
,
212 unsigned int sleep_hi
, u8 max_count
)
217 struct i2c_msg msg1
= {
218 .addr
= tpm_dev
.client
->addr
,
223 if (len
> TPM_I2C_INFINEON_BUFSIZE
)
226 if (!tpm_dev
.client
->adapter
->algo
->master_xfer
)
228 i2c_lock_bus(tpm_dev
.client
->adapter
, I2C_LOCK_SEGMENT
);
230 /* prepend the 'register address' to the buffer */
231 tpm_dev
.buf
[0] = addr
;
232 memcpy(&(tpm_dev
.buf
[1]), buffer
, len
);
235 * NOTE: We have to use these special mechanisms here and unfortunately
236 * cannot rely on the standard behavior of i2c_transfer.
237 * Even for newer chips the smbus functions are not
238 * suitable due to the 32 byte limit of the smbus.
240 for (count
= 0; count
< max_count
; count
++) {
241 rc
= __i2c_transfer(tpm_dev
.client
->adapter
, &msg1
, 1);
244 usleep_range(sleep_low
, sleep_hi
);
247 i2c_unlock_bus(tpm_dev
.client
->adapter
, I2C_LOCK_SEGMENT
);
248 /* take care of 'guard time' */
249 usleep_range(SLEEP_DURATION_LOW
, SLEEP_DURATION_HI
);
251 /* __i2c_transfer returns the number of successfully transferred
253 * So rc should be greater than 0 here otherwise we have an error.
262 * iic_tpm_write() - write to TPM register
263 * @addr: register address to write to
264 * @buffer: containing data to be written
265 * @len: number of bytes to write
267 * Write len bytes from provided buffer to TPM register (little
268 * endian format, i.e. buffer[0] is written as first byte).
270 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
271 * values have to be swapped.
273 * NOTE: use this function instead of the iic_tpm_write_generic function.
275 * Return -EIO on error, 0 on success
277 static int iic_tpm_write(u8 addr
, u8
*buffer
, size_t len
)
279 return iic_tpm_write_generic(addr
, buffer
, len
, SLEEP_DURATION_LOW
,
280 SLEEP_DURATION_HI
, MAX_COUNT
);
284 * This function is needed especially for the cleanup situation after
287 static int iic_tpm_write_long(u8 addr
, u8
*buffer
, size_t len
)
289 return iic_tpm_write_generic(addr
, buffer
, len
, SLEEP_DURATION_LONG_LOW
,
290 SLEEP_DURATION_LONG_HI
, MAX_COUNT_LONG
);
294 TPM_ACCESS_VALID
= 0x80,
295 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
296 TPM_ACCESS_REQUEST_PENDING
= 0x04,
297 TPM_ACCESS_REQUEST_USE
= 0x02,
301 TPM_STS_VALID
= 0x80,
302 TPM_STS_COMMAND_READY
= 0x40,
304 TPM_STS_DATA_AVAIL
= 0x10,
305 TPM_STS_DATA_EXPECT
= 0x08,
309 TIS_SHORT_TIMEOUT
= 750, /* ms */
310 TIS_LONG_TIMEOUT
= 2000, /* 2 sec */
313 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
314 #define TPM_STS(l) (0x0001 | ((l) << 4))
315 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
316 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
318 static bool check_locality(struct tpm_chip
*chip
, int loc
)
323 rc
= iic_tpm_read(TPM_ACCESS(loc
), &buf
, 1);
327 if ((buf
& (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
328 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) {
329 tpm_dev
.locality
= loc
;
336 /* implementation similar to tpm_tis */
337 static void release_locality(struct tpm_chip
*chip
, int loc
, int force
)
340 if (iic_tpm_read(TPM_ACCESS(loc
), &buf
, 1) < 0)
343 if (force
|| (buf
& (TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
)) ==
344 (TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
)) {
345 buf
= TPM_ACCESS_ACTIVE_LOCALITY
;
346 iic_tpm_write(TPM_ACCESS(loc
), &buf
, 1);
350 static int request_locality(struct tpm_chip
*chip
, int loc
)
353 u8 buf
= TPM_ACCESS_REQUEST_USE
;
355 if (check_locality(chip
, loc
))
358 iic_tpm_write(TPM_ACCESS(loc
), &buf
, 1);
360 /* wait for burstcount */
361 stop
= jiffies
+ chip
->timeout_a
;
363 if (check_locality(chip
, loc
))
365 usleep_range(TPM_TIMEOUT_US_LOW
, TPM_TIMEOUT_US_HI
);
366 } while (time_before(jiffies
, stop
));
371 static u8
tpm_tis_i2c_status(struct tpm_chip
*chip
)
373 /* NOTE: since I2C read may fail, return 0 in this case --> time-out */
378 if (iic_tpm_read(TPM_STS(tpm_dev
.locality
), &buf
, 1) < 0)
382 /* if locallity is set STS should not be 0xFF */
383 } while ((buf
== 0xFF) && i
< 10);
388 static void tpm_tis_i2c_ready(struct tpm_chip
*chip
)
390 /* this causes the current command to be aborted */
391 u8 buf
= TPM_STS_COMMAND_READY
;
392 iic_tpm_write_long(TPM_STS(tpm_dev
.locality
), &buf
, 1);
395 static ssize_t
get_burstcount(struct tpm_chip
*chip
)
401 /* wait for burstcount */
402 /* which timeout value, spec has 2 answers (c & d) */
403 stop
= jiffies
+ chip
->timeout_d
;
405 /* Note: STS is little endian */
406 if (iic_tpm_read(TPM_STS(tpm_dev
.locality
)+1, buf
, 3) < 0)
409 burstcnt
= (buf
[2] << 16) + (buf
[1] << 8) + buf
[0];
414 usleep_range(TPM_TIMEOUT_US_LOW
, TPM_TIMEOUT_US_HI
);
415 } while (time_before(jiffies
, stop
));
419 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, unsigned long timeout
,
424 /* check current status */
425 *status
= tpm_tis_i2c_status(chip
);
426 if ((*status
!= 0xFF) && (*status
& mask
) == mask
)
429 stop
= jiffies
+ timeout
;
431 /* since we just checked the status, give the TPM some time */
432 usleep_range(TPM_TIMEOUT_US_LOW
, TPM_TIMEOUT_US_HI
);
433 *status
= tpm_tis_i2c_status(chip
);
434 if ((*status
& mask
) == mask
)
437 } while (time_before(jiffies
, stop
));
442 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
449 while (size
< count
) {
450 burstcnt
= get_burstcount(chip
);
452 /* burstcnt < 0 = TPM is busy */
456 /* limit received data to max. left */
457 if (burstcnt
> (count
- size
))
458 burstcnt
= count
- size
;
460 rc
= iic_tpm_read(TPM_DATA_FIFO(tpm_dev
.locality
),
461 &(buf
[size
]), burstcnt
);
467 /* avoid endless loop in case of broken HW */
468 if (retries
> MAX_COUNT_LONG
)
474 static int tpm_tis_i2c_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
480 if (count
< TPM_HEADER_SIZE
) {
485 /* read first 10 bytes, including tag, paramsize, and result */
486 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
487 if (size
< TPM_HEADER_SIZE
) {
488 dev_err(&chip
->dev
, "Unable to read header\n");
492 expected
= be32_to_cpu(*(__be32
*)(buf
+ 2));
493 if (((size_t) expected
> count
) || (expected
< TPM_HEADER_SIZE
)) {
498 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
499 expected
- TPM_HEADER_SIZE
);
500 if (size
< expected
) {
501 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
506 wait_for_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
, &status
);
507 if (status
& TPM_STS_DATA_AVAIL
) { /* retry? */
508 dev_err(&chip
->dev
, "Error left over data\n");
514 tpm_tis_i2c_ready(chip
);
515 /* The TPM needs some time to clean up here,
516 * so we sleep rather than keeping the bus busy
518 usleep_range(SLEEP_DURATION_RESET_LOW
, SLEEP_DURATION_RESET_HI
);
519 release_locality(chip
, tpm_dev
.locality
, 0);
523 static int tpm_tis_i2c_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
531 if (len
> TPM_I2C_INFINEON_BUFSIZE
)
534 if (request_locality(chip
, 0) < 0)
537 status
= tpm_tis_i2c_status(chip
);
538 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
539 tpm_tis_i2c_ready(chip
);
541 (chip
, TPM_STS_COMMAND_READY
,
542 chip
->timeout_b
, &status
) < 0) {
548 while (count
< len
- 1) {
549 burstcnt
= get_burstcount(chip
);
551 /* burstcnt < 0 = TPM is busy */
555 if (burstcnt
> (len
- 1 - count
))
556 burstcnt
= len
- 1 - count
;
558 rc
= iic_tpm_write(TPM_DATA_FIFO(tpm_dev
.locality
),
559 &(buf
[count
]), burstcnt
);
565 /* avoid endless loop in case of broken HW */
566 if (retries
> MAX_COUNT_LONG
) {
571 wait_for_stat(chip
, TPM_STS_VALID
,
572 chip
->timeout_c
, &status
);
574 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
580 /* write last byte */
581 iic_tpm_write(TPM_DATA_FIFO(tpm_dev
.locality
), &(buf
[count
]), 1);
582 wait_for_stat(chip
, TPM_STS_VALID
, chip
->timeout_c
, &status
);
583 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
589 iic_tpm_write(TPM_STS(tpm_dev
.locality
), &sts
, 1);
593 tpm_tis_i2c_ready(chip
);
594 /* The TPM needs some time to clean up here,
595 * so we sleep rather than keeping the bus busy
597 usleep_range(SLEEP_DURATION_RESET_LOW
, SLEEP_DURATION_RESET_HI
);
598 release_locality(chip
, tpm_dev
.locality
, 0);
602 static bool tpm_tis_i2c_req_canceled(struct tpm_chip
*chip
, u8 status
)
604 return (status
== TPM_STS_COMMAND_READY
);
607 static const struct tpm_class_ops tpm_tis_i2c
= {
608 .flags
= TPM_OPS_AUTO_STARTUP
,
609 .status
= tpm_tis_i2c_status
,
610 .recv
= tpm_tis_i2c_recv
,
611 .send
= tpm_tis_i2c_send
,
612 .cancel
= tpm_tis_i2c_ready
,
613 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
614 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
615 .req_canceled
= tpm_tis_i2c_req_canceled
,
618 static int tpm_tis_i2c_init(struct device
*dev
)
622 struct tpm_chip
*chip
;
624 chip
= tpmm_chip_alloc(dev
, &tpm_tis_i2c
);
626 return PTR_ERR(chip
);
628 /* Default timeouts */
629 chip
->timeout_a
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
630 chip
->timeout_b
= msecs_to_jiffies(TIS_LONG_TIMEOUT
);
631 chip
->timeout_c
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
632 chip
->timeout_d
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
634 if (request_locality(chip
, 0) != 0) {
635 dev_err(dev
, "could not request locality\n");
640 /* read four bytes from DID_VID register */
641 if (iic_tpm_read(TPM_DID_VID(0), (u8
*)&vendor
, 4) < 0) {
642 dev_err(dev
, "could not read vendor id\n");
647 if (vendor
== TPM_TIS_I2C_DID_VID_9645
) {
648 tpm_dev
.chip_type
= SLB9645
;
649 } else if (vendor
== TPM_TIS_I2C_DID_VID_9635
) {
650 tpm_dev
.chip_type
= SLB9635
;
652 dev_err(dev
, "vendor id did not match! ID was %08x\n", vendor
);
657 dev_info(dev
, "1.2 TPM (device-id 0x%X)\n", vendor
>> 16);
661 return tpm_chip_register(chip
);
663 release_locality(chip
, tpm_dev
.locality
, 1);
664 tpm_dev
.client
= NULL
;
669 static const struct i2c_device_id tpm_tis_i2c_table
[] = {
670 {"tpm_i2c_infineon"},
676 MODULE_DEVICE_TABLE(i2c
, tpm_tis_i2c_table
);
679 static const struct of_device_id tpm_tis_i2c_of_match
[] = {
680 {.compatible
= "infineon,tpm_i2c_infineon"},
681 {.compatible
= "infineon,slb9635tt"},
682 {.compatible
= "infineon,slb9645tt"},
685 MODULE_DEVICE_TABLE(of
, tpm_tis_i2c_of_match
);
688 static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops
, tpm_pm_suspend
, tpm_pm_resume
);
690 static int tpm_tis_i2c_probe(struct i2c_client
*client
,
691 const struct i2c_device_id
*id
)
694 struct device
*dev
= &(client
->dev
);
696 if (tpm_dev
.client
!= NULL
) {
697 dev_err(dev
, "This driver only supports one client at a time\n");
698 return -EBUSY
; /* We only support one client */
701 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
702 dev_err(dev
, "no algorithms associated to the i2c bus\n");
706 tpm_dev
.client
= client
;
707 rc
= tpm_tis_i2c_init(&client
->dev
);
709 tpm_dev
.client
= NULL
;
715 static int tpm_tis_i2c_remove(struct i2c_client
*client
)
717 struct tpm_chip
*chip
= tpm_dev
.chip
;
719 tpm_chip_unregister(chip
);
720 release_locality(chip
, tpm_dev
.locality
, 1);
721 tpm_dev
.client
= NULL
;
726 static struct i2c_driver tpm_tis_i2c_driver
= {
727 .id_table
= tpm_tis_i2c_table
,
728 .probe
= tpm_tis_i2c_probe
,
729 .remove
= tpm_tis_i2c_remove
,
731 .name
= "tpm_i2c_infineon",
732 .pm
= &tpm_tis_i2c_ops
,
733 .of_match_table
= of_match_ptr(tpm_tis_i2c_of_match
),
737 module_i2c_driver(tpm_tis_i2c_driver
);
738 MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
739 MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
740 MODULE_VERSION("2.2.0");
741 MODULE_LICENSE("GPL");