tpm_tis_infineon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 Infineon Technologies
  4. *
  5. * Authors:
  6. * Peter Huewe <huewe.external@infineon.com>
  7. *
  8. * Description:
  9. * Device driver for TCG/TCPA TPM (trusted platform module).
  10. * Specifications at www.trustedcomputinggroup.org
  11. *
  12. * This device driver implements the TPM interface as defined in
  13. * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
  14. * Infineon I2C Protocol Stack Specification v0.20.
  15. *
  16. * It is based on the Linux kernel driver tpm.c from Leendert van
  17. * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
  18. *
  19. * Version: 2.1.1
  20. */
  21. #include <common.h>
  22. #include <dm.h>
  23. #include <fdtdec.h>
  24. #include <i2c.h>
  25. #include <log.h>
  26. #include <tpm-v1.h>
  27. #include <linux/delay.h>
  28. #include <linux/errno.h>
  29. #include <linux/compiler.h>
  30. #include <linux/types.h>
  31. #include <linux/unaligned/be_byteshift.h>
  32. #include "tpm_tis.h"
  33. #include "tpm_internal.h"
  34. enum i2c_chip_type {
  35. SLB9635,
  36. SLB9645,
  37. UNKNOWN,
  38. };
  39. /* expected value for DIDVID register */
  40. #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
  41. #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
  42. static const char * const chip_name[] = {
  43. [SLB9635] = "slb9635tt",
  44. [SLB9645] = "slb9645tt",
  45. [UNKNOWN] = "unknown/fallback to slb9635",
  46. };
  47. #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
  48. #define TPM_STS(l) (0x0001 | ((l) << 4))
  49. #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
  50. #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
  51. /*
  52. * tpm_tis_i2c_read() - read from TPM register
  53. * @addr: register address to read from
  54. * @buffer: provided by caller
  55. * @len: number of bytes to read
  56. *
  57. * Read len bytes from TPM register and put them into
  58. * buffer (little-endian format, i.e. first byte is put into buffer[0]).
  59. *
  60. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  61. * values have to be swapped.
  62. *
  63. * Return -EIO on error, 0 on success.
  64. */
  65. static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
  66. size_t len)
  67. {
  68. struct tpm_chip *chip = dev_get_priv(dev);
  69. int rc;
  70. int count;
  71. uint32_t addrbuf = addr;
  72. if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
  73. /* slb9635 protocol should work in both cases */
  74. for (count = 0; count < MAX_COUNT; count++) {
  75. rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
  76. if (rc == 0)
  77. break; /* Success, break to skip sleep */
  78. udelay(SLEEP_DURATION_US);
  79. }
  80. if (rc)
  81. return rc;
  82. /* After the TPM has successfully received the register address
  83. * it needs some time, thus we're sleeping here again, before
  84. * retrieving the data
  85. */
  86. for (count = 0; count < MAX_COUNT; count++) {
  87. udelay(SLEEP_DURATION_US);
  88. rc = dm_i2c_read(dev, 0, buffer, len);
  89. if (rc == 0)
  90. break; /* success, break to skip sleep */
  91. }
  92. } else {
  93. /*
  94. * Use a combined read for newer chips.
  95. * Unfortunately the smbus functions are not suitable due to
  96. * the 32 byte limit of the smbus.
  97. * Retries should usually not be needed, but are kept just to
  98. * be safe on the safe side.
  99. */
  100. for (count = 0; count < MAX_COUNT; count++) {
  101. rc = dm_i2c_read(dev, addr, buffer, len);
  102. if (rc == 0)
  103. break; /* break here to skip sleep */
  104. udelay(SLEEP_DURATION_US);
  105. }
  106. }
  107. /* Take care of 'guard time' */
  108. udelay(SLEEP_DURATION_US);
  109. if (rc)
  110. return rc;
  111. return 0;
  112. }
  113. static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
  114. const u8 *buffer, size_t len,
  115. unsigned int sleep_time_us, u8 max_count)
  116. {
  117. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  118. struct tpm_chip *chip = dev_get_priv(dev);
  119. int rc = 0;
  120. int count;
  121. if (chip->chip_type == SLB9635) {
  122. /* Prepare send buffer to include the address */
  123. priv->buf[0] = addr;
  124. memcpy(&(priv->buf[1]), buffer, len);
  125. buffer = priv->buf;
  126. len++;
  127. addr = 0;
  128. }
  129. for (count = 0; count < max_count; count++) {
  130. rc = dm_i2c_write(dev, addr, buffer, len);
  131. if (rc == 0)
  132. break; /* Success, break to skip sleep */
  133. udelay(sleep_time_us);
  134. }
  135. /* take care of 'guard time' */
  136. udelay(sleep_time_us);
  137. if (rc)
  138. return rc;
  139. return 0;
  140. }
  141. /*
  142. * tpm_tis_i2c_write() - write to TPM register
  143. * @addr: register address to write to
  144. * @buffer: containing data to be written
  145. * @len: number of bytes to write
  146. *
  147. * Write len bytes from provided buffer to TPM register (little
  148. * endian format, i.e. buffer[0] is written as first byte).
  149. *
  150. * NOTE: TPM is big-endian for multi-byte values. Multi-byte
  151. * values have to be swapped.
  152. *
  153. * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
  154. *
  155. * Return -EIO on error, 0 on success
  156. */
  157. static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
  158. size_t len)
  159. {
  160. return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
  161. SLEEP_DURATION_US, MAX_COUNT);
  162. }
  163. /*
  164. * This function is needed especially for the cleanup situation after
  165. * sending TPM_READY
  166. */
  167. static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
  168. size_t len)
  169. {
  170. return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
  171. SLEEP_DURATION_LONG_US,
  172. MAX_COUNT_LONG);
  173. }
  174. static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
  175. {
  176. const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
  177. struct tpm_chip *chip = dev_get_priv(dev);
  178. u8 buf;
  179. int rc;
  180. rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
  181. if (rc < 0)
  182. return rc;
  183. if ((buf & mask) == mask) {
  184. chip->locality = loc;
  185. return loc;
  186. }
  187. return -ENOENT;
  188. }
  189. static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
  190. int force)
  191. {
  192. const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
  193. u8 buf;
  194. if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
  195. return;
  196. if (force || (buf & mask) == mask) {
  197. buf = TPM_ACCESS_ACTIVE_LOCALITY;
  198. tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
  199. }
  200. }
  201. static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
  202. {
  203. struct tpm_chip *chip = dev_get_priv(dev);
  204. unsigned long start, stop;
  205. u8 buf = TPM_ACCESS_REQUEST_USE;
  206. int rc;
  207. rc = tpm_tis_i2c_check_locality(dev, loc);
  208. if (rc >= 0) {
  209. debug("%s: Already have locality\n", __func__);
  210. return loc; /* We already have the locality */
  211. } else if (rc != -ENOENT) {
  212. debug("%s: Failed to get locality: %d\n", __func__, rc);
  213. return rc;
  214. }
  215. rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
  216. if (rc) {
  217. debug("%s: Failed to write to TPM: %d\n", __func__, rc);
  218. return rc;
  219. }
  220. /* Wait for burstcount */
  221. start = get_timer(0);
  222. stop = chip->timeout_a;
  223. do {
  224. rc = tpm_tis_i2c_check_locality(dev, loc);
  225. if (rc >= 0) {
  226. debug("%s: Have locality\n", __func__);
  227. return loc;
  228. } else if (rc != -ENOENT) {
  229. debug("%s: Failed to get locality: %d\n", __func__, rc);
  230. return rc;
  231. }
  232. mdelay(TPM_TIMEOUT_MS);
  233. } while (get_timer(start) < stop);
  234. debug("%s: Timeout getting locality: %d\n", __func__, rc);
  235. return rc;
  236. }
  237. static u8 tpm_tis_i2c_status(struct udevice *dev)
  238. {
  239. struct tpm_chip *chip = dev_get_priv(dev);
  240. /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
  241. u8 buf;
  242. if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
  243. return 0;
  244. else
  245. return buf;
  246. }
  247. static int tpm_tis_i2c_ready(struct udevice *dev)
  248. {
  249. struct tpm_chip *chip = dev_get_priv(dev);
  250. int rc;
  251. /* This causes the current command to be aborted */
  252. u8 buf = TPM_STS_COMMAND_READY;
  253. debug("%s\n", __func__);
  254. rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
  255. if (rc)
  256. debug("%s: rc=%d\n", __func__, rc);
  257. return rc;
  258. }
  259. static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
  260. {
  261. struct tpm_chip *chip = dev_get_priv(dev);
  262. unsigned long start, stop;
  263. ssize_t burstcnt;
  264. u8 addr, buf[3];
  265. /* Wait for burstcount */
  266. /* XXX: Which timeout value? Spec has 2 answers (c & d) */
  267. start = get_timer(0);
  268. stop = chip->timeout_d;
  269. do {
  270. /* Note: STS is little endian */
  271. addr = TPM_STS(chip->locality) + 1;
  272. if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
  273. burstcnt = 0;
  274. else
  275. burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
  276. if (burstcnt)
  277. return burstcnt;
  278. mdelay(TPM_TIMEOUT_MS);
  279. } while (get_timer(start) < stop);
  280. return -EBUSY;
  281. }
  282. static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
  283. unsigned long timeout, int *status)
  284. {
  285. unsigned long start, stop;
  286. /* Check current status */
  287. *status = tpm_tis_i2c_status(dev);
  288. if ((*status & mask) == mask)
  289. return 0;
  290. start = get_timer(0);
  291. stop = timeout;
  292. do {
  293. mdelay(TPM_TIMEOUT_MS);
  294. *status = tpm_tis_i2c_status(dev);
  295. if ((*status & mask) == mask)
  296. return 0;
  297. } while (get_timer(start) < stop);
  298. return -ETIMEDOUT;
  299. }
  300. static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
  301. {
  302. struct tpm_chip *chip = dev_get_priv(dev);
  303. size_t size = 0;
  304. ssize_t burstcnt;
  305. int rc;
  306. while (size < count) {
  307. burstcnt = tpm_tis_i2c_get_burstcount(dev);
  308. /* burstcount < 0 -> tpm is busy */
  309. if (burstcnt < 0)
  310. return burstcnt;
  311. /* Limit received data to max left */
  312. if (burstcnt > (count - size))
  313. burstcnt = count - size;
  314. rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
  315. &(buf[size]), burstcnt);
  316. if (rc == 0)
  317. size += burstcnt;
  318. }
  319. return size;
  320. }
  321. static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
  322. {
  323. struct tpm_chip *chip = dev_get_priv(dev);
  324. int size = 0;
  325. int status;
  326. unsigned int expected;
  327. int rc;
  328. status = tpm_tis_i2c_status(dev);
  329. if (status == TPM_STS_COMMAND_READY)
  330. return -EINTR;
  331. if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
  332. (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
  333. return -EAGAIN;
  334. debug("...got it;\n");
  335. /* Read first 10 bytes, including tag, paramsize, and result */
  336. size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
  337. if (size < TPM_HEADER_SIZE) {
  338. debug("Unable to read header\n");
  339. return size < 0 ? size : -EIO;
  340. }
  341. expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
  342. if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
  343. debug("Error size=%x, expected=%x, count=%x\n", size, expected,
  344. count);
  345. return -ENOSPC;
  346. }
  347. size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
  348. expected - TPM_HEADER_SIZE);
  349. if (size < expected) {
  350. debug("Unable to read remainder of result\n");
  351. return -ETIMEDOUT;
  352. }
  353. rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
  354. &status);
  355. if (rc)
  356. return rc;
  357. if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
  358. debug("Error left over data\n");
  359. return -EIO;
  360. }
  361. return size;
  362. }
  363. static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
  364. {
  365. struct tpm_chip *chip = dev_get_priv(dev);
  366. int rc, status;
  367. size_t burstcnt;
  368. size_t count = 0;
  369. int retry = 0;
  370. u8 sts = TPM_STS_GO;
  371. debug("%s: len=%d\n", __func__, len);
  372. if (len > TPM_DEV_BUFSIZE)
  373. return -E2BIG; /* Command is too long for our tpm, sorry */
  374. if (tpm_tis_i2c_request_locality(dev, 0) < 0)
  375. return -EBUSY;
  376. status = tpm_tis_i2c_status(dev);
  377. if ((status & TPM_STS_COMMAND_READY) == 0) {
  378. rc = tpm_tis_i2c_ready(dev);
  379. if (rc)
  380. return rc;
  381. rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
  382. chip->timeout_b, &status);
  383. if (rc)
  384. return rc;
  385. }
  386. burstcnt = tpm_tis_i2c_get_burstcount(dev);
  387. /* burstcount < 0 -> tpm is busy */
  388. if (burstcnt < 0)
  389. return burstcnt;
  390. while (count < len) {
  391. udelay(300);
  392. if (burstcnt > len - count)
  393. burstcnt = len - count;
  394. #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
  395. if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
  396. burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
  397. #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
  398. rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
  399. &(buf[count]), burstcnt);
  400. if (rc == 0)
  401. count += burstcnt;
  402. else {
  403. debug("%s: error\n", __func__);
  404. if (retry++ > 10)
  405. return -EIO;
  406. rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
  407. chip->timeout_c,
  408. &status);
  409. if (rc)
  410. return rc;
  411. if ((status & TPM_STS_DATA_EXPECT) == 0)
  412. return -EIO;
  413. }
  414. }
  415. /* Go and do it */
  416. rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
  417. if (rc < 0)
  418. return rc;
  419. debug("%s: done, rc=%d\n", __func__, rc);
  420. return len;
  421. }
  422. static int tpm_tis_i2c_cleanup(struct udevice *dev)
  423. {
  424. struct tpm_chip *chip = dev_get_priv(dev);
  425. tpm_tis_i2c_ready(dev);
  426. /*
  427. * The TPM needs some time to clean up here,
  428. * so we sleep rather than keeping the bus busy
  429. */
  430. mdelay(2);
  431. tpm_tis_i2c_release_locality(dev, chip->locality, 0);
  432. return 0;
  433. }
  434. static int tpm_tis_i2c_init(struct udevice *dev)
  435. {
  436. struct tpm_chip *chip = dev_get_priv(dev);
  437. u32 vendor;
  438. u32 expected_did_vid;
  439. int rc;
  440. chip->is_open = 1;
  441. /* Default timeouts - these could move to the device tree */
  442. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  443. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  444. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  445. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  446. rc = tpm_tis_i2c_request_locality(dev, 0);
  447. if (rc < 0)
  448. return rc;
  449. /* Read four bytes from DID_VID register */
  450. if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
  451. tpm_tis_i2c_release_locality(dev, 0, 1);
  452. return -EIO;
  453. }
  454. if (chip->chip_type == SLB9635) {
  455. vendor = be32_to_cpu(vendor);
  456. expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
  457. } else {
  458. /* device id and byte order has changed for newer i2c tpms */
  459. expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
  460. }
  461. if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
  462. pr_err("Vendor id did not match! ID was %08x\n", vendor);
  463. return -ENODEV;
  464. }
  465. chip->vend_dev = vendor;
  466. debug("1.2 TPM (chip type %s device-id 0x%X)\n",
  467. chip_name[chip->chip_type], vendor >> 16);
  468. /*
  469. * A timeout query to TPM can be placed here.
  470. * Standard timeout values are used so far
  471. */
  472. return 0;
  473. }
  474. static int tpm_tis_i2c_open(struct udevice *dev)
  475. {
  476. struct tpm_chip *chip = dev_get_priv(dev);
  477. int rc;
  478. debug("%s: start\n", __func__);
  479. if (chip->is_open)
  480. return -EBUSY;
  481. rc = tpm_tis_i2c_init(dev);
  482. if (rc < 0)
  483. chip->is_open = 0;
  484. return rc;
  485. }
  486. static int tpm_tis_i2c_close(struct udevice *dev)
  487. {
  488. struct tpm_chip *chip = dev_get_priv(dev);
  489. if (chip->is_open) {
  490. tpm_tis_i2c_release_locality(dev, chip->locality, 1);
  491. chip->is_open = 0;
  492. chip->vend_dev = 0;
  493. }
  494. return 0;
  495. }
  496. static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
  497. {
  498. struct tpm_chip *chip = dev_get_priv(dev);
  499. if (size < 50)
  500. return -ENOSPC;
  501. return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
  502. chip->is_open ? "open" : "closed",
  503. chip_name[chip->chip_type],
  504. chip->vend_dev >> 16);
  505. }
  506. static int tpm_tis_i2c_probe(struct udevice *dev)
  507. {
  508. struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
  509. struct tpm_chip *chip = dev_get_priv(dev);
  510. chip->chip_type = dev_get_driver_data(dev);
  511. /* TODO: These need to be checked and tuned */
  512. uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
  513. uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
  514. uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
  515. uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
  516. return 0;
  517. }
  518. static const struct tpm_ops tpm_tis_i2c_ops = {
  519. .open = tpm_tis_i2c_open,
  520. .close = tpm_tis_i2c_close,
  521. .get_desc = tpm_tis_get_desc,
  522. .send = tpm_tis_i2c_send,
  523. .recv = tpm_tis_i2c_recv,
  524. .cleanup = tpm_tis_i2c_cleanup,
  525. };
  526. static const struct udevice_id tpm_tis_i2c_ids[] = {
  527. { .compatible = "infineon,slb9635tt", .data = SLB9635 },
  528. { .compatible = "infineon,slb9645tt", .data = SLB9645 },
  529. { }
  530. };
  531. U_BOOT_DRIVER(tpm_tis_i2c) = {
  532. .name = "tpm_tis_infineon",
  533. .id = UCLASS_TPM,
  534. .of_match = tpm_tis_i2c_ids,
  535. .ops = &tpm_tis_i2c_ops,
  536. .probe = tpm_tis_i2c_probe,
  537. .priv_auto = sizeof(struct tpm_chip),
  538. };