xref: /openbmc/linux/drivers/char/tpm/tpm_tis_core.h (revision bbaf1ff0)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2005, 2006 IBM Corporation
4  * Copyright (C) 2014, 2015 Intel Corporation
5  *
6  * Authors:
7  * Leendert van Doorn <leendert@watson.ibm.com>
8  * Kylene Hall <kjhall@us.ibm.com>
9  *
10  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11  *
12  * Device driver for TCG/TCPA TPM (trusted platform module).
13  * Specifications at www.trustedcomputinggroup.org
14  *
15  * This device driver implements the TPM interface as defined in
16  * the TCG TPM Interface Spec version 1.2, revision 1.0.
17  */
18 
19 #ifndef __TPM_TIS_CORE_H__
20 #define __TPM_TIS_CORE_H__
21 
22 #include "tpm.h"
23 
24 enum tis_access {
25 	TPM_ACCESS_VALID = 0x80,
26 	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
27 	TPM_ACCESS_REQUEST_PENDING = 0x04,
28 	TPM_ACCESS_REQUEST_USE = 0x02,
29 };
30 
31 enum tis_status {
32 	TPM_STS_VALID = 0x80,
33 	TPM_STS_COMMAND_READY = 0x40,
34 	TPM_STS_GO = 0x20,
35 	TPM_STS_DATA_AVAIL = 0x10,
36 	TPM_STS_DATA_EXPECT = 0x08,
37 	TPM_STS_READ_ZERO = 0x23, /* bits that must be zero on read */
38 };
39 
40 enum tis_int_flags {
41 	TPM_GLOBAL_INT_ENABLE = 0x80000000,
42 	TPM_INTF_BURST_COUNT_STATIC = 0x100,
43 	TPM_INTF_CMD_READY_INT = 0x080,
44 	TPM_INTF_INT_EDGE_FALLING = 0x040,
45 	TPM_INTF_INT_EDGE_RISING = 0x020,
46 	TPM_INTF_INT_LEVEL_LOW = 0x010,
47 	TPM_INTF_INT_LEVEL_HIGH = 0x008,
48 	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
49 	TPM_INTF_STS_VALID_INT = 0x002,
50 	TPM_INTF_DATA_AVAIL_INT = 0x001,
51 };
52 
53 enum tis_defaults {
54 	TIS_MEM_LEN = 0x5000,
55 	TIS_SHORT_TIMEOUT = 750,	/* ms */
56 	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
57 	TIS_TIMEOUT_MIN_ATML = 14700,	/* usecs */
58 	TIS_TIMEOUT_MAX_ATML = 15000,	/* usecs */
59 };
60 
61 /* Some timeout values are needed before it is known whether the chip is
62  * TPM 1.0 or TPM 2.0.
63  */
64 #define TIS_TIMEOUT_A_MAX	max_t(int, TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
65 #define TIS_TIMEOUT_B_MAX	max_t(int, TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
66 #define TIS_TIMEOUT_C_MAX	max_t(int, TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
67 #define TIS_TIMEOUT_D_MAX	max_t(int, TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
68 
69 #define	TPM_ACCESS(l)			(0x0000 | ((l) << 12))
70 #define	TPM_INT_ENABLE(l)		(0x0008 | ((l) << 12))
71 #define	TPM_INT_VECTOR(l)		(0x000C | ((l) << 12))
72 #define	TPM_INT_STATUS(l)		(0x0010 | ((l) << 12))
73 #define	TPM_INTF_CAPS(l)		(0x0014 | ((l) << 12))
74 #define	TPM_STS(l)			(0x0018 | ((l) << 12))
75 #define	TPM_STS3(l)			(0x001b | ((l) << 12))
76 #define	TPM_DATA_FIFO(l)		(0x0024 | ((l) << 12))
77 
78 #define	TPM_DID_VID(l)			(0x0F00 | ((l) << 12))
79 #define	TPM_RID(l)			(0x0F04 | ((l) << 12))
80 
81 #define LPC_CNTRL_OFFSET		0x84
82 #define LPC_CLKRUN_EN			(1 << 2)
83 #define INTEL_LEGACY_BLK_BASE_ADDR	0xFED08000
84 #define ILB_REMAP_SIZE			0x100
85 
86 enum tpm_tis_flags {
87 	TPM_TIS_ITPM_WORKAROUND		= 0,
88 	TPM_TIS_INVALID_STATUS		= 1,
89 	TPM_TIS_DEFAULT_CANCELLATION	= 2,
90 	TPM_TIS_IRQ_TESTED		= 3,
91 };
92 
93 struct tpm_tis_data {
94 	u16 manufacturer_id;
95 	struct mutex locality_count_mutex;
96 	unsigned int locality_count;
97 	int locality;
98 	int irq;
99 	unsigned int int_mask;
100 	unsigned long flags;
101 	void __iomem *ilb_base_addr;
102 	u16 clkrun_enabled;
103 	wait_queue_head_t int_queue;
104 	wait_queue_head_t read_queue;
105 	const struct tpm_tis_phy_ops *phy_ops;
106 	unsigned short rng_quality;
107 	unsigned int timeout_min; /* usecs */
108 	unsigned int timeout_max; /* usecs */
109 };
110 
111 /*
112  * IO modes to indicate how many bytes should be read/written at once in the
113  * tpm_tis_phy_ops read_bytes/write_bytes calls. Use TPM_TIS_PHYS_8 to
114  * receive/transmit byte-wise, TPM_TIS_PHYS_16 for two bytes etc.
115  */
116 enum tpm_tis_io_mode {
117 	TPM_TIS_PHYS_8,
118 	TPM_TIS_PHYS_16,
119 	TPM_TIS_PHYS_32,
120 };
121 
122 struct tpm_tis_phy_ops {
123 	/* data is passed in little endian */
124 	int (*read_bytes)(struct tpm_tis_data *data, u32 addr, u16 len,
125 			  u8 *result, enum tpm_tis_io_mode mode);
126 	int (*write_bytes)(struct tpm_tis_data *data, u32 addr, u16 len,
127 			   const u8 *value, enum tpm_tis_io_mode mode);
128 	int (*verify_crc)(struct tpm_tis_data *data, size_t len,
129 			  const u8 *value);
130 };
131 
132 static inline int tpm_tis_read_bytes(struct tpm_tis_data *data, u32 addr,
133 				     u16 len, u8 *result)
134 {
135 	return data->phy_ops->read_bytes(data, addr, len, result,
136 					 TPM_TIS_PHYS_8);
137 }
138 
139 static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result)
140 {
141 	return data->phy_ops->read_bytes(data, addr, 1, result, TPM_TIS_PHYS_8);
142 }
143 
144 static inline int tpm_tis_read16(struct tpm_tis_data *data, u32 addr,
145 				 u16 *result)
146 {
147 	__le16 result_le;
148 	int rc;
149 
150 	rc = data->phy_ops->read_bytes(data, addr, sizeof(u16),
151 				       (u8 *)&result_le, TPM_TIS_PHYS_16);
152 	if (!rc)
153 		*result = le16_to_cpu(result_le);
154 
155 	return rc;
156 }
157 
158 static inline int tpm_tis_read32(struct tpm_tis_data *data, u32 addr,
159 				 u32 *result)
160 {
161 	__le32 result_le;
162 	int rc;
163 
164 	rc = data->phy_ops->read_bytes(data, addr, sizeof(u32),
165 				       (u8 *)&result_le, TPM_TIS_PHYS_32);
166 	if (!rc)
167 		*result = le32_to_cpu(result_le);
168 
169 	return rc;
170 }
171 
172 static inline int tpm_tis_write_bytes(struct tpm_tis_data *data, u32 addr,
173 				      u16 len, const u8 *value)
174 {
175 	return data->phy_ops->write_bytes(data, addr, len, value,
176 					  TPM_TIS_PHYS_8);
177 }
178 
179 static inline int tpm_tis_write8(struct tpm_tis_data *data, u32 addr, u8 value)
180 {
181 	return data->phy_ops->write_bytes(data, addr, 1, &value,
182 					  TPM_TIS_PHYS_8);
183 }
184 
185 static inline int tpm_tis_write32(struct tpm_tis_data *data, u32 addr,
186 				  u32 value)
187 {
188 	__le32 value_le;
189 	int rc;
190 
191 	value_le = cpu_to_le32(value);
192 	rc =  data->phy_ops->write_bytes(data, addr, sizeof(u32),
193 					 (u8 *)&value_le, TPM_TIS_PHYS_32);
194 	return rc;
195 }
196 
197 static inline int tpm_tis_verify_crc(struct tpm_tis_data *data, size_t len,
198 				     const u8 *value)
199 {
200 	if (!data->phy_ops->verify_crc)
201 		return 0;
202 	return data->phy_ops->verify_crc(data, len, value);
203 }
204 
205 static inline bool is_bsw(void)
206 {
207 #ifdef CONFIG_X86
208 	return ((boot_cpu_data.x86_model == INTEL_FAM6_ATOM_AIRMONT) ? 1 : 0);
209 #else
210 	return false;
211 #endif
212 }
213 
214 void tpm_tis_remove(struct tpm_chip *chip);
215 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
216 		      const struct tpm_tis_phy_ops *phy_ops,
217 		      acpi_handle acpi_dev_handle);
218 
219 #ifdef CONFIG_PM_SLEEP
220 int tpm_tis_resume(struct device *dev);
221 #endif
222 
223 #endif
224