1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2022 IBM Corp.
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/compiler.h>
15 #include <linux/types.h>
16 #include <linux/unaligned/be_byteshift.h>
17 #include <asm-generic/gpio.h>
20 #include "tpm_internal.h"
22 struct tpm_tis_chip_data {
23 unsigned int pcr_count;
24 unsigned int pcr_select_min;
27 static uint tpm_tis_i2c_address_to_register(u32 addr)
32 * Adapt register addresses that have changed compared to older TIS
47 static int tpm_tis_i2c_read(struct udevice *dev, u32 addr, u16 len, u8 *in)
51 uint reg = tpm_tis_i2c_address_to_register(addr);
54 rc = dm_i2c_read(dev, reg, in, len);
55 udelay(SLEEP_DURATION_US);
56 } while (rc && count++ < MAX_COUNT);
61 static int tpm_tis_i2c_write(struct udevice *dev, u32 addr, u16 len,
66 uint reg = tpm_tis_i2c_address_to_register(addr);
69 rc = dm_i2c_write(dev, reg, out, len);
70 udelay(SLEEP_DURATION_US);
71 } while (rc && count++ < MAX_COUNT);
76 static int tpm_tis_i2c_read32(struct udevice *dev, u32 addr, u32 *result)
81 rc = tpm_tis_i2c_read(dev, addr, sizeof(u32), (u8 *)&result_le);
83 *result = le32_to_cpu(result_le);
88 static int tpm_tis_i2c_write32(struct udevice *dev, u32 addr, u32 value)
90 __le32 value_le = cpu_to_le32(value);
92 return tpm_tis_i2c_write(dev, addr, sizeof(value), (u8 *)&value_le);
95 static struct tpm_tis_phy_ops phy_ops = {
96 .read_bytes = tpm_tis_i2c_read,
97 .write_bytes = tpm_tis_i2c_write,
98 .read32 = tpm_tis_i2c_read32,
99 .write32 = tpm_tis_i2c_write32,
102 static int tpm_tis_i2c_probe(struct udevice *udev)
104 struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(udev);
105 struct tpm_chip_priv *priv = dev_get_uclass_priv(udev);
109 tpm_tis_ops_register(udev, &phy_ops);
112 * Force locality 0. The core driver doesn't actually write the
113 * locality register and instead just reads/writes various access
114 * bits of the selected locality.
116 rc = dm_i2c_write(udev, 0, &loc, 1);
120 rc = tpm_tis_init(udev);
124 priv->pcr_count = drv_data->pcr_count;
125 priv->pcr_select_min = drv_data->pcr_select_min;
126 priv->version = TPM_V2;
131 static int tpm_tis_i2c_remove(struct udevice *udev)
133 return tpm_tis_cleanup(udev);
136 static const struct tpm_ops tpm_tis_i2c_ops = {
137 .open = tpm_tis_open,
138 .close = tpm_tis_close,
139 .get_desc = tpm_tis_get_desc,
140 .send = tpm_tis_send,
141 .recv = tpm_tis_recv,
142 .cleanup = tpm_tis_cleanup,
145 static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
150 static const struct udevice_id tpm_tis_i2c_ids[] = {
152 .compatible = "nuvoton,npct75x",
153 .data = (ulong)&tpm_tis_std_chip_data,
156 .compatible = "tcg,tpm-tis-i2c",
157 .data = (ulong)&tpm_tis_std_chip_data,
162 U_BOOT_DRIVER(tpm_tis_i2c) = {
163 .name = "tpm_tis_i2c",
165 .of_match = tpm_tis_i2c_ids,
166 .ops = &tpm_tis_i2c_ops,
167 .probe = tpm_tis_i2c_probe,
168 .remove = tpm_tis_i2c_remove,
169 .priv_auto = sizeof(struct tpm_chip),