tpm_tis_st33zp24_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * STMicroelectronics TPM ST33ZP24 I2C UBOOT driver
  3. *
  4. * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
  5. * Author(s): Christophe Ricard <christophe-h.ricard@st.com> for STMicroelectronics.
  6. *
  7. * Description: Device driver for ST33ZP24 I2C TPM TCG.
  8. *
  9. * This device driver implements the TPM interface as defined in
  10. * the TCG TPM Interface Spec version 1.21, revision 1.0 and the
  11. * STMicroelectronics Protocol Stack Specification version 1.2.0.
  12. *
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <fdtdec.h>
  18. #include <i2c.h>
  19. #include <tpm.h>
  20. #include <errno.h>
  21. #include <linux/types.h>
  22. #include <asm/unaligned.h>
  23. #include "tpm_tis.h"
  24. #include "tpm_internal.h"
  25. #define TPM_ACCESS 0x0
  26. #define TPM_STS 0x18
  27. #define TPM_DATA_FIFO 0x24
  28. #define LOCALITY0 0
  29. #define TPM_DUMMY_BYTE 0xAA
  30. #define TPM_ST33ZP24_I2C_SLAVE_ADDR 0x13
  31. #define TPM_WRITE_DIRECTION 0x80
  32. /*
  33. * st33zp24_i2c_write8_reg
  34. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  35. * @param: tpm_register, the tpm tis register where the data should be written
  36. * @param: tpm_data, the tpm_data to write inside the tpm_register
  37. * @param: tpm_size, The length of the data
  38. * @return: Number of byte written successfully else an error code.
  39. */
  40. static int st33zp24_i2c_write8_reg(struct udevice *dev, u8 tpm_register,
  41. const u8 *tpm_data, size_t tpm_size)
  42. {
  43. struct tpm_chip_priv *chip_priv = dev_get_uclass_priv(dev);
  44. chip_priv->buf[0] = tpm_register;
  45. memcpy(chip_priv->buf + 1, tpm_data, tpm_size);
  46. return dm_i2c_write(dev, 0, chip_priv->buf, tpm_size + 1);
  47. }
  48. /*
  49. * st33zp24_i2c_read8_reg
  50. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  51. * @param: tpm_register, the tpm tis register where the data should be read
  52. * @param: tpm_data, the TPM response
  53. * @param: tpm_size, tpm TPM response size to read.
  54. * @return: Number of byte read successfully else an error code.
  55. */
  56. static int st33zp24_i2c_read8_reg(struct udevice *dev, u8 tpm_register,
  57. u8 *tpm_data, size_t tpm_size)
  58. {
  59. int status;
  60. u8 data;
  61. data = TPM_DUMMY_BYTE;
  62. status = st33zp24_i2c_write8_reg(dev, tpm_register, &data, 1);
  63. if (status < 0)
  64. return status;
  65. return dm_i2c_read(dev, 0, tpm_data, tpm_size);
  66. }
  67. /*
  68. * st33zp24_i2c_write
  69. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  70. * @param: phy_id, the phy description
  71. * @param: tpm_register, the tpm tis register where the data should be written
  72. * @param: tpm_data, the tpm_data to write inside the tpm_register
  73. * @param: tpm_size, the length of the data
  74. * @return: number of byte written successfully: should be one if success.
  75. */
  76. static int st33zp24_i2c_write(struct udevice *dev, u8 tpm_register,
  77. const u8 *tpm_data, size_t tpm_size)
  78. {
  79. return st33zp24_i2c_write8_reg(dev, tpm_register | TPM_WRITE_DIRECTION,
  80. tpm_data, tpm_size);
  81. }
  82. /*
  83. * st33zp24_i2c_read
  84. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  85. * @param: phy_id, the phy description
  86. * @param: tpm_register, the tpm tis register where the data should be read
  87. * @param: tpm_data, the TPM response
  88. * @param: tpm_size, tpm TPM response size to read.
  89. * @return: number of byte read successfully: should be one if success.
  90. */
  91. static int st33zp24_i2c_read(struct udevice *dev, u8 tpm_register,
  92. u8 *tpm_data, size_t tpm_size)
  93. {
  94. return st33zp24_i2c_read8_reg(dev, tpm_register, tpm_data, tpm_size);
  95. }
  96. /*
  97. * st33zp24_i2c_release_locality release the active locality
  98. * @param: chip, the tpm chip description.
  99. */
  100. static void st33zp24_i2c_release_locality(struct udevice *dev)
  101. {
  102. u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
  103. st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
  104. }
  105. /*
  106. * st33zp24_i2c_check_locality if the locality is active
  107. * @param: chip, the tpm chip description
  108. * @return: the active locality or -EACCES.
  109. */
  110. static int st33zp24_i2c_check_locality(struct udevice *dev)
  111. {
  112. struct tpm_chip *chip = dev_get_priv(dev);
  113. u8 data;
  114. u8 status;
  115. status = st33zp24_i2c_read(dev, TPM_ACCESS, &data, 1);
  116. if (!status && (data &
  117. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  118. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  119. return chip->locality;
  120. return -EACCES;
  121. }
  122. /*
  123. * st33zp24_i2c_request_locality request the TPM locality
  124. * @param: chip, the chip description
  125. * @return: the active locality or negative value.
  126. */
  127. static int st33zp24_i2c_request_locality(struct udevice *dev)
  128. {
  129. struct tpm_chip *chip = dev_get_priv(dev);
  130. unsigned long start, stop;
  131. long ret;
  132. u8 data;
  133. if (st33zp24_i2c_check_locality(dev) == chip->locality)
  134. return chip->locality;
  135. data = TPM_ACCESS_REQUEST_USE;
  136. ret = st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
  137. if (ret < 0)
  138. return ret;
  139. /* wait for locality activated */
  140. start = get_timer(0);
  141. stop = chip->timeout_a;
  142. do {
  143. if (st33zp24_i2c_check_locality(dev) >= 0)
  144. return chip->locality;
  145. udelay(TPM_TIMEOUT_MS * 1000);
  146. } while (get_timer(start) < stop);
  147. return -EACCES;
  148. }
  149. /*
  150. * st33zp24_i2c_status return the TPM_STS register
  151. * @param: chip, the tpm chip description
  152. * @return: the TPM_STS register value.
  153. */
  154. static u8 st33zp24_i2c_status(struct udevice *dev)
  155. {
  156. u8 data;
  157. st33zp24_i2c_read(dev, TPM_STS, &data, 1);
  158. return data;
  159. }
  160. /*
  161. * st33zp24_i2c_get_burstcount return the burstcount address 0x19 0x1A
  162. * @param: chip, the chip description
  163. * return: the burstcount or -TPM_DRIVER_ERR in case of error.
  164. */
  165. static int st33zp24_i2c_get_burstcount(struct udevice *dev)
  166. {
  167. struct tpm_chip *chip = dev_get_priv(dev);
  168. unsigned long start, stop;
  169. int burstcnt, status;
  170. u8 tpm_reg, temp;
  171. /* wait for burstcount */
  172. start = get_timer(0);
  173. stop = chip->timeout_d;
  174. do {
  175. tpm_reg = TPM_STS + 1;
  176. status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
  177. if (status < 0)
  178. return -EBUSY;
  179. tpm_reg = TPM_STS + 2;
  180. burstcnt = temp;
  181. status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
  182. if (status < 0)
  183. return -EBUSY;
  184. burstcnt |= temp << 8;
  185. if (burstcnt)
  186. return burstcnt;
  187. udelay(TIS_SHORT_TIMEOUT_MS * 1000);
  188. } while (get_timer(start) < stop);
  189. return -EBUSY;
  190. }
  191. /*
  192. * st33zp24_i2c_cancel, cancel the current command execution or
  193. * set STS to COMMAND READY.
  194. * @param: chip, tpm_chip description.
  195. */
  196. static void st33zp24_i2c_cancel(struct udevice *dev)
  197. {
  198. u8 data;
  199. data = TPM_STS_COMMAND_READY;
  200. st33zp24_i2c_write(dev, TPM_STS, &data, 1);
  201. }
  202. /*
  203. * st33zp24_i2c_wait_for_stat wait for a TPM_STS value
  204. * @param: chip, the tpm chip description
  205. * @param: mask, the value mask to wait
  206. * @param: timeout, the timeout
  207. * @param: status,
  208. * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
  209. */
  210. static int st33zp24_i2c_wait_for_stat(struct udevice *dev, u8 mask,
  211. unsigned long timeout, int *status)
  212. {
  213. unsigned long start, stop;
  214. /* Check current status */
  215. *status = st33zp24_i2c_status(dev);
  216. if ((*status & mask) == mask)
  217. return 0;
  218. start = get_timer(0);
  219. stop = timeout;
  220. do {
  221. udelay(TPM_TIMEOUT_MS * 1000);
  222. *status = st33zp24_i2c_status(dev);
  223. if ((*status & mask) == mask)
  224. return 0;
  225. } while (get_timer(start) < stop);
  226. return -ETIME;
  227. }
  228. /*
  229. * st33zp24_i2c_recv_data receive data
  230. * @param: chip, the tpm chip description
  231. * @param: buf, the buffer where the data are received
  232. * @param: count, the number of data to receive
  233. * @return: the number of bytes read from TPM FIFO.
  234. */
  235. static int st33zp24_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
  236. {
  237. struct tpm_chip *chip = dev_get_priv(dev);
  238. int size = 0, burstcnt, len, ret, status;
  239. while (size < count &&
  240. st33zp24_i2c_wait_for_stat(dev, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  241. chip->timeout_c, &status) == 0) {
  242. burstcnt = st33zp24_i2c_get_burstcount(dev);
  243. if (burstcnt < 0)
  244. return burstcnt;
  245. len = min_t(int, burstcnt, count - size);
  246. ret = st33zp24_i2c_read(dev, TPM_DATA_FIFO, buf + size, len);
  247. if (ret < 0)
  248. return ret;
  249. size += len;
  250. }
  251. return size;
  252. }
  253. /*
  254. * st33zp24_i2c_recv received TPM response through TPM phy.
  255. * @param: chip, tpm_chip description.
  256. * @param: buf, the buffer to store data.
  257. * @param: count, the number of bytes that can received (sizeof buf).
  258. * @return: Returns zero in case of success else -EIO.
  259. */
  260. static int st33zp24_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
  261. {
  262. struct tpm_chip *chip = dev_get_priv(dev);
  263. int size, 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) {
  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. };