tis_i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. debug("%s: Node not found\n", __func__);
  62. return -1;
  63. }
  64. parent = fdt_parent_offset(blob, node);
  65. if (parent < 0) {
  66. debug("%s: Cannot find node parent\n", __func__);
  67. return -1;
  68. }
  69. i2c_bus = i2c_get_bus_num_fdt(parent);
  70. if (i2c_bus < 0)
  71. return -1;
  72. dev->i2c_bus = i2c_bus;
  73. dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
  74. #else
  75. dev->i2c_bus = CONFIG_INFINEON_TPM_I2C_BUS;
  76. dev->slave_addr = CONFIG_INFINEON_TPM_I2C_ADDR;
  77. #endif
  78. return 0;
  79. }
  80. int tis_init(void)
  81. {
  82. if (tpm.inited)
  83. return 0;
  84. if (tpm_decode_config(&tpm))
  85. return -1;
  86. if (tpm_select())
  87. return -1;
  88. /*
  89. * Probe TPM twice; the first probing might fail because TPM is asleep,
  90. * and the probing can wake up TPM.
  91. */
  92. if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
  93. debug("%s: fail to probe i2c addr 0x%x\n", __func__,
  94. tpm.slave_addr);
  95. return -1;
  96. }
  97. tpm_deselect();
  98. tpm.inited = 1;
  99. return 0;
  100. }
  101. int tis_open(void)
  102. {
  103. int rc;
  104. if (!tpm.inited)
  105. return -1;
  106. if (tpm_select())
  107. return -1;
  108. rc = tpm_open(tpm.slave_addr);
  109. tpm_deselect();
  110. return rc;
  111. }
  112. int tis_close(void)
  113. {
  114. if (!tpm.inited)
  115. return -1;
  116. if (tpm_select())
  117. return -1;
  118. tpm_close();
  119. tpm_deselect();
  120. return 0;
  121. }
  122. int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
  123. uint8_t *recvbuf, size_t *rbuf_len)
  124. {
  125. int len;
  126. uint8_t buf[4096];
  127. if (!tpm.inited)
  128. return -1;
  129. if (sizeof(buf) < sbuf_size)
  130. return -1;
  131. memcpy(buf, sendbuf, sbuf_size);
  132. if (tpm_select())
  133. return -1;
  134. len = tpm_transmit(buf, sbuf_size);
  135. tpm_deselect();
  136. if (len < 10) {
  137. *rbuf_len = 0;
  138. return -1;
  139. }
  140. memcpy(recvbuf, buf, len);
  141. *rbuf_len = len;
  142. return 0;
  143. }