tpm_tis_st33zp24_i2c.c 13 KB

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