blob: 93f622f291048b7808bdc32b39f2b2c05cfe01ab [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0 */
Rong Changf6267992013-04-12 10:44:57 +00002/*
3 * Copyright (C) 2011 Infineon Technologies
4 *
5 * Authors:
6 * Peter Huewe <huewe.external@infineon.com>
7 *
8 * Version: 2.1.1
9 *
10 * Description:
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
13 *
14 * It is based on the Linux kernel driver tpm.c from Leendert van
15 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
Rong Changf6267992013-04-12 10:44:57 +000016 */
17
Simon Glass4cd7b782015-08-22 18:31:22 -060018#ifndef _TPM_TIS_I2C_H
19#define _TPM_TIS_I2C_H
Rong Changf6267992013-04-12 10:44:57 +000020
21#include <linux/compiler.h>
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +000022#include <linux/types.h>
Rong Changf6267992013-04-12 10:44:57 +000023
Ilias Apalodimas2c9626c2021-11-09 09:02:17 +020024/**
25 * struct tpm_tis_phy_ops - low-level TPM bus operations
26 */
27struct tpm_tis_phy_ops {
28 /* read_bytes() - Read a number of bytes from the device
29 *
30 * @udev: TPM device
31 * @addr: offset from device base
32 * @len: len to read
33 * @result: data read
34 *
35 * @return: 0 on success, negative on failure
36 */
37 int (*read_bytes)(struct udevice *udev, u32 addr, u16 len,
38 u8 *result);
39 /* write_bytes() - Read a number of bytes from the device
40 *
41 * @udev: TPM device
42 * @addr: offset from device base
43 * @len: len to read
44 * @value: data to write
45 *
46 * @return: 0 on success, negative on failure
47 */
48 int (*write_bytes)(struct udevice *udev, u32 addr, u16 len,
49 const u8 *value);
50 /* read32() - Read a 32bit value of the device
51 *
52 * @udev: TPM device
53 * @addr: offset from device base
54 * @result: data read
55 *
56 * @return: 0 on success, negative on failure
57 */
58 int (*read32)(struct udevice *udev, u32 addr, u32 *result);
59 /* write32() - write a 32bit value to the device
60 *
61 * @udev: TPM device
62 * @addr: offset from device base
63 * @src: data to write
64 *
65 * @return: 0 on success, negative on failure
66 */
67 int (*write32)(struct udevice *udev, u32 addr, u32 src);
68};
69
70enum tis_int_flags {
71 TPM_GLOBAL_INT_ENABLE = 0x80000000,
72 TPM_INTF_BURST_COUNT_STATIC = 0x100,
73 TPM_INTF_CMD_READY_INT = 0x080,
74 TPM_INTF_INT_EDGE_FALLING = 0x040,
75 TPM_INTF_INT_EDGE_RISING = 0x020,
76 TPM_INTF_INT_LEVEL_LOW = 0x010,
77 TPM_INTF_INT_LEVEL_HIGH = 0x008,
78 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
79 TPM_INTF_STS_VALID_INT = 0x002,
80 TPM_INTF_DATA_AVAIL_INT = 0x001,
81};
82
83#define TPM_ACCESS(l) (0x0000 | ((l) << 12))
84#define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
85#define TPM_STS(l) (0x0018 | ((l) << 12))
86#define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
87#define TPM_DID_VID(l) (0x0f00 | ((l) << 12))
88#define TPM_RID(l) (0x0f04 | ((l) << 12))
89#define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12))
90
Rong Changf6267992013-04-12 10:44:57 +000091enum tpm_timeout {
Simon Glass42c8ec52015-08-22 18:31:30 -060092 TPM_TIMEOUT_MS = 5,
93 TIS_SHORT_TIMEOUT_MS = 750,
94 TIS_LONG_TIMEOUT_MS = 2000,
95 SLEEP_DURATION_US = 60,
96 SLEEP_DURATION_LONG_US = 210,
Rong Changf6267992013-04-12 10:44:57 +000097};
98
99/* Size of external transmit buffer (used in tpm_transmit)*/
100#define TPM_BUFSIZE 4096
101
Rong Changf6267992013-04-12 10:44:57 +0000102/* Index of Count field in TPM response buffer */
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000103#define TPM_RSP_SIZE_BYTE 2
104#define TPM_RSP_RC_BYTE 6
Rong Changf6267992013-04-12 10:44:57 +0000105
Simon Glass7c735372015-08-22 18:31:24 -0600106struct tpm_chip {
107 int is_open;
Rong Changf6267992013-04-12 10:44:57 +0000108 int locality;
Simon Glassb697e0f2015-08-22 18:31:38 -0600109 u32 vend_dev;
Miquel Raynal06425aa2018-05-15 11:57:04 +0200110 u8 rid;
Tom Wai-Hong Tam1b393db2013-04-12 11:04:37 +0000111 unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* msec */
Christophe Ricard1259dcd2016-01-21 23:27:12 +0100112 ulong chip_type;
Ilias Apalodimas2c9626c2021-11-09 09:02:17 +0200113 struct tpm_tis_phy_ops *phy_ops;
Rong Changf6267992013-04-12 10:44:57 +0000114};
115
Rong Changf6267992013-04-12 10:44:57 +0000116struct tpm_input_header {
117 __be16 tag;
118 __be32 length;
119 __be32 ordinal;
120} __packed;
121
122struct tpm_output_header {
123 __be16 tag;
124 __be32 length;
125 __be32 return_code;
126} __packed;
127
128struct timeout_t {
129 __be32 a;
130 __be32 b;
131 __be32 c;
132 __be32 d;
133} __packed;
134
135struct duration_t {
136 __be32 tpm_short;
137 __be32 tpm_medium;
138 __be32 tpm_long;
139} __packed;
140
141union cap_t {
142 struct timeout_t timeout;
143 struct duration_t duration;
144};
145
146struct tpm_getcap_params_in {
147 __be32 cap;
148 __be32 subcap_size;
149 __be32 subcap;
150} __packed;
151
152struct tpm_getcap_params_out {
153 __be32 cap_size;
154 union cap_t cap;
155} __packed;
156
157union tpm_cmd_header {
158 struct tpm_input_header in;
159 struct tpm_output_header out;
160};
161
162union tpm_cmd_params {
163 struct tpm_getcap_params_out getcap_out;
164 struct tpm_getcap_params_in getcap_in;
165};
166
167struct tpm_cmd_t {
168 union tpm_cmd_header header;
169 union tpm_cmd_params params;
170} __packed;
171
Simon Glass13894bd2015-08-22 18:31:27 -0600172/* Max number of iterations after i2c NAK */
173#define MAX_COUNT 3
174
Johannes Hollandbedbb382020-05-11 15:22:25 +0200175#ifndef __TPM_V2_H
Simon Glass13894bd2015-08-22 18:31:27 -0600176/*
177 * Max number of iterations after i2c NAK for 'long' commands
178 *
179 * We need this especially for sending TPM_READY, since the cleanup after the
180 * transtion to the ready state may take some time, but it is unpredictable
181 * how long it will take.
182 */
183#define MAX_COUNT_LONG 50
184
Simon Glass13894bd2015-08-22 18:31:27 -0600185enum tis_access {
186 TPM_ACCESS_VALID = 0x80,
187 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
188 TPM_ACCESS_REQUEST_PENDING = 0x04,
189 TPM_ACCESS_REQUEST_USE = 0x02,
190};
191
192enum tis_status {
193 TPM_STS_VALID = 0x80,
194 TPM_STS_COMMAND_READY = 0x40,
195 TPM_STS_GO = 0x20,
196 TPM_STS_DATA_AVAIL = 0x10,
197 TPM_STS_DATA_EXPECT = 0x08,
198};
Johannes Hollandbedbb382020-05-11 15:22:25 +0200199#endif
Simon Glass13894bd2015-08-22 18:31:27 -0600200
Ilias Apalodimas2c9626c2021-11-09 09:02:17 +0200201/**
202 * tpm_tis_open - Open the device and request locality 0
203 *
204 * @dev: TPM device
205 *
206 * @return: 0 on success, negative on failure
207 */
208int tpm_tis_open(struct udevice *udev);
209/**
210 * tpm_tis_close - Close the device and release locality
211 *
212 * @dev: TPM device
213 *
214 * @return: 0 on success, negative on failure
215 */
216int tpm_tis_close(struct udevice *udev);
217/** tpm_tis_cleanup - Get the device in ready state and release locality
218 *
219 * @dev: TPM device
220 *
221 * @return: always 0
222 */
223int tpm_tis_cleanup(struct udevice *udev);
224/**
225 * tpm_tis_send - send data to the device
226 *
227 * @dev: TPM device
228 * @buf: buffer to send
229 * @len: size of the buffer
230 *
231 * @return: number of bytes sent or negative on failure
232 */
233int tpm_tis_send(struct udevice *udev, const u8 *buf, size_t len);
234/**
235 * tpm_tis_recv_data - Receive data from a device. Wrapper for tpm_tis_recv
236 *
237 * @dev: TPM device
238 * @buf: buffer to copy data
239 * @size: buffer size
240 *
241 * @return: bytes read or negative on failure
242 */
243int tpm_tis_recv(struct udevice *udev, u8 *buf, size_t count);
244/**
245 * tpm_tis_get_desc - Get the TPM description
246 *
247 * @dev: TPM device
248 * @buf: buffer to fill data
249 * @size: buffer size
250 *
251 * @return: Number of characters written (or would have been written) in buffer
252 */
253int tpm_tis_get_desc(struct udevice *udev, char *buf, int size);
254/**
255 * tpm_tis_init - inititalize the device
256 *
257 * @dev: TPM device
258 *
259 * @return: 0 on success, negative on failure
260 */
261int tpm_tis_init(struct udevice *udev);
262/**
263 * tpm_tis_ops_register - register the PHY ops for the device
264 *
265 * @dev: TPM device
266 * @ops: tpm_tis_phy_ops ops for the device
267 */
268void tpm_tis_ops_register(struct udevice *udev, struct tpm_tis_phy_ops *ops);
Rong Changf6267992013-04-12 10:44:57 +0000269#endif