tis_i2c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. */
  6. #include <config.h>
  7. #include <common.h>
  8. #include <fdtdec.h>
  9. #include <i2c.h>
  10. #include "slb9635_i2c/tpm.h"
  11. DECLARE_GLOBAL_DATA_PTR;
  12. /* TPM configuration */
  13. struct tpm {
  14. int i2c_bus;
  15. int slave_addr;
  16. char inited;
  17. int old_bus;
  18. } tpm;
  19. static int tpm_select(void)
  20. {
  21. int ret;
  22. tpm.old_bus = i2c_get_bus_num();
  23. if (tpm.old_bus != tpm.i2c_bus) {
  24. ret = i2c_set_bus_num(tpm.i2c_bus);
  25. if (ret) {
  26. debug("%s: Fail to set i2c bus %d\n", __func__,
  27. tpm.i2c_bus);
  28. return -1;
  29. }
  30. }
  31. return 0;
  32. }
  33. static int tpm_deselect(void)
  34. {
  35. int ret;
  36. if (tpm.old_bus != i2c_get_bus_num()) {
  37. ret = i2c_set_bus_num(tpm.old_bus);
  38. if (ret) {
  39. debug("%s: Fail to restore i2c bus %d\n",
  40. __func__, tpm.old_bus);
  41. return -1;
  42. }
  43. }
  44. tpm.old_bus = -1;
  45. return 0;
  46. }
  47. /**
  48. * Decode TPM configuration.
  49. *
  50. * @param dev Returns a configuration of TPM device
  51. * @return 0 if ok, -1 on error
  52. */
  53. static int tpm_decode_config(struct tpm *dev)
  54. {
  55. #ifdef CONFIG_OF_CONTROL
  56. const void *blob = gd->fdt_blob;
  57. int node, parent;
  58. int i2c_bus;
  59. node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
  60. if (node < 0) {
  61. node = fdtdec_next_compatible(blob, 0,
  62. COMPAT_INFINEON_SLB9645_TPM);
  63. }
  64. if (node < 0) {
  65. debug("%s: Node not found\n", __func__);
  66. return -1;
  67. }
  68. parent = fdt_parent_offset(blob, node);
  69. if (parent < 0) {
  70. debug("%s: Cannot find node parent\n", __func__);
  71. return -1;
  72. }
  73. i2c_bus = i2c_get_bus_num_fdt(parent);
  74. if (i2c_bus < 0)
  75. return -1;
  76. dev->i2c_bus = i2c_bus;
  77. dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
  78. #else
  79. dev->i2c_bus = CONFIG_INFINEON_TPM_I2C_BUS;
  80. dev->slave_addr = CONFIG_INFINEON_TPM_I2C_ADDR;
  81. #endif
  82. return 0;
  83. }
  84. int tis_init(void)
  85. {
  86. if (tpm.inited)
  87. return 0;
  88. if (tpm_decode_config(&tpm))
  89. return -1;
  90. if (tpm_select())
  91. return -1;
  92. /*
  93. * Probe TPM twice; the first probing might fail because TPM is asleep,
  94. * and the probing can wake up TPM.
  95. */
  96. if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
  97. debug("%s: fail to probe i2c addr 0x%x\n", __func__,
  98. tpm.slave_addr);
  99. return -1;
  100. }
  101. tpm_deselect();
  102. tpm.inited = 1;
  103. return 0;
  104. }
  105. int tis_open(void)
  106. {
  107. int rc;
  108. if (!tpm.inited)
  109. return -1;
  110. if (tpm_select())
  111. return -1;
  112. rc = tpm_open(tpm.slave_addr);
  113. tpm_deselect();
  114. return rc;
  115. }
  116. int tis_close(void)
  117. {
  118. if (!tpm.inited)
  119. return -1;
  120. if (tpm_select())
  121. return -1;
  122. tpm_close();
  123. tpm_deselect();
  124. return 0;
  125. }
  126. int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
  127. uint8_t *recvbuf, size_t *rbuf_len)
  128. {
  129. int len;
  130. uint8_t buf[4096];
  131. if (!tpm.inited)
  132. return -1;
  133. if (sizeof(buf) < sbuf_size)
  134. return -1;
  135. memcpy(buf, sendbuf, sbuf_size);
  136. if (tpm_select())
  137. return -1;
  138. len = tpm_transmit(buf, sbuf_size);
  139. tpm_deselect();
  140. if (len < 10) {
  141. *rbuf_len = 0;
  142. return -1;
  143. }
  144. memcpy(recvbuf, buf, len);
  145. *rbuf_len = len;
  146. return 0;
  147. }