summaryrefslogtreecommitdiff
path: root/drivers/tpm/tpm_tis.h
blob: 93f622f291048b7808bdc32b39f2b2c05cfe01ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2011 Infineon Technologies
 *
 * Authors:
 * Peter Huewe <huewe.external@infineon.com>
 *
 * Version: 2.1.1
 *
 * Description:
 * Device driver for TCG/TCPA TPM (trusted platform module).
 * Specifications at www.trustedcomputinggroup.org
 *
 * It is based on the Linux kernel driver tpm.c from Leendert van
 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
 */

#ifndef _TPM_TIS_I2C_H
#define _TPM_TIS_I2C_H

#include <linux/compiler.h>
#include <linux/types.h>

/**
 * struct tpm_tis_phy_ops - low-level TPM bus operations
 */
struct tpm_tis_phy_ops {
	/* read_bytes() - Read a number of bytes from the device
	 *
	 * @udev:   TPM device
	 * @addr:   offset from device base
	 * @len:    len to read
	 * @result: data read
	 *
	 * @return: 0 on success, negative on failure
	 */
	int (*read_bytes)(struct udevice *udev, u32 addr, u16 len,
			  u8 *result);
	/* write_bytes() - Read a number of bytes from the device
	 *
	 * @udev:   TPM device
	 * @addr:   offset from device base
	 * @len:    len to read
	 * @value:  data to write
	 *
	 * @return: 0 on success, negative on failure
	 */
	int (*write_bytes)(struct udevice *udev, u32 addr, u16 len,
			   const u8 *value);
	/* read32() - Read a 32bit value of the device
	 *
	 * @udev:   TPM device
	 * @addr:   offset from device base
	 * @result: data read
	 *
	 * @return: 0 on success, negative on failure
	 */
	int (*read32)(struct udevice *udev, u32 addr, u32 *result);
	/* write32() - write a 32bit value to the device
	 *
	 * @udev: TPM device
	 * @addr: offset from device base
	 * @src:  data to write
	 *
	 * @return: 0 on success, negative on failure
	 */
	int (*write32)(struct udevice *udev, u32 addr, u32 src);
};

enum tis_int_flags {
	TPM_GLOBAL_INT_ENABLE = 0x80000000,
	TPM_INTF_BURST_COUNT_STATIC = 0x100,
	TPM_INTF_CMD_READY_INT = 0x080,
	TPM_INTF_INT_EDGE_FALLING = 0x040,
	TPM_INTF_INT_EDGE_RISING = 0x020,
	TPM_INTF_INT_LEVEL_LOW = 0x010,
	TPM_INTF_INT_LEVEL_HIGH = 0x008,
	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
	TPM_INTF_STS_VALID_INT = 0x002,
	TPM_INTF_DATA_AVAIL_INT = 0x001,
};

#define TPM_ACCESS(l)                   (0x0000 | ((l) << 12))
#define TPM_INT_ENABLE(l)               (0x0008 | ((l) << 12))
#define TPM_STS(l)                      (0x0018 | ((l) << 12))
#define TPM_DATA_FIFO(l)                (0x0024 | ((l) << 12))
#define TPM_DID_VID(l)                  (0x0f00 | ((l) << 12))
#define TPM_RID(l)                      (0x0f04 | ((l) << 12))
#define TPM_INTF_CAPS(l)                (0x0014 | ((l) << 12))

enum tpm_timeout {
	TPM_TIMEOUT_MS			= 5,
	TIS_SHORT_TIMEOUT_MS		= 750,
	TIS_LONG_TIMEOUT_MS		= 2000,
	SLEEP_DURATION_US		= 60,
	SLEEP_DURATION_LONG_US		= 210,
};

/* Size of external transmit buffer (used in tpm_transmit)*/
#define TPM_BUFSIZE 4096

/* Index of Count field in TPM response buffer */
#define TPM_RSP_SIZE_BYTE	2
#define TPM_RSP_RC_BYTE		6

struct tpm_chip {
	int is_open;
	int locality;
	u32 vend_dev;
	u8 rid;
	unsigned long timeout_a, timeout_b, timeout_c, timeout_d;  /* msec */
	ulong chip_type;
	struct tpm_tis_phy_ops *phy_ops;
};

