blob: 9488b091a2e823cd53680dbd1573c9859b3dc072 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf255d312015-08-22 18:31:31 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassf255d312015-08-22 18:31:31 -06005 */
6
7#include <common.h>
8#include <dm.h>
Simon Glassf255d312015-08-22 18:31:31 -06009#include <linux/unaligned/be_byteshift.h>
Miquel Raynald677bfe2018-05-15 11:57:06 +020010#if defined(CONFIG_TPM_V1)
11#include <tpm-v1.h>
12#endif
Simon Glassf255d312015-08-22 18:31:31 -060013#include "tpm_internal.h"
14
15int tpm_open(struct udevice *dev)
16{
17 struct tpm_ops *ops = tpm_get_ops(dev);
18
19 if (!ops->open)
20 return -ENOSYS;
21
22 return ops->open(dev);
23}
24
25int tpm_close(struct udevice *dev)
26{
27 struct tpm_ops *ops = tpm_get_ops(dev);
28
29 if (!ops->close)
30 return -ENOSYS;
31
32 return ops->close(dev);
33}
34
35int tpm_get_desc(struct udevice *dev, char *buf, int size)
36{
37 struct tpm_ops *ops = tpm_get_ops(dev);
38
39 if (!ops->get_desc)
40 return -ENOSYS;
41
42 return ops->get_desc(dev, buf, size);
43}
44
45/* Returns max number of milliseconds to wait */
46static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv,
47 u32 ordinal)
48{
49 int duration_idx = TPM_UNDEFINED;
50 int duration = 0;
51
52 if (ordinal < TPM_MAX_ORDINAL) {
53 duration_idx = tpm_ordinal_duration[ordinal];
54 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
55 TPM_MAX_PROTECTED_ORDINAL) {
56 duration_idx = tpm_protected_ordinal_duration[
57 ordinal & TPM_PROTECTED_ORDINAL_MASK];
58 }
59
60 if (duration_idx != TPM_UNDEFINED)
61 duration = priv->duration_ms[duration_idx];
62
63 if (duration <= 0)
64 return 2 * 60 * 1000; /* Two minutes timeout */
65 else
66 return duration;
67}
68
69int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size,
70 uint8_t *recvbuf, size_t *recv_size)
71{
72 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
73 struct tpm_ops *ops = tpm_get_ops(dev);
74 ulong start, stop;
75 uint count, ordinal;
76 int ret, ret2;
77
78 if (ops->xfer)
79 return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size);
80
81 if (!ops->send || !ops->recv)
82 return -ENOSYS;
83
84 /* switch endianess: big->little */
85 count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE);
86 ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE);
87
88 if (count == 0) {
89 debug("no data\n");
90 return -ENODATA;
91 }
92 if (count > send_size) {
93 debug("invalid count value %x %zx\n", count, send_size);
94 return -E2BIG;
95 }
96
97 debug("%s: Calling send\n", __func__);
98 ret = ops->send(dev, sendbuf, send_size);
99 if (ret < 0)
100 return ret;
101
102 start = get_timer(0);
103 stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal);
104 do {
105 ret = ops->recv(dev, priv->buf, sizeof(priv->buf));
106 if (ret >= 0) {
107 if (ret > *recv_size)
108 return -ENOSPC;
109 memcpy(recvbuf, priv->buf, ret);
110 *recv_size = ret;
111 ret = 0;
112 break;
113 } else if (ret != -EAGAIN) {
114 return ret;
115 }
116
117 mdelay(priv->retry_time_ms);
118 if (get_timer(start) > stop) {
119 ret = -ETIMEDOUT;
120 break;
121 }
122 } while (ret);
123
124 ret2 = ops->cleanup ? ops->cleanup(dev) : 0;
125
126 return ret2 ? ret2 : ret;
127}
128
129UCLASS_DRIVER(tpm) = {
130 .id = UCLASS_TPM,
131 .name = "tpm",
132 .flags = DM_UC_FLAG_SEQ_ALIAS,
133 .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv),
134};