tpm_atmel_twi.c 3.7 KB

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