Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ATMEL I2C TPM AT97SC3204T
0004  *
0005  * Copyright (C) 2012 V Lab Technologies
0006  *  Teddy Reed <teddy@prosauce.org>
0007  * Copyright (C) 2013, Obsidian Research Corp.
0008  *  Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
0009  * Device driver for ATMEL I2C TPMs.
0010  *
0011  * Teddy Reed determined the basic I2C command flow, unlike other I2C TPM
0012  * devices the raw TCG formatted TPM command data is written via I2C and then
0013  * raw TCG formatted TPM command data is returned via I2C.
0014  *
0015  * TGC status/locality/etc functions seen in the LPC implementation do not
0016  * seem to be present.
0017  */
0018 #include <linux/init.h>
0019 #include <linux/module.h>
0020 #include <linux/moduleparam.h>
0021 #include <linux/slab.h>
0022 #include <linux/i2c.h>
0023 #include "tpm.h"
0024 
0025 #define I2C_DRIVER_NAME "tpm_i2c_atmel"
0026 
0027 #define TPM_I2C_SHORT_TIMEOUT  750     /* ms */
0028 #define TPM_I2C_LONG_TIMEOUT   2000    /* 2 sec */
0029 
0030 #define ATMEL_STS_OK 1
0031 
0032 struct priv_data {
0033     size_t len;
0034     /* This is the amount we read on the first try. 25 was chosen to fit a
0035      * fair number of read responses in the buffer so a 2nd retry can be
0036      * avoided in small message cases. */
0037     u8 buffer[sizeof(struct tpm_header) + 25];
0038 };
0039 
0040 static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
0041 {
0042     struct priv_data *priv = dev_get_drvdata(&chip->dev);
0043     struct i2c_client *client = to_i2c_client(chip->dev.parent);
0044     s32 status;
0045 
0046     priv->len = 0;
0047 
0048     if (len <= 2)
0049         return -EIO;
0050 
0051     status = i2c_master_send(client, buf, len);
0052 
0053     dev_dbg(&chip->dev,
0054         "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
0055         (int)min_t(size_t, 64, len), buf, len, status);
0056 
0057     if (status < 0)
0058         return status;
0059 
0060     /* The upper layer does not support incomplete sends. */
0061     if (status != len)
0062         return -E2BIG;
0063 
0064     return 0;
0065 }
0066 
0067 static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
0068 {
0069     struct priv_data *priv = dev_get_drvdata(&chip->dev);
0070     struct i2c_client *client = to_i2c_client(chip->dev.parent);
0071     struct tpm_header *hdr = (struct tpm_header *)priv->buffer;
0072     u32 expected_len;
0073     int rc;
0074 
0075     if (priv->len == 0)
0076         return -EIO;
0077 
0078     /* Get the message size from the message header, if we didn't get the
0079      * whole message in read_status then we need to re-read the
0080      * message. */
0081     expected_len = be32_to_cpu(hdr->length);
0082     if (expected_len > count)
0083         return -ENOMEM;
0084 
0085     if (priv->len >= expected_len) {
0086         dev_dbg(&chip->dev,
0087             "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
0088             (int)min_t(size_t, 64, expected_len), buf, count,
0089             expected_len);
0090         memcpy(buf, priv->buffer, expected_len);
0091         return expected_len;
0092     }
0093 
0094     rc = i2c_master_recv(client, buf, expected_len);
0095     dev_dbg(&chip->dev,
0096         "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
0097         (int)min_t(size_t, 64, expected_len), buf, count,
0098         expected_len);
0099     return rc;
0100 }
0101 
0102 static void i2c_atmel_cancel(struct tpm_chip *chip)
0103 {
0104     dev_err(&chip->dev, "TPM operation cancellation was requested, but is not supported");
0105 }
0106 
0107 static u8 i2c_atmel_read_status(struct tpm_chip *chip)
0108 {
0109     struct priv_data *priv = dev_get_drvdata(&chip->dev);
0110     struct i2c_client *client = to_i2c_client(chip->dev.parent);
0111     int rc;
0112 
0113     /* The TPM fails the I2C read until it is ready, so we do the entire
0114      * transfer here and buffer it locally. This way the common code can
0115      * properly handle the timeouts. */
0116     priv->len = 0;
0117     memset(priv->buffer, 0, sizeof(priv->buffer));
0118 
0119 
0120     /* Once the TPM has completed the command the command remains readable
0121      * until another command is issued. */
0122     rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
0123     dev_dbg(&chip->dev,
0124         "%s: sts=%d", __func__, rc);
0125     if (rc <= 0)
0126         return 0;
0127 
0128     priv->len = rc;
0129 
0130     return ATMEL_STS_OK;
0131 }
0132 
0133 static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
0134 {
0135     return false;
0136 }
0137 
0138 static const struct tpm_class_ops i2c_atmel = {
0139     .flags = TPM_OPS_AUTO_STARTUP,
0140     .status = i2c_atmel_read_status,
0141     .recv = i2c_atmel_recv,
0142     .send = i2c_atmel_send,
0143     .cancel = i2c_atmel_cancel,
0144     .req_complete_mask = ATMEL_STS_OK,
0145     .req_complete_val = ATMEL_STS_OK,
0146     .req_canceled = i2c_atmel_req_canceled,
0147 };
0148 
0149 static int i2c_atmel_probe(struct i2c_client *client,
0150                const struct i2c_device_id *id)
0151 {
0152     struct tpm_chip *chip;
0153     struct device *dev = &client->dev;
0154     struct priv_data *priv;
0155 
0156     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0157         return -ENODEV;
0158 
0159     chip = tpmm_chip_alloc(dev, &i2c_atmel);
0160     if (IS_ERR(chip))
0161         return PTR_ERR(chip);
0162 
0163     priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
0164     if (!priv)
0165         return -ENOMEM;
0166 
0167     /* Default timeouts */
0168     chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0169     chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
0170     chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0171     chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0172 
0173     dev_set_drvdata(&chip->dev, priv);
0174 
0175     /* There is no known way to probe for this device, and all version
0176      * information seems to be read via TPM commands. Thus we rely on the
0177      * TPM startup process in the common code to detect the device. */
0178 
0179     return tpm_chip_register(chip);
0180 }
0181 
0182 static int i2c_atmel_remove(struct i2c_client *client)
0183 {
0184     struct device *dev = &(client->dev);
0185     struct tpm_chip *chip = dev_get_drvdata(dev);
0186     tpm_chip_unregister(chip);
0187     return 0;
0188 }
0189 
0190 static const struct i2c_device_id i2c_atmel_id[] = {
0191     {I2C_DRIVER_NAME, 0},
0192     {}
0193 };
0194 MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
0195 
0196 #ifdef CONFIG_OF
0197 static const struct of_device_id i2c_atmel_of_match[] = {
0198     {.compatible = "atmel,at97sc3204t"},
0199     {},
0200 };
0201 MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
0202 #endif
0203 
0204 static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
0205 
0206 static struct i2c_driver i2c_atmel_driver = {
0207     .id_table = i2c_atmel_id,
0208     .probe = i2c_atmel_probe,
0209     .remove = i2c_atmel_remove,
0210     .driver = {
0211         .name = I2C_DRIVER_NAME,
0212         .pm = &i2c_atmel_pm_ops,
0213         .of_match_table = of_match_ptr(i2c_atmel_of_match),
0214     },
0215 };
0216 
0217 module_i2c_driver(i2c_atmel_driver);
0218 
0219 MODULE_AUTHOR("Jason Gunthorpe <jgunthorpe@obsidianresearch.com>");
0220 MODULE_DESCRIPTION("Atmel TPM I2C Driver");
0221 MODULE_LICENSE("GPL");