tpm_tis_infineon.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 <i2c.h>
  26. #include <tpm.h>
  27. #include <linux/errno.h>
  28. #include <linux/compiler.h>
  29. #include <linux/types.h>
  30. #include <linux/unaligned/be_byteshift.h>
  31. #include "tpm_tis.h"
  32. #include "tpm_internal.h"
  33. DECLARE_GLOBAL_DATA_PTR;
  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 expected, status;
  326. int rc;
  327. status = tpm_tis_i2c_status(dev);
  328. if (status == TPM_STS_COMMAND_READY)
  329. return -EINTR;
  330. if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
  331. (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
  332. return -EAGAIN;
  333. debug("...got it;\n");
  334. /* Read first 10 bytes, including tag, paramsize, and result */
  335. size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
  336. if (size < TPM_HEADER_SIZE) {
  337. debug("Unable to read header\n");
  338. return size < 0 ? size : -EIO;
  339. }
  340. expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
  341. if ((size_t)expected > count) {
  342. debug("Error size=%x, expected=%x, count=%x\n", size, expected,
  343. count);
  344. return -ENOSPC;
  345. }
  346. size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
  347. expected - TPM_HEADER_SIZE);
  348. if (size < expected) {
  349. debug("Unable to read remainder of result\n");
  350. return -ETIMEDOUT;
  351. }
  352. rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
  353. &status);
  354. if (rc)
  355. return rc;
  356. if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
  357. debug("Error left over data\n");
  358. return -EIO;
  359. }
  360. return size;
  361. }
  362. static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
  363. {
  364. struct tpm_chip *chip = dev_get_priv(dev);
  365. int rc, status;
  366. size_t burstcnt;
  367. size_t count = 0;
  368. int retry = 0;
  369. u8 sts = TPM_STS_GO;
  370. debug("%s: len=%d\n", __func__, len);
  371. if (len > TPM_DEV_BUFSIZE)
  372. return -E2BIG; /* Command is too long for our tpm, sorry */
  373. if (tpm_tis_i2c_request_locality(dev, 0) < 0)
  374. return -EBUSY;
  375. status = tpm_tis_i2c_status(dev);
  376. if ((status & TPM_STS_COMMAND_READY) == 0) {
  377. rc = tpm_tis_i2c_ready(dev);
  378. if (rc)
  379. return rc;
  380. rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
  381. chip->timeout_b, &status);
  382. if (rc)
  383. return rc;
  384. }
  385. burstcnt = tpm_tis_i2c_get_burstcount(dev);
  386. /* burstcount < 0 -> tpm is busy */
  387. if (burstcnt < 0)
  388. return burstcnt;
  389. while (count < len) {
  390. udelay(300);
  391. if (burstcnt > len - count)
  392. burstcnt = len - count;
  393. #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
  394. if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
  395. burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
  396. #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
  397. rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
  398. &(buf[count]), burstcnt);
  399. if (rc == 0)
  400. count += burstcnt;
  401. else {
  402. debug("%s: error\n", __func__);
  403. if (retry++ > 10)
  404. return -EIO;
  405. rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
  406. chip->timeout_c,
  407. &status);
  408. if (rc)
  409. return rc;
  410. if ((status & TPM_STS_DATA_EXPECT) == 0)
  411. return -EIO;
  412. }
  413. }
  414. /* Go and do it */
  415. rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
  416. if (rc < 0)
  417. return rc;
  418. debug("%s: done, rc=%d\n", __func__, rc);
  419. return len;
  420. }
  421. static int tpm_tis_i2c_cleanup(struct udevice *dev)
  422. {
  423. struct tpm_chip *chip = dev_get_priv(dev);
  424. tpm_tis_i2c_ready(dev);
  425. /*
  426. * The TPM needs some time to clean up here,
  427. * so we sleep rather than keeping the bus busy
  428. */
  429. mdelay(2);
  430. tpm_tis_i2c_release_locality(dev, chip->locality, 0);
  431. return 0;
  432. }
  433. static int tpm_tis_i2c_init(struct udevice *dev)
  434. {
  435. struct tpm_chip *chip = dev_get_priv(dev);
  436. u32 vendor;
  437. u32 expected_did_vid;
  438. int rc;
  439. chip->is_open = 1;
  440. /* Default timeouts - these could move to the device tree */
  441. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  442. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  443. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  444. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  445. rc = tpm_tis_i2c_request_locality(dev, 0);
  446. if (rc < 0)
  447. return rc;
  448. /* Read four bytes from DID_VID register */
  449. if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
  450. tpm_tis_i2c_release_locality(dev, 0, 1);
  451. return -EIO;
  452. }
  453. if (chip->chip_type == SLB9635) {
  454. vendor = be32_to_cpu(vendor);
  455. expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
  456. } else {
  457. /* device id and byte order has changed for newer i2c tpms */
  458. expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
  459. }
  460. if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
  461. error("Vendor id did not match! ID was %08x\n", vendor);
  462. return -ENODEV;
  463. }
  464. chip->vend_dev = vendor;
  465. debug("1.2 TPM (chip type %s device-id 0x%X)\n",
  466. chip_name[chip->chip_type], vendor >> 16);
  467. /*
  468. * A timeout query to TPM can be placed here.
  469. * Standard timeout values are used so far
  470. */
  471. return 0;
  472. }
  473. static int tpm_tis_i2c_open(struct udevice *dev)
  474. {
  475. struct tpm_chip *chip = dev_get_priv(dev);
  476. int rc;
  477. debug("%s: start\n", __func__);
  478. if (chip->is_open)
  479. return -EBUSY;
  480. rc = tpm_tis_i2c_init(dev);
  481. if (rc < 0)
  482. chip->is_open = 0;
  483. return rc;
  484. }
  485. static int tpm_tis_i2c_close(struct udevice *dev)
  486. {
  487. struct tpm_chip *chip = dev_get_priv(dev);
  488. if (chip->is_open) {
  489. tpm_tis_i2c_release_locality(dev, chip->locality, 1);
  490. chip->is_open = 0;
  491. chip->vend_dev = 0;
  492. }
  493. return 0;
  494. }
  495. static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
  496. {
  497. struct tpm_chip *chip = dev_get_priv(dev);
  498. if (size < 50)
  499. return -ENOSPC;
  500. return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
  501. chip->is_open ? "open" : "closed",
  502. chip_name[chip->chip_type],
  503. chip->vend_dev >> 16);
  504. }
  505. static int tpm_tis_i2c_probe(struct udevice *dev)
  506. {
  507. struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
  508. struct tpm_chip *chip = dev_get_priv(dev);
  509. chip->chip_type = dev_get_driver_data(dev);
  510. /* TODO: These need to be checked and tuned */
  511. uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
  512. uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
  513. uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
  514. uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
  515. return 0;
  516. }
  517. static const struct tpm_ops tpm_tis_i2c_ops = {
  518. .open = tpm_tis_i2c_open,
  519. .close = tpm_tis_i2c_close,
  520. .get_desc = tpm_tis_get_desc,
  521. .send = tpm_tis_i2c_send,
  522. .recv = tpm_tis_i2c_recv,
  523. .cleanup = tpm_tis_i2c_cleanup,
  524. };
  525. static const struct udevice_id tpm_tis_i2c_ids[] = {
  526. { .compatible = "infineon,slb9635tt", .data = SLB9635 },
  527. { .compatible = "infineon,slb9645tt", .data = SLB9645 },
  528. { }
  529. };
  530. U_BOOT_DRIVER(tpm_tis_i2c) = {
  531. .name = "tpm_tis_infineon",
  532. .id = UCLASS_TPM,
  533. .of_match = tpm_tis_i2c_ids,
  534. .ops = &tpm_tis_i2c_ops,
  535. .probe = tpm_tis_i2c_probe,
  536. .priv_auto_alloc_size = sizeof(struct tpm_chip),
  537. };