1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/acpi.h>
20#include <linux/completion.h>
21#include <linux/i2c.h>
22#include <linux/interrupt.h>
23#include <linux/module.h>
24#include <linux/pm.h>
25#include <linux/slab.h>
26#include <linux/wait.h>
27
28#include "tpm_tis_core.h"
29
30#define TPM_CR50_MAX_BUFSIZE 64
31#define TPM_CR50_TIMEOUT_SHORT_MS 2
32#define TPM_CR50_TIMEOUT_NOIRQ_MS 20
33#define TPM_CR50_I2C_DID_VID 0x00281ae0L
34#define TPM_CR50_I2C_MAX_RETRIES 3
35#define TPM_CR50_I2C_RETRY_DELAY_LO 55
36#define TPM_CR50_I2C_RETRY_DELAY_HI 65
37
38#define TPM_I2C_ACCESS(l) (0x0000 | ((l) << 4))
39#define TPM_I2C_STS(l) (0x0001 | ((l) << 4))
40#define TPM_I2C_DATA_FIFO(l) (0x0005 | ((l) << 4))
41#define TPM_I2C_DID_VID(l) (0x0006 | ((l) << 4))
42
43
44
45
46
47
48
49
50
51
52struct tpm_i2c_cr50_priv_data {
53 int irq;
54 struct completion tpm_ready;
55 u8 buf[TPM_CR50_MAX_BUFSIZE];
56};
57
58
59
60
61
62
63
64
65
66
67
68
69
70static irqreturn_t tpm_cr50_i2c_int_handler(int dummy, void *tpm_info)
71{
72 struct tpm_chip *chip = tpm_info;
73 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
74
75 complete(&priv->tpm_ready);
76
77 return IRQ_HANDLED;
78}
79
80
81
82
83
84
85
86
87
88
89
90
91static int tpm_cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
92{
93 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
94
95
96 if (priv->irq <= 0) {
97 msleep(TPM_CR50_TIMEOUT_NOIRQ_MS);
98 return 0;
99 }
100
101
102 if (!wait_for_completion_timeout(&priv->tpm_ready,
103 msecs_to_jiffies(chip->timeout_a))) {
104 dev_warn(&chip->dev, "Timeout waiting for TPM ready\n");
105 return -ETIMEDOUT;
106 }
107
108 return 0;
109}
110
111
112
113
114
115static void tpm_cr50_i2c_enable_tpm_irq(struct tpm_chip *chip)
116{
117 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
118
119 if (priv->irq > 0) {
120 reinit_completion(&priv->tpm_ready);
121 enable_irq(priv->irq);
122 }
123}
124
125
126
127
128
129static void tpm_cr50_i2c_disable_tpm_irq(struct tpm_chip *chip)
130{
131 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
132
133 if (priv->irq > 0)
134 disable_irq(priv->irq);
135}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150static int tpm_cr50_i2c_transfer_message(struct device *dev,
151 struct i2c_adapter *adapter,
152 struct i2c_msg *msg)
153{
154 unsigned int try;
155 int rc;
156
157 for (try = 0; try < TPM_CR50_I2C_MAX_RETRIES; try++) {
158 rc = __i2c_transfer(adapter, msg, 1);
159 if (rc == 1)
160 return 0;
161 if (try)
162 dev_warn(dev, "i2c transfer failed (attempt %d/%d): %d\n",
163 try + 1, TPM_CR50_I2C_MAX_RETRIES, rc);
164 usleep_range(TPM_CR50_I2C_RETRY_DELAY_LO, TPM_CR50_I2C_RETRY_DELAY_HI);
165 }
166
167
168 return -EIO;
169}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t len)
187{
188 struct i2c_client *client = to_i2c_client(chip->dev.parent);
189 struct i2c_msg msg_reg_addr = {
190 .addr = client->addr,
191 .len = 1,
192 .buf = &addr
193 };
194 struct i2c_msg msg_response = {
195 .addr = client->addr,
196 .flags = I2C_M_RD,
197 .len = len,
198 .buf = buffer
199 };
200 int rc;
201
202 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
203
204
205 tpm_cr50_i2c_enable_tpm_irq(chip);
206
207
208 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_reg_addr);
209 if (rc < 0)
210 goto out;
211
212
213 rc = tpm_cr50_i2c_wait_tpm_ready(chip);
214 if (rc < 0)
215 goto out;
216
217
218 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_response);
219
220out:
221 tpm_cr50_i2c_disable_tpm_irq(chip);
222 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
223
224 if (rc < 0)
225 return rc;
226
227 return 0;
228}
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
246 size_t len)
247{
248 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
249 struct i2c_client *client = to_i2c_client(chip->dev.parent);
250 struct i2c_msg msg = {
251 .addr = client->addr,
252 .len = len + 1,
253 .buf = priv->buf
254 };
255 int rc;
256
257 if (len > TPM_CR50_MAX_BUFSIZE - 1)
258 return -EINVAL;
259
260
261 priv->buf[0] = addr;
262 memcpy(priv->buf + 1, buffer, len);
263
264 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
265
266
267 tpm_cr50_i2c_enable_tpm_irq(chip);
268
269
270 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg);
271 if (rc < 0)
272 goto out;
273
274
275 tpm_cr50_i2c_wait_tpm_ready(chip);
276
277out:
278 tpm_cr50_i2c_disable_tpm_irq(chip);
279 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
280
281 if (rc < 0)
282 return rc;
283
284 return 0;
285}
286
287
288
289
290
291
292
293
294
295static int tpm_cr50_check_locality(struct tpm_chip *chip)
296{
297 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
298 u8 buf;
299 int rc;
300
301 rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(0), &buf, sizeof(buf));
302 if (rc < 0)
303 return rc;
304
305 if ((buf & mask) == mask)
306 return 0;
307
308 return -EIO;
309}
310
311
312
313
314
315
316static void tpm_cr50_release_locality(struct tpm_chip *chip, bool force)
317{
318 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
319 u8 addr = TPM_I2C_ACCESS(0);
320 u8 buf;
321
322 if (tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf)) < 0)
323 return;
324
325 if (force || (buf & mask) == mask) {
326 buf = TPM_ACCESS_ACTIVE_LOCALITY;
327 tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
328 }
329}
330
331
332
333
334
335
336
337
338
339static int tpm_cr50_request_locality(struct tpm_chip *chip)
340{
341 u8 buf = TPM_ACCESS_REQUEST_USE;
342 unsigned long stop;
343 int rc;
344
345 if (!tpm_cr50_check_locality(chip))
346 return 0;
347
348 rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(0), &buf, sizeof(buf));
349 if (rc < 0)
350 return rc;
351
352 stop = jiffies + chip->timeout_a;
353 do {
354 if (!tpm_cr50_check_locality(chip))
355 return 0;
356
357 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
358 } while (time_before(jiffies, stop));
359
360 return -ETIMEDOUT;
361}
362
363
364
365
366
367
368
369
370
371
372static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip)
373{
374 u8 buf[4];
375
376 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(0), buf, sizeof(buf)) < 0)
377 return 0;
378
379 return buf[0];
380}
381
382
383
384
385
386
387
388static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip)
389{
390 u8 buf[4] = { TPM_STS_COMMAND_READY };
391
392 tpm_cr50_i2c_write(chip, TPM_I2C_STS(0), buf, sizeof(buf));
393 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
394}
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask,
411 size_t *burst, u32 *status)
412{
413 unsigned long stop;
414 u8 buf[4];
415
416 *status = 0;
417
418
419 stop = jiffies + chip->timeout_b;
420
421 do {
422 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(0), buf, sizeof(buf)) < 0) {
423 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
424 continue;
425 }
426
427 *status = *buf;
428 *burst = le16_to_cpup((__le16 *)(buf + 1));
429
430 if ((*status & mask) == mask &&
431 *burst > 0 && *burst <= TPM_CR50_MAX_BUFSIZE - 1)
432 return 0;
433
434 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
435 } while (time_before(jiffies, stop));
436
437 dev_err(&chip->dev, "Timeout reading burst and status\n");
438 return -ETIMEDOUT;
439}
440
441
442
443
444
445
446
447
448
449
450
451static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
452{
453
454 u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
455 size_t burstcnt, cur, len, expected;
456 u8 addr = TPM_I2C_DATA_FIFO(0);
457 u32 status;
458 int rc;
459
460 if (buf_len < TPM_HEADER_SIZE)
461 return -EINVAL;
462
463 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
464 if (rc < 0)
465 goto out_err;
466
467 if (burstcnt > buf_len || burstcnt < TPM_HEADER_SIZE) {
468 dev_err(&chip->dev,
469 "Unexpected burstcnt: %zu (max=%zu, min=%d)\n",
470 burstcnt, buf_len, TPM_HEADER_SIZE);
471 rc = -EIO;
472 goto out_err;
473 }
474
475
476 rc = tpm_cr50_i2c_read(chip, addr, buf, burstcnt);
477 if (rc < 0) {
478 dev_err(&chip->dev, "Read of first chunk failed\n");
479 goto out_err;
480 }
481
482
483 expected = be32_to_cpup((__be32 *)(buf + 2));
484 if (expected > buf_len) {
485 dev_err(&chip->dev, "Buffer too small to receive i2c data\n");
486 rc = -E2BIG;
487 goto out_err;
488 }
489
490
491 cur = burstcnt;
492 while (cur < expected) {
493
494 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
495 if (rc < 0)
496 goto out_err;
497
498 len = min_t(size_t, burstcnt, expected - cur);
499 rc = tpm_cr50_i2c_read(chip, addr, buf + cur, len);
500 if (rc < 0) {
501 dev_err(&chip->dev, "Read failed\n");
502 goto out_err;
503 }
504
505 cur += len;
506 }
507
508
509 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
510 if (rc < 0)
511 goto out_err;
512 if (status & TPM_STS_DATA_AVAIL) {
513 dev_err(&chip->dev, "Data still available\n");
514 rc = -EIO;
515 goto out_err;
516 }
517
518 tpm_cr50_release_locality(chip, false);
519 return cur;
520
521out_err:
522
523 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
524 tpm_cr50_i2c_tis_set_ready(chip);
525
526 tpm_cr50_release_locality(chip, false);
527 return rc;
528}
529
530
531
532
533
534
535
536
537
538
539
540static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
541{
542 size_t burstcnt, limit, sent = 0;
543 u8 tpm_go[4] = { TPM_STS_GO };
544 unsigned long stop;
545 u32 status;
546 int rc;
547
548 rc = tpm_cr50_request_locality(chip);
549 if (rc < 0)
550 return rc;
551
552
553 stop = jiffies + chip->timeout_b;
554 while (!(tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
555 if (time_after(jiffies, stop)) {
556 rc = -ETIMEDOUT;
557 goto out_err;
558 }
559
560 tpm_cr50_i2c_tis_set_ready(chip);
561 }
562
563 while (len > 0) {
564 u8 mask = TPM_STS_VALID;
565
566
567 if (sent > 0)
568 mask |= TPM_STS_DATA_EXPECT;
569
570
571 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
572 if (rc < 0)
573 goto out_err;
574
575
576
577
578
579 limit = min_t(size_t, burstcnt - 1, len);
580 rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(0), &buf[sent], limit);
581 if (rc < 0) {
582 dev_err(&chip->dev, "Write failed\n");
583 goto out_err;
584 }
585
586 sent += limit;
587 len -= limit;
588 }
589
590
591 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
592 if (rc < 0)
593 goto out_err;
594 if (status & TPM_STS_DATA_EXPECT) {
595 dev_err(&chip->dev, "Data still expected\n");
596 rc = -EIO;
597 goto out_err;
598 }
599
600
601 rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(0), tpm_go,
602 sizeof(tpm_go));
603 if (rc < 0) {
604 dev_err(&chip->dev, "Start command failed\n");
605 goto out_err;
606 }
607 return 0;
608
609out_err:
610
611 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
612 tpm_cr50_i2c_tis_set_ready(chip);
613
614 tpm_cr50_release_locality(chip, false);
615 return rc;
616}
617
618
619
620
621
622
623
624
625
626static bool tpm_cr50_i2c_req_canceled(struct tpm_chip *chip, u8 status)
627{
628 return status == TPM_STS_COMMAND_READY;
629}
630
631static const struct tpm_class_ops cr50_i2c = {
632 .flags = TPM_OPS_AUTO_STARTUP,
633 .status = &tpm_cr50_i2c_tis_status,
634 .recv = &tpm_cr50_i2c_tis_recv,
635 .send = &tpm_cr50_i2c_tis_send,
636 .cancel = &tpm_cr50_i2c_tis_set_ready,
637 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
638 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
639 .req_canceled = &tpm_cr50_i2c_req_canceled,
640};
641
642static const struct i2c_device_id cr50_i2c_table[] = {
643 {"cr50_i2c", 0},
644 {}
645};
646MODULE_DEVICE_TABLE(i2c, cr50_i2c_table);
647
648#ifdef CONFIG_ACPI
649static const struct acpi_device_id cr50_i2c_acpi_id[] = {
650 { "GOOG0005", 0 },
651 {}
652};
653MODULE_DEVICE_TABLE(acpi, cr50_i2c_acpi_id);
654#endif
655
656#ifdef CONFIG_OF
657static const struct of_device_id of_cr50_i2c_match[] = {
658 { .compatible = "google,cr50", },
659 {}
660};
661MODULE_DEVICE_TABLE(of, of_cr50_i2c_match);
662#endif
663
664
665
666
667
668
669
670
671
672
673static int tpm_cr50_i2c_probe(struct i2c_client *client,
674 const struct i2c_device_id *id)
675{
676 struct tpm_i2c_cr50_priv_data *priv;
677 struct device *dev = &client->dev;
678 struct tpm_chip *chip;
679 u32 vendor;
680 u8 buf[4];
681 int rc;
682
683 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
684 return -ENODEV;
685
686 chip = tpmm_chip_alloc(dev, &cr50_i2c);
687 if (IS_ERR(chip))
688 return PTR_ERR(chip);
689
690 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
691 if (!priv)
692 return -ENOMEM;
693
694
695 chip->flags |= TPM_CHIP_FLAG_TPM2;
696 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED;
697
698
699 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
700 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
701 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
702 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
703
704 dev_set_drvdata(&chip->dev, priv);
705 init_completion(&priv->tpm_ready);
706
707 if (client->irq > 0) {
708 rc = devm_request_irq(dev, client->irq, tpm_cr50_i2c_int_handler,
709 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
710 IRQF_NO_AUTOEN,
711 dev->driver->name, chip);
712 if (rc < 0) {
713 dev_err(dev, "Failed to probe IRQ %d\n", client->irq);
714 return rc;
715 }
716
717 priv->irq = client->irq;
718 } else {
719 dev_warn(dev, "No IRQ, will use %ums delay for TPM ready\n",
720 TPM_CR50_TIMEOUT_NOIRQ_MS);
721 }
722
723 rc = tpm_cr50_request_locality(chip);
724 if (rc < 0) {
725 dev_err(dev, "Could not request locality\n");
726 return rc;
727 }
728
729
730 rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(0), buf, sizeof(buf));
731 if (rc < 0) {
732 dev_err(dev, "Could not read vendor id\n");
733 tpm_cr50_release_locality(chip, true);
734 return rc;
735 }
736
737 vendor = le32_to_cpup((__le32 *)buf);
738 if (vendor != TPM_CR50_I2C_DID_VID) {
739 dev_err(dev, "Vendor ID did not match! ID was %08x\n", vendor);
740 tpm_cr50_release_locality(chip, true);
741 return -ENODEV;
742 }
743
744 dev_info(dev, "cr50 TPM 2.0 (i2c 0x%02x irq %d id 0x%x)\n",
745 client->addr, client->irq, vendor >> 16);
746
747 return tpm_chip_register(chip);
748}
749
750
751
752
753
754
755
756
757
758static int tpm_cr50_i2c_remove(struct i2c_client *client)
759{
760 struct tpm_chip *chip = i2c_get_clientdata(client);
761 struct device *dev = &client->dev;
762
763 if (!chip) {
764 dev_err(dev, "Could not get client data at remove\n");
765 return -ENODEV;
766 }
767
768 tpm_chip_unregister(chip);
769 tpm_cr50_release_locality(chip, true);
770
771 return 0;
772}
773
774static SIMPLE_DEV_PM_OPS(cr50_i2c_pm, tpm_pm_suspend, tpm_pm_resume);
775
776static struct i2c_driver cr50_i2c_driver = {
777 .id_table = cr50_i2c_table,
778 .probe = tpm_cr50_i2c_probe,
779 .remove = tpm_cr50_i2c_remove,
780 .driver = {
781 .name = "cr50_i2c",
782 .pm = &cr50_i2c_pm,
783 .acpi_match_table = ACPI_PTR(cr50_i2c_acpi_id),
784 .of_match_table = of_match_ptr(of_cr50_i2c_match),
785 },
786};
787
788module_i2c_driver(cr50_i2c_driver);
789
790MODULE_DESCRIPTION("cr50 TPM I2C Driver");
791MODULE_LICENSE("GPL");
792