tpm: tpm_tis_i2c: Move definitions into the header file
[karo-tx-uboot.git] / drivers / tpm / tpm_tis_i2c.h
1 /*
2  * Copyright (C) 2011 Infineon Technologies
3  *
4  * Authors:
5  * Peter Huewe <huewe.external@infineon.com>
6  *
7  * Version: 2.1.1
8  *
9  * Description:
10  * Device driver for TCG/TCPA TPM (trusted platform module).
11  * Specifications at www.trustedcomputinggroup.org
12  *
13  * It is based on the Linux kernel driver tpm.c from Leendert van
14  * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
15  *
16  * SPDX-License-Identifier:     GPL-2.0
17  */
18
19 #ifndef _TPM_TIS_I2C_H
20 #define _TPM_TIS_I2C_H
21
22 #include <linux/compiler.h>
23 #include <linux/types.h>
24
25 enum tpm_timeout {
26         TPM_TIMEOUT = 5,        /* msecs */
27 };
28
29 /* Size of external transmit buffer (used in tpm_transmit)*/
30 #define TPM_BUFSIZE 4096
31
32 /* Index of Count field in TPM response buffer */
33 #define TPM_RSP_SIZE_BYTE       2
34 #define TPM_RSP_RC_BYTE         6
35
36 /* Max buffer size supported by our tpm */
37 #define TPM_DEV_BUFSIZE         1260
38
39 enum i2c_chip_type {
40         SLB9635,
41         SLB9645,
42         UNKNOWN,
43 };
44
45 struct tpm_chip {
46         bool inited;
47         int is_open;
48         u8 req_complete_mask;
49         u8 req_complete_val;
50         u8 req_canceled;
51         int irq;
52         int locality;
53         unsigned long timeout_a, timeout_b, timeout_c, timeout_d;  /* msec */
54         unsigned long duration[3];  /* msec */
55         struct udevice *dev;
56         u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)];  /* Max buffer size + addr */
57         enum i2c_chip_type chip_type;
58 };
59
60 struct tpm_input_header {
61         __be16 tag;
62         __be32 length;
63         __be32 ordinal;
64 } __packed;
65
66 struct tpm_output_header {
67         __be16 tag;
68         __be32 length;
69         __be32 return_code;
70 } __packed;
71
72 struct timeout_t {
73         __be32 a;
74         __be32 b;
75         __be32 c;
76         __be32 d;
77 } __packed;
78
79 struct duration_t {
80         __be32 tpm_short;
81         __be32 tpm_medium;
82         __be32 tpm_long;
83 } __packed;
84
85 union cap_t {
86         struct timeout_t timeout;
87         struct duration_t duration;
88 };
89
90 struct tpm_getcap_params_in {
91         __be32 cap;
92         __be32 subcap_size;
93         __be32 subcap;
94 } __packed;
95
96 struct tpm_getcap_params_out {
97         __be32 cap_size;
98         union cap_t cap;
99 } __packed;
100
101 union tpm_cmd_header {
102         struct tpm_input_header in;
103         struct tpm_output_header out;
104 };
105
106 union tpm_cmd_params {
107         struct tpm_getcap_params_out getcap_out;
108         struct tpm_getcap_params_in getcap_in;
109 };
110
111 struct tpm_cmd_t {
112         union tpm_cmd_header header;
113         union tpm_cmd_params params;
114 } __packed;
115
116 /* Max number of iterations after i2c NAK */
117 #define MAX_COUNT               3
118
119 /*
120  * Max number of iterations after i2c NAK for 'long' commands
121  *
122  * We need this especially for sending TPM_READY, since the cleanup after the
123  * transtion to the ready state may take some time, but it is unpredictable
124  * how long it will take.
125  */
126 #define MAX_COUNT_LONG          50
127
128 #define SLEEP_DURATION          60      /* in usec */
129 #define SLEEP_DURATION_LONG     210     /* in usec */
130
131 #define TPM_HEADER_SIZE         10
132
133 enum tis_access {
134         TPM_ACCESS_VALID                = 0x80,
135         TPM_ACCESS_ACTIVE_LOCALITY      = 0x20,
136         TPM_ACCESS_REQUEST_PENDING      = 0x04,
137         TPM_ACCESS_REQUEST_USE          = 0x02,
138 };
139
140 enum tis_status {
141         TPM_STS_VALID                   = 0x80,
142         TPM_STS_COMMAND_READY           = 0x40,
143         TPM_STS_GO                      = 0x20,
144         TPM_STS_DATA_AVAIL              = 0x10,
145         TPM_STS_DATA_EXPECT             = 0x08,
146 };
147
148 enum tis_defaults {
149         TIS_SHORT_TIMEOUT               = 750,  /* ms */
150         TIS_LONG_TIMEOUT                = 2000, /* ms */
151 };
152
153 /* expected value for DIDVID register */
154 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
155 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
156
157 #define TPM_ACCESS(l)                   (0x0000 | ((l) << 4))
158 #define TPM_STS(l)                      (0x0001 | ((l) << 4))
159 #define TPM_DATA_FIFO(l)                (0x0005 | ((l) << 4))
160 #define TPM_DID_VID(l)                  (0x0006 | ((l) << 4))
161
162 enum tpm_duration {
163         TPM_SHORT = 0,
164         TPM_MEDIUM = 1,
165         TPM_LONG = 2,
166         TPM_UNDEFINED,
167 };
168
169 /* Extended error numbers from linux (see errno.h) */
170 #define ECANCELED       125     /* Operation Canceled */
171
172 /* Timer frequency. Corresponds to msec timer resolution*/
173 #define HZ              1000
174
175 #define TPM_MAX_ORDINAL                 243
176 #define TPM_MAX_PROTECTED_ORDINAL       12
177 #define TPM_PROTECTED_ORDINAL_MASK      0xFF
178
179 #define TPM_CMD_COUNT_BYTE      2
180 #define TPM_CMD_ORDINAL_BYTE    6
181
182 /*
183  * Array with one entry per ordinal defining the maximum amount
184  * of time the chip could take to return the result.  The ordinal
185  * designation of short, medium or long is defined in a table in
186  * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
187  * values of the SHORT, MEDIUM, and LONG durations are retrieved
188  * from the chip during initialization with a call to tpm_get_timeouts.
189  */
190 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
191         TPM_UNDEFINED,          /* 0 */
192         TPM_UNDEFINED,
193         TPM_UNDEFINED,
194         TPM_UNDEFINED,
195         TPM_UNDEFINED,
196         TPM_UNDEFINED,          /* 5 */
197         TPM_UNDEFINED,
198         TPM_UNDEFINED,
199         TPM_UNDEFINED,
200         TPM_UNDEFINED,
201         TPM_SHORT,              /* 10 */
202         TPM_SHORT,
203 };
204
205 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
206         TPM_UNDEFINED,          /* 0 */
207         TPM_UNDEFINED,
208         TPM_UNDEFINED,
209         TPM_UNDEFINED,
210         TPM_UNDEFINED,
211         TPM_UNDEFINED,          /* 5 */
212         TPM_UNDEFINED,
213         TPM_UNDEFINED,
214         TPM_UNDEFINED,
215         TPM_UNDEFINED,
216         TPM_SHORT,              /* 10 */
217         TPM_SHORT,
218         TPM_MEDIUM,
219         TPM_LONG,
220         TPM_LONG,
221         TPM_MEDIUM,             /* 15 */
222         TPM_SHORT,
223         TPM_SHORT,
224         TPM_MEDIUM,
225         TPM_LONG,
226         TPM_SHORT,              /* 20 */
227         TPM_SHORT,
228         TPM_MEDIUM,
229         TPM_MEDIUM,
230         TPM_MEDIUM,
231         TPM_SHORT,              /* 25 */
232         TPM_SHORT,
233         TPM_MEDIUM,
234         TPM_SHORT,
235         TPM_SHORT,
236         TPM_MEDIUM,             /* 30 */
237         TPM_LONG,
238         TPM_MEDIUM,
239         TPM_SHORT,
240         TPM_SHORT,
241         TPM_SHORT,              /* 35 */
242         TPM_MEDIUM,
243         TPM_MEDIUM,
244         TPM_UNDEFINED,
245         TPM_UNDEFINED,
246         TPM_MEDIUM,             /* 40 */
247         TPM_LONG,
248         TPM_MEDIUM,
249         TPM_SHORT,
250         TPM_SHORT,
251         TPM_SHORT,              /* 45 */
252         TPM_SHORT,
253         TPM_SHORT,
254         TPM_SHORT,
255         TPM_LONG,
256         TPM_MEDIUM,             /* 50 */
257         TPM_MEDIUM,
258         TPM_UNDEFINED,
259         TPM_UNDEFINED,
260         TPM_UNDEFINED,
261         TPM_UNDEFINED,          /* 55 */
262         TPM_UNDEFINED,
263         TPM_UNDEFINED,
264         TPM_UNDEFINED,
265         TPM_UNDEFINED,
266         TPM_MEDIUM,             /* 60 */
267         TPM_MEDIUM,
268         TPM_MEDIUM,
269         TPM_SHORT,
270         TPM_SHORT,
271         TPM_MEDIUM,             /* 65 */
272         TPM_UNDEFINED,
273         TPM_UNDEFINED,
274         TPM_UNDEFINED,
275         TPM_UNDEFINED,
276         TPM_SHORT,              /* 70 */
277         TPM_SHORT,
278         TPM_UNDEFINED,
279         TPM_UNDEFINED,
280         TPM_UNDEFINED,
281         TPM_UNDEFINED,          /* 75 */
282         TPM_UNDEFINED,
283         TPM_UNDEFINED,
284         TPM_UNDEFINED,
285         TPM_UNDEFINED,
286         TPM_LONG,               /* 80 */
287         TPM_UNDEFINED,
288         TPM_MEDIUM,
289         TPM_LONG,
290         TPM_SHORT,
291         TPM_UNDEFINED,          /* 85 */
292         TPM_UNDEFINED,
293         TPM_UNDEFINED,
294         TPM_UNDEFINED,
295         TPM_UNDEFINED,
296         TPM_SHORT,              /* 90 */
297         TPM_SHORT,
298         TPM_SHORT,
299         TPM_SHORT,
300         TPM_SHORT,
301         TPM_UNDEFINED,          /* 95 */
302         TPM_UNDEFINED,
303         TPM_UNDEFINED,
304         TPM_UNDEFINED,
305         TPM_UNDEFINED,
306         TPM_MEDIUM,             /* 100 */
307         TPM_SHORT,
308         TPM_SHORT,
309         TPM_UNDEFINED,
310         TPM_UNDEFINED,
311         TPM_UNDEFINED,          /* 105 */
312         TPM_UNDEFINED,
313         TPM_UNDEFINED,
314         TPM_UNDEFINED,
315         TPM_UNDEFINED,
316         TPM_SHORT,              /* 110 */
317         TPM_SHORT,
318         TPM_SHORT,
319         TPM_SHORT,
320         TPM_SHORT,
321         TPM_SHORT,              /* 115 */
322         TPM_SHORT,
323         TPM_SHORT,
324         TPM_UNDEFINED,
325         TPM_UNDEFINED,
326         TPM_LONG,               /* 120 */
327         TPM_LONG,
328         TPM_MEDIUM,
329         TPM_UNDEFINED,
330         TPM_SHORT,
331         TPM_SHORT,              /* 125 */
332         TPM_SHORT,
333         TPM_LONG,
334         TPM_SHORT,
335         TPM_SHORT,
336         TPM_SHORT,              /* 130 */
337         TPM_MEDIUM,
338         TPM_UNDEFINED,
339         TPM_SHORT,
340         TPM_MEDIUM,
341         TPM_UNDEFINED,          /* 135 */
342         TPM_UNDEFINED,
343         TPM_UNDEFINED,
344         TPM_UNDEFINED,
345         TPM_UNDEFINED,
346         TPM_SHORT,              /* 140 */
347         TPM_SHORT,
348         TPM_UNDEFINED,
349         TPM_UNDEFINED,
350         TPM_UNDEFINED,
351         TPM_UNDEFINED,          /* 145 */
352         TPM_UNDEFINED,
353         TPM_UNDEFINED,
354         TPM_UNDEFINED,
355         TPM_UNDEFINED,
356         TPM_SHORT,              /* 150 */
357         TPM_MEDIUM,
358         TPM_MEDIUM,
359         TPM_SHORT,
360         TPM_SHORT,
361         TPM_UNDEFINED,          /* 155 */
362         TPM_UNDEFINED,
363         TPM_UNDEFINED,
364         TPM_UNDEFINED,
365         TPM_UNDEFINED,
366         TPM_SHORT,              /* 160 */
367         TPM_SHORT,
368         TPM_SHORT,
369         TPM_SHORT,
370         TPM_UNDEFINED,
371         TPM_UNDEFINED,          /* 165 */
372         TPM_UNDEFINED,
373         TPM_UNDEFINED,
374         TPM_UNDEFINED,
375         TPM_UNDEFINED,
376         TPM_LONG,               /* 170 */
377         TPM_UNDEFINED,
378         TPM_UNDEFINED,
379         TPM_UNDEFINED,
380         TPM_UNDEFINED,
381         TPM_UNDEFINED,          /* 175 */
382         TPM_UNDEFINED,
383         TPM_UNDEFINED,
384         TPM_UNDEFINED,
385         TPM_UNDEFINED,
386         TPM_MEDIUM,             /* 180 */
387         TPM_SHORT,
388         TPM_MEDIUM,
389         TPM_MEDIUM,
390         TPM_MEDIUM,
391         TPM_MEDIUM,             /* 185 */
392         TPM_SHORT,
393         TPM_UNDEFINED,
394         TPM_UNDEFINED,
395         TPM_UNDEFINED,
396         TPM_UNDEFINED,          /* 190 */
397         TPM_UNDEFINED,
398         TPM_UNDEFINED,
399         TPM_UNDEFINED,
400         TPM_UNDEFINED,
401         TPM_UNDEFINED,          /* 195 */
402         TPM_UNDEFINED,
403         TPM_UNDEFINED,
404         TPM_UNDEFINED,
405         TPM_UNDEFINED,
406         TPM_SHORT,              /* 200 */
407         TPM_UNDEFINED,
408         TPM_UNDEFINED,
409         TPM_UNDEFINED,
410         TPM_SHORT,
411         TPM_SHORT,              /* 205 */
412         TPM_SHORT,
413         TPM_SHORT,
414         TPM_SHORT,
415         TPM_SHORT,
416         TPM_MEDIUM,             /* 210 */
417         TPM_UNDEFINED,
418         TPM_MEDIUM,
419         TPM_MEDIUM,
420         TPM_MEDIUM,
421         TPM_UNDEFINED,          /* 215 */
422         TPM_MEDIUM,
423         TPM_UNDEFINED,
424         TPM_UNDEFINED,
425         TPM_SHORT,
426         TPM_SHORT,              /* 220 */
427         TPM_SHORT,
428         TPM_SHORT,
429         TPM_SHORT,
430         TPM_SHORT,
431         TPM_UNDEFINED,          /* 225 */
432         TPM_UNDEFINED,
433         TPM_UNDEFINED,
434         TPM_UNDEFINED,
435         TPM_UNDEFINED,
436         TPM_SHORT,              /* 230 */
437         TPM_LONG,
438         TPM_MEDIUM,
439         TPM_UNDEFINED,
440         TPM_UNDEFINED,
441         TPM_UNDEFINED,          /* 235 */
442         TPM_UNDEFINED,
443         TPM_UNDEFINED,
444         TPM_UNDEFINED,
445         TPM_UNDEFINED,
446         TPM_SHORT,              /* 240 */
447         TPM_UNDEFINED,
448         TPM_MEDIUM,
449 };
450
451 #endif