drivers/tpm/tpm.c is a TPM core driver port from Linux.
So far in u-boot only infineon i2c driver is using it but it could fit
for others...

Introduce a new tpm uclass so that every TPM driver can register against it and
and take benefit of common functions and data such as tpm_transmit,
tpm_register_hardware & tpm_remove_hardware.
Finally tis_init, tis_open, tis_close, tis_sendrecv are using ops allowing
to introduce proprietary instructions.
Also this patch convert tpm_i2c_infineon for using this tpm uclass.

Signed-off-by: Christophe Ricard <[email protected]>
---

 README                                      |   8 +-
 drivers/tpm/Makefile                        |   2 +-
 drivers/tpm/tpm.c                           | 275 +++++++---------------------
 drivers/tpm/tpm_i2c_infineon.c              | 271 ++++++++++++++++-----------
 drivers/tpm/tpm_private.h                   |  23 ++-
 include/dm/platform_data/tpm_i2c_infineon.h |  23 +++
 include/dm/uclass-id.h                      |   1 +
 7 files changed, 270 insertions(+), 333 deletions(-)
 create mode 100644 include/dm/platform_data/tpm_i2c_infineon.h

diff --git a/README b/README
index a563aa1..506ff6c 100644
--- a/README
+++ b/README
@@ -1489,19 +1489,13 @@ The following options need to be configured:
                Support for PWM modul on the imx6.
 
 - TPM Support:
-               CONFIG_TPM
+               CONFIG_DM_TPM
                Support TPM devices.
 
                CONFIG_TPM_I2C_INFINEON
                Support for infineon i2c bus TPM devices. Only one device
                per system is supported at this time.
 
-                       CONFIG_TPM_TIS_I2C_BUS_NUMBER
-                       Define the the i2c bus number for the TPM device
-
-                       CONFIG_TPM_TIS_I2C_SLAVE_ADDRESS
-                       Define the TPM's address on the i2c bus
-
                        CONFIG_TPM_TIS_I2C_BURST_LIMITATION
                        Define the burst count bytes upper limit
 
diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
index fea246f..bd2cd6d 100644
--- a/drivers/tpm/Makefile
+++ b/drivers/tpm/Makefile
@@ -5,7 +5,7 @@
 
 # TODO: Merge tpm_tis_lpc.c with tpm.c
 obj-$(CONFIG_TPM_ATMEL_TWI) += tpm_atmel_twi.o
-obj-$(CONFIG_TPM_TIS_I2C) += tpm.o
+obj-$(CONFIG_DM_TPM) += tpm.o
 obj-$(CONFIG_TPM_INFINEON_I2C) += tpm_i2c_infineon.o
 obj-$(CONFIG_TPM_TIS_LPC) += tpm_tis_lpc.o
 obj-$(CONFIG_TPM_TIS_SANDBOX) += tpm_tis_sandbox.o
diff --git a/drivers/tpm/tpm.c b/drivers/tpm/tpm.c
index a650892..caf208d 100644
--- a/drivers/tpm/tpm.c
+++ b/drivers/tpm/tpm.c
@@ -36,8 +36,6 @@
 #include <common.h>
 #include <dm.h>
 #include <linux/compiler.h>
-#include <fdtdec.h>
-#include <i2c.h>
 #include <tpm.h>
 #include <asm-generic/errno.h>
 #include <linux/types.h>
