#include <config.h>
#include <common.h>
+#include <dm.h>
#include <linux/compiler.h>
#include <fdtdec.h>
#include <i2c.h>
/* TPM configuration */
struct tpm {
- int i2c_bus;
- int slave_addr;
+ struct udevice *dev;
char inited;
- int old_bus;
} 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;
}
-static int tpm_open(uint32_t dev_addr)
+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_addr);
+ rc = tpm_vendor_init(dev);
if (rc < 0)
g_chip.is_open = 0;
return rc;
}
}
-static int tpm_select(void)
-{
- 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(void)
-{
- 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;
-}
-
/**
* Decode TPM configuration.
*
*/
static int tpm_decode_config(struct tpm *dev)
{
-#ifdef CONFIG_OF_CONTROL
const void *blob = gd->fdt_blob;
- int node, parent;
- int i2c_bus;
+ struct udevice *bus;
+ int chip_addr;
+ int parent;
+ int node;
+ int ret;
node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
if (node < 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;
- 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
+
+ /*
+ * 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;
+ }
+
return 0;
}
if (tpm_decode_config(&tpm))
return -1;
- if (tpm_select())
- return -1;
-
- /*
- * 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;
- }
-
- tpm_deselect();
+ debug("%s: done\n", __func__);
tpm.inited = 1;
if (!tpm.inited)
return -1;
- if (tpm_select())
- return -1;
-
- rc = tpm_open(tpm.slave_addr);
-
- tpm_deselect();
+ rc = tpm_open_dev(tpm.dev);
return rc;
}
if (!tpm.inited)
return -1;
- if (tpm_select())
- return -1;
-
tpm_close();
- tpm_deselect();
-
return 0;
}
memcpy(buf, sendbuf, sbuf_size);
- if (tpm_select())
- return -1;
-
len = tpm_transmit(buf, sbuf_size);
- tpm_deselect();
-
if (len < 10) {
*rbuf_len = 0;
return -1;