Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_TPM |
| 8 | |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <time.h> |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 12 | #include <tpm_api.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 13 | #include <tpm-v1.h> |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 14 | #include <tpm-v2.h> |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 15 | #include <dm/lists.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/unaligned/be_byteshift.h> |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 18 | #include "tpm_internal.h" |
| 19 | |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 20 | #define TPM_RNG_DRV_NAME "tpm-rng" |
| 21 | |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 22 | int tpm_open(struct udevice *dev) |
| 23 | { |
| 24 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 25 | |
| 26 | if (!ops->open) |
| 27 | return -ENOSYS; |
| 28 | |
| 29 | return ops->open(dev); |
| 30 | } |
| 31 | |
| 32 | int tpm_close(struct udevice *dev) |
| 33 | { |
| 34 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 35 | |
| 36 | if (!ops->close) |
| 37 | return -ENOSYS; |
| 38 | |
| 39 | return ops->close(dev); |
| 40 | } |
| 41 | |
| 42 | int tpm_get_desc(struct udevice *dev, char *buf, int size) |
| 43 | { |
| 44 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 45 | |
| 46 | if (!ops->get_desc) |
| 47 | return -ENOSYS; |
| 48 | |
| 49 | return ops->get_desc(dev, buf, size); |
| 50 | } |
| 51 | |
Simon Glass | 11a075c | 2022-08-30 21:05:36 -0600 | [diff] [blame] | 52 | int tpm_report_state(struct udevice *dev, char *buf, int size) |
| 53 | { |
| 54 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 55 | |
| 56 | if (!ops->report_state) |
| 57 | return -ENOSYS; |
| 58 | |
| 59 | return ops->report_state(dev, buf, size); |
| 60 | } |
| 61 | |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 62 | /* Returns max number of milliseconds to wait */ |
| 63 | static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv, |
| 64 | u32 ordinal) |
| 65 | { |
| 66 | int duration_idx = TPM_UNDEFINED; |
| 67 | int duration = 0; |
| 68 | |
| 69 | if (ordinal < TPM_MAX_ORDINAL) { |
| 70 | duration_idx = tpm_ordinal_duration[ordinal]; |
| 71 | } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < |
| 72 | TPM_MAX_PROTECTED_ORDINAL) { |
| 73 | duration_idx = tpm_protected_ordinal_duration[ |
| 74 | ordinal & TPM_PROTECTED_ORDINAL_MASK]; |
| 75 | } |
| 76 | |
| 77 | if (duration_idx != TPM_UNDEFINED) |
| 78 | duration = priv->duration_ms[duration_idx]; |
| 79 | |
| 80 | if (duration <= 0) |
| 81 | return 2 * 60 * 1000; /* Two minutes timeout */ |
| 82 | else |
| 83 | return duration; |
| 84 | } |
| 85 | |
| 86 | int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size, |
| 87 | uint8_t *recvbuf, size_t *recv_size) |
| 88 | { |
| 89 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
| 90 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 91 | ulong start, stop; |
| 92 | uint count, ordinal; |
Simon Glass | bfb409b | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 93 | int ret, ret2 = 0; |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 94 | |
| 95 | if (ops->xfer) |
| 96 | return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size); |
| 97 | |
| 98 | if (!ops->send || !ops->recv) |
| 99 | return -ENOSYS; |
| 100 | |
| 101 | /* switch endianess: big->little */ |
| 102 | count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE); |
| 103 | ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE); |
| 104 | |
| 105 | if (count == 0) { |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame] | 106 | log_debug("no data\n"); |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 107 | return -ENODATA; |
| 108 | } |
| 109 | if (count > send_size) { |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame] | 110 | log_debug("invalid count value %x %zx\n", count, send_size); |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 111 | return -E2BIG; |
| 112 | } |
| 113 | |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame] | 114 | log_debug("%s: Calling send\n", __func__); |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 115 | ret = ops->send(dev, sendbuf, send_size); |
| 116 | if (ret < 0) |
| 117 | return ret; |
| 118 | |
| 119 | start = get_timer(0); |
| 120 | stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal); |
| 121 | do { |
| 122 | ret = ops->recv(dev, priv->buf, sizeof(priv->buf)); |
| 123 | if (ret >= 0) { |
| 124 | if (ret > *recv_size) |
| 125 | return -ENOSPC; |
| 126 | memcpy(recvbuf, priv->buf, ret); |
| 127 | *recv_size = ret; |
| 128 | ret = 0; |
| 129 | break; |
| 130 | } else if (ret != -EAGAIN) { |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | mdelay(priv->retry_time_ms); |
| 135 | if (get_timer(start) > stop) { |
| 136 | ret = -ETIMEDOUT; |
| 137 | break; |
| 138 | } |
| 139 | } while (ret); |
| 140 | |
Simon Glass | bfb409b | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 141 | if (ret) { |
| 142 | if (ops->cleanup) { |
| 143 | ret2 = ops->cleanup(dev); |
| 144 | if (ret2) |
| 145 | return log_msg_ret("cleanup", ret2); |
| 146 | } |
| 147 | return log_msg_ret("xfer", ret); |
| 148 | } |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 149 | |
Simon Glass | bfb409b | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 150 | return 0; |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 151 | } |
| 152 | |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 153 | static int tpm_uclass_post_probe(struct udevice *dev) |
| 154 | { |
| 155 | int ret; |
| 156 | const char *drv = TPM_RNG_DRV_NAME; |
| 157 | struct udevice *child; |
| 158 | |
Simon Glass | 4863b6b | 2023-02-05 15:44:19 -0700 | [diff] [blame] | 159 | if (IS_ENABLED(CONFIG_TPM_RNG)) { |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 160 | ret = device_find_first_child_by_uclass(dev, UCLASS_RNG, |
| 161 | &child); |
| 162 | |
| 163 | if (ret != -ENODEV) { |
| 164 | log_debug("RNG child already added to the TPM device\n"); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | ret = device_bind_driver(dev, drv, TPM_RNG_DRV_NAME, &child); |
| 169 | if (ret) |
| 170 | return log_msg_ret("bind", ret); |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 176 | UCLASS_DRIVER(tpm) = { |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 177 | .id = UCLASS_TPM, |
| 178 | .name = "tpm", |
| 179 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 180 | #if CONFIG_IS_ENABLED(OF_REAL) |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 181 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 1a82542 | 2018-10-01 12:22:27 -0600 | [diff] [blame] | 182 | #endif |
Sughosh Ganu | 5b60b46 | 2022-07-22 21:32:05 +0530 | [diff] [blame] | 183 | .post_probe = tpm_uclass_post_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 184 | .per_device_auto = sizeof(struct tpm_chip_priv), |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 185 | }; |