tpm-uclass.c 3.1 KB

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