blob: 04041095109af5b967a46d6b7a02c6822150d953 [file] [log] [blame]
Stefan Reinauerd518c7a2013-11-04 17:38:32 -08001/*
2 * Copyright (C) 2011 Infineon Technologies
3 * Copyright 2013 Google Inc.
4 *
Stefan Reinauerd518c7a2013-11-04 17:38:32 -08005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but without any warranty; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080014 */
15
16#include <stdint.h>
17#include <string.h>
18#include <assert.h>
19#include <delay.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080020#include <device/i2c.h>
Julius Werner9ff8f6f2015-02-23 14:31:09 -080021#include <endian.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080022#include <tpm.h>
23#include "tpm.h"
Sourabh Banerjee8c916ec2015-01-20 15:18:40 +053024#include <timer.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080025
26#include <console/console.h>
27
28/* global structure for tpm chip data */
29struct tpm_chip g_chip;
30
31#define TPM_CMD_COUNT_BYTE 2
32#define TPM_CMD_ORDINAL_BYTE 6
Sourabh Banerjee8c916ec2015-01-20 15:18:40 +053033#define TPM_VALID_STATUS (1 << 7)
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080034
35int tis_open(void)
36{
37 int rc;
38
39 if (g_chip.is_open) {
40 printk(BIOS_DEBUG, "tis_open() called twice.\n");
41 return -1;
42 }
43
44 rc = tpm_vendor_init(CONFIG_DRIVER_TPM_I2C_BUS,
45 CONFIG_DRIVER_TPM_I2C_ADDR);
46
47 if (rc < 0)
48 g_chip.is_open = 0;
49
50 if (rc) {
51 return -1;
52 }
53
54 return 0;
55}
56
57int tis_close(void)
58{
59 if (g_chip.is_open) {
60 tpm_vendor_cleanup(&g_chip);
61 g_chip.is_open = 0;
62 }
63
64 return 0;
65}
66
67int tis_init(void)
68{
69 int bus = CONFIG_DRIVER_TPM_I2C_BUS;
70 int chip = CONFIG_DRIVER_TPM_I2C_ADDR;
Sourabh Banerjee8c916ec2015-01-20 15:18:40 +053071 struct stopwatch sw;
72 uint8_t buf = 0;
73 int ret;
74 long sw_run_duration = 750;
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080075
76 /*
Sourabh Banerjee8c916ec2015-01-20 15:18:40 +053077 * Probe TPM. Check if the TPM_ACCESS register's ValidSts bit is set(1)
78 * If the bit remains clear(0) then claim that init has failed.
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080079 */
Sourabh Banerjee8c916ec2015-01-20 15:18:40 +053080 stopwatch_init_msecs_expire(&sw, sw_run_duration);
81 do {
82 ret = i2c_readb(bus, chip, 0, &buf);
83 if (!ret && (buf & TPM_VALID_STATUS)) {
84 sw_run_duration = stopwatch_duration_msecs(&sw);
85 break;
86 }
87 } while (!stopwatch_expired(&sw));
88
89 printk(BIOS_INFO,
90 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
91 __func__, (buf & TPM_VALID_STATUS) ? "set" : "clear",
92 (buf & TPM_VALID_STATUS) >> 7, sw_run_duration);
93
94 /*
95 * Claim failure if the ValidSts (bit 7) is clear.
96 */
97 if (!(buf & TPM_VALID_STATUS))
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080098 return -1;
99
100 return 0;
101}
102
103static ssize_t tpm_transmit(const uint8_t *buf, size_t bufsiz)
104{
105 int rc;
106 uint32_t count, ordinal;
107
108 struct tpm_chip *chip = &g_chip;
109
110 memcpy(&count, buf + TPM_CMD_COUNT_BYTE, sizeof(count));
111 count = be32_to_cpu(count);
112 memcpy(&ordinal, buf + TPM_CMD_ORDINAL_BYTE, sizeof(ordinal));
113 ordinal = be32_to_cpu(ordinal);
114
115 if (count == 0) {
116 printk(BIOS_DEBUG, "tpm_transmit: no data\n");
117 return -1;
118 }
119 if (count > bufsiz) {
120 printk(BIOS_DEBUG, "tpm_transmit: invalid count value %x %zx\n",
121 count, bufsiz);
122 return -1;
123 }
124
125 ASSERT(chip->vendor.send);
126 rc = chip->vendor.send(chip, (uint8_t *) buf, count);
127 if (rc < 0) {
128 printk(BIOS_DEBUG, "tpm_transmit: tpm_send error\n");
129 goto out;
130 }
131
132 if (chip->vendor.irq)
133 goto out_recv;
134
135 int timeout = 2 * 60 * 1000; /* two minutes timeout */
136 while (timeout) {
137 ASSERT(chip->vendor.status);
138 uint8_t status = chip->vendor.status(chip);
139 if ((status & chip->vendor.req_complete_mask) ==
140 chip->vendor.req_complete_val) {
141 goto out_recv;
142 }
143
144 if ((status == chip->vendor.req_canceled)) {
145 printk(BIOS_DEBUG, "tpm_transmit: Operation Canceled\n");
146 rc = -1;
147 goto out;
148 }
149 mdelay(TPM_TIMEOUT);
150 timeout--;
151 }
152
153 ASSERT(chip->vendor.cancel);
154 chip->vendor.cancel(chip);
155 printk(BIOS_DEBUG, "tpm_transmit: Operation Timed out\n");
156 rc = -1; //ETIME;
157 goto out;
158
159out_recv:
160
161 rc = chip->vendor.recv(chip, (uint8_t *) buf, TPM_BUFSIZE);
162 if (rc < 0)
Furquan Shaikh251eef12014-07-22 11:12:15 -0700163 printk(BIOS_DEBUG, "tpm_transmit: tpm_recv: error %d\n", rc);
Stefan Reinauerd518c7a2013-11-04 17:38:32 -0800164out:
165 return rc;
166}
167
168int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
169 uint8_t *recvbuf, size_t *rbuf_len)
170{
171 uint8_t buf[TPM_BUFSIZE];
172
173 if (sizeof(buf) < sbuf_size)
174 return -1;
175
176 memcpy(buf, sendbuf, sbuf_size);
177
178 int len = tpm_transmit(buf, sbuf_size);
179
180 if (len < 10) {
181 *rbuf_len = 0;
182 return -1;
183 }
184
185 if (len > *rbuf_len) {
186 *rbuf_len = len;
187 return -1;
188 }
189
190 memcpy(recvbuf, buf, len);
191 *rbuf_len = len;
192
193 return 0;
194}