blob: 8b39b12766f6cdd80ac53fef0f83df5beb599775 [file] [log] [blame]
developerd82d9fc2022-06-23 19:03:51 +08001From 6110010f7b88392a3094f2aaec91ee54151cde2a Mon Sep 17 00:00:00 2001
2From: "SkyLake.Huang" <skylake.huang@mediatek.com>
3Date: Thu, 23 Jun 2022 18:43:02 +0800
4Subject: [PATCH] drivers: char: tpm: Add calibration example for SPI TPM
5 module
6
7Signed-off-by: SkyLake.Huang <skylake.huang@mediatek.com>
8---
9 drivers/char/tpm/tpm_tis_core.c | 19 +++++++++++++++++++
10 drivers/char/tpm/tpm_tis_core.h | 2 ++
11 drivers/char/tpm/tpm_tis_spi.c | 7 +++++++
12 3 files changed, 28 insertions(+)
13
14diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
15index 70f785994..b9898a56d 100644
16--- a/drivers/char/tpm/tpm_tis_core.c
17+++ b/drivers/char/tpm/tpm_tis_core.c
18@@ -817,6 +817,21 @@ static const struct tpm_class_ops tpm_tis = {
19 .clk_enable = tpm_tis_clkrun_enable,
20 };
21
22+int tpm_tis_cal_read(void *priv, u32 *addr, int addrlen, u8 *buf, int readlen)
23+{
24+ int rc;
25+ u32 vendor;
26+
27+ rc = tpm_tis_read32((struct tpm_tis_data *)priv, TPM_DID_VID(0), &vendor);
28+ if (rc < 0)
29+ return -EIO;
30+
31+ buf[0] = (vendor >> 24) & 0xff;
32+ buf[1] = (vendor >> 16) & 0xff;
33+
34+ return 0;
35+}
36+
37 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
38 const struct tpm_tis_phy_ops *phy_ops,
39 acpi_handle acpi_dev_handle)
40@@ -864,6 +879,10 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
41 if (chip->ops->clk_enable != NULL)
42 chip->ops->clk_enable(chip, true);
43
44+ rc = priv->phy_ops->do_calibration(priv, dev);
45+ if (rc)
46+ goto out_err;
47+
48 if (wait_startup(chip, 0) != 0) {
49 rc = -ENODEV;
50 goto out_err;
51diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h
52index 7337819f5..7bb0bc8b6 100644
53--- a/drivers/char/tpm/tpm_tis_core.h
54+++ b/drivers/char/tpm/tpm_tis_core.h
55@@ -106,6 +106,7 @@ struct tpm_tis_phy_ops {
56 int (*read16)(struct tpm_tis_data *data, u32 addr, u16 *result);
57 int (*read32)(struct tpm_tis_data *data, u32 addr, u32 *result);
58 int (*write32)(struct tpm_tis_data *data, u32 addr, u32 src);
59+ int (*do_calibration)(struct tpm_tis_data *data, struct device *dev);
60 };
61
62 static inline int tpm_tis_read_bytes(struct tpm_tis_data *data, u32 addr,
63@@ -158,6 +159,7 @@ static inline bool is_bsw(void)
64 }
65
66 void tpm_tis_remove(struct tpm_chip *chip);
67+int tpm_tis_cal_read(void *priv, u32 *addr, int addrlen, u8 *buf, int readlen);
68 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
69 const struct tpm_tis_phy_ops *phy_ops,
70 acpi_handle acpi_dev_handle);
71diff --git a/drivers/char/tpm/tpm_tis_spi.c b/drivers/char/tpm/tpm_tis_spi.c
72index 19513e622..3be2d53a5 100644
73--- a/drivers/char/tpm/tpm_tis_spi.c
74+++ b/drivers/char/tpm/tpm_tis_spi.c
75@@ -184,12 +184,19 @@ static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
76 return rc;
77 }
78
79+int tpm_tis_spi_do_calibration(struct tpm_tis_data *priv, struct device *dev) {
80+ struct spi_device *spi = container_of(dev,
81+ struct spi_device, dev);
82+ return spi_do_calibration(spi->master, spi, tpm_tis_cal_read, priv);
83+}
84+
85 static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
86 .read_bytes = tpm_tis_spi_read_bytes,
87 .write_bytes = tpm_tis_spi_write_bytes,
88 .read16 = tpm_tis_spi_read16,
89 .read32 = tpm_tis_spi_read32,
90 .write32 = tpm_tis_spi_write32,
91+ .do_calibration = tpm_tis_spi_do_calibration,
92 };
93
94 static int tpm_tis_spi_probe(struct spi_device *dev)
95--
962.18.0
97