tpm-uclass.c 2.9 KB

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