tpm_i2c_atmel.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ATMEL I2C TPM AT97SC3204T
  4. *
  5. * Copyright (C) 2012 V Lab Technologies
  6. * Teddy Reed <[email protected]>
  7. * Copyright (C) 2013, Obsidian Research Corp.
  8. * Jason Gunthorpe <[email protected]>
  9. * Device driver for ATMEL I2C TPMs.
  10. *
  11. * Teddy Reed determined the basic I2C command flow, unlike other I2C TPM
  12. * devices the raw TCG formatted TPM command data is written via I2C and then
  13. * raw TCG formatted TPM command data is returned via I2C.
  14. *
  15. * TGC status/locality/etc functions seen in the LPC implementation do not
  16. * seem to be present.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include "tpm.h"
  24. #define I2C_DRIVER_NAME "tpm_i2c_atmel"
  25. #define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
  26. #define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
  27. #define ATMEL_STS_OK 1
  28. struct priv_data {
  29. size_t len;
  30. /* This is the amount we read on the first try. 25 was chosen to fit a
  31. * fair number of read responses in the buffer so a 2nd retry can be
  32. * avoided in small message cases. */
  33. u8 buffer[sizeof(struct tpm_header) + 25];
  34. };
  35. static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
  36. {
  37. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  38. struct i2c_client *client = to_i2c_client(chip->dev.parent);
  39. s32 status;
  40. priv->len = 0;
  41. if (len <= 2)
  42. return -EIO;
  43. status = i2c_master_send(client, buf, len);
  44. dev_dbg(&chip->dev,
  45. "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
  46. (int)min_t(size_t, 64, len), buf, len, status);
  47. if (status < 0)
  48. return status;
  49. /* The upper layer does not support incomplete sends. */
  50. if (status != len)
  51. return -E2BIG;
  52. return 0;
  53. }
  54. static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  55. {
  56. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  57. struct i2c_client *client = to_i2c_client(chip->dev.parent);
  58. struct tpm_header *hdr = (struct tpm_header *)priv->buffer;
  59. u32 expected_len;
  60. int rc;
  61. if (priv->len == 0)
  62. return -EIO;
  63. /* Get the message size from the message header, if we didn't get the
  64. * whole message in read_status then we need to re-read the
  65. * message. */
  66. expected_len = be32_to_cpu(hdr->length);
  67. if (expected_len > count)
  68. return -ENOMEM;
  69. if (priv->len >= expected_len) {
  70. dev_dbg(&chip->dev,
  71. "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
  72. (int)min_t(size_t, 64, expected_len), buf, count,
  73. expected_len);
  74. memcpy(buf, priv->buffer, expected_len);
  75. return expected_len;
  76. }
  77. rc = i2c_master_recv(client, buf, expected_len);
  78. dev_dbg(&chip->dev,
  79. "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
  80. (int)min_t(size_t, 64, expected_len), buf, count,
  81. expected_len);
  82. return rc;
  83. }
  84. static void i2c_atmel_cancel(struct tpm_chip *chip)
  85. {
  86. dev_err(&chip->dev, "TPM operation cancellation was requested, but is not supported");
  87. }
  88. static u8 i2c_atmel_read_status(struct tpm_chip *chip)
  89. {
  90. struct priv_data *priv = dev_get_drvdata(&chip->dev);
  91. struct i2c_client *client = to_i2c_client(chip->dev.parent);
  92. int rc;
  93. /* The TPM fails the I2C read until it is ready, so we do the entire
  94. * transfer here and buffer it locally. This way the common code can
  95. * properly handle the timeouts. */
  96. priv->len = 0;
  97. memset(priv->buffer, 0, sizeof(priv->buffer));
  98. /* Once the TPM has completed the command the command remains readable
  99. * until another command is issued. */
  100. rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
  101. dev_dbg(&chip->dev,
  102. "%s: sts=%d", __func__, rc);
  103. if (rc <= 0)
  104. return 0;
  105. priv->len = rc;
  106. return ATMEL_STS_OK;
  107. }
  108. static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
  109. {
  110. return false;
  111. }
  112. static const struct tpm_class_ops i2c_atmel = {
  113. .flags = TPM_OPS_AUTO_STARTUP,
  114. .status = i2c_atmel_read_status,
  115. .recv = i2c_atmel_recv,
  116. .send = i2c_atmel_send,
  117. .cancel = i2c_atmel_cancel,
  118. .req_complete_mask = ATMEL_STS_OK,
  119. .req_complete_val = ATMEL_STS_OK,
  120. .req_canceled = i2c_atmel_req_canceled,
  121. };
  122. static int i2c_atmel_probe(struct i2c_client *client,
  123. const struct i2c_device_id *id)
  124. {
  125. struct tpm_chip *chip;
  126. struct device *dev = &client->dev;
  127. struct priv_data *priv;
  128. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  129. return -ENODEV;
  130. chip = tpmm_chip_alloc(dev, &i2c_atmel);
  131. if (IS_ERR(chip))
  132. return PTR_ERR(chip);
  133. priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
  134. if (!priv)
  135. return -ENOMEM;
  136. /* Default timeouts */
  137. chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  138. chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
  139. chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  140. chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  141. dev_set_drvdata(&chip->dev, priv);
  142. /* There is no known way to probe for this device, and all version
  143. * information seems to be read via TPM commands. Thus we rely on the
  144. * TPM startup process in the common code to detect the device. */
  145. return tpm_chip_register(chip);
  146. }
  147. static void i2c_atmel_remove(struct i2c_client *client)
  148. {
  149. struct device *dev = &(client->dev);
  150. struct tpm_chip *chip = dev_get_drvdata(dev);
  151. tpm_chip_unregister(chip);
  152. }
  153. static const struct i2c_device_id i2c_atmel_id[] = {
  154. {I2C_DRIVER_NAME, 0},
  155. {}
  156. };
  157. MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
  158. #ifdef CONFIG_OF
  159. static const struct of_device_id i2c_atmel_of_match[] = {
  160. {.compatible = "atmel,at97sc3204t"},
  161. {},
  162. };
  163. MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
  164. #endif
  165. static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
  166. static struct i2c_driver i2c_atmel_driver = {
  167. .id_table = i2c_atmel_id,
  168. .probe = i2c_atmel_probe,
  169. .remove = i2c_atmel_remove,
  170. .driver = {
  171. .name = I2C_DRIVER_NAME,
  172. .pm = &i2c_atmel_pm_ops,
  173. .of_match_table = of_match_ptr(i2c_atmel_of_match),
  174. },
  175. };
  176. module_i2c_driver(i2c_atmel_driver);
  177. MODULE_AUTHOR("Jason Gunthorpe <[email protected]>");
  178. MODULE_DESCRIPTION("Atmel TPM I2C Driver");
  179. MODULE_LICENSE("GPL");