tpm_atmel_twi.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2013 Guntermann & Drunck, GmbH
  3. *
  4. * Written by Dirk Eibach <eibach@gdsys.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <tpm.h>
  11. #include <i2c.h>
  12. #include <asm/unaligned.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. res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
  76. if (res) {
  77. printf("i2c_write returned %d\n", res);
  78. return -1;
  79. }
  80. start = get_timer(0);
  81. while ((res = i2c_read(0x29, 0, 0, recvbuf, 10))) {
  82. /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
  83. if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
  84. puts("tpm timed out\n");
  85. return -1;
  86. }
  87. udelay(100);
  88. }
  89. if (!res) {
  90. *recv_len = get_unaligned_be32(recvbuf + 2);
  91. if (*recv_len > 10)
  92. res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
  93. }
  94. if (res) {
  95. printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
  96. #ifdef DEBUG
  97. print_buffer(0, recvbuf, 1, *recv_len, 0);
  98. #endif
  99. }
  100. #ifdef DEBUG
  101. if (!res) {
  102. printf("read from TPM (%d bytes):\n", *recv_len);
  103. print_buffer(0, recvbuf, 1, *recv_len, 0);
  104. }
  105. #endif
  106. return res;
  107. }
  108. static int tpm_atmel_twi_probe(struct udevice *dev)
  109. {
  110. return 0;
  111. }
  112. static const struct udevice_id tpm_atmel_twi_ids[] = {
  113. { .compatible = "atmel,at97sc3204t"},
  114. { }
  115. };
  116. static const struct tpm_ops tpm_atmel_twi_ops = {
  117. .open = tpm_atmel_twi_open,
  118. .close = tpm_atmel_twi_close,
  119. .xfer = tpm_atmel_twi_xfer,
  120. .get_desc = tpm_atmel_twi_get_desc,
  121. };
  122. U_BOOT_DRIVER(tpm_atmel_twi) = {
  123. .name = "tpm_atmel_twi",
  124. .id = UCLASS_TPM,
  125. .of_match = tpm_atmel_twi_ids,
  126. .ops = &tpm_atmel_twi_ops,
  127. .probe = tpm_atmel_twi_probe,
  128. };