drivers/i2c/tpm: Make driver safe for use in x86 pre-ram

Use CAR accessors where needed for accessing static data.
In some cases this required some minor restructuring to pass
in a variable instead of use a global one.

For the tpm_vendor_init the structure no longer has useful
defaults, which nobody was depending on anyway.  This now
requires the caller to provide a non-zero address.

Tested by enabling I2C TPM on reef and compiling successfully.

Change-Id: I8e02fbcebf5fe10c4122632eda1c48b247478289
Signed-off-by: Duncan Laurie <dlaurie@chromium.org>
Reviewed-on: https://review.coreboot.org/16394
Tested-by: build bot (Jenkins)
Reviewed-by: Furquan Shaikh <furquan@google.com>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
This commit is contained in:
Duncan Laurie 2016-08-31 13:51:14 -07:00
parent 4a560769ad
commit 40ae1706a4
3 changed files with 61 additions and 65 deletions

View File

@ -13,6 +13,7 @@
* GNU General Public License for more details.
*/
#include <arch/early_variables.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
@ -26,7 +27,7 @@
#include <console/console.h>
/* global structure for tpm chip data */
struct tpm_chip g_chip;
static struct tpm_chip g_chip CAR_GLOBAL;
#define TPM_CMD_COUNT_BYTE 2
#define TPM_CMD_ORDINAL_BYTE 6
@ -34,18 +35,18 @@ struct tpm_chip g_chip;
int tis_open(void)
{
struct tpm_chip *chip = car_get_var_ptr(&g_chip);
int rc;
if (g_chip.is_open) {
if (chip->is_open) {
printk(BIOS_DEBUG, "tis_open() called twice.\n");
return -1;
}
rc = tpm_vendor_init(CONFIG_DRIVER_TPM_I2C_BUS,
rc = tpm_vendor_init(chip, CONFIG_DRIVER_TPM_I2C_BUS,
CONFIG_DRIVER_TPM_I2C_ADDR);
if (rc < 0)
g_chip.is_open = 0;
chip->is_open = 0;
if (rc) {
return -1;
@ -56,9 +57,11 @@ int tis_open(void)
int tis_close(void)
{
if (g_chip.is_open) {
tpm_vendor_cleanup(&g_chip);
g_chip.is_open = 0;
struct tpm_chip *chip = car_get_var_ptr(&g_chip);
if (chip->is_open) {
tpm_vendor_cleanup(chip);
chip->is_open = 0;
}
return 0;
@ -104,8 +107,7 @@ static ssize_t tpm_transmit(const uint8_t *buf, size_t bufsiz)
{
int rc;
uint32_t count, ordinal;
struct tpm_chip *chip = &g_chip;
struct tpm_chip *chip = car_get_var_ptr(&g_chip);
memcpy(&count, buf + TPM_CMD_COUNT_BYTE, sizeof(count));
count = be32_to_cpu(count);

View File

@ -28,6 +28,7 @@
* GNU General Public License for more details.
*/
#include <arch/early_variables.h>
#include <stdint.h>
#include <string.h>
#include <types.h>
@ -37,9 +38,6 @@
#include <endian.h>
#include "tpm.h"
/* Address of the TPM on the I2C bus */
#define TPM_I2C_ADDR 0x20
/* max. number of iterations after I2C NAK */
#define MAX_COUNT 3
@ -78,10 +76,7 @@ struct tpm_inf_dev {
enum i2c_chip_type chip_type;
};
static struct tpm_inf_dev tpm_dev = {
.bus = -1,
.addr = TPM_I2C_ADDR
};
static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
/*
* iic_tpm_read() - read from TPM register
@ -99,15 +94,18 @@ static struct tpm_inf_dev tpm_dev = {
*/
static int iic_tpm_read(uint8_t addr, uint8_t *buffer, size_t len)
{
struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
int rc;
int count;
if (tpm_dev.bus < 0)
if (tpm_dev->addr == 0)
return -1;
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++) {
rc = i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1);
rc = i2c_write_raw(tpm_dev->bus, tpm_dev->addr,
&addr, 1);
if (rc == 0)
break; /* success, break to skip sleep */
@ -123,7 +121,7 @@ static int iic_tpm_read(uint8_t addr, uint8_t *buffer, size_t len)
*/
for (count = 0; count < MAX_COUNT; count++) {
udelay(SLEEP_DURATION);
rc = i2c_read_raw(tpm_dev.bus, tpm_dev.addr,
rc = i2c_read_raw(tpm_dev->bus, tpm_dev->addr,
buffer, len);
if (rc == 0)
break; /* success, break to skip sleep */
@ -136,13 +134,13 @@ static int iic_tpm_read(uint8_t addr, uint8_t *buffer, size_t len)
* retries should usually not be needed, but are kept just to
* be safe on the safe side.
*/
struct i2c_seg aseg = { .read = 0, .chip = tpm_dev.addr,
struct i2c_seg aseg = { .read = 0, .chip = tpm_dev->addr,
.buf = &addr, .len = 1 };
struct i2c_seg dseg = { .read = 1, .chip = tpm_dev.addr,
struct i2c_seg dseg = { .read = 1, .chip = tpm_dev->addr,
.buf = buffer, .len = len };
for (count = 0; count < MAX_COUNT; count++) {
rc = i2c_transfer(tpm_dev.bus, &aseg, 1) ||
i2c_transfer(tpm_dev.bus, &dseg, 1);
rc = i2c_transfer(tpm_dev->bus, &aseg, 1) ||
i2c_transfer(tpm_dev->bus, &dseg, 1);
if (rc == 0)
break; /* break here to skip sleep */
udelay(SLEEP_DURATION);
@ -161,6 +159,7 @@ static int iic_tpm_write_generic(uint8_t addr, uint8_t *buffer, size_t len,
unsigned int sleep_time,
uint8_t max_count)
{
struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
int rc = 0;
int count;
@ -170,14 +169,14 @@ static int iic_tpm_write_generic(uint8_t addr, uint8_t *buffer, size_t len,
}
/* prepare send buffer */
tpm_dev.buf[0] = addr;
memcpy(&(tpm_dev.buf[1]), buffer, len);
tpm_dev->buf[0] = addr;
memcpy(&(tpm_dev->buf[1]), buffer, len);
if (tpm_dev.bus < 0)
if (tpm_dev->addr == 0)
return -1;
for (count = 0; count < max_count; count++) {
rc = i2c_write_raw(tpm_dev.bus, tpm_dev.addr,
tpm_dev.buf, len + 1);
rc = i2c_write_raw(tpm_dev->bus, tpm_dev->addr,
tpm_dev->buf, len + 1);
if (rc == 0)
break; /* success, break to skip sleep */
@ -480,55 +479,53 @@ out_err:
return -1;
}
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,
.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,
};
/* Initialization of I2C TPM */
int tpm_vendor_init(unsigned bus, uint32_t dev_addr)
int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr)
{
struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
uint32_t vendor;
unsigned int old_addr;
struct tpm_chip *chip;
extern struct tpm_chip g_chip;
old_addr = tpm_dev.addr;
if (dev_addr != 0)
tpm_dev.addr = dev_addr;
tpm_dev.bus = bus;
if (dev_addr == 0) {
printk(BIOS_ERR, "%s: missing device address\n", __func__);
return -1;
}
chip = &g_chip;
memcpy(&chip->vendor, &tpm_tis_i2c, sizeof(struct tpm_vendor_specific));
tpm_dev->bus = bus;
tpm_dev->addr = dev_addr;
memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
chip->is_open = 1;
chip->vendor.status = &tpm_tis_i2c_status;
chip->vendor.recv = &tpm_tis_i2c_recv;
chip->vendor.send = &tpm_tis_i2c_send;
chip->vendor.cancel = &tpm_tis_i2c_ready;
chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
/* Disable interrupts (not supported) */
chip->vendor.irq = 0;
if (request_locality(chip, 0) != 0)
goto out_err;
return -1;
/* Read four bytes from DID_VID register */
if (iic_tpm_read(TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
goto out_release;
goto out_err;
if (vendor == TPM_TIS_I2C_DID_VID_9645) {
tpm_dev.chip_type = SLB9645;
tpm_dev->chip_type = SLB9645;
} else if (be32_to_cpu(vendor) == TPM_TIS_I2C_DID_VID_9635) {
tpm_dev.chip_type = SLB9635;
tpm_dev->chip_type = SLB9635;
} else {
printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized.\n", vendor);
goto out_release;
goto out_err;
}
printk(BIOS_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.
@ -537,11 +534,8 @@ int tpm_vendor_init(unsigned bus, uint32_t dev_addr)
return 0;
out_release:
release_locality(chip, 0, 1);
out_err:
tpm_dev.addr = old_addr;
release_locality(chip, 0, 1);
return -1;
}

View File

@ -47,9 +47,9 @@ enum tpm_timeout {
struct tpm_chip;
struct tpm_vendor_specific {
const uint8_t req_complete_mask;
const uint8_t req_complete_val;
const uint8_t req_canceled;
uint8_t req_complete_mask;
uint8_t req_complete_val;
uint8_t req_canceled;
int irq;
int (*recv)(struct tpm_chip *, uint8_t *, size_t);
int (*send)(struct tpm_chip *, uint8_t *, size_t);
@ -121,7 +121,7 @@ struct tpm_cmd_t {
/* ---------- Interface for TPM vendor ------------ */
int tpm_vendor_init(unsigned bus, uint32_t dev_addr);
int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr);
void tpm_vendor_cleanup(struct tpm_chip *chip);