1 // Implementation of a TPM driver for the TPM TIS interface
3 // Copyright (C) 2006-2011 IBM Corporation
6 // Stefan Berger <stefanb@linux.vnet.ibm.com>
8 // This file may be distributed under the terms of the GNU LGPLv3 license.
10 #include "config.h" // CONFIG_TPM_TIS_SHA1THRESHOLD
11 #include "string.h" // memcpy
12 #include "util.h" // msleep
13 #include "x86.h" // readl
14 #include "hw/tpm_drivers.h" // struct tpm_driver
15 #include "tcgbios.h" // TCG_*
17 static const u32 tis_default_timeouts[4] = {
18 TIS_DEFAULT_TIMEOUT_A,
19 TIS_DEFAULT_TIMEOUT_B,
20 TIS_DEFAULT_TIMEOUT_C,
21 TIS_DEFAULT_TIMEOUT_D,
24 static const u32 tpm_default_durations[3] = {
25 TPM_DEFAULT_DURATION_SHORT,
26 TPM_DEFAULT_DURATION_MEDIUM,
27 TPM_DEFAULT_DURATION_LONG,
30 /* determined values */
31 static u32 tpm_default_dur[3];
32 static u32 tpm_default_to[4];
35 /* if device is not there, return '0', '1' otherwise */
36 static u32 tis_probe(void)
42 u32 didvid = readl(TIS_REG(0, TIS_REG_DID_VID));
44 if ((didvid != 0) && (didvid != 0xffffffff))
50 static u32 tis_init(void)
55 writeb(TIS_REG(0, TIS_REG_INT_ENABLE), 0);
57 if (tpm_drivers[TIS_DRIVER_IDX].durations == NULL) {
58 u32 *durations = tpm_default_dur;
59 memcpy(durations, tpm_default_durations,
60 sizeof(tpm_default_durations));
61 tpm_drivers[TIS_DRIVER_IDX].durations = durations;
64 if (tpm_drivers[TIS_DRIVER_IDX].timeouts == NULL) {
65 u32 *timeouts = tpm_default_to;
66 memcpy(timeouts, tis_default_timeouts,
67 sizeof(tis_default_timeouts));
68 tpm_drivers[TIS_DRIVER_IDX].timeouts = timeouts;
75 static void set_timeouts(u32 timeouts[4], u32 durations[3])
80 u32 *tos = tpm_drivers[TIS_DRIVER_IDX].timeouts;
81 u32 *dus = tpm_drivers[TIS_DRIVER_IDX].durations;
83 if (tos && tos != tis_default_timeouts && timeouts)
84 memcpy(tos, timeouts, 4 * sizeof(u32));
85 if (dus && dus != tpm_default_durations && durations)
86 memcpy(dus, durations, 3 * sizeof(u32));
90 static u32 tis_wait_sts(u8 locty, u32 time, u8 mask, u8 expect)
98 u8 sts = readb(TIS_REG(locty, TIS_REG_STS));
99 if ((sts & mask) == expect) {
109 static u32 tis_activate(u8 locty)
117 u32 timeout_a = tpm_drivers[TIS_DRIVER_IDX].timeouts[TIS_TIMEOUT_TYPE_A];
119 if (!(readb(TIS_REG(locty, TIS_REG_ACCESS)) &
120 TIS_ACCESS_ACTIVE_LOCALITY)) {
121 /* release locality in use top-downwards */
122 for (l = 4; l >= 0; l--)
123 writeb(TIS_REG(l, TIS_REG_ACCESS),
124 TIS_ACCESS_ACTIVE_LOCALITY);
127 /* request access to locality */
128 writeb(TIS_REG(locty, TIS_REG_ACCESS), TIS_ACCESS_REQUEST_USE);
130 acc = readb(TIS_REG(locty, TIS_REG_ACCESS));
131 if ((acc & TIS_ACCESS_ACTIVE_LOCALITY)) {
132 writeb(TIS_REG(locty, TIS_REG_STS), TIS_STS_COMMAND_READY);
133 rc = tis_wait_sts(locty, timeout_a,
134 TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY);
140 static u32 tis_find_active_locality(void)
147 for (locty = 0; locty <= 4; locty++) {
148 if ((readb(TIS_REG(locty, TIS_REG_ACCESS)) &
149 TIS_ACCESS_ACTIVE_LOCALITY))
158 static u32 tis_ready(void)
164 u8 locty = tis_find_active_locality();
165 u32 timeout_b = tpm_drivers[TIS_DRIVER_IDX].timeouts[TIS_TIMEOUT_TYPE_B];
167 writeb(TIS_REG(locty, TIS_REG_STS), TIS_STS_COMMAND_READY);
168 rc = tis_wait_sts(locty, timeout_b,
169 TIS_STS_COMMAND_READY, TIS_STS_COMMAND_READY);
174 static u32 tis_senddata(const u8 *const data, u32 len)
184 u8 locty = tis_find_active_locality();
185 u32 timeout_d = tpm_drivers[TIS_DRIVER_IDX].timeouts[TIS_TIMEOUT_TYPE_D];
188 while (burst == 0 && ctr < timeout_d) {
189 burst = readl(TIS_REG(locty, TIS_REG_STS)) >> 8;
197 rc = TCG_RESPONSE_TIMEOUT;
202 writeb(TIS_REG(locty, TIS_REG_DATA_FIFO), data[offset++]);
205 if (burst == 0 || offset == len)
216 static u32 tis_readresp(u8 *buffer, u32 *len)
224 u8 locty = tis_find_active_locality();
226 while (offset < *len) {
227 buffer[offset] = readb(TIS_REG(locty, TIS_REG_DATA_FIFO));
229 sts = readb(TIS_REG(locty, TIS_REG_STS));
231 if ((sts & TIS_STS_DATA_AVAILABLE) == 0)
241 static u32 tis_waitdatavalid(void)
247 u8 locty = tis_find_active_locality();
248 u32 timeout_c = tpm_drivers[TIS_DRIVER_IDX].timeouts[TIS_TIMEOUT_TYPE_C];
250 if (tis_wait_sts(locty, timeout_c, TIS_STS_VALID, TIS_STS_VALID) != 0)
251 rc = TCG_NO_RESPONSE;
256 static u32 tis_waitrespready(enum tpmDurationType to_t)
262 u8 locty = tis_find_active_locality();
263 u32 timeout = tpm_drivers[TIS_DRIVER_IDX].durations[to_t];
265 writeb(TIS_REG(locty ,TIS_REG_STS), TIS_STS_TPM_GO);
267 if (tis_wait_sts(locty, timeout,
268 TIS_STS_DATA_AVAILABLE, TIS_STS_DATA_AVAILABLE) != 0)
269 rc = TCG_NO_RESPONSE;
275 struct tpm_driver tpm_drivers[TPM_NUM_DRIVERS] = {
280 .set_timeouts = set_timeouts,
283 .activate = tis_activate,
285 .senddata = tis_senddata,
286 .readresp = tis_readresp,
287 .waitdatavalid = tis_waitdatavalid,
288 .waitrespready = tis_waitrespready,
289 .sha1threshold = 100 * 1024,