1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/i2c.h>
25#include <linux/module.h>
26#include <linux/wait.h>
27#include "tpm.h"
28
29
30#define TPM_BUFSIZE 1260
31
32
33#define MAX_COUNT 3
34
35#define SLEEP_DURATION_LOW 55
36#define SLEEP_DURATION_HI 65
37
38
39
40
41
42
43#define MAX_COUNT_LONG 50
44
45#define SLEEP_DURATION_LONG_LOW 200
46#define SLEEP_DURATION_LONG_HI 220
47
48
49#define SLEEP_DURATION_RESET_LOW 2400
50#define SLEEP_DURATION_RESET_HI 2600
51
52
53#define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000)
54#define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000)
55
56
57#define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L
58#define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
59
60enum i2c_chip_type {
61 SLB9635,
62 SLB9645,
63 UNKNOWN,
64};
65
66
67struct tpm_inf_dev {
68 struct i2c_client *client;
69 int locality;
70 u8 buf[TPM_BUFSIZE + sizeof(u8)];
71 struct tpm_chip *chip;
72 enum i2c_chip_type chip_type;
73 unsigned int adapterlimit;
74};
75
76static struct tpm_inf_dev tpm_dev;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
98{
99
100 struct i2c_msg msg1 = {
101 .addr = tpm_dev.client->addr,
102 .len = 1,
103 .buf = &addr
104 };
105 struct i2c_msg msg2 = {
106 .addr = tpm_dev.client->addr,
107 .flags = I2C_M_RD,
108 .len = len,
109 .buf = buffer
110 };
111 struct i2c_msg msgs[] = {msg1, msg2};
112
113 int rc = 0;
114 int count;
115 unsigned int msglen = len;
116
117
118 if (!tpm_dev.client->adapter->algo->master_xfer)
119 return -EOPNOTSUPP;
120 i2c_lock_adapter(tpm_dev.client->adapter);
121
122 if (tpm_dev.chip_type == SLB9645) {
123
124
125
126
127
128
129 for (count = 0; count < MAX_COUNT; count++) {
130 rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
131 if (rc > 0)
132 break;
133 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
134 }
135 } else {
136
137
138
139 while (len > 0) {
140
141 for (count = 0; count < MAX_COUNT; count++) {
142 rc = __i2c_transfer(tpm_dev.client->adapter,
143 &msg1, 1);
144 if (rc > 0)
145 break;
146
147 usleep_range(SLEEP_DURATION_LOW,
148 SLEEP_DURATION_HI);
149 }
150
151 if (rc <= 0)
152 goto out;
153
154
155
156
157
158 for (count = 0; count < MAX_COUNT; count++) {
159 if (tpm_dev.adapterlimit) {
160 msglen = min_t(unsigned int,
161 tpm_dev.adapterlimit,
162 len);
163 msg2.len = msglen;
164 }
165 usleep_range(SLEEP_DURATION_LOW,
166 SLEEP_DURATION_HI);
167 rc = __i2c_transfer(tpm_dev.client->adapter,
168 &msg2, 1);
169 if (rc > 0) {
170
171
172
173 if (msglen > len)
174 len = 0;
175 else
176 len -= msglen;
177 msg2.buf += msglen;
178 break;
179 }
180
181
182
183
184 if (rc == -EOPNOTSUPP)
185 tpm_dev.adapterlimit =
186 I2C_SMBUS_BLOCK_MAX;
187 }
188
189 if (rc <= 0)
190 goto out;
191 }
192 }
193
194out:
195 i2c_unlock_adapter(tpm_dev.client->adapter);
196
197 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
198
199
200
201
202
203 if (rc <= 0)
204 return -EIO;
205
206 return 0;
207}
208
209static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
210 unsigned int sleep_low,
211 unsigned int sleep_hi, u8 max_count)
212{
213 int rc = -EIO;
214 int count;
215
216 struct i2c_msg msg1 = {
217 .addr = tpm_dev.client->addr,
218 .len = len + 1,
219 .buf = tpm_dev.buf
220 };
221
222 if (len > TPM_BUFSIZE)
223 return -EINVAL;
224
225 if (!tpm_dev.client->adapter->algo->master_xfer)
226 return -EOPNOTSUPP;
227 i2c_lock_adapter(tpm_dev.client->adapter);
228
229
230 tpm_dev.buf[0] = addr;
231 memcpy(&(tpm_dev.buf[1]), buffer, len);
232
233
234
235
236
237
238
239 for (count = 0; count < max_count; count++) {
240 rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
241 if (rc > 0)
242 break;
243 usleep_range(sleep_low, sleep_hi);
244 }
245
246 i2c_unlock_adapter(tpm_dev.client->adapter);
247
248 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
249
250
251
252
253
254 if (rc <= 0)
255 return -EIO;
256
257 return 0;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
277{
278 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW,
279 SLEEP_DURATION_HI, MAX_COUNT);
280}
281
282
283
284
285
286static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
287{
288 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW,
289 SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG);
290}
291
292enum tis_access {
293 TPM_ACCESS_VALID = 0x80,
294 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
295 TPM_ACCESS_REQUEST_PENDING = 0x04,
296 TPM_ACCESS_REQUEST_USE = 0x02,
297};
298
299enum tis_status {
300 TPM_STS_VALID = 0x80,
301 TPM_STS_COMMAND_READY = 0x40,
302 TPM_STS_GO = 0x20,
303 TPM_STS_DATA_AVAIL = 0x10,
304 TPM_STS_DATA_EXPECT = 0x08,
305};
306
307enum tis_defaults {
308 TIS_SHORT_TIMEOUT = 750,
309 TIS_LONG_TIMEOUT = 2000,
310};
311
312#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
313#define TPM_STS(l) (0x0001 | ((l) << 4))
314#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
315#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
316
317static bool check_locality(struct tpm_chip *chip, int loc)
318{
319 u8 buf;
320 int rc;
321
322 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
323 if (rc < 0)
324 return false;
325
326 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
327 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
328 tpm_dev.locality = loc;
329 return true;
330 }
331
332 return false;
333}
334
335
336static void release_locality(struct tpm_chip *chip, int loc, int force)
337{
338 u8 buf;
339 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
340 return;
341
342 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
343 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
344 buf = TPM_ACCESS_ACTIVE_LOCALITY;
345 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
346 }
347}
348
349static int request_locality(struct tpm_chip *chip, int loc)
350{
351 unsigned long stop;
352 u8 buf = TPM_ACCESS_REQUEST_USE;
353
354 if (check_locality(chip, loc))
355 return loc;
356
357 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
358
359
360 stop = jiffies + chip->timeout_a;
361 do {
362 if (check_locality(chip, loc))
363 return loc;
364 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
365 } while (time_before(jiffies, stop));
366
367 return -ETIME;
368}
369
370static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
371{
372
373 u8 buf = 0xFF;
374 u8 i = 0;
375
376 do {
377 if (iic_tpm_read(TPM_STS(tpm_dev.locality), &buf, 1) < 0)
378 return 0;
379
380 i++;
381
382 } while ((buf == 0xFF) && i < 10);
383
384 return buf;
385}
386
387static void tpm_tis_i2c_ready(struct tpm_chip *chip)
388{
389
390 u8 buf = TPM_STS_COMMAND_READY;
391 iic_tpm_write_long(TPM_STS(tpm_dev.locality), &buf, 1);
392}
393
394static ssize_t get_burstcount(struct tpm_chip *chip)
395{
396 unsigned long stop;
397 ssize_t burstcnt;
398 u8 buf[3];
399
400
401
402 stop = jiffies + chip->timeout_d;
403 do {
404
405 if (iic_tpm_read(TPM_STS(tpm_dev.locality)+1, buf, 3) < 0)
406 burstcnt = 0;
407 else
408 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
409
410 if (burstcnt)
411 return burstcnt;
412
413 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
414 } while (time_before(jiffies, stop));
415 return -EBUSY;
416}
417
418static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
419 int *status)
420{
421 unsigned long stop;
422
423
424 *status = tpm_tis_i2c_status(chip);
425 if ((*status != 0xFF) && (*status & mask) == mask)
426 return 0;
427
428 stop = jiffies + timeout;
429 do {
430
431 usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI);
432 *status = tpm_tis_i2c_status(chip);
433 if ((*status & mask) == mask)
434 return 0;
435
436 } while (time_before(jiffies, stop));
437
438 return -ETIME;
439}
440
441static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
442{
443 size_t size = 0;
444 ssize_t burstcnt;
445 u8 retries = 0;
446 int rc;
447
448 while (size < count) {
449 burstcnt = get_burstcount(chip);
450
451
452 if (burstcnt < 0)
453 return burstcnt;
454
455
456 if (burstcnt > (count - size))
457 burstcnt = count - size;
458
459 rc = iic_tpm_read(TPM_DATA_FIFO(tpm_dev.locality),
460 &(buf[size]), burstcnt);
461 if (rc == 0)
462 size += burstcnt;
463 else if (rc < 0)
464 retries++;
465
466
467 if (retries > MAX_COUNT_LONG)
468 return -EIO;
469 }
470 return size;
471}
472
473static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
474{
475 int size = 0;
476 int status;
477 u32 expected;
478
479 if (count < TPM_HEADER_SIZE) {
480 size = -EIO;
481 goto out;
482 }
483
484
485 size = recv_data(chip, buf, TPM_HEADER_SIZE);
486 if (size < TPM_HEADER_SIZE) {
487 dev_err(&chip->dev, "Unable to read header\n");
488 goto out;
489 }
490
491 expected = be32_to_cpu(*(__be32 *)(buf + 2));
492 if (((size_t) expected > count) || (expected < TPM_HEADER_SIZE)) {
493 size = -EIO;
494 goto out;
495 }
496
497 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
498 expected - TPM_HEADER_SIZE);
499 if (size < expected) {
500 dev_err(&chip->dev, "Unable to read remainder of result\n");
501 size = -ETIME;
502 goto out;
503 }
504
505 wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
506 if (status & TPM_STS_DATA_AVAIL) {
507 dev_err(&chip->dev, "Error left over data\n");
508 size = -EIO;
509 goto out;
510 }
511
512out:
513 tpm_tis_i2c_ready(chip);
514
515
516
517 usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
518 release_locality(chip, tpm_dev.locality, 0);
519 return size;
520}
521
522static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
523{
524 int rc, status;
525 ssize_t burstcnt;
526 size_t count = 0;
527 u8 retries = 0;
528 u8 sts = TPM_STS_GO;
529
530 if (len > TPM_BUFSIZE)
531 return -E2BIG;
532
533 if (request_locality(chip, 0) < 0)
534 return -EBUSY;
535
536 status = tpm_tis_i2c_status(chip);
537 if ((status & TPM_STS_COMMAND_READY) == 0) {
538 tpm_tis_i2c_ready(chip);
539 if (wait_for_stat
540 (chip, TPM_STS_COMMAND_READY,
541 chip->timeout_b, &status) < 0) {
542 rc = -ETIME;
543 goto out_err;
544 }
545 }
546
547 while (count < len - 1) {
548 burstcnt = get_burstcount(chip);
549
550
551 if (burstcnt < 0)
552 return burstcnt;
553
554 if (burstcnt > (len - 1 - count))
555 burstcnt = len - 1 - count;
556
557 rc = iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality),
558 &(buf[count]), burstcnt);
559 if (rc == 0)
560 count += burstcnt;
561 else if (rc < 0)
562 retries++;
563
564
565 if (retries > MAX_COUNT_LONG) {
566 rc = -EIO;
567 goto out_err;
568 }
569
570 wait_for_stat(chip, TPM_STS_VALID,
571 chip->timeout_c, &status);
572
573 if ((status & TPM_STS_DATA_EXPECT) == 0) {
574 rc = -EIO;
575 goto out_err;
576 }
577 }
578
579
580 iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), &(buf[count]), 1);
581 wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
582 if ((status & TPM_STS_DATA_EXPECT) != 0) {
583 rc = -EIO;
584 goto out_err;
585 }
586
587
588 iic_tpm_write(TPM_STS(tpm_dev.locality), &sts, 1);
589
590 return len;
591out_err:
592 tpm_tis_i2c_ready(chip);
593
594
595
596 usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI);
597 release_locality(chip, tpm_dev.locality, 0);
598 return rc;
599}
600
601static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
602{
603 return (status == TPM_STS_COMMAND_READY);
604}
605
606static const struct tpm_class_ops tpm_tis_i2c = {
607 .flags = TPM_OPS_AUTO_STARTUP,
608 .status = tpm_tis_i2c_status,
609 .recv = tpm_tis_i2c_recv,
610 .send = tpm_tis_i2c_send,
611 .cancel = tpm_tis_i2c_ready,
612 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
613 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
614 .req_canceled = tpm_tis_i2c_req_canceled,
615};
616
617static int tpm_tis_i2c_init(struct device *dev)
618{
619 u32 vendor;
620 int rc = 0;
621 struct tpm_chip *chip;
622
623 chip = tpmm_chip_alloc(dev, &tpm_tis_i2c);
624 if (IS_ERR(chip))
625 return PTR_ERR(chip);
626
627
628 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
629 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
630 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
631 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
632
633 if (request_locality(chip, 0) != 0) {
634 dev_err(dev, "could not request locality\n");
635 rc = -ENODEV;
636 goto out_err;
637 }
638
639
640 if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) {
641 dev_err(dev, "could not read vendor id\n");
642 rc = -EIO;
643 goto out_release;
644 }
645
646 if (vendor == TPM_TIS_I2C_DID_VID_9645) {
647 tpm_dev.chip_type = SLB9645;
648 } else if (vendor == TPM_TIS_I2C_DID_VID_9635) {
649 tpm_dev.chip_type = SLB9635;
650 } else {
651 dev_err(dev, "vendor id did not match! ID was %08x\n", vendor);
652 rc = -ENODEV;
653 goto out_release;
654 }
655
656 dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
657
658 tpm_dev.chip = chip;
659
660 return tpm_chip_register(chip);
661out_release:
662 release_locality(chip, tpm_dev.locality, 1);
663 tpm_dev.client = NULL;
664out_err:
665 return rc;
666}
667
668static const struct i2c_device_id tpm_tis_i2c_table[] = {
669 {"tpm_i2c_infineon"},
670 {"slb9635tt"},
671 {"slb9645tt"},
672 {},
673};
674
675MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table);
676
677#ifdef CONFIG_OF
678static const struct of_device_id tpm_tis_i2c_of_match[] = {
679 {.compatible = "infineon,tpm_i2c_infineon"},
680 {.compatible = "infineon,slb9635tt"},
681 {.compatible = "infineon,slb9645tt"},
682 {},
683};
684MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match);
685#endif
686
687static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume);
688
689static int tpm_tis_i2c_probe(struct i2c_client *client,
690 const struct i2c_device_id *id)
691{
692 int rc;
693 struct device *dev = &(client->dev);
694
695 if (tpm_dev.client != NULL) {
696 dev_err(dev, "This driver only supports one client at a time\n");
697 return -EBUSY;
698 }
699
700 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
701 dev_err(dev, "no algorithms associated to the i2c bus\n");
702 return -ENODEV;
703 }
704
705 tpm_dev.client = client;
706 rc = tpm_tis_i2c_init(&client->dev);
707 if (rc != 0) {
708 tpm_dev.client = NULL;
709 rc = -ENODEV;
710 }
711 return rc;
712}
713
714static int tpm_tis_i2c_remove(struct i2c_client *client)
715{
716 struct tpm_chip *chip = tpm_dev.chip;
717
718 tpm_chip_unregister(chip);
719 release_locality(chip, tpm_dev.locality, 1);
720 tpm_dev.client = NULL;
721
722 return 0;
723}
724
725static struct i2c_driver tpm_tis_i2c_driver = {
726 .id_table = tpm_tis_i2c_table,
727 .probe = tpm_tis_i2c_probe,
728 .remove = tpm_tis_i2c_remove,
729 .driver = {
730 .name = "tpm_i2c_infineon",
731 .owner = THIS_MODULE,
732 .pm = &tpm_tis_i2c_ops,
733 .of_match_table = of_match_ptr(tpm_tis_i2c_of_match),
734 },
735};
736
737module_i2c_driver(tpm_tis_i2c_driver);
738MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>");
739MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver");
740MODULE_VERSION("2.2.0");
741MODULE_LICENSE("GPL");
742