blob: 19c0502d8738bde47d58f6f458fd58175b8bab57 [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 Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glassf255d312015-08-22 18:31:31 -060010#include <linux/unaligned/be_byteshift.h>
Miquel Raynald677bfe2018-05-15 11:57:06 +020011#include <tpm-v1.h>
Miquel Raynalff322452018-05-15 11:57:08 +020012#include <tpm-v2.h>
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;
Simon Glass88307652020-04-08 16:57:25 -060076 int ret, ret2 = 0;
Simon Glassf255d312015-08-22 18:31:31 -060077
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
Simon Glass88307652020-04-08 16:57:25 -0600124 if (ret) {
125 if (ops->cleanup) {
126 ret2 = ops->cleanup(dev);
127 if (ret2)
128 return log_msg_ret("cleanup", ret2);
129 }
130 return log_msg_ret("xfer", ret);
131 }
Simon Glassf255d312015-08-22 18:31:31 -0600132
Simon Glass88307652020-04-08 16:57:25 -0600133 return 0;
Simon Glassf255d312015-08-22 18:31:31 -0600134}
135
136UCLASS_DRIVER(tpm) = {
Simon Glass6322a7b2018-10-01 12:22:22 -0600137 .id = UCLASS_TPM,
138 .name = "tpm",
139 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassc89d32a2018-10-01 12:22:27 -0600140#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
141 .post_bind = dm_scan_fdt_dev,
142#endif
Simon Glassf255d312015-08-22 18:31:31 -0600143 .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv),
144};