struct tpm_input_header {
	__be16 tag;
	__be32 length;
	__be32 ordinal;
} __packed;

struct tpm_output_header {
	__be16 tag;
	__be32 length;
	__be32 return_code;
} __packed;

struct timeout_t {
	__be32 a;
	__be32 b;
	__be32 c;
	__be32 d;
} __packed;

struct duration_t {
	__be32 tpm_short;
	__be32 tpm_medium;
	__be32 tpm_long;
} __packed;

union cap_t {
	struct timeout_t timeout;
	struct duration_t duration;
};

struct tpm_getcap_params_in {
	__be32 cap;
	__be32 subcap_size;
	__be32 subcap;
} __packed;

struct tpm_getcap_params_out {
	__be32 cap_size;
	union cap_t cap;
} __packed;

union tpm_cmd_header {
	struct tpm_input_header in;
	struct tpm_output_header out;
};

union tpm_cmd_params {
	struct tpm_getcap_params_out getcap_out;
	struct tpm_getcap_params_in getcap_in;
};

struct tpm_cmd_t {
	union tpm_cmd_header header;
	union tpm_cmd_params params;
} __packed;

/* Max number of iterations after i2c NAK */
#define MAX_COUNT		3

#ifndef __TPM_V2_H
/*
 * Max number of iterations after i2c NAK for 'long' commands
 *
 * We need this especially for sending TPM_READY, since the cleanup after the
 * transtion to the ready state may take some time, but it is unpredictable
 * how long it will take.
 */
#define MAX_COUNT_LONG		50

enum tis_access {
	TPM_ACCESS_VALID		= 0x80,
	TPM_ACCESS_ACTIVE_LOCALITY	= 0x20,
	TPM_ACCESS_REQUEST_PENDING	= 0x04,
	TPM_ACCESS_REQUEST_USE		= 0x02,
};

enum tis_status {
	TPM_STS_VALID			= 0x80,
	TPM_STS_COMMAND_READY		= 0x40,
	TPM_STS_GO			= 0x20,
	TPM_STS_DATA_AVAIL		= 0x10,
	TPM_STS_DATA_EXPECT		= 0x08,
};
#endif

/**
 * tpm_tis_open - Open the device and request locality 0
 *
 * @dev:  TPM device
 *
 * @return: 0 on success, negative on failure
 */
int tpm_tis_open(struct udevice *udev);
/**
 * tpm_tis_close - Close the device and release locality
 *
 * @dev:  TPM device
 *
 * @return: 0 on success, negative on failure
 */
int tpm_tis_close(struct udevice *udev);
/** tpm_tis_cleanup - Get the device in ready state and release locality
 *
 * @dev:  TPM device
 *
 * @return: always 0
 */
int tpm_tis_cleanup(struct udevice *udev);
/**
 * tpm_tis_send - send data to the device
 *
 * @dev:  TPM device
 * @buf:  buffer to send
 * @len:  size of the buffer
 *
 * @return: number of bytes sent or negative on failure
 */
int tpm_tis_send(struct udevice *udev, const u8 *buf, size_t len);
/**
 * tpm_tis_recv_data - Receive data from a device. Wrapper for tpm_tis_recv
 *
 * @dev:  TPM device
 * @buf:  buffer to copy data
 * @size: buffer size
 *
 * @return: bytes read or negative on failure
 */
int tpm_tis_recv(struct udevice *udev, u8 *buf, size_t count);
/**
 * tpm_tis_get_desc - Get the TPM description
 *
 * @dev:  TPM device
 * @buf:  buffer to fill data
 * @size: buffer size
 *
 * @return: Number of characters written (or would have been written) in buffer
 */
int tpm_tis_get_desc(struct udevice *udev, char *buf, int size);
/**
 * tpm_tis_init - inititalize the device
 *
 * @dev:  TPM device
 *
 * @return: 0 on success, negative on failure
 */
int tpm_tis_init(struct udevice *udev);
/**
 * tpm_tis_ops_register - register the PHY ops for the device
 *
 * @dev: TPM device
 * @ops: tpm_tis_phy_ops ops for the device
 */
void tpm_tis_ops_register(struct udevice *udev, struct tpm_tis_phy_ops *ops);
#endif