Remove address from GPLv2 headers
[coreboot.git] / src / drivers / i2c / tpm / tis.c
blobfca43160be0de4d49ce5a6a6863b2e6d44f83812
1 /*
2 * Copyright (C) 2011 Infineon Technologies
3 * Copyright 2013 Google Inc.
5 * See file CREDITS for list of people who contributed to this
6 * project.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but without any warranty; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc.
23 #include <stdint.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <delay.h>
27 #include <device/i2c.h>
28 #include <endian.h>
29 #include <tpm.h>
30 #include "tpm.h"
31 #include <timer.h>
33 #include <console/console.h>
35 /* global structure for tpm chip data */
36 struct tpm_chip g_chip;
38 #define TPM_CMD_COUNT_BYTE 2
39 #define TPM_CMD_ORDINAL_BYTE 6
40 #define TPM_VALID_STATUS (1 << 7)
42 int tis_open(void)
44 int rc;
46 if (g_chip.is_open) {
47 printk(BIOS_DEBUG, "tis_open() called twice.\n");
48 return -1;
51 rc = tpm_vendor_init(CONFIG_DRIVER_TPM_I2C_BUS,
52 CONFIG_DRIVER_TPM_I2C_ADDR);
54 if (rc < 0)
55 g_chip.is_open = 0;
57 if (rc) {
58 return -1;
61 return 0;
64 int tis_close(void)
66 if (g_chip.is_open) {
67 tpm_vendor_cleanup(&g_chip);
68 g_chip.is_open = 0;
71 return 0;
74 int tis_init(void)
76 int bus = CONFIG_DRIVER_TPM_I2C_BUS;
77 int chip = CONFIG_DRIVER_TPM_I2C_ADDR;
78 struct stopwatch sw;
79 uint8_t buf = 0;
80 int ret;
81 long sw_run_duration = 750;
84 * Probe TPM. Check if the TPM_ACCESS register's ValidSts bit is set(1)
85 * If the bit remains clear(0) then claim that init has failed.
87 stopwatch_init_msecs_expire(&sw, sw_run_duration);
88 do {
89 ret = i2c_readb(bus, chip, 0, &buf);
90 if (!ret && (buf & TPM_VALID_STATUS)) {
91 sw_run_duration = stopwatch_duration_msecs(&sw);
92 break;
94 } while (!stopwatch_expired(&sw));
96 printk(BIOS_INFO,
97 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
98 __func__, (buf & TPM_VALID_STATUS) ? "set" : "clear",
99 (buf & TPM_VALID_STATUS) >> 7, sw_run_duration);
102 * Claim failure if the ValidSts (bit 7) is clear.
104 if (!(buf & TPM_VALID_STATUS))
105 return -1;
107 return 0;
110 static ssize_t tpm_transmit(const uint8_t *buf, size_t bufsiz)
112 int rc;
113 uint32_t count, ordinal;
115 struct tpm_chip *chip = &g_chip;
117 memcpy(&count, buf + TPM_CMD_COUNT_BYTE, sizeof(count));
118 count = be32_to_cpu(count);
119 memcpy(&ordinal, buf + TPM_CMD_ORDINAL_BYTE, sizeof(ordinal));
120 ordinal = be32_to_cpu(ordinal);
122 if (count == 0) {
123 printk(BIOS_DEBUG, "tpm_transmit: no data\n");
124 return -1;
126 if (count > bufsiz) {
127 printk(BIOS_DEBUG, "tpm_transmit: invalid count value %x %zx\n",
128 count, bufsiz);
129 return -1;
132 ASSERT(chip->vendor.send);
133 rc = chip->vendor.send(chip, (uint8_t *) buf, count);
134 if (rc < 0) {
135 printk(BIOS_DEBUG, "tpm_transmit: tpm_send error\n");
136 goto out;
139 if (chip->vendor.irq)
140 goto out_recv;
142 int timeout = 2 * 60 * 1000; /* two minutes timeout */
143 while (timeout) {
144 ASSERT(chip->vendor.status);
145 uint8_t status = chip->vendor.status(chip);
146 if ((status & chip->vendor.req_complete_mask) ==
147 chip->vendor.req_complete_val) {
148 goto out_recv;
151 if ((status == chip->vendor.req_canceled)) {
152 printk(BIOS_DEBUG, "tpm_transmit: Operation Canceled\n");
153 rc = -1;
154 goto out;
156 mdelay(TPM_TIMEOUT);
157 timeout--;
160 ASSERT(chip->vendor.cancel);
161 chip->vendor.cancel(chip);
162 printk(BIOS_DEBUG, "tpm_transmit: Operation Timed out\n");
163 rc = -1; //ETIME;
164 goto out;
166 out_recv:
168 rc = chip->vendor.recv(chip, (uint8_t *) buf, TPM_BUFSIZE);
169 if (rc < 0)
170 printk(BIOS_DEBUG, "tpm_transmit: tpm_recv: error %d\n", rc);
171 out:
172 return rc;
175 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
176 uint8_t *recvbuf, size_t *rbuf_len)
178 uint8_t buf[TPM_BUFSIZE];
180 if (sizeof(buf) < sbuf_size)
181 return -1;
183 memcpy(buf, sendbuf, sbuf_size);
185 int len = tpm_transmit(buf, sbuf_size);
187 if (len < 10) {
188 *rbuf_len = 0;
189 return -1;
192 if (len > *rbuf_len) {
193 *rbuf_len = len;
194 return -1;
197 memcpy(recvbuf, buf, len);
198 *rbuf_len = len;
200 return 0;