@@ -47,21 +45,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-/* TPM configuration */
-struct tpm {
-#ifdef CONFIG_DM_I2C
-       struct udevice *dev;
-#else
-       int i2c_bus;
-       int slave_addr;
-       int old_bus;
-#endif
-       char inited;
-} tpm;
-
-/* Global structure for tpm chip data */
-static struct tpm_chip g_chip;
-
 enum tpm_duration {
        TPM_SHORT = 0,
        TPM_MEDIUM = 1,
@@ -375,14 +358,12 @@ static unsigned long tpm_calc_ordinal_duration(struct 
tpm_chip *chip,
                return duration;
 }
 
-static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
+static ssize_t tpm_transmit(struct tpm_chip *chip, const unsigned char *buf, 
size_t bufsiz)
 {
        int rc;
        u32 count, ordinal;
        unsigned long start, stop;
 
-       struct tpm_chip *chip = &g_chip;
-
        /* switch endianess: big->little */
        count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
        ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
@@ -441,233 +422,102 @@ out:
        return rc;
 }
 
-#ifdef CONFIG_DM_I2C
-static int tpm_open_dev(struct udevice *dev)
-{
-       int rc;
-
-       debug("%s: start\n", __func__);
-       if (g_chip.is_open)
-               return -EBUSY;
-       rc = tpm_vendor_init_dev(dev);
-       if (rc < 0)
-               g_chip.is_open = 0;
-       return rc;
-}
-#else
-static int tpm_open(uint32_t dev_addr)
-{
-       int rc;
-
-       if (g_chip.is_open)
-               return -EBUSY;
-       rc = tpm_vendor_init(dev_addr);
-       if (rc < 0)
-               g_chip.is_open = 0;
-       return rc;
-}
-#endif
-static void tpm_close(void)
-{
-       if (g_chip.is_open) {
-               tpm_vendor_cleanup(&g_chip);
-               g_chip.is_open = 0;
-       }
-}
-
-static int tpm_select(void)
-{
-#ifndef CONFIG_DM_I2C
-       int ret;
-
-       tpm.old_bus = i2c_get_bus_num();
-       if (tpm.old_bus != tpm.i2c_bus) {
-               ret = i2c_set_bus_num(tpm.i2c_bus);
-               if (ret) {
-                       debug("%s: Fail to set i2c bus %d\n", __func__,
-                             tpm.i2c_bus);
-                       return -1;
-               }
-       }
-#endif
-       return 0;
-}
-
-static int tpm_deselect(void)
+void tpm_remove_hardware(struct udevice *dev)
 {
-#ifndef CONFIG_DM_I2C
-       int ret;
-
-       if (tpm.old_bus != i2c_get_bus_num()) {
-               ret = i2c_set_bus_num(tpm.old_bus);
-               if (ret) {
-                       debug("%s: Fail to restore i2c bus %d\n",
-                             __func__, tpm.old_bus);
-                       return -1;
-               }
-       }
-       tpm.old_bus = -1;
-#endif
-       return 0;
 }
 
-/**
- * Decode TPM configuration.
- *
- * @param dev  Returns a configuration of TPM device
- * @return 0 if ok, -1 on error
- */
-static int tpm_decode_config(struct tpm *dev)
+struct tpm_chip *tpm_register_hardware(struct udevice *dev,
+                       const struct tpm_vendor_specific *entry)
 {
-       const void *blob = gd->fdt_blob;
-       int parent;
-       int node;
-
-       node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
-       if (node < 0) {
-               node = fdtdec_next_compatible(blob, 0,
-                               COMPAT_INFINEON_SLB9645_TPM);
-       }
-       if (node < 0) {
-               debug("%s: Node not found\n", __func__);
-               return -1;
-       }
-       parent = fdt_parent_offset(blob, node);
-       if (parent < 0) {
-               debug("%s: Cannot find node parent\n", __func__);
-               return -1;
-       }
-#ifdef CONFIG_DM_I2C
-       struct udevice *bus;
-       int chip_addr;
-       int ret;
-
-       /*
-        * TODO([email protected]): Remove this when driver model supports
-        * TPMs
-        */
-       ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
-       if (ret) {
-               debug("Cannot find bus for node '%s: ret=%d'\n",
-                     fdt_get_name(blob, parent, NULL), ret);
-               return ret;
-       }
-
-       chip_addr = fdtdec_get_int(blob, node, "reg", -1);
-       if (chip_addr == -1) {
-               debug("Cannot find reg property for node '%s: ret=%d'\n",
-                     fdt_get_name(blob, node, NULL), ret);
-               return ret;
-       }
-       /*
-        * TODO([email protected]): Older TPMs will need to use the older method
-        * in iic_tpm_read() so the offset length needs to be 0 here.
-        */
-       ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
-       if (ret) {
-               debug("Cannot find device for node '%s: ret=%d'\n",
-                     fdt_get_name(blob, node, NULL), ret);
-               return ret;
-       }
-#else
-       int i2c_bus;
-
-       i2c_bus = i2c_get_bus_num_fdt(parent);
-       if (i2c_bus < 0)
-               return -1;
-       dev->i2c_bus = i2c_bus;
-       dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
-#endif
-
-       return 0;
-}
-
-struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
-{
-       struct tpm_chip *chip;
+       struct tpm_chip *chip = dev_get_uclass_priv(dev);
 
        /* Driver specific per-device data */
-       chip = &g_chip;
        memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
-       chip->is_open = 1;
 
        return chip;
 }
 
 int tis_init(void)
 {
-       if (tpm.inited)
-               return 0;
-
-       if (tpm_decode_config(&tpm))
-               return -1;
-
-       if (tpm_select())
-               return -1;
+       int ret;
+       struct udevice *dev;
+       const struct dm_tpm_ops *ops;
 
-#ifndef CONFIG_DM_I2C
-       /*
-        * Probe TPM twice; the first probing might fail because TPM is asleep,
-        * and the probing can wake up TPM.
-        */
-       if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
-               debug("%s: fail to probe i2c addr 0x%x\n", __func__,
-                     tpm.slave_addr);
-               return -1;
+       ret = uclass_get_device(UCLASS_TPM, 0, &dev);
+       if (ret) {
+               printf("TIS: Can't find any TPM\n");
+               return -EINVAL;
        }
-#endif
-
-       tpm_deselect();
-       debug("%s: done\n", __func__);
 
-       tpm.inited = 1;
+       ops = device_get_ops(dev);
+       if (ops && ops->init)
+               return ops->init(dev);
 
        return 0;
 }
 
 int tis_open(void)
 {
-       int rc;
-
-       if (!tpm.inited)
-               return -1;
+       int ret;
+       struct udevice *dev;
+       struct tpm_chip *chip;
+       const struct dm_tpm_ops *ops;
 
-       if (tpm_select())
-               return -1;
+       ret = uclass_get_device(UCLASS_TPM, 0, &dev);
+       if (ret) {
+               printf("TIS: Can't find any TPM\n");
+               return -EINVAL;
+       }
 
-#ifdef CONFIG_DM_I2C
-       rc = tpm_open_dev(tpm.dev);
-#else
-       rc = tpm_open(tpm.slave_addr);
-#endif
+       chip = dev_get_uclass_priv(dev);
+       chip->is_open = 1;
 
-       tpm_deselect();
+       ops = device_get_ops(dev);
+       if (ops && ops->open)
+               return ops->open(dev);
 
-       return rc;
+       return 0;
 }
 
 int tis_close(void)
 {
-       if (!tpm.inited)
-               return -1;
+       int ret;
+       struct udevice *dev;
+       struct tpm_chip *chip;
+       const struct dm_tpm_ops *ops;
 
-       if (tpm_select())
-               return -1;
+       ret = uclass_get_device(UCLASS_TPM, 0, &dev);
+       if (ret) {
+               printf("TIS: Can't find any TPM\n");
+               return -EINVAL;
+       }
 
-       tpm_close();
+       chip = dev_get_uclass_priv(dev);
+       chip->is_open = 0;
 
-       tpm_deselect();
+       ops = device_get_ops(dev);
+       if (ops && ops->close)
+               return ops->close(dev);
 
        return 0;
 }
 
 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
-               uint8_t *recvbuf, size_t *rbuf_len)
+                uint8_t *recvbuf, size_t *rbuf_len)
 {
-       int len;
+       struct udevice *dev;
+       struct tpm_chip *chip;
+       int len, ret;
        uint8_t buf[4096];
 
-       if (!tpm.inited)
+       ret = uclass_get_device(UCLASS_TPM, 0, &dev);
+       if (ret) {
+               printf("TIS: Can't find any TPM\n");
+               return -EINVAL;
+       }
+
+       chip = dev_get_uclass_priv(dev);
+       if (!chip->is_open)
                return -1;
 
        if (sizeof(buf) < sbuf_size)
@@ -675,12 +525,7 @@ int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
 
        memcpy(buf, sendbuf, sbuf_size);
 
-       if (tpm_select())
-               return -1;
-
-       len = tpm_transmit(buf, sbuf_size);
-
-       tpm_deselect();
+       len = tpm_transmit(chip, buf, sbuf_size);
 
        if (len < 10) {
                *rbuf_len = 0;
@@ -692,3 +537,9 @@ int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
 
        return 0;
 }
+
+UCLASS_DRIVER(tpm) = {
+       .id             = UCLASS_TPM,
+       .name           = "tpm",
+       .per_device_auto_alloc_size = sizeof(struct tpm_chip),
+};
diff --git a/drivers/tpm/tpm_i2c_infineon.c b/drivers/tpm/tpm_i2c_infineon.c
index ee4dfea..b39ebd2 100644
--- a/drivers/tpm/tpm_i2c_infineon.c
+++ b/drivers/tpm/tpm_i2c_infineon.c
@@ -45,6 +45,7 @@
 #include <asm-generic/errno.h>
 #include <linux/types.h>
 #include <linux/unaligned/be_byteshift.h>
+#include <dm/platform_data/tpm_i2c_infineon.h>
 
 #include "tpm_private.h"
 
@@ -123,25 +124,16 @@ static const char * const chip_name[] = {
 
 /* Structure to store I2C TPM specific stuff */
 struct tpm_dev {
-#ifdef CONFIG_DM_I2C
-       struct udevice *dev;
-#else
-       uint addr;
-#endif
+       uint slave_addr;
+       uint i2c_bus;
+       uint old_bus;
        u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)];  /* Max buffer size + addr */
        enum i2c_chip_type chip_type;
 };
 
