aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/tpm
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/tpm')
-rw-r--r--drivers/char/tpm/Kconfig12
-rw-r--r--drivers/char/tpm/Makefile1
-rw-r--r--drivers/char/tpm/tpm.c114
-rw-r--r--drivers/char/tpm/tpm.h52
-rw-r--r--drivers/char/tpm/tpm_acpi.c8
-rw-r--r--drivers/char/tpm/tpm_atmel.c7
-rw-r--r--drivers/char/tpm/tpm_i2c_infineon.c7
-rw-r--r--drivers/char/tpm/tpm_i2c_stm_st33.c887
-rw-r--r--drivers/char/tpm/tpm_i2c_stm_st33.h61
-rw-r--r--drivers/char/tpm/tpm_ibmvtpm.c15
-rw-r--r--drivers/char/tpm/tpm_nsc.c7
-rw-r--r--drivers/char/tpm/tpm_tis.c64
12 files changed, 1155 insertions, 80 deletions
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index 915875e431d2..dbfd56446c31 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -75,10 +75,20 @@ config TCG_INFINEON
75 75
76config TCG_IBMVTPM 76config TCG_IBMVTPM
77 tristate "IBM VTPM Interface" 77 tristate "IBM VTPM Interface"
78 depends on PPC64 78 depends on PPC_PSERIES
79 ---help--- 79 ---help---
80 If you have IBM virtual TPM (VTPM) support say Yes and it 80 If you have IBM virtual TPM (VTPM) support say Yes and it
81 will be accessible from within Linux. To compile this driver 81 will be accessible from within Linux. To compile this driver
82 as a module, choose M here; the module will be called tpm_ibmvtpm. 82 as a module, choose M here; the module will be called tpm_ibmvtpm.
83 83
84config TCG_ST33_I2C
85 tristate "STMicroelectronics ST33 I2C TPM"
86 depends on I2C
87 depends on GPIOLIB
88 ---help---
89 If you have a TPM security chip from STMicroelectronics working with
90 an I2C bus say Yes and it will be accessible from within Linux.
91 To compile this driver as a module, choose M here; the module will be
92 called tpm_stm_st33_i2c.
93
84endif # TCG_TPM 94endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index 5b3fc8bc6c13..a3736c97c65a 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -17,3 +17,4 @@ obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
17obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o 17obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
18obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o 18obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
19obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o 19obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
20obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm.c
index 93211df52aab..0d2e82f95577 100644
--- a/drivers/char/tpm/tpm.c
+++ b/drivers/char/tpm/tpm.c
@@ -40,8 +40,9 @@ enum tpm_duration {
40}; 40};
41 41
42#define TPM_MAX_ORDINAL 243 42#define TPM_MAX_ORDINAL 243
43#define TPM_MAX_PROTECTED_ORDINAL 12 43#define TSC_MAX_ORDINAL 12
44#define TPM_PROTECTED_ORDINAL_MASK 0xFF 44#define TPM_PROTECTED_COMMAND 0x00
45#define TPM_CONNECTION_COMMAND 0x40
45 46
46/* 47/*
47 * Bug workaround - some TPM's don't flush the most 48 * Bug workaround - some TPM's don't flush the most
@@ -65,21 +66,6 @@ static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
65 * values of the SHORT, MEDIUM, and LONG durations are retrieved 66 * values of the SHORT, MEDIUM, and LONG durations are retrieved
66 * from the chip during initialization with a call to tpm_get_timeouts. 67 * from the chip during initialization with a call to tpm_get_timeouts.
67 */ 68 */
68static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
69 TPM_UNDEFINED, /* 0 */
70 TPM_UNDEFINED,
71 TPM_UNDEFINED,
72 TPM_UNDEFINED,
73 TPM_UNDEFINED,
74 TPM_UNDEFINED, /* 5 */
75 TPM_UNDEFINED,
76 TPM_UNDEFINED,
77 TPM_UNDEFINED,
78 TPM_UNDEFINED,
79 TPM_SHORT, /* 10 */
80 TPM_SHORT,
81};
82
83static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { 69static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
84 TPM_UNDEFINED, /* 0 */ 70 TPM_UNDEFINED, /* 0 */
85 TPM_UNDEFINED, 71 TPM_UNDEFINED,
@@ -351,14 +337,11 @@ unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
351{ 337{
352 int duration_idx = TPM_UNDEFINED; 338 int duration_idx = TPM_UNDEFINED;
353 int duration = 0; 339 int duration = 0;
340 u8 category = (ordinal >> 24) & 0xFF;
354 341
355 if (ordinal < TPM_MAX_ORDINAL) 342 if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) ||
343 (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL))
356 duration_idx = tpm_ordinal_duration[ordinal]; 344 duration_idx = tpm_ordinal_duration[ordinal];
357 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
358 TPM_MAX_PROTECTED_ORDINAL)
359 duration_idx =
360 tpm_protected_ordinal_duration[ordinal &
361 TPM_PROTECTED_ORDINAL_MASK];
362 345
363 if (duration_idx != TPM_UNDEFINED) 346 if (duration_idx != TPM_UNDEFINED)
364 duration = chip->vendor.duration[duration_idx]; 347 duration = chip->vendor.duration[duration_idx];
@@ -410,7 +393,7 @@ static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
410 chip->vendor.req_complete_val) 393 chip->vendor.req_complete_val)
411 goto out_recv; 394 goto out_recv;
412 395
413 if ((status == chip->vendor.req_canceled)) { 396 if (chip->vendor.req_canceled(chip, status)) {
414 dev_err(chip->dev, "Operation Canceled\n"); 397 dev_err(chip->dev, "Operation Canceled\n");
415 rc = -ECANCELED; 398 rc = -ECANCELED;
416 goto out; 399 goto out;
@@ -468,7 +451,7 @@ static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
468 return -EFAULT; 451 return -EFAULT;
469 452
470 err = be32_to_cpu(cmd->header.out.return_code); 453 err = be32_to_cpu(cmd->header.out.return_code);
471 if (err != 0) 454 if (err != 0 && desc)
472 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); 455 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
473 456
474 return err; 457 return err;
@@ -528,6 +511,25 @@ void tpm_gen_interrupt(struct tpm_chip *chip)
528} 511}
529EXPORT_SYMBOL_GPL(tpm_gen_interrupt); 512EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
530 513
514#define TPM_ORD_STARTUP cpu_to_be32(153)
515#define TPM_ST_CLEAR cpu_to_be16(1)
516#define TPM_ST_STATE cpu_to_be16(2)
517#define TPM_ST_DEACTIVATED cpu_to_be16(3)
518static const struct tpm_input_header tpm_startup_header = {
519 .tag = TPM_TAG_RQU_COMMAND,
520 .length = cpu_to_be32(12),
521 .ordinal = TPM_ORD_STARTUP
522};
523
524static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
525{
526 struct tpm_cmd_t start_cmd;
527 start_cmd.header.in = tpm_startup_header;
528 start_cmd.params.startup_in.startup_type = startup_type;
529 return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
530 "attempting to start the TPM");
531}
532
531int tpm_get_timeouts(struct tpm_chip *chip) 533int tpm_get_timeouts(struct tpm_chip *chip)
532{ 534{
533 struct tpm_cmd_t tpm_cmd; 535 struct tpm_cmd_t tpm_cmd;
@@ -541,11 +543,28 @@ int tpm_get_timeouts(struct tpm_chip *chip)
541 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 543 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
542 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 544 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
543 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 545 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
546 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
544 547
545 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 548 if (rc == TPM_ERR_INVALID_POSTINIT) {
546 "attempting to determine the timeouts"); 549 /* The TPM is not started, we are the first to talk to it.
547 if (rc) 550 Execute a startup command. */
551 dev_info(chip->dev, "Issuing TPM_STARTUP");
552 if (tpm_startup(chip, TPM_ST_CLEAR))
553 return rc;
554
555 tpm_cmd.header.in = tpm_getcap_header;
556 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
557 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
558 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
559 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
560 NULL);
561 }
562 if (rc) {
563 dev_err(chip->dev,
564 "A TPM error (%zd) occurred attempting to determine the timeouts\n",
565 rc);
548 goto duration; 566 goto duration;
567 }
549 568
550 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || 569 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
551 be32_to_cpu(tpm_cmd.header.out.length) 570 be32_to_cpu(tpm_cmd.header.out.length)
@@ -824,7 +843,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
824{ 843{
825 int rc; 844 int rc;
826 unsigned int loops; 845 unsigned int loops;
827 unsigned int delay_msec = 1000; 846 unsigned int delay_msec = 100;
828 unsigned long duration; 847 unsigned long duration;
829 struct tpm_cmd_t cmd; 848 struct tpm_cmd_t cmd;
830 849
@@ -845,6 +864,14 @@ int tpm_do_selftest(struct tpm_chip *chip)
845 cmd.header.in = pcrread_header; 864 cmd.header.in = pcrread_header;
846 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0); 865 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
847 rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE); 866 rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
867 /* Some buggy TPMs will not respond to tpm_tis_ready() for
868 * around 300ms while the self test is ongoing, keep trying
869 * until the self test duration expires. */
870 if (rc == -ETIME) {
871 dev_info(chip->dev, HW_ERR "TPM command timed out during continue self test");
872 msleep(delay_msec);
873 continue;
874 }
848 875
849 if (rc < TPM_HEADER_SIZE) 876 if (rc < TPM_HEADER_SIZE)
850 return -EFAULT; 877 return -EFAULT;
@@ -1075,12 +1102,28 @@ ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1075} 1102}
1076EXPORT_SYMBOL_GPL(tpm_store_cancel); 1103EXPORT_SYMBOL_GPL(tpm_store_cancel);
1077 1104
1105static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, bool check_cancel,
1106 bool *canceled)
1107{
1108 u8 status = chip->vendor.status(chip);
1109
1110 *canceled = false;
1111 if ((status & mask) == mask)
1112 return true;
1113 if (check_cancel && chip->vendor.req_canceled(chip, status)) {
1114 *canceled = true;
1115 return true;
1116 }
1117 return false;
1118}
1119
1078int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 1120int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1079 wait_queue_head_t *queue) 1121 wait_queue_head_t *queue, bool check_cancel)
1080{ 1122{
1081 unsigned long stop; 1123 unsigned long stop;
1082 long rc; 1124 long rc;
1083 u8 status; 1125 u8 status;
1126 bool canceled = false;
1084 1127
1085 /* check current status */ 1128 /* check current status */
1086 status = chip->vendor.status(chip); 1129 status = chip->vendor.status(chip);
@@ -1095,11 +1138,14 @@ again:
1095 if ((long)timeout <= 0) 1138 if ((long)timeout <= 0)
1096 return -ETIME; 1139 return -ETIME;
1097 rc = wait_event_interruptible_timeout(*queue, 1140 rc = wait_event_interruptible_timeout(*queue,
1098 ((chip->vendor.status(chip) 1141 wait_for_tpm_stat_cond(chip, mask, check_cancel,
1099 & mask) == mask), 1142 &canceled),
1100 timeout); 1143 timeout);
1101 if (rc > 0) 1144 if (rc > 0) {
1145 if (canceled)
1146 return -ECANCELED;
1102 return 0; 1147 return 0;
1148 }
1103 if (rc == -ERESTARTSYS && freezing(current)) { 1149 if (rc == -ERESTARTSYS && freezing(current)) {
1104 clear_thread_flag(TIF_SIGPENDING); 1150 clear_thread_flag(TIF_SIGPENDING);
1105 goto again; 1151 goto again;
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 8ef7649a50aa..81b52015f669 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -47,6 +47,7 @@ enum tpm_addr {
47#define TPM_WARN_DOING_SELFTEST 0x802 47#define TPM_WARN_DOING_SELFTEST 0x802
48#define TPM_ERR_DEACTIVATED 0x6 48#define TPM_ERR_DEACTIVATED 0x6
49#define TPM_ERR_DISABLED 0x7 49#define TPM_ERR_DISABLED 0x7
50#define TPM_ERR_INVALID_POSTINIT 38
50 51
51#define TPM_HEADER_SIZE 10 52#define TPM_HEADER_SIZE 10
52extern ssize_t tpm_show_pubek(struct device *, struct device_attribute *attr, 53extern ssize_t tpm_show_pubek(struct device *, struct device_attribute *attr,
@@ -77,7 +78,7 @@ struct tpm_chip;
77struct tpm_vendor_specific { 78struct tpm_vendor_specific {
78 const u8 req_complete_mask; 79 const u8 req_complete_mask;
79 const u8 req_complete_val; 80 const u8 req_complete_val;
80 const u8 req_canceled; 81 bool (*req_canceled)(struct tpm_chip *chip, u8 status);
81 void __iomem *iobase; /* ioremapped address */ 82 void __iomem *iobase; /* ioremapped address */
82 unsigned long base; /* TPM base address */ 83 unsigned long base; /* TPM base address */
83 84
@@ -100,13 +101,19 @@ struct tpm_vendor_specific {
100 bool timeout_adjusted; 101 bool timeout_adjusted;
101 unsigned long duration[3]; /* jiffies */ 102 unsigned long duration[3]; /* jiffies */
102 bool duration_adjusted; 103 bool duration_adjusted;
103 void *data; 104 void *priv;
104 105
105 wait_queue_head_t read_queue; 106 wait_queue_head_t read_queue;
106 wait_queue_head_t int_queue; 107 wait_queue_head_t int_queue;
108
109 u16 manufacturer_id;
107}; 110};
108 111
112#define TPM_VPRIV(c) (c)->vendor.priv
113
109#define TPM_VID_INTEL 0x8086 114#define TPM_VID_INTEL 0x8086
115#define TPM_VID_WINBOND 0x1050
116#define TPM_VID_STM 0x104A
110 117
111struct tpm_chip { 118struct tpm_chip {
112 struct device *dev; /* Device stuff */ 119 struct device *dev; /* Device stuff */
@@ -154,13 +161,13 @@ struct tpm_input_header {
154 __be16 tag; 161 __be16 tag;
155 __be32 length; 162 __be32 length;
156 __be32 ordinal; 163 __be32 ordinal;
157}__attribute__((packed)); 164} __packed;
158 165
159struct tpm_output_header { 166struct tpm_output_header {
160 __be16 tag; 167 __be16 tag;
161 __be32 length; 168 __be32 length;
162 __be32 return_code; 169 __be32 return_code;
163}__attribute__((packed)); 170} __packed;
164 171
165struct stclear_flags_t { 172struct stclear_flags_t {
166 __be16 tag; 173 __be16 tag;
@@ -169,14 +176,14 @@ struct stclear_flags_t {
169 u8 physicalPresence; 176 u8 physicalPresence;
170 u8 physicalPresenceLock; 177 u8 physicalPresenceLock;
171 u8 bGlobalLock; 178 u8 bGlobalLock;
172}__attribute__((packed)); 179} __packed;
173 180
174struct tpm_version_t { 181struct tpm_version_t {
175 u8 Major; 182 u8 Major;
176 u8 Minor; 183 u8 Minor;
177 u8 revMajor; 184 u8 revMajor;
178 u8 revMinor; 185 u8 revMinor;
179}__attribute__((packed)); 186} __packed;
180 187
181struct tpm_version_1_2_t { 188struct tpm_version_1_2_t {
182 __be16 tag; 189 __be16 tag;
@@ -184,20 +191,20 @@ struct tpm_version_1_2_t {
184 u8 Minor; 191 u8 Minor;
185 u8 revMajor; 192 u8 revMajor;
186 u8 revMinor; 193 u8 revMinor;
187}__attribute__((packed)); 194} __packed;
188 195
189struct timeout_t { 196struct timeout_t {
190 __be32 a; 197 __be32 a;
191 __be32 b; 198 __be32 b;
192 __be32 c; 199 __be32 c;
193 __be32 d; 200 __be32 d;
194}__attribute__((packed)); 201} __packed;
195 202
196struct duration_t { 203struct duration_t {
197 __be32 tpm_short; 204 __be32 tpm_short;
198 __be32 tpm_medium; 205 __be32 tpm_medium;
199 __be32 tpm_long; 206 __be32 tpm_long;
200}__attribute__((packed)); 207} __packed;
201 208
202struct permanent_flags_t { 209struct permanent_flags_t {
203 __be16 tag; 210 __be16 tag;
@@ -221,7 +228,7 @@ struct permanent_flags_t {
221 u8 tpmEstablished; 228 u8 tpmEstablished;
222 u8 maintenanceDone; 229 u8 maintenanceDone;
223 u8 disableFullDALogicInfo; 230 u8 disableFullDALogicInfo;
224}__attribute__((packed)); 231} __packed;
225 232
226typedef union { 233typedef union {
227 struct permanent_flags_t perm_flags; 234 struct permanent_flags_t perm_flags;
@@ -239,12 +246,12 @@ struct tpm_getcap_params_in {
239 __be32 cap; 246 __be32 cap;
240 __be32 subcap_size; 247 __be32 subcap_size;
241 __be32 subcap; 248 __be32 subcap;
242}__attribute__((packed)); 249} __packed;
243 250
244struct tpm_getcap_params_out { 251struct tpm_getcap_params_out {
245 __be32 cap_size; 252 __be32 cap_size;
246 cap_t cap; 253 cap_t cap;
247}__attribute__((packed)); 254} __packed;
248 255
249struct tpm_readpubek_params_out { 256struct tpm_readpubek_params_out {
250 u8 algorithm[4]; 257 u8 algorithm[4];
@@ -255,7 +262,7 @@ struct tpm_readpubek_params_out {
255 __be32 keysize; 262 __be32 keysize;
256 u8 modulus[256]; 263 u8 modulus[256];
257 u8 checksum[20]; 264 u8 checksum[20];
258}__attribute__((packed)); 265} __packed;
259 266
260typedef union { 267typedef union {
261 struct tpm_input_header in; 268 struct tpm_input_header in;
@@ -265,16 +272,16 @@ typedef union {
265#define TPM_DIGEST_SIZE 20 272#define TPM_DIGEST_SIZE 20
266struct tpm_pcrread_out { 273struct tpm_pcrread_out {
267 u8 pcr_result[TPM_DIGEST_SIZE]; 274 u8 pcr_result[TPM_DIGEST_SIZE];
268}__attribute__((packed)); 275} __packed;
269 276
270struct tpm_pcrread_in { 277struct tpm_pcrread_in {
271 __be32 pcr_idx; 278 __be32 pcr_idx;
272}__attribute__((packed)); 279} __packed;
273 280
274struct tpm_pcrextend_in { 281struct tpm_pcrextend_in {
275 __be32 pcr_idx; 282 __be32 pcr_idx;
276 u8 hash[TPM_DIGEST_SIZE]; 283 u8 hash[TPM_DIGEST_SIZE];
277}__attribute__((packed)); 284} __packed;
278 285
279/* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18 286/* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
280 * bytes, but 128 is still a relatively large number of random bytes and 287 * bytes, but 128 is still a relatively large number of random bytes and
@@ -285,11 +292,15 @@ struct tpm_pcrextend_in {
285struct tpm_getrandom_out { 292struct tpm_getrandom_out {
286 __be32 rng_data_len; 293 __be32 rng_data_len;
287 u8 rng_data[TPM_MAX_RNG_DATA]; 294 u8 rng_data[TPM_MAX_RNG_DATA];
288}__attribute__((packed)); 295} __packed;
289 296
290struct tpm_getrandom_in { 297struct tpm_getrandom_in {
291 __be32 num_bytes; 298 __be32 num_bytes;
292}__attribute__((packed)); 299} __packed;
300
301struct tpm_startup_in {
302 __be16 startup_type;
303} __packed;
293 304
294typedef union { 305typedef union {
295 struct tpm_getcap_params_out getcap_out; 306 struct tpm_getcap_params_out getcap_out;
@@ -301,12 +312,13 @@ typedef union {
301 struct tpm_pcrextend_in pcrextend_in; 312 struct tpm_pcrextend_in pcrextend_in;
302 struct tpm_getrandom_in getrandom_in; 313 struct tpm_getrandom_in getrandom_in;
303 struct tpm_getrandom_out getrandom_out; 314 struct tpm_getrandom_out getrandom_out;
315 struct tpm_startup_in startup_in;
304} tpm_cmd_params; 316} tpm_cmd_params;
305 317
306struct tpm_cmd_t { 318struct tpm_cmd_t {
307 tpm_cmd_header header; 319 tpm_cmd_header header;
308 tpm_cmd_params params; 320 tpm_cmd_params params;
309}__attribute__((packed)); 321} __packed;
310 322
311ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *); 323ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
312 324
@@ -326,7 +338,7 @@ extern void tpm_remove_hardware(struct device *);
326extern int tpm_pm_suspend(struct device *); 338extern int tpm_pm_suspend(struct device *);
327extern int tpm_pm_resume(struct device *); 339extern int tpm_pm_resume(struct device *);
328extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long, 340extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
329 wait_queue_head_t *); 341 wait_queue_head_t *, bool);
330 342
331#ifdef CONFIG_ACPI 343#ifdef CONFIG_ACPI
332extern int tpm_add_ppi(struct kobject *); 344extern int tpm_add_ppi(struct kobject *);
diff --git a/drivers/char/tpm/tpm_acpi.c b/drivers/char/tpm/tpm_acpi.c
index 56051d0c97a2..64420b3396a2 100644
--- a/drivers/char/tpm/tpm_acpi.c
+++ b/drivers/char/tpm/tpm_acpi.c
@@ -33,13 +33,13 @@ struct acpi_tcpa {
33 u16 platform_class; 33 u16 platform_class;
34 union { 34 union {
35 struct client_hdr { 35 struct client_hdr {
36 u32 log_max_len __attribute__ ((packed)); 36 u32 log_max_len __packed;
37 u64 log_start_addr __attribute__ ((packed)); 37 u64 log_start_addr __packed;
38 } client; 38 } client;
39 struct server_hdr { 39 struct server_hdr {
40 u16 reserved; 40 u16 reserved;
41 u64 log_max_len __attribute__ ((packed)); 41 u64 log_max_len __packed;
42 u64 log_start_addr __attribute__ ((packed)); 42 u64 log_start_addr __packed;
43 } server; 43 } server;
44 }; 44 };
45}; 45};
diff --git a/drivers/char/tpm/tpm_atmel.c b/drivers/char/tpm/tpm_atmel.c
index 678d57019dc4..99d6820c611d 100644
--- a/drivers/char/tpm/tpm_atmel.c
+++ b/drivers/char/tpm/tpm_atmel.c
@@ -116,6 +116,11 @@ static u8 tpm_atml_status(struct tpm_chip *chip)
116 return ioread8(chip->vendor.iobase + 1); 116 return ioread8(chip->vendor.iobase + 1);
117} 117}
118 118
119static bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status)
120{
121 return (status == ATML_STATUS_READY);
122}
123
119static const struct file_operations atmel_ops = { 124static const struct file_operations atmel_ops = {
120 .owner = THIS_MODULE, 125 .owner = THIS_MODULE,
121 .llseek = no_llseek, 126 .llseek = no_llseek,
@@ -147,7 +152,7 @@ static const struct tpm_vendor_specific tpm_atmel = {
147 .status = tpm_atml_status, 152 .status = tpm_atml_status,
148 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL, 153 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
149 .req_complete_val = ATML_STATUS_DATA_AVAIL, 154 .req_complete_val = ATML_STATUS_DATA_AVAIL,
150 .req_canceled = ATML_STATUS_READY, 155 .req_canceled = tpm_atml_req_canceled,
151 .attr_group = &atmel_attr_grp, 156 .attr_group = &atmel_attr_grp,
152 .miscdev = { .fops = &atmel_ops, }, 157 .miscdev = { .fops = &atmel_ops, },
153}; 158};
diff --git a/drivers/char/tpm/tpm_i2c_infineon.c b/drivers/char/tpm/tpm_i2c_infineon.c
index fb447bd0cb61..8fe7ac3d095b 100644
--- a/drivers/char/tpm/tpm_i2c_infineon.c
+++ b/drivers/char/tpm/tpm_i2c_infineon.c
@@ -505,6 +505,11 @@ out_err:
505 return rc; 505 return rc;
506} 506}
507 507
508static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
509{
510 return (status == TPM_STS_COMMAND_READY);
511}
512
508static const struct file_operations tis_ops = { 513static const struct file_operations tis_ops = {
509 .owner = THIS_MODULE, 514 .owner = THIS_MODULE,
510 .llseek = no_llseek, 515 .llseek = no_llseek,
@@ -550,7 +555,7 @@ static struct tpm_vendor_specific tpm_tis_i2c = {
550 .cancel = tpm_tis_i2c_ready, 555 .cancel = tpm_tis_i2c_ready,
551 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 556 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
552 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 557 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
553 .req_canceled = TPM_STS_COMMAND_READY, 558 .req_canceled = tpm_tis_i2c_req_canceled,
554 .attr_group = &tis_attr_grp, 559 .attr_group = &tis_attr_grp,
555 .miscdev.fops = &tis_ops, 560 .miscdev.fops = &tis_ops,
556}; 561};
diff --git a/drivers/char/tpm/tpm_i2c_stm_st33.c b/drivers/char/tpm/tpm_i2c_stm_st33.c
new file mode 100644
index 000000000000..1f5f71e14abe
--- /dev/null
+++ b/drivers/char/tpm/tpm_i2c_stm_st33.c
@@ -0,0 +1,887 @@
1/*
2 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009, 2010 STMicroelectronics
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * STMicroelectronics version 1.2.0, Copyright (C) 2010
20 * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
21 * This is free software, and you are welcome to redistribute it
22 * under certain conditions.
23 *
24 * @Author: Christophe RICARD tpmsupport@st.com
25 *
26 * @File: tpm_stm_st33_i2c.c
27 *
28 * @Synopsis:
29 * 09/15/2010: First shot driver tpm_tis driver for
30 lpc is used as model.
31 */
32
33#include <linux/pci.h>
34#include <linux/module.h>
35#include <linux/platform_device.h>
36#include <linux/i2c.h>
37#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/init.h>
43#include <linux/wait.h>
44#include <linux/string.h>
45#include <linux/interrupt.h>
46#include <linux/spinlock.h>
47#include <linux/sysfs.h>
48#include <linux/gpio.h>
49#include <linux/sched.h>
50#include <linux/uaccess.h>
51#include <linux/io.h>
52#include <linux/slab.h>
53#include <linux/sched.h>
54
55#include "tpm.h"
56#include "tpm_i2c_stm_st33.h"
57
58enum stm33zp24_access {
59 TPM_ACCESS_VALID = 0x80,
60 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
61 TPM_ACCESS_REQUEST_PENDING = 0x04,
62 TPM_ACCESS_REQUEST_USE = 0x02,
63};
64
65enum stm33zp24_status {
66 TPM_STS_VALID = 0x80,
67 TPM_STS_COMMAND_READY = 0x40,
68 TPM_STS_GO = 0x20,
69 TPM_STS_DATA_AVAIL = 0x10,
70 TPM_STS_DATA_EXPECT = 0x08,
71};
72
73enum stm33zp24_int_flags {
74 TPM_GLOBAL_INT_ENABLE = 0x80,
75 TPM_INTF_CMD_READY_INT = 0x080,
76 TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
77 TPM_INTF_WAKE_UP_READY_INT = 0x020,
78 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
79 TPM_INTF_STS_VALID_INT = 0x002,
80 TPM_INTF_DATA_AVAIL_INT = 0x001,
81};
82
83enum tis_defaults {
84 TIS_SHORT_TIMEOUT = 750,
85 TIS_LONG_TIMEOUT = 2000,
86};
87
88/*
89 * write8_reg
90 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
91 * @param: tpm_register, the tpm tis register where the data should be written
92 * @param: tpm_data, the tpm_data to write inside the tpm_register
93 * @param: tpm_size, The length of the data
94 * @return: Returns negative errno, or else the number of bytes written.
95 */
96static int write8_reg(struct i2c_client *client, u8 tpm_register,
97 u8 *tpm_data, u16 tpm_size)
98{
99 struct st33zp24_platform_data *pin_infos;
100
101 pin_infos = client->dev.platform_data;
102
103 pin_infos->tpm_i2c_buffer[0][0] = tpm_register;
104 memcpy(&pin_infos->tpm_i2c_buffer[0][1], tpm_data, tpm_size);
105 return i2c_master_send(client, pin_infos->tpm_i2c_buffer[0],
106 tpm_size + 1);
107} /* write8_reg() */
108
109/*
110 * read8_reg
111 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
112 * @param: tpm_register, the tpm tis register where the data should be read
113 * @param: tpm_data, the TPM response
114 * @param: tpm_size, tpm TPM response size to read.
115 * @return: number of byte read successfully: should be one if success.
116 */
117static int read8_reg(struct i2c_client *client, u8 tpm_register,
118 u8 *tpm_data, int tpm_size)
119{
120 u8 status = 0;
121 u8 data;
122
123 data = TPM_DUMMY_BYTE;
124 status = write8_reg(client, tpm_register, &data, 1);
125 if (status == 2)
126 status = i2c_master_recv(client, tpm_data, tpm_size);
127 return status;
128} /* read8_reg() */
129
130/*
131 * I2C_WRITE_DATA
132 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
133 * @param: client, the chip description
134 * @param: tpm_register, the tpm tis register where the data should be written
135 * @param: tpm_data, the tpm_data to write inside the tpm_register
136 * @param: tpm_size, The length of the data
137 * @return: number of byte written successfully: should be one if success.
138 */
139#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \
140 (write8_reg(client, tpm_register | \
141 TPM_WRITE_DIRECTION, tpm_data, tpm_size))
142
143/*
144 * I2C_READ_DATA
145 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
146 * @param: tpm, the chip description
147 * @param: tpm_register, the tpm tis register where the data should be read
148 * @param: tpm_data, the TPM response
149 * @param: tpm_size, tpm TPM response size to read.
150 * @return: number of byte read successfully: should be one if success.
151 */
152#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \
153 (read8_reg(client, tpm_register, tpm_data, tpm_size))
154
155/*
156 * clear_interruption
157 * clear the TPM interrupt register.
158 * @param: tpm, the chip description
159 */
160static void clear_interruption(struct i2c_client *client)
161{
162 u8 interrupt;
163 I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
164 I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1);
165 I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
166} /* clear_interruption() */
167
168/*
169 * _wait_for_interrupt_serirq_timeout
170 * @param: tpm, the chip description
171 * @param: timeout, the timeout of the interrupt
172 * @return: the status of the interruption.
173 */
174static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
175 unsigned long timeout)
176{
177 long status;
178 struct i2c_client *client;
179 struct st33zp24_platform_data *pin_infos;
180
181 client = (struct i2c_client *) TPM_VPRIV(chip);
182 pin_infos = client->dev.platform_data;
183
184 status = wait_for_completion_interruptible_timeout(
185 &pin_infos->irq_detection,
186 timeout);
187 if (status > 0)
188 enable_irq(gpio_to_irq(pin_infos->io_serirq));
189 gpio_direction_input(pin_infos->io_serirq);
190
191 return status;
192} /* wait_for_interrupt_serirq_timeout() */
193
194static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
195 unsigned long timeout)
196{
197 int status = 2;
198 struct i2c_client *client;
199
200 client = (struct i2c_client *) TPM_VPRIV(chip);
201
202 status = _wait_for_interrupt_serirq_timeout(chip, timeout);
203 if (!status) {
204 status = -EBUSY;
205 } else{
206 clear_interruption(client);
207 if (condition)
208 status = 1;
209 }
210 return status;
211}
212
213/*
214 * tpm_stm_i2c_cancel, cancel is not implemented.
215 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
216 */
217static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
218{
219 struct i2c_client *client;
220 u8 data;
221
222 client = (struct i2c_client *) TPM_VPRIV(chip);
223
224 data = TPM_STS_COMMAND_READY;
225 I2C_WRITE_DATA(client, TPM_STS, &data, 1);
226 if (chip->vendor.irq)
227 wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
228} /* tpm_stm_i2c_cancel() */
229
230/*
231 * tpm_stm_spi_status return the TPM_STS register
232 * @param: chip, the tpm chip description
233 * @return: the TPM_STS register value.
234 */
235static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
236{
237 struct i2c_client *client;
238 u8 data;
239 client = (struct i2c_client *) TPM_VPRIV(chip);
240
241 I2C_READ_DATA(client, TPM_STS, &data, 1);
242 return data;
243} /* tpm_stm_i2c_status() */
244
245
246/*
247 * check_locality if the locality is active
248 * @param: chip, the tpm chip description
249 * @return: the active locality or -EACCESS.
250 */
251static int check_locality(struct tpm_chip *chip)
252{
253 struct i2c_client *client;
254 u8 data;
255 u8 status;
256
257 client = (struct i2c_client *) TPM_VPRIV(chip);
258
259 status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1);
260 if (status && (data &
261 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
262 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
263 return chip->vendor.locality;
264
265 return -EACCES;
266
267} /* check_locality() */
268
269/*
270 * request_locality request the TPM locality
271 * @param: chip, the chip description
272 * @return: the active locality or EACCESS.
273 */
274static int request_locality(struct tpm_chip *chip)
275{
276 unsigned long stop;
277 long rc;
278 struct i2c_client *client;
279 u8 data;
280
281 client = (struct i2c_client *) TPM_VPRIV(chip);
282
283 if (check_locality(chip) == chip->vendor.locality)
284 return chip->vendor.locality;
285
286 data = TPM_ACCESS_REQUEST_USE;
287 rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
288 if (rc < 0)
289 goto end;
290
291 if (chip->vendor.irq) {
292 rc = wait_for_serirq_timeout(chip, (check_locality
293 (chip) >= 0),
294 chip->vendor.timeout_a);
295 if (rc > 0)
296 return chip->vendor.locality;
297 } else{
298 stop = jiffies + chip->vendor.timeout_a;
299 do {
300 if (check_locality(chip) >= 0)
301 return chip->vendor.locality;
302 msleep(TPM_TIMEOUT);
303 } while (time_before(jiffies, stop));
304 }
305 rc = -EACCES;
306end:
307 return rc;
308} /* request_locality() */
309
310/*
311 * release_locality release the active locality
312 * @param: chip, the tpm chip description.
313 */
314static void release_locality(struct tpm_chip *chip)
315{
316 struct i2c_client *client;
317 u8 data;
318
319 client = (struct i2c_client *) TPM_VPRIV(chip);
320 data = TPM_ACCESS_ACTIVE_LOCALITY;
321
322 I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
323}
324
325/*
326 * get_burstcount return the burstcount address 0x19 0x1A
327 * @param: chip, the chip description
328 * return: the burstcount.
329 */
330static int get_burstcount(struct tpm_chip *chip)
331{
332 unsigned long stop;
333 int burstcnt, status;
334 u8 tpm_reg, temp;
335
336 struct i2c_client *client = (struct i2c_client *) TPM_VPRIV(chip);
337
338 stop = jiffies + chip->vendor.timeout_d;
339 do {
340 tpm_reg = TPM_STS + 1;
341 status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
342 if (status < 0)
343 goto end;
344
345 tpm_reg = tpm_reg + 1;
346 burstcnt = temp;
347 status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
348 if (status < 0)
349 goto end;
350
351 burstcnt |= temp << 8;
352 if (burstcnt)
353 return burstcnt;
354 msleep(TPM_TIMEOUT);
355 } while (time_before(jiffies, stop));
356
357end:
358 return -EBUSY;
359} /* get_burstcount() */
360
361/*
362 * wait_for_stat wait for a TPM_STS value
363 * @param: chip, the tpm chip description
364 * @param: mask, the value mask to wait
365 * @param: timeout, the timeout
366 * @param: queue, the wait queue.
367 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
368 */
369static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
370 wait_queue_head_t *queue)
371{
372 unsigned long stop;
373 long rc;
374 u8 status;
375
376 if (chip->vendor.irq) {
377 rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status
378 (chip) & mask) ==
379 mask), timeout);
380 if (rc > 0)
381 return 0;
382 } else{
383 stop = jiffies + timeout;
384 do {
385 msleep(TPM_TIMEOUT);
386 status = tpm_stm_i2c_status(chip);
387 if ((status & mask) == mask)
388 return 0;
389 } while (time_before(jiffies, stop));
390 }
391 return -ETIME;
392} /* wait_for_stat() */
393
394/*
395 * recv_data receive data
396 * @param: chip, the tpm chip description
397 * @param: buf, the buffer where the data are received
398 * @param: count, the number of data to receive
399 * @return: the number of bytes read from TPM FIFO.
400 */
401static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
402{
403 int size = 0, burstcnt, len;
404 struct i2c_client *client;
405
406 client = (struct i2c_client *) TPM_VPRIV(chip);
407
408 while (size < count &&
409 wait_for_stat(chip,
410 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
411 chip->vendor.timeout_c,
412 &chip->vendor.read_queue)
413 == 0) {
414 burstcnt = get_burstcount(chip);
415 len = min_t(int, burstcnt, count - size);
416 I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len);
417 size += len;
418 }
419 return size;
420}
421
422/*
423 * tpm_ioserirq_handler the serirq irq handler
424 * @param: irq, the tpm chip description
425 * @param: dev_id, the description of the chip
426 * @return: the status of the handler.
427 */
428static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
429{
430 struct tpm_chip *chip = dev_id;
431 struct i2c_client *client;
432 struct st33zp24_platform_data *pin_infos;
433
434 disable_irq_nosync(irq);
435
436 client = (struct i2c_client *) TPM_VPRIV(chip);
437 pin_infos = client->dev.platform_data;
438
439 complete(&pin_infos->irq_detection);
440 return IRQ_HANDLED;
441} /* tpm_ioserirq_handler() */
442
443
444/*
445 * tpm_stm_i2c_send send TPM commands through the I2C bus.
446 *
447 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
448 * @param: buf, the buffer to send.
449 * @param: count, the number of bytes to send.
450 * @return: In case of success the number of bytes sent.
451 * In other case, a < 0 value describing the issue.
452 */
453static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
454 size_t len)
455{
456 u32 status,
457 burstcnt = 0, i, size;
458 int ret;
459 u8 data;
460 struct i2c_client *client;
461
462 if (chip == NULL)
463 return -EBUSY;
464 if (len < TPM_HEADER_SIZE)
465 return -EBUSY;
466
467 client = (struct i2c_client *)TPM_VPRIV(chip);
468
469 client->flags = 0;
470
471 ret = request_locality(chip);
472 if (ret < 0)
473 return ret;
474
475 status = tpm_stm_i2c_status(chip);
476 if ((status & TPM_STS_COMMAND_READY) == 0) {
477 tpm_stm_i2c_cancel(chip);
478 if (wait_for_stat
479 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
480 &chip->vendor.int_queue) < 0) {
481 ret = -ETIME;
482 goto out_err;
483 }
484 }
485
486 for (i = 0 ; i < len - 1 ;) {
487 burstcnt = get_burstcount(chip);
488 size = min_t(int, len - i - 1, burstcnt);
489 ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf, size);
490 if (ret < 0)
491 goto out_err;
492
493 i += size;
494 }
495
496 status = tpm_stm_i2c_status(chip);
497 if ((status & TPM_STS_DATA_EXPECT) == 0) {
498 ret = -EIO;
499 goto out_err;
500 }
501
502 ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1);
503 if (ret < 0)
504 goto out_err;
505
506 status = tpm_stm_i2c_status(chip);
507 if ((status & TPM_STS_DATA_EXPECT) != 0) {
508 ret = -EIO;
509 goto out_err;
510 }
511
512 data = TPM_STS_GO;
513 I2C_WRITE_DATA(client, TPM_STS, &data, 1);
514
515 return len;
516out_err:
517 tpm_stm_i2c_cancel(chip);
518 release_locality(chip);
519 return ret;
520}
521
522/*
523 * tpm_stm_i2c_recv received TPM response through the I2C bus.
524 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
525 * @param: buf, the buffer to store datas.
526 * @param: count, the number of bytes to send.
527 * @return: In case of success the number of bytes received.
528 * In other case, a < 0 value describing the issue.
529 */
530static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
531 size_t count)
532{
533 int size = 0;
534 int expected;
535
536 if (chip == NULL)
537 return -EBUSY;
538
539 if (count < TPM_HEADER_SIZE) {
540 size = -EIO;
541 goto out;
542 }
543
544 size = recv_data(chip, buf, TPM_HEADER_SIZE);
545 if (size < TPM_HEADER_SIZE) {
546 dev_err(chip->dev, "Unable to read header\n");
547 goto out;
548 }
549
550 expected = be32_to_cpu(*(__be32 *) (buf + 2));
551 if (expected > count) {
552 size = -EIO;
553 goto out;
554 }
555
556 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
557 expected - TPM_HEADER_SIZE);
558 if (size < expected) {
559 dev_err(chip->dev, "Unable to read remainder of result\n");
560 size = -ETIME;
561 goto out;
562 }
563
564out:
565 chip->vendor.cancel(chip);
566 release_locality(chip);
567 return size;
568}
569
570static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status)
571{
572 return (status == TPM_STS_COMMAND_READY);
573}
574
575static const struct file_operations tpm_st33_i2c_fops = {
576 .owner = THIS_MODULE,
577 .llseek = no_llseek,
578 .read = tpm_read,
579 .write = tpm_write,
580 .open = tpm_open,
581 .release = tpm_release,
582};
583
584static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
585static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
586static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
587static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
588static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
589static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
590static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
591static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
592
593static struct attribute *stm_tpm_attrs[] = {
594 &dev_attr_pubek.attr,
595 &dev_attr_pcrs.attr,
596 &dev_attr_enabled.attr,
597 &dev_attr_active.attr,
598 &dev_attr_owned.attr,
599 &dev_attr_temp_deactivated.attr,
600 &dev_attr_caps.attr,
601 &dev_attr_cancel.attr, NULL,
602};
603
604static struct attribute_group stm_tpm_attr_grp = {
605 .attrs = stm_tpm_attrs
606};
607
608static struct tpm_vendor_specific st_i2c_tpm = {
609 .send = tpm_stm_i2c_send,
610 .recv = tpm_stm_i2c_recv,
611 .cancel = tpm_stm_i2c_cancel,
612 .status = tpm_stm_i2c_status,
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_st33_i2c_req_canceled,
616 .attr_group = &stm_tpm_attr_grp,
617 .miscdev = {.fops = &tpm_st33_i2c_fops,},
618};
619
620static int interrupts ;
621module_param(interrupts, int, 0444);
622MODULE_PARM_DESC(interrupts, "Enable interrupts");
623
624static int power_mgt = 1;
625module_param(power_mgt, int, 0444);
626MODULE_PARM_DESC(power_mgt, "Power Management");
627
628/*
629 * tpm_st33_i2c_probe initialize the TPM device
630 * @param: client, the i2c_client drescription (TPM I2C description).
631 * @param: id, the i2c_device_id struct.
632 * @return: 0 in case of success.
633 * -1 in other case.
634 */
635static int
636tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
637{
638 int err;
639 u8 intmask;
640 struct tpm_chip *chip;
641 struct st33zp24_platform_data *platform_data;
642
643 if (client == NULL) {
644 pr_info("%s: i2c client is NULL. Device not accessible.\n",
645 __func__);
646 err = -ENODEV;
647 goto end;
648 }
649
650 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
651 dev_info(&client->dev, "client not i2c capable\n");
652 err = -ENODEV;
653 goto end;
654 }
655
656 chip = tpm_register_hardware(&client->dev, &st_i2c_tpm);
657 if (!chip) {
658 dev_info(&client->dev, "fail chip\n");
659 err = -ENODEV;
660 goto end;
661 }
662
663 platform_data = client->dev.platform_data;
664
665 if (!platform_data) {
666 dev_info(&client->dev, "chip not available\n");
667 err = -ENODEV;
668 goto _tpm_clean_answer;
669 }
670
671 platform_data->tpm_i2c_buffer[0] =
672 kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
673 if (platform_data->tpm_i2c_buffer[0] == NULL) {
674 err = -ENOMEM;
675 goto _tpm_clean_answer;
676 }
677 platform_data->tpm_i2c_buffer[1] =
678 kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
679 if (platform_data->tpm_i2c_buffer[1] == NULL) {
680 err = -ENOMEM;
681 goto _tpm_clean_response1;
682 }
683
684 TPM_VPRIV(chip) = client;
685
686 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
687 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
688 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
689 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
690
691 chip->vendor.locality = LOCALITY0;
692
693 if (power_mgt) {
694 err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
695 if (err)
696 goto _gpio_init1;
697 gpio_set_value(platform_data->io_lpcpd, 1);
698 }
699
700 if (interrupts) {
701 init_completion(&platform_data->irq_detection);
702 if (request_locality(chip) != LOCALITY0) {
703 err = -ENODEV;
704 goto _tpm_clean_response2;
705 }
706 err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
707 if (err)
708 goto _gpio_init2;
709
710 clear_interruption(client);
711 err = request_irq(gpio_to_irq(platform_data->io_serirq),
712 &tpm_ioserirq_handler,
713 IRQF_TRIGGER_HIGH,
714 "TPM SERIRQ management", chip);
715 if (err < 0) {
716 dev_err(chip->dev , "TPM SERIRQ signals %d not available\n",
717 gpio_to_irq(platform_data->io_serirq));
718 goto _irq_set;
719 }
720
721 err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1);
722 if (err < 0)
723 goto _irq_set;
724
725 intmask |= TPM_INTF_CMD_READY_INT
726 | TPM_INTF_FIFO_AVALAIBLE_INT
727 | TPM_INTF_WAKE_UP_READY_INT
728 | TPM_INTF_LOCALITY_CHANGE_INT
729 | TPM_INTF_STS_VALID_INT
730 | TPM_INTF_DATA_AVAIL_INT;
731
732 err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1);
733 if (err < 0)
734 goto _irq_set;
735
736 intmask = TPM_GLOBAL_INT_ENABLE;
737 err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1);
738 if (err < 0)
739 goto _irq_set;
740
741 err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1);
742 if (err < 0)
743 goto _irq_set;
744
745 chip->vendor.irq = interrupts;
746
747 tpm_gen_interrupt(chip);
748 }
749
750 tpm_get_timeouts(chip);
751
752 i2c_set_clientdata(client, chip);
753
754 dev_info(chip->dev, "TPM I2C Initialized\n");
755 return 0;
756_irq_set:
757 free_irq(gpio_to_irq(platform_data->io_serirq), (void *) chip);
758_gpio_init2:
759 if (interrupts)
760 gpio_free(platform_data->io_serirq);
761_gpio_init1:
762 if (power_mgt)
763 gpio_free(platform_data->io_lpcpd);
764_tpm_clean_response2:
765 kzfree(platform_data->tpm_i2c_buffer[1]);
766 platform_data->tpm_i2c_buffer[1] = NULL;
767_tpm_clean_response1:
768 kzfree(platform_data->tpm_i2c_buffer[0]);
769 platform_data->tpm_i2c_buffer[0] = NULL;
770_tpm_clean_answer:
771 tpm_remove_hardware(chip->dev);
772end:
773 pr_info("TPM I2C initialisation fail\n");
774 return err;
775}
776
777/*
778 * tpm_st33_i2c_remove remove the TPM device
779 * @param: client, the i2c_client drescription (TPM I2C description).
780 clear_bit(0, &chip->is_open);
781 * @return: 0 in case of success.
782 */
783static int tpm_st33_i2c_remove(struct i2c_client *client)
784{
785 struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client);
786 struct st33zp24_platform_data *pin_infos =
787 ((struct i2c_client *) TPM_VPRIV(chip))->dev.platform_data;
788
789 if (pin_infos != NULL) {
790 free_irq(pin_infos->io_serirq, chip);
791
792 gpio_free(pin_infos->io_serirq);
793 gpio_free(pin_infos->io_lpcpd);
794
795 tpm_remove_hardware(chip->dev);
796
797 if (pin_infos->tpm_i2c_buffer[1] != NULL) {
798 kzfree(pin_infos->tpm_i2c_buffer[1]);
799 pin_infos->tpm_i2c_buffer[1] = NULL;
800 }
801 if (pin_infos->tpm_i2c_buffer[0] != NULL) {
802 kzfree(pin_infos->tpm_i2c_buffer[0]);
803 pin_infos->tpm_i2c_buffer[0] = NULL;
804 }
805 }
806
807 return 0;
808}
809
810#ifdef CONFIG_PM_SLEEP
811/*
812 * tpm_st33_i2c_pm_suspend suspend the TPM device
813 * Added: Work around when suspend and no tpm application is running, suspend
814 * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
815 * TPM core)
816 * @param: client, the i2c_client drescription (TPM I2C description).
817 * @param: mesg, the power management message.
818 * @return: 0 in case of success.
819 */
820static int tpm_st33_i2c_pm_suspend(struct device *dev)
821{
822 struct tpm_chip *chip = dev_get_drvdata(dev);
823 struct st33zp24_platform_data *pin_infos = dev->platform_data;
824 int ret = 0;
825
826 if (power_mgt)
827 gpio_set_value(pin_infos->io_lpcpd, 0);
828 else{
829 if (chip->data_buffer == NULL)
830 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
831 ret = tpm_pm_suspend(dev);
832 }
833 return ret;
834} /* tpm_st33_i2c_suspend() */
835
836/*
837 * tpm_st33_i2c_pm_resume resume the TPM device
838 * @param: client, the i2c_client drescription (TPM I2C description).
839 * @return: 0 in case of success.
840 */
841static int tpm_st33_i2c_pm_resume(struct device *dev)
842{
843 struct tpm_chip *chip = dev_get_drvdata(dev);
844 struct st33zp24_platform_data *pin_infos = dev->platform_data;
845
846 int ret = 0;
847
848 if (power_mgt) {
849 gpio_set_value(pin_infos->io_lpcpd, 1);
850 ret = wait_for_serirq_timeout(chip,
851 (chip->vendor.status(chip) &
852 TPM_STS_VALID) == TPM_STS_VALID,
853 chip->vendor.timeout_b);
854 } else{
855 if (chip->data_buffer == NULL)
856 chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
857 ret = tpm_pm_resume(dev);
858 if (!ret)
859 tpm_do_selftest(chip);
860 }
861 return ret;
862} /* tpm_st33_i2c_pm_resume() */
863#endif
864
865static const struct i2c_device_id tpm_st33_i2c_id[] = {
866 {TPM_ST33_I2C, 0},
867 {}
868};
869MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id);
870static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend, tpm_st33_i2c_pm_resume);
871static struct i2c_driver tpm_st33_i2c_driver = {
872 .driver = {
873 .owner = THIS_MODULE,
874 .name = TPM_ST33_I2C,
875 .pm = &tpm_st33_i2c_ops,
876 },
877 .probe = tpm_st33_i2c_probe,
878 .remove = tpm_st33_i2c_remove,
879 .id_table = tpm_st33_i2c_id
880};
881
882module_i2c_driver(tpm_st33_i2c_driver);
883
884MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
885MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
886MODULE_VERSION("1.2.0");
887MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_i2c_stm_st33.h b/drivers/char/tpm/tpm_i2c_stm_st33.h
new file mode 100644
index 000000000000..439a43249aa6
--- /dev/null
+++ b/drivers/char/tpm/tpm_i2c_stm_st33.h
@@ -0,0 +1,61 @@
1/*
2 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009, 2010 STMicroelectronics
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * STMicroelectronics version 1.2.0, Copyright (C) 2010
20 * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
21 * This is free software, and you are welcome to redistribute it
22 * under certain conditions.
23 *
24 * @Author: Christophe RICARD tpmsupport@st.com
25 *
26 * @File: stm_st33_tpm_i2c.h
27 *
28 * @Date: 09/15/2010
29 */
30#ifndef __STM_ST33_TPM_I2C_MAIN_H__
31#define __STM_ST33_TPM_I2C_MAIN_H__
32
33#define TPM_ACCESS (0x0)
34#define TPM_STS (0x18)
35#define TPM_HASH_END (0x20)
36#define TPM_DATA_FIFO (0x24)
37#define TPM_HASH_DATA (0x24)
38#define TPM_HASH_START (0x28)
39#define TPM_INTF_CAPABILITY (0x14)
40#define TPM_INT_STATUS (0x10)
41#define TPM_INT_ENABLE (0x08)
42
43#define TPM_DUMMY_BYTE 0xAA
44#define TPM_WRITE_DIRECTION 0x80
45#define TPM_HEADER_SIZE 10
46#define TPM_BUFSIZE 2048
47
48#define LOCALITY0 0
49
50#define TPM_ST33_I2C "st33zp24_i2c"
51
52struct st33zp24_platform_data {
53 int io_serirq;
54 int io_lpcpd;
55 struct i2c_client *client;
56 u8 *tpm_i2c_buffer[2]; /* 0 Request 1 Response */
57 struct completion irq_detection;
58 struct mutex lock;
59};
60
61#endif /* __STM_ST33_TPM_I2C_MAIN_H__ */
diff --git a/drivers/char/tpm/tpm_ibmvtpm.c b/drivers/char/tpm/tpm_ibmvtpm.c
index 9978609d93b2..56b07c35a13e 100644
--- a/drivers/char/tpm/tpm_ibmvtpm.c
+++ b/drivers/char/tpm/tpm_ibmvtpm.c
@@ -64,7 +64,7 @@ static struct ibmvtpm_dev *ibmvtpm_get_data(const struct device *dev)
64{ 64{
65 struct tpm_chip *chip = dev_get_drvdata(dev); 65 struct tpm_chip *chip = dev_get_drvdata(dev);
66 if (chip) 66 if (chip)
67 return (struct ibmvtpm_dev *)chip->vendor.data; 67 return (struct ibmvtpm_dev *)TPM_VPRIV(chip);
68 return NULL; 68 return NULL;
69} 69}
70 70
@@ -83,7 +83,7 @@ static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
83 u16 len; 83 u16 len;
84 int sig; 84 int sig;
85 85
86 ibmvtpm = (struct ibmvtpm_dev *)chip->vendor.data; 86 ibmvtpm = (struct ibmvtpm_dev *)TPM_VPRIV(chip);
87 87
88 if (!ibmvtpm->rtce_buf) { 88 if (!ibmvtpm->rtce_buf) {
89 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); 89 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
@@ -127,7 +127,7 @@ static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
127 u64 *word = (u64 *) &crq; 127 u64 *word = (u64 *) &crq;
128 int rc; 128 int rc;
129 129
130 ibmvtpm = (struct ibmvtpm_dev *)chip->vendor.data; 130 ibmvtpm = (struct ibmvtpm_dev *)TPM_VPRIV(chip);
131 131
132 if (!ibmvtpm->rtce_buf) { 132 if (!ibmvtpm->rtce_buf) {
133 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); 133 dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
@@ -398,6 +398,11 @@ static int tpm_ibmvtpm_resume(struct device *dev)
398 return rc; 398 return rc;
399} 399}
400 400
401static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
402{
403 return (status == 0);
404}
405
401static const struct file_operations ibmvtpm_ops = { 406static const struct file_operations ibmvtpm_ops = {
402 .owner = THIS_MODULE, 407 .owner = THIS_MODULE,
403 .llseek = no_llseek, 408 .llseek = no_llseek,
@@ -441,7 +446,7 @@ static const struct tpm_vendor_specific tpm_ibmvtpm = {
441 .status = tpm_ibmvtpm_status, 446 .status = tpm_ibmvtpm_status,
442 .req_complete_mask = 0, 447 .req_complete_mask = 0,
443 .req_complete_val = 0, 448 .req_complete_val = 0,
444 .req_canceled = 0, 449 .req_canceled = tpm_ibmvtpm_req_canceled,
445 .attr_group = &ibmvtpm_attr_grp, 450 .attr_group = &ibmvtpm_attr_grp,
446 .miscdev = { .fops = &ibmvtpm_ops, }, 451 .miscdev = { .fops = &ibmvtpm_ops, },
447}; 452};
@@ -647,7 +652,7 @@ static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
647 652
648 ibmvtpm->dev = dev; 653 ibmvtpm->dev = dev;
649 ibmvtpm->vdev = vio_dev; 654 ibmvtpm->vdev = vio_dev;
650 chip->vendor.data = (void *)ibmvtpm; 655 TPM_VPRIV(chip) = (void *)ibmvtpm;
651 656
652 spin_lock_init(&ibmvtpm->rtce_lock); 657 spin_lock_init(&ibmvtpm->rtce_lock);
653 658
diff --git a/drivers/char/tpm/tpm_nsc.c b/drivers/char/tpm/tpm_nsc.c
index 640c9a427b59..770c46f8eb30 100644
--- a/drivers/char/tpm/tpm_nsc.c
+++ b/drivers/char/tpm/tpm_nsc.c
@@ -227,6 +227,11 @@ static u8 tpm_nsc_status(struct tpm_chip *chip)
227 return inb(chip->vendor.base + NSC_STATUS); 227 return inb(chip->vendor.base + NSC_STATUS);
228} 228}
229 229
230static bool tpm_nsc_req_canceled(struct tpm_chip *chip, u8 status)
231{
232 return (status == NSC_STATUS_RDY);
233}
234
230static const struct file_operations nsc_ops = { 235static const struct file_operations nsc_ops = {
231 .owner = THIS_MODULE, 236 .owner = THIS_MODULE,
232 .llseek = no_llseek, 237 .llseek = no_llseek,
@@ -258,7 +263,7 @@ static const struct tpm_vendor_specific tpm_nsc = {
258 .status = tpm_nsc_status, 263 .status = tpm_nsc_status,
259 .req_complete_mask = NSC_STATUS_OBF, 264 .req_complete_mask = NSC_STATUS_OBF,
260 .req_complete_val = NSC_STATUS_OBF, 265 .req_complete_val = NSC_STATUS_OBF,
261 .req_canceled = NSC_STATUS_RDY, 266 .req_canceled = tpm_nsc_req_canceled,
262 .attr_group = &nsc_attr_grp, 267 .attr_group = &nsc_attr_grp,
263 .miscdev = { .fops = &nsc_ops, }, 268 .miscdev = { .fops = &nsc_ops, },
264}; 269};
diff --git a/drivers/char/tpm/tpm_tis.c b/drivers/char/tpm/tpm_tis.c
index ea31dafbcac2..8a41b6be23a0 100644
--- a/drivers/char/tpm/tpm_tis.c
+++ b/drivers/char/tpm/tpm_tis.c
@@ -84,6 +84,9 @@ static int is_itpm(struct pnp_dev *dev)
84 struct acpi_device *acpi = pnp_acpi_device(dev); 84 struct acpi_device *acpi = pnp_acpi_device(dev);
85 struct acpi_hardware_id *id; 85 struct acpi_hardware_id *id;
86 86
87 if (!acpi)
88 return 0;
89
87 list_for_each_entry(id, &acpi->pnp.ids, list) { 90 list_for_each_entry(id, &acpi->pnp.ids, list) {
88 if (!strcmp("INTC0102", id->id)) 91 if (!strcmp("INTC0102", id->id))
89 return 1; 92 return 1;
@@ -98,6 +101,22 @@ static inline int is_itpm(struct pnp_dev *dev)
98} 101}
99#endif 102#endif
100 103
104/* Before we attempt to access the TPM we must see that the valid bit is set.
105 * The specification says that this bit is 0 at reset and remains 0 until the
106 * 'TPM has gone through its self test and initialization and has established
107 * correct values in the other bits.' */
108static int wait_startup(struct tpm_chip *chip, int l)
109{
110 unsigned long stop = jiffies + chip->vendor.timeout_a;
111 do {
112 if (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
113 TPM_ACCESS_VALID)
114 return 0;
115 msleep(TPM_TIMEOUT);
116 } while (time_before(jiffies, stop));
117 return -1;
118}
119
101static int check_locality(struct tpm_chip *chip, int l) 120static int check_locality(struct tpm_chip *chip, int l)
102{ 121{
103 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) & 122 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
@@ -198,7 +217,7 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
198 wait_for_tpm_stat(chip, 217 wait_for_tpm_stat(chip,
199 TPM_STS_DATA_AVAIL | TPM_STS_VALID, 218 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
200 chip->vendor.timeout_c, 219 chip->vendor.timeout_c,
201 &chip->vendor.read_queue) 220 &chip->vendor.read_queue, true)
202 == 0) { 221 == 0) {
203 burstcnt = get_burstcount(chip); 222 burstcnt = get_burstcount(chip);
204 for (; burstcnt > 0 && size < count; burstcnt--) 223 for (; burstcnt > 0 && size < count; burstcnt--)
@@ -241,7 +260,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
241 } 260 }
242 261
243 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, 262 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
244 &chip->vendor.int_queue); 263 &chip->vendor.int_queue, false);
245 status = tpm_tis_status(chip); 264 status = tpm_tis_status(chip);
246 if (status & TPM_STS_DATA_AVAIL) { /* retry? */ 265 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
247 dev_err(chip->dev, "Error left over data\n"); 266 dev_err(chip->dev, "Error left over data\n");
@@ -277,7 +296,7 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
277 tpm_tis_ready(chip); 296 tpm_tis_ready(chip);
278 if (wait_for_tpm_stat 297 if (wait_for_tpm_stat
279 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b, 298 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
280 &chip->vendor.int_queue) < 0) { 299 &chip->vendor.int_queue, false) < 0) {
281 rc = -ETIME; 300 rc = -ETIME;
282 goto out_err; 301 goto out_err;
283 } 302 }
@@ -292,7 +311,7 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
292 } 311 }
293 312
294 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, 313 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
295 &chip->vendor.int_queue); 314 &chip->vendor.int_queue, false);
296 status = tpm_tis_status(chip); 315 status = tpm_tis_status(chip);
297 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) { 316 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
298 rc = -EIO; 317 rc = -EIO;
@@ -304,7 +323,7 @@ static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
304 iowrite8(buf[count], 323 iowrite8(buf[count],
305 chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality)); 324 chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
306 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, 325 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
307 &chip->vendor.int_queue); 326 &chip->vendor.int_queue, false);
308 status = tpm_tis_status(chip); 327 status = tpm_tis_status(chip);
309 if ((status & TPM_STS_DATA_EXPECT) != 0) { 328 if ((status & TPM_STS_DATA_EXPECT) != 0) {
310 rc = -EIO; 329 rc = -EIO;
@@ -342,7 +361,7 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
342 if (wait_for_tpm_stat 361 if (wait_for_tpm_stat
343 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, 362 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
344 tpm_calc_ordinal_duration(chip, ordinal), 363 tpm_calc_ordinal_duration(chip, ordinal),
345 &chip->vendor.read_queue) < 0) { 364 &chip->vendor.read_queue, false) < 0) {
346 rc = -ETIME; 365 rc = -ETIME;
347 goto out_err; 366 goto out_err;
348 } 367 }
@@ -374,7 +393,7 @@ static int probe_itpm(struct tpm_chip *chip)
374 if (vendor != TPM_VID_INTEL) 393 if (vendor != TPM_VID_INTEL)
375 return 0; 394 return 0;
376 395
377 itpm = 0; 396 itpm = false;
378 397
379 rc = tpm_tis_send_data(chip, cmd_getticks, len); 398 rc = tpm_tis_send_data(chip, cmd_getticks, len);
380 if (rc == 0) 399 if (rc == 0)
@@ -383,7 +402,7 @@ static int probe_itpm(struct tpm_chip *chip)
383 tpm_tis_ready(chip); 402 tpm_tis_ready(chip);
384 release_locality(chip, chip->vendor.locality, 0); 403 release_locality(chip, chip->vendor.locality, 0);
385 404
386 itpm = 1; 405 itpm = true;
387 406
388 rc = tpm_tis_send_data(chip, cmd_getticks, len); 407 rc = tpm_tis_send_data(chip, cmd_getticks, len);
389 if (rc == 0) { 408 if (rc == 0) {
@@ -400,6 +419,19 @@ out:
400 return rc; 419 return rc;
401} 420}
402 421
422static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
423{
424 switch (chip->vendor.manufacturer_id) {
425 case TPM_VID_WINBOND:
426 return ((status == TPM_STS_VALID) ||
427 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
428 case TPM_VID_STM:
429 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
430 default:
431 return (status == TPM_STS_COMMAND_READY);
432 }
433}
434
403static const struct file_operations tis_ops = { 435static const struct file_operations tis_ops = {
404 .owner = THIS_MODULE, 436 .owner = THIS_MODULE,
405 .llseek = no_llseek, 437 .llseek = no_llseek,
@@ -445,7 +477,7 @@ static struct tpm_vendor_specific tpm_tis = {
445 .cancel = tpm_tis_ready, 477 .cancel = tpm_tis_ready,
446 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 478 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
447 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 479 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
448 .req_canceled = TPM_STS_COMMAND_READY, 480 .req_canceled = tpm_tis_req_canceled,
449 .attr_group = &tis_attr_grp, 481 .attr_group = &tis_attr_grp,
450 .miscdev = { 482 .miscdev = {
451 .fops = &tis_ops,}, 483 .fops = &tis_ops,},
@@ -502,7 +534,7 @@ static irqreturn_t tis_int_handler(int dummy, void *dev_id)
502 return IRQ_HANDLED; 534 return IRQ_HANDLED;
503} 535}
504 536
505static bool interrupts = 1; 537static bool interrupts = true;
506module_param(interrupts, bool, 0444); 538module_param(interrupts, bool, 0444);
507MODULE_PARM_DESC(interrupts, "Enable interrupts"); 539MODULE_PARM_DESC(interrupts, "Enable interrupts");
508 540
@@ -528,12 +560,18 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
528 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 560 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
529 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); 561 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
530 562
563 if (wait_startup(chip, 0) != 0) {
564 rc = -ENODEV;
565 goto out_err;
566 }
567
531 if (request_locality(chip, 0) != 0) { 568 if (request_locality(chip, 0) != 0) {
532 rc = -ENODEV; 569 rc = -ENODEV;
533 goto out_err; 570 goto out_err;
534 } 571 }
535 572
536 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0)); 573 vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
574 chip->vendor.manufacturer_id = vendor;
537 575
538 dev_info(dev, 576 dev_info(dev,
539 "1.2 TPM (device-id 0x%X, rev-id %d)\n", 577 "1.2 TPM (device-id 0x%X, rev-id %d)\n",
@@ -545,7 +583,7 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
545 rc = -ENODEV; 583 rc = -ENODEV;
546 goto out_err; 584 goto out_err;
547 } 585 }
548 itpm = (probe == 0) ? 0 : 1; 586 itpm = !!probe;
549 } 587 }
550 588
551 if (itpm) 589 if (itpm)
@@ -741,10 +779,10 @@ static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
741 if (pnp_irq_valid(pnp_dev, 0)) 779 if (pnp_irq_valid(pnp_dev, 0))
742 irq = pnp_irq(pnp_dev, 0); 780 irq = pnp_irq(pnp_dev, 0);
743 else 781 else
744 interrupts = 0; 782 interrupts = false;
745 783
746 if (is_itpm(pnp_dev)) 784 if (is_itpm(pnp_dev))
747 itpm = 1; 785 itpm = true;
748 786
749 return tpm_tis_init(&pnp_dev->dev, start, len, irq); 787 return tpm_tis_init(&pnp_dev->dev, start, len, irq);
750} 788}