Add a new uclass for TPMs which uses almost the same TIS (TPM Interface Specification) as is currently implemented. Since init() is handled by the normal driver model probe() method, we don't need to implement that. Also rename the transfer method to xfer() which is a less clumbsy name. Once all drivers and users are converted to driver model we can remove the old code. Signed-off-by: Simon Glass <sjg@chromium.org> Acked-by: Christophe Ricard<christophe-h.ricard@st.com> Reviewed-by: Heiko Schocher <hs@denx.de>master
parent
42c8ec56c5
commit
f255d31f90
@ -0,0 +1,133 @@ |
||||
/*
|
||||
* Copyright (c) 2015 Google, Inc |
||||
* Written by Simon Glass <sjg@chromium.org> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <common.h> |
||||
#include <dm.h> |
||||
#include <tpm.h> |
||||
#include <linux/unaligned/be_byteshift.h> |
||||
#include "tpm_internal.h" |
||||
|
||||
int tpm_open(struct udevice *dev) |
||||
{ |
||||
struct tpm_ops *ops = tpm_get_ops(dev); |
||||
|
||||
if (!ops->open) |
||||
return -ENOSYS; |
||||
|
||||
return ops->open(dev); |
||||
} |
||||
|
||||
int tpm_close(struct udevice *dev) |
||||
{ |
||||
struct tpm_ops *ops = tpm_get_ops(dev); |
||||
|
||||
if (!ops->close) |
||||
return -ENOSYS; |
||||
|
||||
return ops->close(dev); |
||||
} |
||||
|
||||
int tpm_get_desc(struct udevice *dev, char *buf, int size) |
||||
{ |
||||
struct tpm_ops *ops = tpm_get_ops(dev); |
||||
|
||||
if (!ops->get_desc) |
||||
return -ENOSYS; |
||||
|
||||
return ops->get_desc(dev, buf, size); |
||||
} |
||||
|
||||
/* Returns max number of milliseconds to wait */ |
||||
static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv, |
||||
u32 ordinal) |
||||
{ |
||||
int duration_idx = TPM_UNDEFINED; |
||||
int duration = 0; |
||||
|
||||
if (ordinal < TPM_MAX_ORDINAL) { |
||||
duration_idx = tpm_ordinal_duration[ordinal]; |
||||
} else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < |
||||
TPM_MAX_PROTECTED_ORDINAL) { |
||||
duration_idx = tpm_protected_ordinal_duration[ |
||||
ordinal & TPM_PROTECTED_ORDINAL_MASK]; |
||||
} |
||||
|
||||
if (duration_idx != TPM_UNDEFINED) |
||||
duration = priv->duration_ms[duration_idx]; |
||||
|
||||
if (duration <= 0) |
||||
return 2 * 60 * 1000; /* Two minutes timeout */ |
||||
else |
||||
return duration; |
||||
} |
||||
|
||||
int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size, |
||||
uint8_t *recvbuf, size_t *recv_size) |
||||
{ |
||||
struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
||||
struct tpm_ops *ops = tpm_get_ops(dev); |
||||
ulong start, stop; |
||||
uint count, ordinal; |
||||
int ret, ret2; |
||||
|
||||
if (ops->xfer) |
||||
return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size); |
||||
|
||||
if (!ops->send || !ops->recv) |
||||
return -ENOSYS; |
||||
|
||||
/* switch endianess: big->little */ |
||||
count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE); |
||||
ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE); |
||||
|
||||
if (count == 0) { |
||||
debug("no data\n"); |
||||
return -ENODATA; |
||||
} |
||||
if (count > send_size) { |
||||
debug("invalid count value %x %zx\n", count, send_size); |
||||
return -E2BIG; |
||||
} |
||||
|
||||
debug("%s: Calling send\n", __func__); |
||||
ret = ops->send(dev, sendbuf, send_size); |
||||
if (ret < 0) |
||||
return ret; |
||||
|
||||
start = get_timer(0); |
||||
stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal); |
||||
do { |
||||
ret = ops->recv(dev, priv->buf, sizeof(priv->buf)); |
||||
if (ret >= 0) { |
||||
if (ret > *recv_size) |
||||
return -ENOSPC; |
||||
memcpy(recvbuf, priv->buf, ret); |
||||
*recv_size = ret; |
||||
ret = 0; |
||||
break; |
||||
} else if (ret != -EAGAIN) { |
||||
return ret; |
||||
} |
||||
|
||||
mdelay(priv->retry_time_ms); |
||||
if (get_timer(start) > stop) { |
||||
ret = -ETIMEDOUT; |
||||
break; |
||||
} |
||||
} while (ret); |
||||
|
||||
ret2 = ops->cleanup ? ops->cleanup(dev) : 0; |
||||
|
||||
return ret2 ? ret2 : ret; |
||||
} |
||||
|
||||
UCLASS_DRIVER(tpm) = { |
||||
.id = UCLASS_TPM, |
||||
.name = "tpm", |
||||
.flags = DM_UC_FLAG_SEQ_ALIAS, |
||||
.per_device_auto_alloc_size = sizeof(struct tpm_chip_priv), |
||||
}; |
@ -0,0 +1,287 @@ |
||||
/*
|
||||
* Copyright (c) 2015 Google, Inc |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#ifndef __tpm_internal_h |
||||
#define __tpm_internal_h |
||||
|
||||
enum { |
||||
TPM_MAX_ORDINAL = 243, |
||||
TPM_MAX_PROTECTED_ORDINAL = 12, |
||||
TPM_PROTECTED_ORDINAL_MASK = 0xff, |
||||
TPM_CMD_COUNT_BYTE = 2, |
||||
TPM_CMD_ORDINAL_BYTE = 6, |
||||
}; |
||||
|
||||
/*
|
||||
* Array with one entry per ordinal defining the maximum amount |
||||
* of time the chip could take to return the result. The ordinal |
||||
* designation of short, medium or long is defined in a table in |
||||
* TCG Specification TPM Main Part 2 TPM Structures Section 17. The |
||||
* values of the SHORT, MEDIUM, and LONG durations are retrieved |
||||
* from the chip during initialization with a call to tpm_get_timeouts. |
||||
*/ |
||||
static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = { |
||||
TPM_UNDEFINED, /* 0 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 5 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 10 */ |
||||
TPM_SHORT, |
||||
}; |
||||
|
||||
static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { |
||||
TPM_UNDEFINED, /* 0 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 5 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 10 */ |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, |
||||
TPM_LONG, |
||||
TPM_LONG, |
||||
TPM_MEDIUM, /* 15 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, |
||||
TPM_LONG, |
||||
TPM_SHORT, /* 20 */ |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_SHORT, /* 25 */ |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, /* 30 */ |
||||
TPM_LONG, |
||||
TPM_MEDIUM, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 35 */ |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, /* 40 */ |
||||
TPM_LONG, |
||||
TPM_MEDIUM, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 45 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_LONG, |
||||
TPM_MEDIUM, /* 50 */ |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 55 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, /* 60 */ |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, /* 65 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 70 */ |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 75 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_LONG, /* 80 */ |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, |
||||
TPM_LONG, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, /* 85 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 90 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, /* 95 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, /* 100 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 105 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 110 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 115 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_LONG, /* 120 */ |
||||
TPM_LONG, |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 125 */ |
||||
TPM_SHORT, |
||||
TPM_LONG, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 130 */ |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, /* 135 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 140 */ |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 145 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 150 */ |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, /* 155 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 160 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 165 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_LONG, /* 170 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 175 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, /* 180 */ |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, /* 185 */ |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 190 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 195 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 200 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 205 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_MEDIUM, /* 210 */ |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, /* 215 */ |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, |
||||
TPM_SHORT, /* 220 */ |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_SHORT, |
||||
TPM_UNDEFINED, /* 225 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 230 */ |
||||
TPM_LONG, |
||||
TPM_MEDIUM, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, /* 235 */ |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_UNDEFINED, |
||||
TPM_SHORT, /* 240 */ |
||||
TPM_UNDEFINED, |
||||
TPM_MEDIUM, |
||||
}; |
||||
|
||||
#endif |
Loading…
Reference in new issue