tpm_tis_infineon.c 15 KB

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