tpm_tis_st33zp24_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * STMicroelectronics TPM ST33ZP24 I2C UBOOT driver
  4. *
  5. * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
  6. * Author(s): Christophe Ricard <christophe-h.ricard@st.com> for STMicroelectronics.
  7. *
  8. * Description: Device driver for ST33ZP24 I2C TPM TCG.
  9. *
  10. * This device driver implements the TPM interface as defined in
  11. * the TCG TPM Interface Spec version 1.21, revision 1.0 and the
  12. * STMicroelectronics Protocol Stack Specification version 1.2.0.
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <fdtdec.h>
  17. #include <i2c.h>
  18. #include <log.h>
  19. #include <tpm-v1.h>
  20. #include <errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/types.h>
  23. #include <asm/unaligned.h>
  24. #include "tpm_tis.h"
  25. #include "tpm_internal.h"
  26. #define TPM_ACCESS 0x0
  27. #define TPM_STS 0x18
  28. #define TPM_DATA_FIFO 0x24
  29. #define LOCALITY0 0
  30. #define TPM_DUMMY_BYTE 0xAA
  31. #define TPM_ST33ZP24_I2C_SLAVE_ADDR 0x13
  32. #define TPM_WRITE_DIRECTION 0x80
  33. /*
  34. * st33zp24_i2c_write8_reg
  35. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  36. * @param: tpm_register, the tpm tis register where the data should be written
  37. * @param: tpm_data, the tpm_data to write inside the tpm_register
  38. * @param: tpm_size, The length of the data
  39. * @return: Number of byte written successfully else an error code.
  40. */
  41. static int st33zp24_i2c_write8_reg(struct udevice *dev, u8 tpm_register,
  42. const u8 *tpm_data, size_t tpm_size)
  43. {
  44. struct tpm_chip_priv *chip_priv = dev_get_uclass_priv(dev);
  45. chip_priv->buf[0] = tpm_register;
  46. memcpy(chip_priv->buf + 1, tpm_data, tpm_size);
  47. return dm_i2c_write(dev, 0, chip_priv->buf, tpm_size + 1);
  48. }
  49. /*
  50. * st33zp24_i2c_read8_reg
  51. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  52. * @param: tpm_register, the tpm tis register where the data should be read
  53. * @param: tpm_data, the TPM response
  54. * @param: tpm_size, tpm TPM response size to read.
  55. * @return: Number of byte read successfully else an error code.
  56. */
  57. static int st33zp24_i2c_read8_reg(struct udevice *dev, u8 tpm_register,
  58. u8 *tpm_data, size_t tpm_size)
  59. {
  60. int status;
  61. u8 data;
  62. data = TPM_DUMMY_BYTE;
  63. status = st33zp24_i2c_write8_reg(dev, tpm_register, &data, 1);
  64. if (status < 0)
  65. return status;
  66. return dm_i2c_read(dev, 0, tpm_data, tpm_size);
  67. }
  68. /*
  69. * st33zp24_i2c_write
  70. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  71. * @param: phy_id, the phy description
  72. * @param: tpm_register, the tpm tis register where the data should be written
  73. * @param: tpm_data, the tpm_data to write inside the tpm_register
  74. * @param: tpm_size, the length of the data
  75. * @return: number of byte written successfully: should be one if success.
  76. */
  77. static int st33zp24_i2c_write(struct udevice *dev, u8 tpm_register,
  78. const u8 *tpm_data, size_t tpm_size)
  79. {
  80. return st33zp24_i2c_write8_reg(dev, tpm_register | TPM_WRITE_DIRECTION,
  81. tpm_data, tpm_size);
  82. }
  83. /*
  84. * st33zp24_i2c_read
  85. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  86. * @param: phy_id, the phy description
  87. * @param: tpm_register, the tpm tis register where the data should be read
  88. * @param: tpm_data, the TPM response
  89. * @param: tpm_size, tpm TPM response size to read.
  90. * @return: number of byte read successfully: should be one if success.
  91. */
  92. static int st33zp24_i2c_read(struct udevice *dev, u8 tpm_register,
  93. u8 *tpm_data, size_t tpm_size)
  94. {
  95. return st33zp24_i2c_read8_reg(dev, tpm_register, tpm_data, tpm_size);
  96. }
  97. /*
  98. * st33zp24_i2c_release_locality release the active locality
  99. * @param: chip, the tpm chip description.
  100. */
  101. static void st33zp24_i2c_release_locality(struct udevice *dev)
  102. {
  103. u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
  104. st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
  105. }
  106. /*
  107. * st33zp24_i2c_check_locality if the locality is active
  108. * @param: chip, the tpm chip description
  109. * @return: the active locality or -EACCES.
  110. */
  111. static int st33zp24_i2c_check_locality(struct udevice *dev)
  112. {
  113. struct tpm_chip *chip = dev_get_priv(dev);
  114. u8 data;
  115. u8 status;
  116. status = st33zp24_i2c_read(dev, TPM_ACCESS, &data, 1);
  117. if (!status && (data &
  118. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  119. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  120. return chip->locality;
  121. return -EACCES;
  122. }
  123. /*
  124. * st33zp24_i2c_request_locality request the TPM locality
  125. * @param: chip, the chip description
  126. * @return: the active locality or negative value.
  127. */
  128. static int st33zp24_i2c_request_locality(struct udevice *dev)
  129. {
  130. struct tpm_chip *chip = dev_get_priv(dev);
  131. unsigned long start, stop;
  132. long ret;
  133. u8 data;
  134. if (st33zp24_i2c_check_locality(dev) == chip->locality)
  135. return chip->locality;
  136. data = TPM_ACCESS_REQUEST_USE;
  137. ret = st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
  138. if (ret < 0)
  139. return ret;
  140. /* wait for locality activated */
  141. start = get_timer(0);
  142. stop = chip->timeout_a;
  143. do {
  144. if (st33zp24_i2c_check_locality(dev) >= 0)
  145. return chip->locality;
  146. udelay(TPM_TIMEOUT_MS * 1000);
  147. } while (get_timer(start) < stop);
  148. return -EACCES;
  149. }
  150. /*
  151. * st33zp24_i2c_status return the TPM_STS register
  152. * @param: chip, the tpm chip description
  153. * @return: the TPM_STS register value.
  154. */
  155. static u8 st33zp24_i2c_status(struct udevice *dev)
  156. {
  157. u8 data;
  158. st33zp24_i2c_read(dev, TPM_STS, &data, 1);
  159. return data;
  160. }
  161. /*
  162. * st33zp24_i2c_get_burstcount return the burstcount address 0x19 0x1A
  163. * @param: chip, the chip description
  164. * return: the burstcount or -TPM_DRIVER_ERR in case of error.
  165. */
  166. static int st33zp24_i2c_get_burstcount(struct udevice *dev)
  167. {
  168. struct tpm_chip *chip = dev_get_priv(dev);
  169. unsigned long start, stop;
  170. int burstcnt, status;
  171. u8 tpm_reg, temp;
  172. /* wait for burstcount */
  173. start = get_timer(0);
  174. stop = chip->timeout_d;
  175. do {
  176. tpm_reg = TPM_STS + 1;
  177. status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
  178. if (status < 0)
  179. return -EBUSY;
  180. tpm_reg = TPM_STS + 2;
  181. burstcnt = temp;
  182. status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
  183. if (status < 0)
  184. return -EBUSY;
  185. burstcnt |= temp << 8;
  186. if (burstcnt)
  187. return burstcnt;
  188. udelay(TIS_SHORT_TIMEOUT_MS * 1000);
  189. } while (get_timer(start) < stop);
  190. return -EBUSY;
  191. }
  192. /*
  193. * st33zp24_i2c_cancel, cancel the current command execution or
  194. * set STS to COMMAND READY.
  195. * @param: chip, tpm_chip description.
  196. */
  197. static void st33zp24_i2c_cancel(struct udevice *dev)
  198. {
  199. u8 data;
  200. data = TPM_STS_COMMAND_READY;
  201. st33zp24_i2c_write(dev, TPM_STS, &data, 1);
  202. }
  203. /*
  204. * st33zp24_i2c_wait_for_stat wait for a TPM_STS value
  205. * @param: chip, the tpm chip description
  206. * @param: mask, the value mask to wait
  207. * @param: timeout, the timeout
  208. * @param: status,
  209. * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
  210. */
  211. static int st33zp24_i2c_wait_for_stat(struct udevice *dev, u8 mask,
  212. unsigned long timeout, int *status)
  213. {
  214. unsigned long start, stop;
  215. /* Check current status */
  216. *status = st33zp24_i2c_status(dev);
  217. if ((*status & mask) == mask)
  218. return 0;
  219. start = get_timer(0);
  220. stop = timeout;
  221. do {
  222. udelay(TPM_TIMEOUT_MS * 1000);
  223. *status = st33zp24_i2c_status(dev);
  224. if ((*status & mask) == mask)
  225. return 0;
  226. } while (get_timer(start) < stop);
  227. return -ETIME;
  228. }
  229. /*
  230. * st33zp24_i2c_recv_data receive data
  231. * @param: chip, the tpm chip description
  232. * @param: buf, the buffer where the data are received
  233. * @param: count, the number of data to receive
  234. * @return: the number of bytes read from TPM FIFO.
  235. */
  236. static int st33zp24_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
  237. {
  238. struct tpm_chip *chip = dev_get_priv(dev);
  239. int size = 0, burstcnt, len, ret, status;
  240. while (size < count &&
  241. st33zp24_i2c_wait_for_stat(dev, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  242. chip->timeout_c, &status) == 0) {
  243. burstcnt = st33zp24_i2c_get_burstcount(dev);
  244. if (burstcnt < 0)
  245. return burstcnt;
  246. len = min_t(int, burstcnt, count - size);
  247. ret = st33zp24_i2c_read(dev, TPM_DATA_FIFO, buf + size, len);
  248. if (ret < 0)
  249. return ret;
  250. size += len;
  251. }
  252. return size;
  253. }
  254. /*
  255. * st33zp24_i2c_recv received TPM response through TPM phy.
  256. * @param: chip, tpm_chip description.
  257. * @param: buf, the buffer to store data.
  258. * @param: count, the number of bytes that can received (sizeof buf).
  259. * @return: Returns zero in case of success else -EIO.
  260. */
  261. static int st33zp24_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
  262. {
  263. struct tpm_chip *chip = dev_get_priv(dev);
  264. int size;
  265. unsigned int expected;
  266. if (!chip)
  267. return -ENODEV;
  268. if (count < TPM_HEADER_SIZE) {
  269. size = -EIO;
  270. goto out;
  271. }
  272. size = st33zp24_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
  273. if (size < TPM_HEADER_SIZE) {
  274. debug("TPM error, unable to read header\n");
  275. goto out;
  276. }
  277. expected = get_unaligned_be32(buf + 2);
  278. if (expected > count || expected < TPM_HEADER_SIZE) {
  279. size = -EIO;
  280. goto out;
  281. }
  282. size += st33zp24_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
  283. expected - TPM_HEADER_SIZE);
  284. if (size < expected) {
  285. debug("TPM error, unable to read remaining bytes of result\n");
  286. size = -EIO;
  287. goto out;
  288. }
  289. out:
  290. st33zp24_i2c_cancel(dev);
  291. st33zp24_i2c_release_locality(dev);
  292. return size;
  293. }
  294. /*
  295. * st33zp24_i2c_send send TPM commands through TPM phy.
  296. * @param: chip, tpm_chip description.
  297. * @param: buf, the buffer to send.
  298. * @param: len, the number of bytes to send.
  299. * @return: Returns zero in case of success else the negative error code.
  300. */
  301. static int st33zp24_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
  302. {
  303. struct tpm_chip *chip = dev_get_priv(dev);
  304. u32 i, size;
  305. int burstcnt, ret, status;
  306. u8 data, tpm_stat;
  307. if (!chip)
  308. return -ENODEV;
  309. if (len < TPM_HEADER_SIZE)
  310. return -EIO;
  311. ret = st33zp24_i2c_request_locality(dev);
  312. if (ret < 0)
  313. return ret;
  314. tpm_stat = st33zp24_i2c_status(dev);
  315. if ((tpm_stat & TPM_STS_COMMAND_READY) == 0) {
  316. st33zp24_i2c_cancel(dev);
  317. if (st33zp24_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
  318. chip->timeout_b, &status) < 0) {
  319. ret = -ETIME;
  320. goto out_err;
  321. }
  322. }
  323. for (i = 0; i < len - 1;) {
  324. burstcnt = st33zp24_i2c_get_burstcount(dev);
  325. if (burstcnt < 0)
  326. return burstcnt;
  327. size = min_t(int, len - i - 1, burstcnt);
  328. ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + i, size);
  329. if (ret < 0)
  330. goto out_err;
  331. i += size;
  332. }
  333. tpm_stat = st33zp24_i2c_status(dev);
  334. if ((tpm_stat & TPM_STS_DATA_EXPECT) == 0) {
  335. ret = -EIO;
  336. goto out_err;
  337. }
  338. ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + len - 1, 1);
  339. if (ret < 0)
  340. goto out_err;
  341. tpm_stat = st33zp24_i2c_status(dev);
  342. if ((tpm_stat & TPM_STS_DATA_EXPECT) != 0) {
  343. ret = -EIO;
  344. goto out_err;
  345. }
  346. data = TPM_STS_GO;
  347. ret = st33zp24_i2c_write(dev, TPM_STS, &data, 1);
  348. if (ret < 0)
  349. goto out_err;
  350. return len;
  351. out_err:
  352. st33zp24_i2c_cancel(dev);
  353. st33zp24_i2c_release_locality(dev);
  354. return ret;
  355. }
  356. static int st33zp24_i2c_cleanup(struct udevice *dev)
  357. {
  358. st33zp24_i2c_cancel(dev);
  359. /*
  360. * The TPM needs some time to clean up here,
  361. * so we sleep rather than keeping the bus busy
  362. */
  363. mdelay(2);
  364. st33zp24_i2c_release_locality(dev);
  365. return 0;
  366. }
  367. static int st33zp24_i2c_init(struct udevice *dev)
  368. {
  369. struct tpm_chip *chip = dev_get_priv(dev);
  370. chip->is_open = 1;
  371. /* Default timeouts - these could move to the device tree */
  372. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  373. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  374. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  375. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  376. chip->locality = LOCALITY0;
  377. /*
  378. * A timeout query to TPM can be placed here.
  379. * Standard timeout values are used so far
  380. */
  381. return 0;
  382. }
  383. static int st33zp24_i2c_open(struct udevice *dev)
  384. {
  385. struct tpm_chip *chip = dev_get_priv(dev);
  386. int rc;
  387. debug("%s: start\n", __func__);
  388. if (chip->is_open)
  389. return -EBUSY;
  390. rc = st33zp24_i2c_init(dev);
  391. if (rc < 0)
  392. chip->is_open = 0;
  393. return rc;
  394. }
  395. static int st33zp24_i2c_close(struct udevice *dev)
  396. {
  397. struct tpm_chip *chip = dev_get_priv(dev);
  398. if (chip->is_open) {
  399. st33zp24_i2c_release_locality(dev);
  400. chip->is_open = 0;
  401. chip->vend_dev = 0;
  402. }
  403. return 0;
  404. }
  405. static int st33zp24_i2c_get_desc(struct udevice *dev, char *buf, int size)
  406. {
  407. struct tpm_chip *chip = dev_get_priv(dev);
  408. if (size < 50)
  409. return -ENOSPC;
  410. return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
  411. chip->is_open ? "open" : "closed",
  412. dev->name,
  413. chip->vend_dev >> 16);
  414. }
  415. static const struct tpm_ops st33zp24_i2c_tpm_ops = {
  416. .open = st33zp24_i2c_open,
  417. .close = st33zp24_i2c_close,
  418. .recv = st33zp24_i2c_recv,
  419. .send = st33zp24_i2c_send,
  420. .cleanup = st33zp24_i2c_cleanup,
  421. .get_desc = st33zp24_i2c_get_desc,
  422. };
  423. static int st33zp24_i2c_probe(struct udevice *dev)
  424. {
  425. struct tpm_chip *chip = dev_get_priv(dev);
  426. /* Default timeouts */
  427. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  428. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  429. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  430. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  431. chip->locality = LOCALITY0;
  432. i2c_set_chip_offset_len(dev, 0);
  433. debug("ST33ZP24 I2C TPM from STMicroelectronics found\n");
  434. return 0;
  435. }
  436. static int st33zp24_i2c_remove(struct udevice *dev)
  437. {
  438. st33zp24_i2c_release_locality(dev);
  439. return 0;
  440. }
  441. static const struct udevice_id st33zp24_i2c_ids[] = {
  442. { .compatible = "st,st33zp24-i2c" },
  443. { }
  444. };
  445. U_BOOT_DRIVER(st33zp24_i2c) = {
  446. .name = "st33zp24-i2c",
  447. .id = UCLASS_TPM,
  448. .of_match = of_match_ptr(st33zp24_i2c_ids),
  449. .probe = st33zp24_i2c_probe,
  450. .remove = st33zp24_i2c_remove,
  451. .ops = &st33zp24_i2c_tpm_ops,
  452. .priv_auto = sizeof(struct tpm_chip),
  453. };