-static struct tpm_dev tpm_dev = {
-#ifndef CONFIG_DM_I2C
-       .addr = TPM_I2C_ADDR
-#endif
-};
-
-static struct tpm_dev tpm_dev;
-
 /*
  * iic_tpm_read() - read from TPM register
+ * @chip: tpm chip to deal with
  * @addr: register address to read from
  * @buffer: provided by caller
  * @len: number of bytes to read
@@ -154,21 +146,18 @@ static struct tpm_dev tpm_dev;
  *
  * Return -EIO on error, 0 on success.
  */
-static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
+static int iic_tpm_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t len)
 {
        int rc;
        int count;
        uint32_t addrbuf = addr;
+       struct tpm_dev *tpm_dev = TPM_VPRIV(chip);
 
-       if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
+       if ((tpm_dev->chip_type == SLB9635) || (tpm_dev->chip_type == UNKNOWN)) 
{
                /* slb9635 protocol should work in both cases */
                for (count = 0; count < MAX_COUNT; count++) {
-#ifdef CONFIG_DM_I2C
-                       rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
-#else
-                       rc = i2c_write(tpm_dev.addr, 0, 0,
+                       rc = i2c_write(tpm_dev->slave_addr, 0, 0,
                                       (uchar *)&addrbuf, 1);
-#endif
                        if (rc == 0)
                                break;  /* Success, break to skip sleep */
                        udelay(SLEEP_DURATION);
@@ -182,11 +171,7 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
                 */
                for (count = 0; count < MAX_COUNT; count++) {
                        udelay(SLEEP_DURATION);
-#ifdef CONFIG_DM_I2C
-                       rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
-#else
-                       rc = i2c_read(tpm_dev.addr, 0, 0, buffer, len);
-#endif
+                       rc = i2c_read(tpm_dev->slave_addr, 0, 0, buffer, len);
                        if (rc == 0)
                                break;  /* success, break to skip sleep */
                }
@@ -199,11 +184,7 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
                 * be safe on the safe side.
                 */
                for (count = 0; count < MAX_COUNT; count++) {
-#ifdef CONFIG_DM_I2C
-                       rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
-#else
-                       rc = i2c_read(tpm_dev.addr, addr, 1, buffer, len);
-#endif
+                       rc = i2c_read(tpm_dev->slave_addr, addr, 1, buffer, 
len);
                        if (rc == 0)
                                break;  /* break here to skip sleep */
                        udelay(SLEEP_DURATION);
@@ -218,26 +199,21 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
        return 0;
 }
 
-static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
+static int iic_tpm_write_generic(struct tpm_chip *chip, u8 addr, u8 *buffer, 
size_t len,
                unsigned int sleep_time, u8 max_count)
 {
        int rc = 0;
        int count;
+       struct tpm_dev *tpm_dev = TPM_VPRIV(chip);
 
        /* Prepare send buffer */
-#ifndef CONFIG_DM_I2C
-       tpm_dev.buf[0] = addr;
-       memcpy(&(tpm_dev.buf[1]), buffer, len);
-       buffer = tpm_dev.buf;
+       tpm_dev->buf[0] = addr;
+       memcpy(&(tpm_dev->buf[1]), buffer, len);
+       buffer = tpm_dev->buf;
        len++;
-#endif
 
        for (count = 0; count < max_count; count++) {
-#ifdef CONFIG_DM_I2C
-               rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
-#else
-               rc = i2c_write(tpm_dev.addr, 0, 0, buffer, len);
-#endif
+               rc = i2c_write(tpm_dev->slave_addr, 0, 0, buffer, len);
                if (rc == 0)
                        break;  /* Success, break to skip sleep */
                udelay(sleep_time);
@@ -253,6 +229,7 @@ static int iic_tpm_write_generic(u8 addr, u8 *buffer, 
size_t len,
 
 /*
  * iic_tpm_write() - write to TPM register
+ * @chip: tpm chip to deal with
  * @addr: register address to write to
  * @buffer: containing data to be written
  * @len: number of bytes to write
@@ -267,9 +244,9 @@ static int iic_tpm_write_generic(u8 addr, u8 *buffer, 
size_t len,
  *
  * Return -EIO on error, 0 on success
  */
-static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
+static int iic_tpm_write(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t 
len)
 {
-       return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
+       return iic_tpm_write_generic(chip, addr, buffer, len, SLEEP_DURATION,
                        MAX_COUNT);
 }
 
@@ -277,9 +254,9 @@ static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
  * This function is needed especially for the cleanup situation after
  * sending TPM_READY
  */
-static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
+static int iic_tpm_write_long(struct tpm_chip *chip, u8 addr, u8 *buffer, 
size_t len)
 {
-       return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
+       return iic_tpm_write_generic(chip, addr, buffer, len, 
SLEEP_DURATION_LONG,
                        MAX_COUNT_LONG);
 }
 
@@ -289,7 +266,7 @@ static int check_locality(struct tpm_chip *chip, int loc)
        u8 buf;
        int rc;
 
-       rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
+       rc = iic_tpm_read(chip, TPM_ACCESS(loc), &buf, 1);
        if (rc < 0)
                return rc;
 
@@ -306,12 +283,12 @@ static void release_locality(struct tpm_chip *chip, int 
loc, int force)
        const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
        u8 buf;
 
-       if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
+       if (iic_tpm_read(chip, TPM_ACCESS(loc), &buf, 1) < 0)
                return;
 
        if (force || (buf & mask) == mask) {
                buf = TPM_ACCESS_ACTIVE_LOCALITY;
-               iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
+               iic_tpm_write(chip, TPM_ACCESS(loc), &buf, 1);
        }
 }
 
@@ -324,7 +301,7 @@ static int request_locality(struct tpm_chip *chip, int loc)
        if (check_locality(chip, loc) >= 0)
                return loc;  /* We already have the locality */
 
-       rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
+       rc = iic_tpm_write(chip, TPM_ACCESS(loc), &buf, 1);
        if (rc)
                return rc;
 
@@ -340,18 +317,18 @@ static int request_locality(struct tpm_chip *chip, int 
loc)
        return -1;
 }
 
-static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
+static u8 tpm_i2c_tis_status(struct tpm_chip *chip)
 {
        /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
        u8 buf;
 
-       if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
+       if (iic_tpm_read(chip, TPM_STS(chip->vendor.locality), &buf, 1) < 0)
                return 0;
        else
                return buf;
 }
 
-static void tpm_tis_i2c_ready(struct tpm_chip *chip)
+static void tpm_i2c_tis_ready(struct tpm_chip *chip)
 {
        int rc;
 
@@ -359,7 +336,7 @@ static void tpm_tis_i2c_ready(struct tpm_chip *chip)
        u8 buf = TPM_STS_COMMAND_READY;
 
        debug("%s\n", __func__);
-       rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
+       rc = iic_tpm_write_long(chip, TPM_STS(chip->vendor.locality), &buf, 1);
        if (rc)
                debug("%s: rc=%d\n", __func__, rc);
 }
@@ -377,7 +354,7 @@ static ssize_t get_burstcount(struct tpm_chip *chip)
        do {
                /* Note: STS is little endian */
                addr = TPM_STS(chip->vendor.locality) + 1;
-               if (iic_tpm_read(addr, buf, 3) < 0)
+               if (iic_tpm_read(chip, addr, buf, 3) < 0)
                        burstcnt = 0;
                else
                        burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
@@ -396,7 +373,7 @@ static int wait_for_stat(struct tpm_chip *chip, u8 mask, 
unsigned long timeout,
        unsigned long start, stop;
 
        /* Check current status */
-       *status = tpm_tis_i2c_status(chip);
+       *status = tpm_i2c_tis_status(chip);
        if ((*status & mask) == mask)
                return 0;
 
@@ -404,7 +381,7 @@ static int wait_for_stat(struct tpm_chip *chip, u8 mask, 
unsigned long timeout,
        stop = timeout;
        do {
                udelay(TPM_TIMEOUT * 1000);
-               *status = tpm_tis_i2c_status(chip);
+               *status = tpm_i2c_tis_status(chip);
                if ((*status & mask) == mask)
                        return 0;
        } while (get_timer(start) < stop);
@@ -429,7 +406,7 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, size_t 
count)
                if (burstcnt > (count - size))
                        burstcnt = count - size;
 
-               rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
+               rc = iic_tpm_read(chip, TPM_DATA_FIFO(chip->vendor.locality),
                                &(buf[size]), burstcnt);
                if (rc == 0)
                        size += burstcnt;
@@ -438,7 +415,43 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, 
size_t count)
        return size;
 }
 
-static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
+static int tpm_select(struct tpm_chip *chip)
+{
+       struct tpm_dev *tpm = TPM_VPRIV(chip);
+       int ret;
+
+       tpm->old_bus = i2c_get_bus_num();
+       if (tpm->old_bus != tpm->i2c_bus) {
+               ret = i2c_set_bus_num(tpm->i2c_bus);
+               if (ret) {
+                       debug("%s: Fail to set i2c bus %d\n", __func__,
+                             tpm->i2c_bus);
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+static int tpm_deselect(struct tpm_chip *chip)
+{
+       struct tpm_dev *tpm = TPM_VPRIV(chip);
+       int ret;
+
+       if (tpm->old_bus != i2c_get_bus_num()) {
+               ret = i2c_set_bus_num(tpm->old_bus);
+               if (ret) {
+                       debug("%s: Fail to restore i2c bus %d\n",
+                             __func__, tpm->old_bus);
+                       return -1;
+               }
+       }
+       tpm->old_bus = -1;
+
+       return 0;
+}
+
+static int tpm_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
        int size = 0;
        int expected, status;
@@ -448,6 +461,10 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 
*buf, size_t count)
                goto out;
        }
 
+       size = tpm_select(chip);
+       if (size < 0)
+               return size;
+
        /* Read first 10 bytes, including tag, paramsize, and result */
        size = recv_data(chip, buf, TPM_HEADER_SIZE);
        if (size < TPM_HEADER_SIZE) {
@@ -479,18 +496,18 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 
*buf, size_t count)
        }
 
 out:
-       tpm_tis_i2c_ready(chip);
+       tpm_i2c_tis_ready(chip);
        /*
         * The TPM needs some time to clean up here,
         * so we sleep rather than keeping the bus busy
         */
        udelay(2000);
        release_locality(chip, chip->vendor.locality, 0);
-
+       tpm_deselect(chip);
        return size;
 }
 
-static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
+static int tpm_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
 {
        int rc, status;
        size_t burstcnt;
@@ -502,12 +519,16 @@ static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 
*buf, size_t len)
        if (len > TPM_DEV_BUFSIZE)
                return -E2BIG;  /* Command is too long for our tpm, sorry */
 
+       rc = tpm_select(chip);
+       if (rc < 0)
+               return rc;
+
        if (request_locality(chip, 0) < 0)
                return -EBUSY;
 
-       status = tpm_tis_i2c_status(chip);
+       status = tpm_i2c_tis_status(chip);
        if ((status & TPM_STS_COMMAND_READY) == 0) {
-               tpm_tis_i2c_ready(chip);
+               tpm_i2c_tis_ready(chip);
                if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
                                  chip->vendor.timeout_b, &status) < 0) {
                        rc = -ETIME;
@@ -531,7 +552,7 @@ static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, 
size_t len)
                        burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
 
-               rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
+               rc = iic_tpm_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
                                &(buf[count]), burstcnt);
                if (rc == 0)
                        count += burstcnt;
@@ -554,29 +575,29 @@ static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 
*buf, size_t len)
        }
 
        /* Go and do it */
