tpm_tis_i2c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Copyright (C) 2011 Infineon Technologies
  3. *
  4. * Authors:
  5. * Peter Huewe <huewe.external@infineon.com>
  6. *
  7. * Description:
  8. * Device driver for TCG/TCPA TPM (trusted platform module).
  9. * Specifications at www.trustedcomputinggroup.org
  10. *
  11. * This device driver implements the TPM interface as defined in
  12. * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
  13. * Infineon I2C Protocol Stack Specification v0.20.
  14. *
  15. * It is based on the Linux kernel driver tpm.c from Leendert van
  16. * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
  17. *
  18. * Version: 2.1.1
  19. *
  20. * SPDX-License-Identifier: GPL-2.0
  21. */
  22. #include <common.h>
  23. #include <dm.h>
  24. #include <fdtdec.h>
  25. #include <linux/compiler.h>
  26. #include <i2c.h>
  27. #include <tpm.h>
  28. #include <asm-generic/errno.h>
  29. #include <linux/types.h>
  30. #include <linux/unaligned/be_byteshift.h>
  31. #include "tpm_tis_i2c.h"
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static const char * const chip_name[] = {
  34. [SLB9635] = "slb9635tt",
  35. [SLB9645] = "slb9645tt",
  36. [UNKNOWN] = "unknown/fallback to slb9635",
  37. };
  38. static struct tpm_chip g_chip;
  39. /*
  40. * tpm_tis_i2c_read() - read from TPM register
  41. * @addr: register address to read from
  42. * @buffer: provided by caller
  43. * @len: number of bytes to read
  44. *
  45. * Read len bytes from TPM register and put them into
  46. * buffer (little-endian format, i.e. first byte is put into buffer[0]).
  47. *
  48. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  49. * values have to be swapped.
  50. *
  51. * Return -EIO on error, 0 on success.
  52. */
  53. static int tpm_tis_i2c_read(u8 addr, u8 *buffer, size_t len)
  54. {
  55. int rc;
  56. int count;
  57. uint32_t addrbuf = addr;
  58. if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
  59. /* slb9635 protocol should work in both cases */
  60. for (count = 0; count < MAX_COUNT; count++) {
  61. rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
  62. if (rc == 0)
  63. break; /* Success, break to skip sleep */
  64. udelay(SLEEP_DURATION_US);
  65. }
  66. if (rc)
  67. return -rc;
  68. /* After the TPM has successfully received the register address
  69. * it needs some time, thus we're sleeping here again, before
  70. * retrieving the data
  71. */
  72. for (count = 0; count < MAX_COUNT; count++) {
  73. udelay(SLEEP_DURATION_US);
  74. rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
  75. if (rc == 0)
  76. break; /* success, break to skip sleep */
  77. }
  78. } else {
  79. /*
  80. * Use a combined read for newer chips.
  81. * Unfortunately the smbus functions are not suitable due to
  82. * the 32 byte limit of the smbus.
  83. * Retries should usually not be needed, but are kept just to
  84. * be safe on the safe side.
  85. */
  86. for (count = 0; count < MAX_COUNT; count++) {
  87. rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
  88. if (rc == 0)
  89. break; /* break here to skip sleep */
  90. udelay(SLEEP_DURATION_US);
  91. }
  92. }
  93. /* Take care of 'guard time' */
  94. udelay(SLEEP_DURATION_US);
  95. if (rc)
  96. return -rc;
  97. return 0;
  98. }
  99. static int tpm_tis_i2c_write_generic(u8 addr, u8 *buffer, size_t len,
  100. unsigned int sleep_time_us, u8 max_count)
  101. {
  102. int rc = 0;
  103. int count;
  104. for (count = 0; count < max_count; count++) {
  105. rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
  106. if (rc == 0)
  107. break; /* Success, break to skip sleep */
  108. udelay(sleep_time_us);
  109. }
  110. /* take care of 'guard time' */
  111. udelay(sleep_time_us);
  112. if (rc)
  113. return -rc;
  114. return 0;
  115. }
  116. /*
  117. * tpm_tis_i2c_write() - write to TPM register
  118. * @addr: register address to write to
  119. * @buffer: containing data to be written
  120. * @len: number of bytes to write
  121. *
  122. * Write len bytes from provided buffer to TPM register (little
  123. * endian format, i.e. buffer[0] is written as first byte).
  124. *
  125. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  126. * values have to be swapped.
  127. *
  128. * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
  129. *
  130. * Return -EIO on error, 0 on success
  131. */
  132. static int tpm_tis_i2c_write(u8 addr, u8 *buffer, size_t len)
  133. {
  134. return tpm_tis_i2c_write_generic(addr, buffer, len, SLEEP_DURATION_US,
  135. MAX_COUNT);
  136. }
  137. /*
  138. * This function is needed especially for the cleanup situation after
  139. * sending TPM_READY
  140. */
  141. static int tpm_tis_i2c_write_long(u8 addr, u8 *buffer, size_t len)
  142. {
  143. return tpm_tis_i2c_write_generic(addr, buffer, len,
  144. SLEEP_DURATION_LONG_US,
  145. MAX_COUNT_LONG);
  146. }
  147. static int tpm_tis_i2c_check_locality(struct tpm_chip *chip, int loc)
  148. {
  149. const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
  150. u8 buf;
  151. int rc;
  152. rc = tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1);
  153. if (rc < 0)
  154. return rc;
  155. if ((buf & mask) == mask) {
  156. chip->locality = loc;
  157. return loc;
  158. }
  159. return -1;
  160. }
  161. static void tpm_tis_i2c_release_locality(struct tpm_chip *chip, int loc,
  162. int force)
  163. {
  164. const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
  165. u8 buf;
  166. if (tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1) < 0)
  167. return;
  168. if (force || (buf & mask) == mask) {
  169. buf = TPM_ACCESS_ACTIVE_LOCALITY;
  170. tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
  171. }
  172. }
  173. static int tpm_tis_i2c_request_locality(struct tpm_chip *chip, int loc)
  174. {
  175. unsigned long start, stop;
  176. u8 buf = TPM_ACCESS_REQUEST_USE;
  177. int rc;
  178. if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
  179. return loc; /* We already have the locality */
  180. rc = tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
  181. if (rc)
  182. return rc;
  183. /* Wait for burstcount */
  184. start = get_timer(0);
  185. stop = chip->timeout_a;
  186. do {
  187. if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
  188. return loc;
  189. mdelay(TPM_TIMEOUT_MS);
  190. } while (get_timer(start) < stop);
  191. return -1;
  192. }
  193. static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
  194. {
  195. /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
  196. u8 buf;
  197. if (tpm_tis_i2c_read(TPM_STS(chip->locality), &buf, 1) < 0)
  198. return 0;
  199. else
  200. return buf;
  201. }
  202. static void tpm_tis_i2c_ready(struct tpm_chip *chip)
  203. {
  204. int rc;
  205. /* This causes the current command to be aborted */
  206. u8 buf = TPM_STS_COMMAND_READY;
  207. debug("%s\n", __func__);
  208. rc = tpm_tis_i2c_write_long(TPM_STS(chip->locality), &buf, 1);
  209. if (rc)
  210. debug("%s: rc=%d\n", __func__, rc);
  211. }
  212. static ssize_t tpm_tis_i2c_get_burstcount(struct tpm_chip *chip)
  213. {
  214. unsigned long start, stop;
  215. ssize_t burstcnt;
  216. u8 addr, buf[3];
  217. /* Wait for burstcount */
  218. /* XXX: Which timeout value? Spec has 2 answers (c & d) */
  219. start = get_timer(0);
  220. stop = chip->timeout_d;
  221. do {
  222. /* Note: STS is little endian */
  223. addr = TPM_STS(chip->locality) + 1;
  224. if (tpm_tis_i2c_read(addr, buf, 3) < 0)
  225. burstcnt = 0;
  226. else
  227. burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
  228. if (burstcnt)
  229. return burstcnt;
  230. mdelay(TPM_TIMEOUT_MS);
  231. } while (get_timer(start) < stop);
  232. return -EBUSY;
  233. }
  234. static int tpm_tis_i2c_wait_for_stat(struct tpm_chip *chip, u8 mask,
  235. unsigned long timeout, int *status)
  236. {
  237. unsigned long start, stop;
  238. /* Check current status */
  239. *status = tpm_tis_i2c_status(chip);
  240. if ((*status & mask) == mask)
  241. return 0;
  242. start = get_timer(0);
  243. stop = timeout;
  244. do {
  245. mdelay(TPM_TIMEOUT_MS);
  246. *status = tpm_tis_i2c_status(chip);
  247. if ((*status & mask) == mask)
  248. return 0;
  249. } while (get_timer(start) < stop);
  250. return -ETIME;
  251. }
  252. static int tpm_tis_i2c_recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  253. {
  254. size_t size = 0;
  255. ssize_t burstcnt;
  256. int rc;
  257. while (size < count) {
  258. burstcnt = tpm_tis_i2c_get_burstcount(chip);
  259. /* burstcount < 0 -> tpm is busy */
  260. if (burstcnt < 0)
  261. return burstcnt;
  262. /* Limit received data to max left */
  263. if (burstcnt > (count - size))
  264. burstcnt = count - size;
  265. rc = tpm_tis_i2c_read(TPM_DATA_FIFO(chip->locality),
  266. &(buf[size]), burstcnt);
  267. if (rc == 0)
  268. size += burstcnt;
  269. }
  270. return size;
  271. }
  272. static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  273. {
  274. int size = 0;
  275. int expected, status;
  276. if (count < TPM_HEADER_SIZE) {
  277. size = -EIO;
  278. goto out;
  279. }
  280. /* Read first 10 bytes, including tag, paramsize, and result */
  281. size = tpm_tis_i2c_recv_data(chip, buf, TPM_HEADER_SIZE);
  282. if (size < TPM_HEADER_SIZE) {
  283. error("Unable to read header\n");
  284. goto out;
  285. }
  286. expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
  287. if ((size_t)expected > count) {
  288. error("Error size=%x, expected=%x, count=%x\n", size, expected,
  289. count);
  290. size = -EIO;
  291. goto out;
  292. }
  293. size += tpm_tis_i2c_recv_data(chip, &buf[TPM_HEADER_SIZE],
  294. expected - TPM_HEADER_SIZE);
  295. if (size < expected) {
  296. error("Unable to read remainder of result\n");
  297. size = -ETIME;
  298. goto out;
  299. }
  300. tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c,
  301. &status);
  302. if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
  303. error("Error left over data\n");
  304. size = -EIO;
  305. goto out;
  306. }
  307. out:
  308. tpm_tis_i2c_ready(chip);
  309. /*
  310. * The TPM needs some time to clean up here,
  311. * so we sleep rather than keeping the bus busy
  312. */
  313. mdelay(2);
  314. tpm_tis_i2c_release_locality(chip, chip->locality, 0);
  315. return size;
  316. }
  317. static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
  318. {
  319. int rc, status;
  320. size_t burstcnt;
  321. size_t count = 0;
  322. int retry = 0;
  323. u8 sts = TPM_STS_GO;
  324. debug("%s: len=%d\n", __func__, len);
  325. if (len > TPM_DEV_BUFSIZE)
  326. return -E2BIG; /* Command is too long for our tpm, sorry */
  327. if (tpm_tis_i2c_request_locality(chip, 0) < 0)
  328. return -EBUSY;
  329. status = tpm_tis_i2c_status(chip);
  330. if ((status & TPM_STS_COMMAND_READY) == 0) {
  331. tpm_tis_i2c_ready(chip);
  332. if (tpm_tis_i2c_wait_for_stat(chip, TPM_STS_COMMAND_READY,
  333. chip->timeout_b, &status) < 0) {
  334. rc = -ETIME;
  335. goto out_err;
  336. }
  337. }
  338. burstcnt = tpm_tis_i2c_get_burstcount(chip);
  339. /* burstcount < 0 -> tpm is busy */
  340. if (burstcnt < 0)
  341. return burstcnt;
  342. while (count < len) {
  343. udelay(300);
  344. if (burstcnt > len - count)
  345. burstcnt = len - count;
  346. #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
  347. if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
  348. burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
  349. #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
  350. rc = tpm_tis_i2c_write(TPM_DATA_FIFO(chip->locality),
  351. &(buf[count]), burstcnt);
  352. if (rc == 0)
  353. count += burstcnt;
  354. else {
  355. debug("%s: error\n", __func__);
  356. if (retry++ > 10) {
  357. rc = -EIO;
  358. goto out_err;
  359. }
  360. rc = tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID,
  361. chip->timeout_c,
  362. &status);
  363. if (rc)
  364. goto out_err;
  365. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  366. rc = -EIO;
  367. goto out_err;
  368. }
  369. }
  370. }
  371. /* Go and do it */
  372. tpm_tis_i2c_write(TPM_STS(chip->locality), &sts, 1);
  373. debug("done\n");
  374. return len;
  375. out_err:
  376. debug("%s: out_err\n", __func__);
  377. tpm_tis_i2c_ready(chip);
  378. /*
  379. * The TPM needs some time to clean up here,
  380. * so we sleep rather than keeping the bus busy
  381. */
  382. mdelay(2);
  383. tpm_tis_i2c_release_locality(chip, chip->locality, 0);
  384. return rc;
  385. }
  386. static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
  387. {
  388. #if CONFIG_IS_ENABLED(OF_CONTROL)
  389. const void *blob = gd->fdt_blob;
  390. if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
  391. return SLB9645;
  392. if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
  393. return SLB9635;
  394. #endif
  395. return UNKNOWN;
  396. }
  397. static int tpm_tis_i2c_init(struct udevice *dev)
  398. {
  399. struct tpm_chip *chip = &g_chip;
  400. u32 vendor;
  401. u32 expected_did_vid;
  402. g_chip.dev = dev;
  403. g_chip.chip_type = tpm_tis_i2c_chip_type();
  404. chip->is_open = 1;
  405. /* Disable interrupts (not supported) */
  406. chip->irq = 0;
  407. /* Default timeouts - these could move to the device tree */
  408. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  409. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  410. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  411. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  412. chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
  413. chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
  414. chip->req_canceled = TPM_STS_COMMAND_READY;
  415. if (tpm_tis_i2c_request_locality(chip, 0) < 0)
  416. return -ENODEV;
  417. /* Read four bytes from DID_VID register */
  418. if (tpm_tis_i2c_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
  419. tpm_tis_i2c_release_locality(chip, 0, 1);
  420. return -EIO;
  421. }
  422. if (g_chip.chip_type == SLB9635) {
  423. vendor = be32_to_cpu(vendor);
  424. expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
  425. } else {
  426. /* device id and byte order has changed for newer i2c tpms */
  427. expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
  428. }
  429. if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
  430. error("Vendor id did not match! ID was %08x\n", vendor);
  431. return -ENODEV;
  432. }
  433. debug("1.2 TPM (chip type %s device-id 0x%X)\n",
  434. chip_name[g_chip.chip_type], vendor >> 16);
  435. /*
  436. * A timeout query to TPM can be placed here.
  437. * Standard timeout values are used so far
  438. */
  439. return 0;
  440. }
  441. /* Returns max number of milliseconds to wait */
  442. static unsigned long tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip *chip,
  443. u32 ordinal)
  444. {
  445. int duration_idx = TPM_UNDEFINED;
  446. int duration = 0;
  447. if (ordinal < TPM_MAX_ORDINAL) {
  448. duration_idx = tpm_ordinal_duration[ordinal];
  449. } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
  450. TPM_MAX_PROTECTED_ORDINAL) {
  451. duration_idx = tpm_protected_ordinal_duration[
  452. ordinal & TPM_PROTECTED_ORDINAL_MASK];
  453. }
  454. if (duration_idx != TPM_UNDEFINED)
  455. duration = chip->duration[duration_idx];
  456. if (duration <= 0)
  457. return 2 * 60 * HZ; /* Two minutes timeout */
  458. else
  459. return duration;
  460. }
  461. static ssize_t tpm_tis_i2c_transmit(const unsigned char *buf, size_t bufsiz)
  462. {
  463. int rc;
  464. u32 count, ordinal;
  465. unsigned long start, stop;
  466. struct tpm_chip *chip = &g_chip;
  467. /* switch endianess: big->little */
  468. count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
  469. ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
  470. if (count == 0) {
  471. error("no data\n");
  472. return -ENODATA;
  473. }
  474. if (count > bufsiz) {
  475. error("invalid count value %x %zx\n", count, bufsiz);
  476. return -E2BIG;
  477. }
  478. debug("Calling send\n");
  479. rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
  480. debug(" ... done calling send\n");
  481. if (rc < 0) {
  482. error("tpm_transmit: tpm_send: error %d\n", rc);
  483. goto out;
  484. }
  485. if (chip->irq)
  486. goto out_recv;
  487. start = get_timer(0);
  488. stop = tpm_tis_i2c_calc_ordinal_duration(chip, ordinal);
  489. do {
  490. debug("waiting for status... %ld %ld\n", start, stop);
  491. u8 status = tpm_tis_i2c_status(chip);
  492. if ((status & chip->req_complete_mask) ==
  493. chip->req_complete_val) {
  494. debug("...got it;\n");
  495. goto out_recv;
  496. }
  497. if (status == chip->req_canceled) {
  498. error("Operation Canceled\n");
  499. rc = -ECANCELED;
  500. goto out;
  501. }
  502. mdelay(TPM_TIMEOUT_MS);
  503. } while (get_timer(start) < stop);
  504. tpm_tis_i2c_ready(chip);
  505. error("Operation Timed out\n");
  506. rc = -ETIME;
  507. goto out;
  508. out_recv:
  509. debug("out_recv: reading response...\n");
  510. rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
  511. if (rc < 0)
  512. error("tpm_transmit: tpm_recv: error %d\n", rc);
  513. out:
  514. return rc;
  515. }
  516. /**
  517. * Decode TPM configuration.
  518. *
  519. * @param dev Returns a configuration of TPM device
  520. * @return 0 if ok, -1 on error
  521. */
  522. static int tpm_tis_i2c_decode_config(struct tpm_chip *chip)
  523. {
  524. const void *blob = gd->fdt_blob;
  525. struct udevice *bus;
  526. int chip_addr;
  527. int parent;
  528. int node;
  529. int ret;
  530. node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
  531. if (node < 0) {
  532. node = fdtdec_next_compatible(blob, 0,
  533. COMPAT_INFINEON_SLB9645_TPM);
  534. }
  535. if (node < 0) {
  536. debug("%s: Node not found\n", __func__);
  537. return -1;
  538. }
  539. parent = fdt_parent_offset(blob, node);
  540. if (parent < 0) {
  541. debug("%s: Cannot find node parent\n", __func__);
  542. return -1;
  543. }
  544. /*
  545. * TODO(sjg@chromium.org): Remove this when driver model supports
  546. * TPMs
  547. */
  548. ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
  549. if (ret) {
  550. debug("Cannot find bus for node '%s: ret=%d'\n",
  551. fdt_get_name(blob, parent, NULL), ret);
  552. return ret;
  553. }
  554. chip_addr = fdtdec_get_int(blob, node, "reg", -1);
  555. if (chip_addr == -1) {
  556. debug("Cannot find reg property for node '%s: ret=%d'\n",
  557. fdt_get_name(blob, node, NULL), ret);
  558. return ret;
  559. }
  560. /*
  561. * TODO(sjg@chromium.org): Older TPMs will need to use the older method
  562. * in tpm_tis_i2c_read() so the offset length needs to be 0 here.
  563. */
  564. ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev);
  565. if (ret) {
  566. debug("Cannot find device for node '%s: ret=%d'\n",
  567. fdt_get_name(blob, node, NULL), ret);
  568. return ret;
  569. }
  570. return 0;
  571. }
  572. int tis_init(void)
  573. {
  574. if (g_chip.inited)
  575. return 0;
  576. if (tpm_tis_i2c_decode_config(&g_chip))
  577. return -1;
  578. debug("%s: done\n", __func__);
  579. g_chip.inited = 1;
  580. return 0;
  581. }
  582. int tis_open(void)
  583. {
  584. int rc;
  585. if (!g_chip.inited)
  586. return -1;
  587. debug("%s: start\n", __func__);
  588. if (g_chip.is_open)
  589. return -EBUSY;
  590. rc = tpm_tis_i2c_init(g_chip.dev);
  591. if (rc < 0)
  592. g_chip.is_open = 0;
  593. return rc;
  594. }
  595. int tis_close(void)
  596. {
  597. if (!g_chip.inited)
  598. return -1;
  599. if (g_chip.is_open) {
  600. tpm_tis_i2c_release_locality(&g_chip, g_chip.locality, 1);
  601. g_chip.is_open = 0;
  602. }
  603. return 0;
  604. }
  605. int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
  606. uint8_t *recvbuf, size_t *rbuf_len)
  607. {
  608. int len;
  609. uint8_t buf[4096];
  610. if (!g_chip.inited)
  611. return -1;
  612. if (sizeof(buf) < sbuf_size)
  613. return -1;
  614. memcpy(buf, sendbuf, sbuf_size);
  615. len = tpm_tis_i2c_transmit(buf, sbuf_size);
  616. if (len < 10) {
  617. *rbuf_len = 0;
  618. return -1;
  619. }
  620. memcpy(recvbuf, buf, len);
  621. *rbuf_len = len;
  622. return 0;
  623. }