Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | f255d31 | 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 | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 10 | #include <linux/unaligned/be_byteshift.h> |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 11 | #include <tpm-v1.h> |
Miquel Raynal | ff32245 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 12 | #include <tpm-v2.h> |
Simon Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 13 | #include "tpm_internal.h" |
| 14 | |
| 15 | int 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 | |
| 25 | int 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 | |
| 35 | int 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 */ |
| 46 | static 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 | |
| 69 | int 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 Glass | 8830765 | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 76 | int ret, ret2 = 0; |
Simon Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 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 | |
Simon Glass | 8830765 | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 124 | 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 Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 132 | |
Simon Glass | 8830765 | 2020-04-08 16:57:25 -0600 | [diff] [blame] | 133 | return 0; |
Simon Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | UCLASS_DRIVER(tpm) = { |
Simon Glass | 6322a7b | 2018-10-01 12:22:22 -0600 | [diff] [blame] | 137 | .id = UCLASS_TPM, |
| 138 | .name = "tpm", |
| 139 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | c89d32a | 2018-10-01 12:22:27 -0600 | [diff] [blame] | 140 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 141 | .post_bind = dm_scan_fdt_dev, |
| 142 | #endif |
Simon Glass | f255d31 | 2015-08-22 18:31:31 -0600 | [diff] [blame] | 143 | .per_device_auto_alloc_size = sizeof(struct tpm_chip_priv), |
| 144 | }; |