#include <config.h>
#include <common.h>
+#include <dm.h>
#include <linux/compiler.h>
#include <fdtdec.h>
#include <i2c.h>
/* TPM configuration */
struct tpm {
+#ifdef CONFIG_DM_I2C
+ struct udevice *dev;
+#else
int i2c_bus;
int slave_addr;
- char inited;
int old_bus;
+#endif
+ char inited;
} tpm;
/* Global structure for tpm chip data */
static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
{
- ssize_t rc;
+ int rc;
u32 count, ordinal;
unsigned long start, stop;
return -E2BIG;
}
+ debug("Calling send\n");
rc = chip->vendor.send(chip, (u8 *)buf, count);
+ debug(" ... done calling send\n");
if (rc < 0) {
- error("tpm_transmit: tpm_send: error %zd\n", rc);
+ error("tpm_transmit: tpm_send: error %d\n", rc);
goto out;
}
start = get_timer(0);
stop = tpm_calc_ordinal_duration(chip, ordinal);
do {
- debug("waiting for status...\n");
+ debug("waiting for status... %ld %ld\n", start, stop);
u8 status = chip->vendor.status(chip);
if ((status & chip->vendor.req_complete_mask) ==
chip->vendor.req_complete_val) {
debug("out_recv: reading response...\n");
rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
if (rc < 0)
- error("tpm_transmit: tpm_recv: error %zd\n", rc);
+ error("tpm_transmit: tpm_recv: error %d\n", rc);
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);
g_chip.is_open = 0;
return rc;
}
-
+#endif
static void tpm_close(void)
{
if (g_chip.is_open) {
static int tpm_select(void)
{
+#ifndef CONFIG_DM_I2C
int ret;
tpm.old_bus = i2c_get_bus_num();
return -1;
}
}
+#endif
return 0;
}
static int tpm_deselect(void)
{
+#ifndef CONFIG_DM_I2C
int ret;
if (tpm.old_bus != i2c_get_bus_num()) {
}
}
tpm.old_bus = -1;
+#endif
return 0;
}
*/
static int tpm_decode_config(struct tpm *dev)
{
-#ifdef CONFIG_OF_CONTROL
const void *blob = gd->fdt_blob;
- int node, parent;
- int i2c_bus;
+ int parent;
+ int node;
node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
if (node < 0) {
debug("%s: Cannot find node parent\n", __func__);
return -1;
}
+#ifdef CONFIG_DM_I2C
+ struct udevice *bus;
+ int chip_addr;
+ int ret;
+
+ /*
+ * TODO(sjg@chromium.org): 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(sjg@chromium.org): 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");
-#else
- dev->i2c_bus = CONFIG_TPM_TIS_I2C_BUS_NUMBER;
- dev->slave_addr = CONFIG_TPM_TIS_I2C_SLAVE_ADDRESS;
#endif
+
return 0;
}
if (tpm_select())
return -1;
+#ifndef CONFIG_DM_I2C
/*
* Probe TPM twice; the first probing might fail because TPM is asleep,
* and the probing can wake up TPM.
tpm.slave_addr);
return -1;
}
+#endif
tpm_deselect();
+ debug("%s: done\n", __func__);
tpm.inited = 1;
if (tpm_select())
return -1;
+#ifdef CONFIG_DM_I2C
+ rc = tpm_open_dev(tpm.dev);
+#else
rc = tpm_open(tpm.slave_addr);
+#endif
tpm_deselect();
*/
#include <common.h>
+#include <dm.h>
#include <fdtdec.h>
#include <linux/compiler.h>
#include <i2c.h>
/* Structure to store I2C TPM specific stuff */
struct tpm_dev {
+#ifdef CONFIG_DM_I2C
+ struct udevice *dev;
+#else
uint addr;
+#endif
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;
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,
(uchar *)&addrbuf, 1);
+#endif
if (rc == 0)
break; /* Success, break to skip sleep */
udelay(SLEEP_DURATION);
*/
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
if (rc == 0)
break; /* success, break to skip sleep */
}
* 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
if (rc == 0)
break; /* break here to skip sleep */
udelay(SLEEP_DURATION);
int count;
/* Prepare send buffer */
+#ifndef CONFIG_DM_I2C
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++) {
- rc = i2c_write(tpm_dev.addr, 0, 0, tpm_dev.buf, len + 1);
+#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
if (rc == 0)
break; /* Success, break to skip sleep */
udelay(sleep_time);
}
/* take care of 'guard time' */
- udelay(SLEEP_DURATION);
+ udelay(sleep_time);
if (rc)
return -rc;
{
unsigned long start, stop;
u8 buf = TPM_ACCESS_REQUEST_USE;
+ int rc;
if (check_locality(chip, loc) >= 0)
return loc; /* We already have the locality */
- iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
+ rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
+ if (rc)
+ return rc;
/* Wait for burstcount */
start = get_timer(0);
static void tpm_tis_i2c_ready(struct tpm_chip *chip)
{
+ int rc;
+
/* This causes the current command to be aborted */
u8 buf = TPM_STS_COMMAND_READY;
- iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
+ debug("%s\n", __func__);
+ rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
+ if (rc)
+ debug("%s: rc=%d\n", __func__, rc);
}
static ssize_t get_burstcount(struct tpm_chip *chip)
expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
if ((size_t)expected > count) {
+ error("Error size=%x, expected=%x, count=%x\n", size, expected,
+ count);
size = -EIO;
goto out;
}
static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
{
int rc, status;
- ssize_t burstcnt;
+ size_t burstcnt;
size_t count = 0;
int retry = 0;
u8 sts = TPM_STS_GO;
+ debug("%s: len=%d\n", __func__, len);
if (len > TPM_DEV_BUFSIZE)
return -E2BIG; /* Command is too long for our tpm, sorry */
if (burstcnt < 0)
return burstcnt;
- while (count < len - 1) {
- if (burstcnt > len - 1 - count)
- burstcnt = len - 1 - count;
+ while (count < len) {
+ udelay(300);
+ if (burstcnt > len - count)
+ burstcnt = len - count;
#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
if (rc == 0)
count += burstcnt;
else {
- retry++;
- wait_for_stat(chip, TPM_STS_VALID,
- chip->vendor.timeout_c, &status);
+ debug("%s: error\n", __func__);
+ if (retry++ > 10) {
+ rc = -EIO;
+ goto out_err;
+ }
+ rc = wait_for_stat(chip, TPM_STS_VALID,
+ chip->vendor.timeout_c, &status);
+ if (rc)
+ goto out_err;
if ((status & TPM_STS_DATA_EXPECT) == 0) {
rc = -EIO;
}
}
- /* Write last byte */
- iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
- wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
- if ((status & TPM_STS_DATA_EXPECT) != 0) {
- rc = -EIO;
- goto out_err;
- }
-
/* Go and do it */
iic_tpm_write(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);
/*
* The TPM needs some time to clean up here,
return UNKNOWN;
}
-/* Initialisation of i2c tpm */
-int tpm_vendor_init(uint32_t dev_addr)
+static int tpm_vendor_init_common(void)
{
+ struct tpm_chip *chip;
u32 vendor;
u32 expected_did_vid;
- uint old_addr;
- int rc = 0;
- struct tpm_chip *chip;
-
- old_addr = tpm_dev.addr;
- if (dev_addr != 0)
- tpm_dev.addr = dev_addr;
tpm_dev.chip_type = tpm_vendor_chip_type();
chip = tpm_register_hardware(&tpm_tis_i2c);
- if (chip < 0) {
- rc = -ENODEV;
- goto out_err;
- }
+ if (chip < 0)
+ return -ENODEV;
/* Disable interrupts (not supported) */
chip->vendor.irq = 0;
chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
- if (request_locality(chip, 0) < 0) {
- rc = -ENODEV;
- goto out_err;
- }
+ if (request_locality(chip, 0) < 0)
+ return -ENODEV;
/* Read four bytes from DID_VID register */
if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
- rc = -EIO;
- goto out_release;
+ release_locality(chip, 0, 1);
+ return -EIO;
}
if (tpm_dev.chip_type == SLB9635) {
if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
error("Vendor id did not match! ID was %08x\n", vendor);
- rc = -ENODEV;
- goto out_release;
+ return -ENODEV;
}
debug("1.2 TPM (chip type %s device-id 0x%X)\n",
*/
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)
+{
+ uint old_addr;
+ int rc = 0;
-out_release:
- release_locality(chip, 0, 1);
+ old_addr = tpm_dev.addr;
+ if (dev_addr != 0)
+ tpm_dev.addr = dev_addr;
+
+ rc = tpm_vendor_init_common();
+ if (rc)
+ tpm_dev.addr = old_addr;
-out_err:
- tpm_dev.addr = old_addr;
return rc;
}
+#endif
void tpm_vendor_cleanup(struct tpm_chip *chip)
{