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