tpm_tis_st33zp24_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * STMicroelectronics TPM ST33ZP24 I2C UBOOT driver
  3. *
  4. * Copyright (C) 2016 STMicroelectronics
  5. *
  6. * Description: Device driver for ST33ZP24 I2C TPM TCG.
  7. *
  8. * This device driver implements the TPM interface as defined in
  9. * the TCG TPM Interface Spec version 1.21, revision 1.0 and the
  10. * STMicroelectronics Protocol Stack Specification version 1.2.0.
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <fdtdec.h>
  17. #include <i2c.h>
  18. #include <tpm.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, expected;
  263. if (!chip)
  264. return -ENODEV;
  265. if (count < TPM_HEADER_SIZE) {
  266. size = -EIO;
  267. goto out;
  268. }
  269. size = st33zp24_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
  270. if (size < TPM_HEADER_SIZE) {
  271. debug("TPM error, unable to read header\n");
  272. goto out;
  273. }
  274. expected = get_unaligned_be32(buf + 2);
  275. if (expected > count) {
  276. size = -EIO;
  277. goto out;
  278. }
  279. size += st33zp24_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
  280. expected - TPM_HEADER_SIZE);
  281. if (size < expected) {
  282. debug("TPM error, unable to read remaining bytes of result\n");
  283. size = -EIO;
  284. goto out;
  285. }
  286. out:
  287. st33zp24_i2c_cancel(dev);
  288. st33zp24_i2c_release_locality(dev);
  289. return size;
  290. }
  291. /*
  292. * st33zp24_i2c_send send TPM commands through TPM phy.
  293. * @param: chip, tpm_chip description.
  294. * @param: buf, the buffer to send.
  295. * @param: len, the number of bytes to send.
  296. * @return: Returns zero in case of success else the negative error code.
  297. */
  298. static int st33zp24_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
  299. {
  300. struct tpm_chip *chip = dev_get_priv(dev);
  301. u32 i, size;
  302. int burstcnt, ret, status;
  303. u8 data, tpm_stat;
  304. if (!chip)
  305. return -ENODEV;
  306. if (len < TPM_HEADER_SIZE)
  307. return -EIO;
  308. ret = st33zp24_i2c_request_locality(dev);
  309. if (ret < 0)
  310. return ret;
  311. tpm_stat = st33zp24_i2c_status(dev);
  312. if ((tpm_stat & TPM_STS_COMMAND_READY) == 0) {
  313. st33zp24_i2c_cancel(dev);
  314. if (st33zp24_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
  315. chip->timeout_b, &status) < 0) {
  316. ret = -ETIME;
  317. goto out_err;
  318. }
  319. }
  320. for (i = 0; i < len - 1;) {
  321. burstcnt = st33zp24_i2c_get_burstcount(dev);
  322. if (burstcnt < 0)
  323. return burstcnt;
  324. size = min_t(int, len - i - 1, burstcnt);
  325. ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + i, size);
  326. if (ret < 0)
  327. goto out_err;
  328. i += size;
  329. }
  330. tpm_stat = st33zp24_i2c_status(dev);
  331. if ((tpm_stat & TPM_STS_DATA_EXPECT) == 0) {
  332. ret = -EIO;
  333. goto out_err;
  334. }
  335. ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + len - 1, 1);
  336. if (ret < 0)
  337. goto out_err;
  338. tpm_stat = st33zp24_i2c_status(dev);
  339. if ((tpm_stat & TPM_STS_DATA_EXPECT) != 0) {
  340. ret = -EIO;
  341. goto out_err;
  342. }
  343. data = TPM_STS_GO;
  344. ret = st33zp24_i2c_write(dev, TPM_STS, &data, 1);
  345. if (ret < 0)
  346. goto out_err;
  347. return len;
  348. out_err:
  349. st33zp24_i2c_cancel(dev);
  350. st33zp24_i2c_release_locality(dev);
  351. return ret;
  352. }
  353. static int st33zp24_i2c_cleanup(struct udevice *dev)
  354. {
  355. st33zp24_i2c_cancel(dev);
  356. /*
  357. * The TPM needs some time to clean up here,
  358. * so we sleep rather than keeping the bus busy
  359. */
  360. mdelay(2);
  361. st33zp24_i2c_release_locality(dev);
  362. return 0;
  363. }
  364. static int st33zp24_i2c_init(struct udevice *dev)
  365. {
  366. struct tpm_chip *chip = dev_get_priv(dev);
  367. chip->is_open = 1;
  368. /* Default timeouts - these could move to the device tree */
  369. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  370. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  371. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  372. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  373. chip->locality = LOCALITY0;
  374. /*
  375. * A timeout query to TPM can be placed here.
  376. * Standard timeout values are used so far
  377. */
  378. return 0;
  379. }
  380. static int st33zp24_i2c_open(struct udevice *dev)
  381. {
  382. struct tpm_chip *chip = dev_get_priv(dev);
  383. int rc;
  384. debug("%s: start\n", __func__);
  385. if (chip->is_open)
  386. return -EBUSY;
  387. rc = st33zp24_i2c_init(dev);
  388. if (rc < 0)
  389. chip->is_open = 0;
  390. return rc;
  391. }
  392. static int st33zp24_i2c_close(struct udevice *dev)
  393. {
  394. struct tpm_chip *chip = dev_get_priv(dev);
  395. if (chip->is_open) {
  396. st33zp24_i2c_release_locality(dev);
  397. chip->is_open = 0;
  398. chip->vend_dev = 0;
  399. }
  400. return 0;
  401. }
  402. static int st33zp24_i2c_get_desc(struct udevice *dev, char *buf, int size)
  403. {
  404. struct tpm_chip *chip = dev_get_priv(dev);
  405. if (size < 50)
  406. return -ENOSPC;
  407. return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
  408. chip->is_open ? "open" : "closed",
  409. dev->name,
  410. chip->vend_dev >> 16);
  411. }
  412. static const struct tpm_ops st33zp24_i2c_tpm_ops = {
  413. .open = st33zp24_i2c_open,
  414. .close = st33zp24_i2c_close,
  415. .recv = st33zp24_i2c_recv,
  416. .send = st33zp24_i2c_send,
  417. .cleanup = st33zp24_i2c_cleanup,
  418. .get_desc = st33zp24_i2c_get_desc,
  419. };
  420. static int st33zp24_i2c_probe(struct udevice *dev)
  421. {
  422. struct tpm_chip *chip = dev_get_priv(dev);
  423. /* Default timeouts */
  424. chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
  425. chip->timeout_b = TIS_LONG_TIMEOUT_MS;
  426. chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
  427. chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
  428. chip->locality = LOCALITY0;
  429. i2c_set_chip_offset_len(dev, 0);
  430. debug("ST33ZP24 I2C TPM from STMicroelectronics found\n");
  431. return 0;
  432. }
  433. static int st33zp24_i2c_remove(struct udevice *dev)
  434. {
  435. st33zp24_i2c_release_locality(dev);
  436. return 0;
  437. }
  438. static const struct udevice_id st33zp24_i2c_ids[] = {
  439. { .compatible = "st,st33zp24-i2c" },
  440. { }
  441. };
  442. U_BOOT_DRIVER(st33zp24_i2c) = {
  443. .name = "st33zp24-i2c",
  444. .id = UCLASS_TPM,
  445. .of_match = of_match_ptr(st33zp24_i2c_ids),
  446. .probe = st33zp24_i2c_probe,
  447. .remove = st33zp24_i2c_remove,
  448. .ops = &st33zp24_i2c_tpm_ops,
  449. .priv_auto_alloc_size = sizeof(struct tpm_chip),
  450. };