tpm_atmel_twi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013 Guntermann & Drunck, GmbH
  4. *
  5. * Written by Dirk Eibach <dirk.eibach@gdsys.cc>
  6. */
  7. #include <common.h>
  8. #include <display_options.h>
  9. #include <dm.h>
  10. #include <tpm-v1.h>
  11. #include <i2c.h>
  12. #include <asm/unaligned.h>
  13. #include <linux/delay.h>
  14. #include "tpm_internal.h"
  15. #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
  16. generating/exporting keys */
  17. /*
  18. * tpm_atmel_twi_open()
  19. *
  20. * Requests access to locality 0 for the caller. After all commands have been
  21. * completed the caller is supposed to call tis_close().
  22. *
  23. * Returns 0 on success, -1 on failure.
  24. */
  25. static int tpm_atmel_twi_open(struct udevice *dev)
  26. {
  27. return 0;
  28. }
  29. /*
  30. * tpm_atmel_twi_close()
  31. *
  32. * terminate the currect session with the TPM by releasing the locked
  33. * locality. Returns 0 on success of -1 on failure (in case lock
  34. * removal did not succeed).
  35. */
  36. static int tpm_atmel_twi_close(struct udevice *dev)
  37. {
  38. return 0;
  39. }
  40. /*
  41. * tpm_atmel_twi_get_desc()
  42. *
  43. * @dev: Device to check
  44. * @buf: Buffer to put the string
  45. * @size: Maximum size of buffer
  46. * Return: length of string, or -ENOSPC it no space
  47. */
  48. static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
  49. {
  50. if (size < 50)
  51. return -ENOSPC;
  52. return snprintf(buf, size, "Atmel AT97SC3204T I2C 1.2 TPM (%s)", dev->name);
  53. }
  54. /*
  55. * tpm_atmel_twi_xfer()
  56. *
  57. * Send the requested data to the TPM and then try to get its response
  58. *
  59. * @sendbuf - buffer of the data to send
  60. * @send_size size of the data to send
  61. * @recvbuf - memory to save the response to
  62. * @recv_len - pointer to the size of the response buffer
  63. *
  64. * Returns 0 on success (and places the number of response bytes at recv_len)
  65. * or -1 on failure.
  66. */
  67. static int tpm_atmel_twi_xfer(struct udevice *dev,
  68. const uint8_t *sendbuf, size_t send_size,
  69. uint8_t *recvbuf, size_t *recv_len)
  70. {
  71. int res;
  72. unsigned long start;
  73. #ifdef DEBUG
  74. memset(recvbuf, 0xcc, *recv_len);
  75. printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len);
  76. print_buffer(0, (void *)sendbuf, 1, send_size, 0);
  77. #endif
  78. res = dm_i2c_write(dev, 0, sendbuf, send_size);
  79. if (res) {
  80. printf("i2c_write returned %d\n", res);
  81. return -1;
  82. }
  83. start = get_timer(0);
  84. while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
  85. {
  86. /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
  87. if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
  88. puts("tpm timed out\n");
  89. return -1;
  90. }
  91. udelay(100);
  92. }
  93. if (!res) {
  94. unsigned int hdr_recv_len;
  95. hdr_recv_len = get_unaligned_be32(recvbuf + 2);
  96. if (hdr_recv_len < 10) {
  97. puts("tpm response header too small\n");
  98. return -1;
  99. } else if (hdr_recv_len > *recv_len) {
  100. puts("tpm response length is bigger than receive buffer\n");
  101. return -1;
  102. } else {
  103. *recv_len = hdr_recv_len;
  104. res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
  105. }
  106. }
  107. if (res) {
  108. printf("i2c_read returned %d (rlen=%zu)\n", res, *recv_len);
  109. #ifdef DEBUG
  110. print_buffer(0, recvbuf, 1, *recv_len, 0);
  111. #endif
  112. }
  113. #ifdef DEBUG
  114. if (!res) {
  115. printf("read from TPM (%d bytes):\n", *recv_len);
  116. print_buffer(0, recvbuf, 1, *recv_len, 0);
  117. }
  118. #endif
  119. return res;
  120. }
  121. static int tpm_atmel_twi_probe(struct udevice *dev)
  122. {
  123. i2c_set_chip_offset_len(dev, 0);
  124. return 0;
  125. }
  126. static const struct udevice_id tpm_atmel_twi_ids[] = {
  127. { .compatible = "atmel,at97sc3204t"},
  128. { }
  129. };
  130. static const struct tpm_ops tpm_atmel_twi_ops = {
  131. .open = tpm_atmel_twi_open,
  132. .close = tpm_atmel_twi_close,
  133. .xfer = tpm_atmel_twi_xfer,
  134. .get_desc = tpm_atmel_twi_get_desc,
  135. };
  136. U_BOOT_DRIVER(tpm_atmel_twi) = {
  137. .name = "tpm_atmel_twi",
  138. .id = UCLASS_TPM,
  139. .of_match = tpm_atmel_twi_ids,
  140. .ops = &tpm_atmel_twi_ops,
  141. .probe = tpm_atmel_twi_probe,
  142. };