tpm_atmel_twi.c 3.7 KB

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