-       iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
+       iic_tpm_write(chip, TPM_STS(chip->vendor.locality), &sts, 1);
        debug("done\n");
 
        return len;
 
 out_err:
        debug("%s: out_err\n", __func__);
-       tpm_tis_i2c_ready(chip);
+       tpm_i2c_tis_ready(chip);
        /*
         * The TPM needs some time to clean up here,
         * so we sleep rather than keeping the bus busy
         */
        udelay(2000);
        release_locality(chip, chip->vendor.locality, 0);
-
+       tpm_deselect(chip);
        return rc;
 }
 
-static struct tpm_vendor_specific tpm_tis_i2c = {
-       .status = tpm_tis_i2c_status,
-       .recv = tpm_tis_i2c_recv,
-       .send = tpm_tis_i2c_send,
-       .cancel = tpm_tis_i2c_ready,
+static struct tpm_vendor_specific tpm_i2c_tis = {
+       .status = tpm_i2c_tis_status,
+       .recv = tpm_i2c_tis_recv,
+       .send = tpm_i2c_tis_send,
+       .cancel = tpm_i2c_tis_ready,
        .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
        .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
        .req_canceled = TPM_STS_COMMAND_READY,
@@ -597,17 +618,13 @@ static enum i2c_chip_type tpm_vendor_chip_type(void)
        return UNKNOWN;
 }
 
-static int tpm_vendor_init_common(void)
+static int tpm_vendor_init_common(struct tpm_chip *chip)
 {
-       struct tpm_chip *chip;
        u32 vendor;
        u32 expected_did_vid;
+       struct tpm_dev *tpm_dev = TPM_VPRIV(chip);
 
-       tpm_dev.chip_type = tpm_vendor_chip_type();
-
-       chip = tpm_register_hardware(&tpm_tis_i2c);
-       if (chip < 0)
-               return -ENODEV;
+       tpm_dev->chip_type = tpm_vendor_chip_type();
 
        /* Disable interrupts (not supported) */
        chip->vendor.irq = 0;
@@ -622,12 +639,12 @@ static int tpm_vendor_init_common(void)
                return  -ENODEV;
 
        /* Read four bytes from DID_VID register */
-       if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
+       if (iic_tpm_read(chip, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
                release_locality(chip, 0, 1);
                return -EIO;
        }
 
-       if (tpm_dev.chip_type == SLB9635) {
+       if (tpm_dev->chip_type == SLB9635) {
                vendor = be32_to_cpu(vendor);
                expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
        } else {
@@ -635,13 +652,13 @@ static int tpm_vendor_init_common(void)
                expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
        }
 
-       if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
+       if (tpm_dev->chip_type != UNKNOWN && vendor != expected_did_vid) {
                error("Vendor id did not match! ID was %08x\n", vendor);
                return -ENODEV;
        }
 
        debug("1.2 TPM (chip type %s device-id 0x%X)\n",
-             chip_name[tpm_dev.chip_type], vendor >> 16);
+             chip_name[tpm_dev->chip_type], vendor >> 16);
 
        /*
         * A timeout query to TPM can be placed here.
@@ -651,33 +668,79 @@ static int tpm_vendor_init_common(void)
        return 0;
 }
 
-#ifdef CONFIG_DM_I2C
-/* Initialisation of i2c tpm */
-int tpm_vendor_init_dev(struct udevice *dev)
-{
-       tpm_dev.dev = dev;
-       return tpm_vendor_init_common();
-}
-#else
-/* Initialisation of i2c tpm */
-int tpm_vendor_init(uint32_t dev_addr)
+static int tpm_i2c_tis_probe(struct udevice *dev)
 {
-       uint old_addr;
-       int rc = 0;
+       struct tpm_chip *chip;
+       struct tpm_dev *tpm_dev = dev_get_priv(dev);
+       struct tpm_i2c_tis_platdata *platdata = dev_get_platdata(dev);
+
+       chip = tpm_register_hardware(dev, &tpm_i2c_tis);
+       if (chip < 0)
+               return -ENODEV;
 
-       old_addr = tpm_dev.addr;
-       if (dev_addr != 0)
-               tpm_dev.addr = dev_addr;
+       TPM_VPRIV(chip) = tpm_dev;
 
-       rc = tpm_vendor_init_common();
-       if (rc)
-               tpm_dev.addr = old_addr;
+       tpm_dev->slave_addr = platdata->slave_addr;
+       tpm_dev->i2c_bus = platdata->i2c_bus;
 
-       return rc;
+       return tpm_vendor_init_common(chip);
 }
-#endif
 
-void tpm_vendor_cleanup(struct tpm_chip *chip)
+static int tpm_i2c_tis_remove(struct udevice *dev)
 {
+       struct tpm_chip *chip = dev_get_uclass_priv(dev);
+
        release_locality(chip, chip->vendor.locality, 1);
+       return 0;
 }
+
+#ifdef CONFIG_CONTROL_OF
+static const struct udevice_id tpm_i2c_tis_ids[] = {
+       { .compatible = "infineon,tpm_i2c_infineon"},
+       { .compatible = "infineon,slb9635tt"},
+       { .compatible = "infineon,slb9645tt"},
+       {},
+};
+
+static int tpm_i2c_tis_ofdata_to_platdata(struct udevice *dev)
+{
+       int parent, node, i2c_bus;
+       const void *blob = gd->fdt_blob;
+       struct tpm_tis_platdata *platdata = dev_get_platdata(dev);
+
+       node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
+       if (node < 0) {
+               node = fdtdec_next_compatible(blob, 0,
+                               COMPAT_INFINEON_SLB9645_TPM);
+       }
+       if (node < 0) {
+               debug("%s: Node not found\n", __func__);
+               return -1;
+       }
+       parent = fdt_parent_offset(blob, node);
+       if (parent < 0) {
+               debug("%s: Cannot find node parent\n", __func__);
+               return -1;
+       }
+
+       i2c_bus = i2c_get_bus_num_fdt(parent);
+       if (i2c_bus < 0)
+               return -1;
+
+       platdata->i2c_bus = i2c_bus;
+       platdata->slave_addr = fdtdec_get_addr(blob, node, "reg");
+
+       return 0;
+}
+#endif
+
+U_BOOT_DRIVER(tpm_i2c_infineon) = {
+       .name   = "tpm_i2c_infineon",
+       .id     = UCLASS_TPM,
+       .of_match = of_match_ptr(tpm_i2c_tis_ids),
+       .ofdata_to_platdata = of_match_ptr(tpm_i2c_tis_ofdata_to_platdata),
+       .probe  = tpm_i2c_tis_probe,
+       .remove = tpm_i2c_tis_remove,
+       .priv_auto_alloc_size = sizeof(struct tpm_dev),
+       .platdata_auto_alloc_size = sizeof(struct tpm_i2c_tis_platdata),
+};
diff --git a/drivers/tpm/tpm_private.h b/drivers/tpm/tpm_private.h
index 8894c98..1f5f53f 100644
--- a/drivers/tpm/tpm_private.h
+++ b/drivers/tpm/tpm_private.h
@@ -52,6 +52,15 @@ enum tpm_timeout {
 
 struct tpm_chip;
 
+struct dm_tpm_ops {
+       int (*init)(struct udevice *);
+       int (*open)(struct udevice *);
+       int (*close)(struct udevice *);
+       int (*sendrecv)(struct udevice *,
+                       const uint8_t *, size_t,
+                       uint8_t *, size_t *);
+};
+
 struct tpm_vendor_specific {
        const u8 req_complete_mask;
        const u8 req_complete_val;
@@ -64,8 +73,11 @@ struct tpm_vendor_specific {
        int locality;
        unsigned long timeout_a, timeout_b, timeout_c, timeout_d;  /* msec */
        unsigned long duration[3];  /* msec */
+       void *priv;
 };
 
+#define TPM_VPRIV(c)     ((c)->vendor.priv)
+
 struct tpm_chip {
        int is_open;
        struct tpm_vendor_specific vendor;
@@ -127,14 +139,7 @@ struct tpm_cmd_t {
        union tpm_cmd_params params;
 } __packed;
 
-struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *);
-
-int tpm_vendor_init(uint32_t dev_addr);
-
-struct udevice;
-int tpm_vendor_init_dev(struct udevice *dev);
-
-void tpm_vendor_cleanup(struct tpm_chip *chip);
-
+struct tpm_chip *tpm_register_hardware(struct udevice *dev,
+                               const struct tpm_vendor_specific *);
 
 #endif
diff --git a/include/dm/platform_data/tpm_i2c_infineon.h 
b/include/dm/platform_data/tpm_i2c_infineon.h
new file mode 100644
index 0000000..4f9d7e6
--- /dev/null
+++ b/include/dm/platform_data/tpm_i2c_infineon.h
@@ -0,0 +1,23 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __TPM_TIS_I2C_H__
+#define __TPM_TIS_I2C_H__
+
+struct tpm_i2c_tis_platdata {
+       int i2c_bus;
+       uint8_t slave_addr;
+} __packed;
+
+#endif
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index c744044..031daf2 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -58,6 +58,7 @@ enum uclass_id {
        UCLASS_USB_DEV_GENERIC, /* USB generic device */
        UCLASS_USB_HUB,         /* USB hub */
        UCLASS_VIDEO_BRIDGE,    /* Video bridge, e.g. DisplayPort to LVDS */
+       UCLASS_TPM,             /* TPM */
 
        UCLASS_COUNT,
        UCLASS_INVALID = -1,
-- 
2.1.4

_______________________________________________
U-Boot mailing list
[email protected]
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to