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