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 <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 12 | #include <linux/delay.h> |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 13 | #include <linux/unaligned/be_byteshift.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 14 | #include <tpm-v1.h> |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 15 | #include <tpm-v2.h> |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 16 | #include "tpm_internal.h" |
| 17 | |
| 18 | int tpm_open(struct udevice *dev) |
| 19 | { |
| 20 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 21 | |
| 22 | if (!ops->open) |
| 23 | return -ENOSYS; |
| 24 | |
| 25 | return ops->open(dev); |
| 26 | } |
| 27 | |
| 28 | int tpm_close(struct udevice *dev) |
| 29 | { |
| 30 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 31 | |
| 32 | if (!ops->close) |
| 33 | return -ENOSYS; |
| 34 | |
| 35 | return ops->close(dev); |
| 36 | } |
| 37 | |
| 38 | int tpm_get_desc(struct udevice *dev, char *buf, int size) |
| 39 | { |
| 40 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 41 | |
| 42 | if (!ops->get_desc) |
| 43 | return -ENOSYS; |
| 44 | |
| 45 | return ops->get_desc(dev, buf, size); |
| 46 | } |
| 47 | |
| 48 | /* Returns max number of milliseconds to wait */ |
| 49 | static ulong tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip_priv *priv, |
| 50 | u32 ordinal) |
| 51 | { |
| 52 | int duration_idx = TPM_UNDEFINED; |
| 53 | int duration = 0; |
| 54 | |
| 55 | if (ordinal < TPM_MAX_ORDINAL) { |
| 56 | duration_idx = tpm_ordinal_duration[ordinal]; |
| 57 | } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < |
| 58 | TPM_MAX_PROTECTED_ORDINAL) { |
| 59 | duration_idx = tpm_protected_ordinal_duration[ |
| 60 | ordinal & TPM_PROTECTED_ORDINAL_MASK]; |
| 61 | } |
| 62 | |
| 63 | if (duration_idx != TPM_UNDEFINED) |
| 64 | duration = priv->duration_ms[duration_idx]; |
| 65 | |
| 66 | if (duration <= 0) |
| 67 | return 2 * 60 * 1000; /* Two minutes timeout */ |
| 68 | else |
| 69 | return duration; |
| 70 | } |
| 71 | |
| 72 | int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size, |
| 73 | uint8_t *recvbuf, size_t *recv_size) |
| 74 | { |
| 75 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
| 76 | struct tpm_ops *ops = tpm_get_ops(dev); |
| 77 | ulong start, stop; |
| 78 | uint count, ordinal; |
Simon Glass | bfb409b | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 79 | int ret, ret2 = 0; |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 80 | |
| 81 | if (ops->xfer) |
| 82 | return ops->xfer(dev, sendbuf, send_size, recvbuf, recv_size); |
| 83 | |
| 84 | if (!ops->send || !ops->recv) |
| 85 | return -ENOSYS; |
| 86 | |
| 87 | /* switch endianess: big->little */ |
| 88 | count = get_unaligned_be32(sendbuf + TPM_CMD_COUNT_BYTE); |
| 89 | ordinal = get_unaligned_be32(sendbuf + TPM_CMD_ORDINAL_BYTE); |
| 90 | |
| 91 | if (count == 0) { |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame^] | 92 | log_debug("no data\n"); |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 93 | return -ENODATA; |
| 94 | } |
| 95 | if (count > send_size) { |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame^] | 96 | log_debug("invalid count value %x %zx\n", count, send_size); |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 97 | return -E2BIG; |
| 98 | } |
| 99 | |
Simon Glass | 2797ebf | 2021-02-06 14:23:33 -0700 | [diff] [blame^] | 100 | log_debug("%s: Calling send\n", __func__); |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 101 | ret = ops->send(dev, sendbuf, send_size); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | |
| 105 | start = get_timer(0); |
| 106 | stop = tpm_tis_i2c_calc_ordinal_duration(priv, ordinal); |
| 107 | do { |
| 108 | ret = ops->recv(dev, priv->buf, sizeof(priv->buf)); |
| 109 | if (ret >= 0) { |
| 110 | if (ret > *recv_size) |
| 111 | return -ENOSPC; |
| 112 | memcpy(recvbuf, priv->buf, ret); |
| 113 | *recv_size = ret; |
| 114 | ret = 0; |
| 115 | break; |
| 116 | } else if (ret != -EAGAIN) { |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | mdelay(priv->retry_time_ms); |
| 121 | if (get_timer(start) > stop) { |
| 122 | ret = -ETIMEDOUT; |
| 123 | break; |
| 124 | } |
| 125 | } while (ret); |
| 126 | |
Simon Glass | bfb409b | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 127 | if (ret) { |
| 128 | if (ops->cleanup) { |
| 129 | ret2 = ops->cleanup(dev); |
| 130 | if (ret2) |
| 131 | return log_msg_ret("cleanup", ret2); |
| 132 | } |
| 133 | return log_msg_ret("xfer", ret); |
| 134 | } |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 135 | |
Simon Glass | bfb409b | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 136 | return 0; |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | UCLASS_DRIVER(tpm) = { |
Simon Glass | bed4352 | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 140 | .id = UCLASS_TPM, |
| 141 | .name = "tpm", |
| 142 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 1a82542 | 2018-10-01 12:22:27 -0600 | [diff] [blame] | 143 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 144 | .post_bind = dm_scan_fdt_dev, |
| 145 | #endif |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 146 | .per_device_auto = sizeof(struct tpm_chip_priv), |
Simon Glass | 6c7a232 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 147 | }; |