tpm_i2c_atmel.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * ATMEL I2C TPM AT97SC3204T
  3. *
  4. * Copyright (C) 2012 V Lab Technologies
  5. * Teddy Reed <teddy@prosauce.org>
  6. * Copyright (C) 2013, Obsidian Research Corp.
  7. * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
  8. * Device driver for ATMEL I2C TPMs.
  9. *
  10. * Teddy Reed determined the basic I2C command flow, unlike other I2C TPM
  11. * devices the raw TCG formatted TPM command data is written via I2C and then
  12. * raw TCG formatted TPM command data is returned via I2C.
  13. *
  14. * TGC status/locality/etc functions seen in the LPC implementation do not
  15. * seem to be present.
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see http://www.gnu.org/licenses/>.
  29. */
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/slab.h>
  34. #include <linux/i2c.h>
  35. #include "tpm.h"
  36. #define I2C_DRIVER_NAME "tpm_i2c_atmel"
  37. #define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
  38. #define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
  39. #define ATMEL_STS_OK 1
  40. struct priv_data {
  41. size_t len;
  42. /* This is the amount we read on the first try. 25 was chosen to fit a
  43. * fair number of read responses in the buffer so a 2nd retry can be
  44. * avoided in small message cases. */
  45. u8 buffer[sizeof(struct tpm_output_header) + 25];
  46. };
  47. static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
  48. {
  49. struct priv_data *priv = chip->vendor.priv;
  50. struct i2c_client *client = to_i2c_client(chip->dev);
  51. s32 status;
  52. priv->len = 0;
  53. if (len <= 2)
  54. return -EIO;
  55. status = i2c_master_send(client, buf, len);
  56. dev_dbg(chip->dev,
  57. "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
  58. (int)min_t(size_t, 64, len), buf, len, status);
  59. return status;
  60. }
  61. static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  62. {
  63. struct priv_data *priv = chip->vendor.priv;
  64. struct i2c_client *client = to_i2c_client(chip->dev);
  65. struct tpm_output_header *hdr =
  66. (struct tpm_output_header *)priv->buffer;
  67. u32 expected_len;
  68. int rc;
  69. if (priv->len == 0)
  70. return -EIO;
  71. /* Get the message size from the message header, if we didn't get the
  72. * whole message in read_status then we need to re-read the
  73. * message. */
  74. expected_len = be32_to_cpu(hdr->length);
  75. if (expected_len > count)
  76. return -ENOMEM;
  77. if (priv->len >= expected_len) {
  78. dev_dbg(chip->dev,
  79. "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
  80. (int)min_t(size_t, 64, expected_len), buf, count,
  81. expected_len);
  82. memcpy(buf, priv->buffer, expected_len);
  83. return expected_len;
  84. }
  85. rc = i2c_master_recv(client, buf, expected_len);
  86. dev_dbg(chip->dev,
  87. "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
  88. (int)min_t(size_t, 64, expected_len), buf, count,
  89. expected_len);
  90. return rc;
  91. }
  92. static void i2c_atmel_cancel(struct tpm_chip *chip)
  93. {
  94. dev_err(chip->dev, "TPM operation cancellation was requested, but is not supported");
  95. }
  96. static u8 i2c_atmel_read_status(struct tpm_chip *chip)
  97. {
  98. struct priv_data *priv = chip->vendor.priv;
  99. struct i2c_client *client = to_i2c_client(chip->dev);
  100. int rc;
  101. /* The TPM fails the I2C read until it is ready, so we do the entire
  102. * transfer here and buffer it locally. This way the common code can
  103. * properly handle the timeouts. */
  104. priv->len = 0;
  105. memset(priv->buffer, 0, sizeof(priv->buffer));
  106. /* Once the TPM has completed the command the command remains readable
  107. * until another command is issued. */
  108. rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
  109. dev_dbg(chip->dev,
  110. "%s: sts=%d", __func__, rc);
  111. if (rc <= 0)
  112. return 0;
  113. priv->len = rc;
  114. return ATMEL_STS_OK;
  115. }
  116. static const struct file_operations i2c_atmel_ops = {
  117. .owner = THIS_MODULE,
  118. .llseek = no_llseek,
  119. .open = tpm_open,
  120. .read = tpm_read,
  121. .write = tpm_write,
  122. .release = tpm_release,
  123. };
  124. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  125. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  126. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  127. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  128. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  129. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
  130. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  131. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  132. static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
  133. static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
  134. static struct attribute *i2c_atmel_attrs[] = {
  135. &dev_attr_pubek.attr,
  136. &dev_attr_pcrs.attr,
  137. &dev_attr_enabled.attr,
  138. &dev_attr_active.attr,
  139. &dev_attr_owned.attr,
  140. &dev_attr_temp_deactivated.attr,
  141. &dev_attr_caps.attr,
  142. &dev_attr_cancel.attr,
  143. &dev_attr_durations.attr,
  144. &dev_attr_timeouts.attr,
  145. NULL,
  146. };
  147. static struct attribute_group i2c_atmel_attr_grp = {
  148. .attrs = i2c_atmel_attrs
  149. };
  150. static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
  151. {
  152. return 0;
  153. }
  154. static const struct tpm_vendor_specific i2c_atmel = {
  155. .status = i2c_atmel_read_status,
  156. .recv = i2c_atmel_recv,
  157. .send = i2c_atmel_send,
  158. .cancel = i2c_atmel_cancel,
  159. .req_complete_mask = ATMEL_STS_OK,
  160. .req_complete_val = ATMEL_STS_OK,
  161. .req_canceled = i2c_atmel_req_canceled,
  162. .attr_group = &i2c_atmel_attr_grp,
  163. .miscdev.fops = &i2c_atmel_ops,
  164. };
  165. static int i2c_atmel_probe(struct i2c_client *client,
  166. const struct i2c_device_id *id)
  167. {
  168. int rc;
  169. struct tpm_chip *chip;
  170. struct device *dev = &client->dev;
  171. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  172. return -ENODEV;
  173. chip = tpm_register_hardware(dev, &i2c_atmel);
  174. if (!chip) {
  175. dev_err(dev, "%s() error in tpm_register_hardware\n", __func__);
  176. return -ENODEV;
  177. }
  178. chip->vendor.priv = devm_kzalloc(dev, sizeof(struct priv_data),
  179. GFP_KERNEL);
  180. /* Default timeouts */
  181. chip->vendor.timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  182. chip->vendor.timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
  183. chip->vendor.timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  184. chip->vendor.timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
  185. chip->vendor.irq = 0;
  186. /* There is no known way to probe for this device, and all version
  187. * information seems to be read via TPM commands. Thus we rely on the
  188. * TPM startup process in the common code to detect the device. */
  189. if (tpm_get_timeouts(chip)) {
  190. rc = -ENODEV;
  191. goto out_err;
  192. }
  193. if (tpm_do_selftest(chip)) {
  194. rc = -ENODEV;
  195. goto out_err;
  196. }
  197. return 0;
  198. out_err:
  199. tpm_dev_vendor_release(chip);
  200. tpm_remove_hardware(chip->dev);
  201. return rc;
  202. }
  203. static int i2c_atmel_remove(struct i2c_client *client)
  204. {
  205. struct device *dev = &(client->dev);
  206. struct tpm_chip *chip = dev_get_drvdata(dev);
  207. if (chip)
  208. tpm_dev_vendor_release(chip);
  209. tpm_remove_hardware(dev);
  210. kfree(chip);
  211. return 0;
  212. }
  213. static const struct i2c_device_id i2c_atmel_id[] = {
  214. {I2C_DRIVER_NAME, 0},
  215. {}
  216. };
  217. MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
  218. #ifdef CONFIG_OF
  219. static const struct of_device_id i2c_atmel_of_match[] = {
  220. {.compatible = "atmel,at97sc3204t"},
  221. {},
  222. };
  223. MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
  224. #endif
  225. static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
  226. static struct i2c_driver i2c_atmel_driver = {
  227. .id_table = i2c_atmel_id,
  228. .probe = i2c_atmel_probe,
  229. .remove = i2c_atmel_remove,
  230. .driver = {
  231. .name = I2C_DRIVER_NAME,
  232. .owner = THIS_MODULE,
  233. .pm = &i2c_atmel_pm_ops,
  234. .of_match_table = of_match_ptr(i2c_atmel_of_match),
  235. },
  236. };
  237. module_i2c_driver(i2c_atmel_driver);
  238. MODULE_AUTHOR("Jason Gunthorpe <jgunthorpe@obsidianresearch.com>");
  239. MODULE_DESCRIPTION("Atmel TPM I2C Driver");
  240. MODULE_LICENSE("GPL");