blob: bc40e852a2941cb35ab3a74460d16f10891e5db6 [file] [log] [blame]
Patrick Georgi593124d2020-05-10 19:44:08 +02001/* SPDX-License-Identifier: BSD-3-Clause */
2/* This is a driver for a SPI interfaced TPM2 device.
Vadim Bendeburye31d2432016-04-09 18:33:49 -07003 *
4 * It assumes that the required SPI interface has been initialized before the
5 * driver is started. A 'sruct spi_slave' pointer passed at initialization is
6 * used to direct traffic to the correct SPI interface. This dirver does not
7 * provide a way to instantiate multiple TPM devices. Also, to keep things
8 * simple, the driver unconditionally uses of TPM locality zero.
9 *
10 * References to documentation are based on the TCG issued "TPM Profile (PTP)
11 * Specification Revision 00.43".
12 */
13
Furquan Shaikh260b2972017-04-07 13:26:01 -070014#include <assert.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070015#include <commonlib/endian.h>
16#include <console/console.h>
17#include <delay.h>
18#include <endian.h>
19#include <string.h>
20#include <timer.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +020021#include <security/tpm/tis.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -070022
23#include "tpm.h"
24
Vadim Bendebury05155c02016-06-23 12:03:18 -070025#define TPM_LOCALITY_0_SPI_BASE 0x00d40000
26
Vadim Bendeburye31d2432016-04-09 18:33:49 -070027/* Assorted TPM2 registers for interface type FIFO. */
Vadim Bendebury05155c02016-06-23 12:03:18 -070028#define TPM_ACCESS_REG (TPM_LOCALITY_0_SPI_BASE + 0)
29#define TPM_STS_REG (TPM_LOCALITY_0_SPI_BASE + 0x18)
30#define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
31#define TPM_DID_VID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf00)
32#define TPM_RID_REG (TPM_LOCALITY_0_SPI_BASE + 0xf04)
Vadim Bendebury58826fc2016-06-23 18:17:33 -070033#define TPM_FW_VER (TPM_LOCALITY_0_SPI_BASE + 0xf90)
Jes Klinkedcae8072020-07-29 14:22:41 -070034#define CR50_BOARD_CFG (TPM_LOCALITY_0_SPI_BASE + 0xfe0)
35
36#define CR50_BOARD_CFG_LOCKBIT_MASK 0x80000000U
37#define CR50_BOARD_CFG_FEATUREBITS_MASK 0x3FFFFFFFU
38
39#define CR50_BOARD_CFG_100US_READY_PULSE 0x00000001U
40#define CR50_BOARD_CFG_VALUE \
41 (CONFIG(CR50_USE_LONG_INTERRUPT_PULSES) \
42 ? CR50_BOARD_CFG_100US_READY_PULSE : 0)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070043
Shelley Chen85eb0312017-11-07 14:24:19 -080044#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
45
Furquan Shaikhbdf86a62017-04-03 23:52:01 -070046/* SPI slave structure for TPM device. */
Patrick Georgic9b13592019-11-29 11:47:47 +010047static struct spi_slave spi_slave;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070048
49/* Cached TPM device identification. */
Patrick Georgic9b13592019-11-29 11:47:47 +010050static struct tpm2_info tpm_info;
Jes Klinkedcae8072020-07-29 14:22:41 -070051struct cr50_firmware_version {
52 int epoch;
53 int major;
54 int minor;
55};
56static struct cr50_firmware_version cr50_firmware_version;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070057
58/*
Martin Rothf48acbd2020-07-24 12:24:27 -060059 * TODO(vbendeb): make CONFIG(DEBUG_TPM) an int to allow different level of
Vadim Bendeburye31d2432016-04-09 18:33:49 -070060 * debug traces. Right now it is either 0 or 1.
61 */
Martin Rothc25c1eb2020-07-24 12:26:21 -060062static const int debug_level_ = CONFIG(DEBUG_TPM);
Vadim Bendeburye31d2432016-04-09 18:33:49 -070063
Vadim Bendeburye31d2432016-04-09 18:33:49 -070064/*
65 * SPI frame header for TPM transactions is 4 bytes in size, it is described
66 * in section "6.4.6 Spi Bit Protocol".
67 */
68typedef struct {
69 unsigned char body[4];
70} spi_frame_header;
71
72void tpm2_get_info(struct tpm2_info *info)
73{
Patrick Georgic9b13592019-11-29 11:47:47 +010074 *info = tpm_info;
Vadim Bendeburye31d2432016-04-09 18:33:49 -070075}
76
Aaron Durbin64031672018-04-21 14:45:32 -060077__weak int tis_plat_irq_status(void)
Jeffy Chen19e3d332017-03-03 18:24:02 +080078{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010079 static int warning_displayed;
Jeffy Chen19e3d332017-03-03 18:24:02 +080080
Arthur Heymans0ca944b2019-11-20 19:51:06 +010081 if (!warning_displayed) {
Jeffy Chen19e3d332017-03-03 18:24:02 +080082 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 10ms to wait on Cr50!\n");
Arthur Heymans0ca944b2019-11-20 19:51:06 +010083 warning_displayed = 1;
Jeffy Chen19e3d332017-03-03 18:24:02 +080084 }
85 mdelay(10);
86
87 return 1;
88}
89
90/*
Elyes HAOUAS6688f462018-08-29 17:22:44 +020091 * TPM may trigger a IRQ after finish processing previous transfer.
92 * Waiting for this IRQ to sync TPM status.
Jeffy Chen19e3d332017-03-03 18:24:02 +080093 *
94 * Returns 1 on success, 0 on failure (timeout).
95 */
96static int tpm_sync(void)
97{
98 struct stopwatch sw;
99
Furquan Shaikh260b2972017-04-07 13:26:01 -0700100 stopwatch_init_msecs_expire(&sw, 10);
Jeffy Chen19e3d332017-03-03 18:24:02 +0800101 while (!tis_plat_irq_status()) {
102 if (stopwatch_expired(&sw)) {
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200103 printk(BIOS_ERR, "Timeout wait for TPM IRQ!\n");
Jeffy Chen19e3d332017-03-03 18:24:02 +0800104 return 0;
105 }
106 }
107 return 1;
108}
109
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700110/*
111 * Each TPM2 SPI transaction starts the same: CS is asserted, the 4 byte
112 * header is sent to the TPM, the master waits til TPM is ready to continue.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800113 *
114 * Returns 1 on success, 0 on failure (TPM SPI flow control timeout.)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700115 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600116static int start_transaction(int read_write, size_t bytes, unsigned int addr)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700117{
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100118 spi_frame_header header, header_resp;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700119 uint8_t byte;
120 int i;
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100121 int ret;
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800122 struct stopwatch sw;
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100123 static int tpm_sync_needed;
124 static struct stopwatch wake_up_sw;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700125
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100126 if (CONFIG(TPM_CR50)) {
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700127 /*
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100128 * First Cr50 access in each coreboot stage where TPM is used will be
129 * prepended by a wake up pulse on the CS line.
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700130 */
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100131 int wakeup_needed = 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700132
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100133 /* Wait for TPM to finish previous transaction if needed */
134 if (tpm_sync_needed) {
135 tpm_sync();
136 /*
137 * During the first invocation of this function on each stage
138 * this if () clause code does not run (as tpm_sync_needed
139 * value is zero), during all following invocations the
140 * stopwatch below is guaranteed to be started.
141 */
142 if (!stopwatch_expired(&wake_up_sw))
143 wakeup_needed = 0;
144 } else {
145 tpm_sync_needed = 1;
146 }
Vadim Bendebury3b62d6b2017-10-30 18:29:03 -0700147
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100148 if (wakeup_needed) {
149 /* Just in case Cr50 is asleep. */
150 spi_claim_bus(&spi_slave);
151 udelay(1);
152 spi_release_bus(&spi_slave);
153 udelay(100);
154 }
155
156 /*
157 * The Cr50 on H1 does not go to sleep for 1 second after any
158 * SPI slave activity, let's be conservative and limit the
159 * window to 900 ms.
160 */
161 stopwatch_init_msecs_expire(&wake_up_sw, 900);
162 }
Jeffy Chenf9a40ea2017-03-03 18:24:02 +0800163
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700164 /*
165 * The first byte of the frame header encodes the transaction type
166 * (read or write) and transfer size (set to lentgh - 1), limited to
167 * 64 bytes.
168 */
169 header.body[0] = (read_write ? 0x80 : 0) | 0x40 | (bytes - 1);
170
171 /* The rest of the frame header is the TPM register address. */
172 for (i = 0; i < 3; i++)
173 header.body[i + 1] = (addr >> (8 * (2 - i))) & 0xff;
174
175 /* CS assert wakes up the slave. */
Patrick Georgic9b13592019-11-29 11:47:47 +0100176 spi_claim_bus(&spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700177
178 /*
179 * The TCG TPM over SPI specification introduces the notion of SPI
180 * flow control (Section "6.4.5 Flow Control").
181 *
182 * Again, the slave (TPM device) expects each transaction to start
183 * with a 4 byte header trasmitted by master. The header indicates if
184 * the master needs to read or write a register, and the register
185 * address.
186 *
187 * If the slave needs to stall the transaction (for instance it is not
188 * ready to send the register value to the master), it sets the MOSI
189 * line to 0 during the last clock of the 4 byte header. In this case
190 * the master is supposed to start polling the SPI bus, one byte at
191 * time, until the last bit in the received byte (transferred during
192 * the last clock of the byte) is set to 1.
193 *
194 * Due to some SPI controllers' shortcomings (Rockchip comes to
195 * mind...) we trasmit the 4 byte header without checking the byte
196 * transmitted by the TPM during the transaction's last byte.
197 *
198 * We know that cr50 is guaranteed to set the flow control bit to 0
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100199 * during the header transfer. Real TPM2 are fast enough to not require
200 * to stall the master. They might still use this feature, so test the
201 * last bit after shifting in the address bytes.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700202 * crosbug.com/p/52132 has been opened to track this.
203 */
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100204
205 header_resp.body[3] = 0;
206 if (CONFIG(TPM_CR50))
207 ret = spi_xfer(&spi_slave, header.body, sizeof(header.body), NULL, 0);
208 else
209 ret = spi_xfer(&spi_slave, header.body, sizeof(header.body),
210 header_resp.body, sizeof(header_resp.body));
211 if (ret) {
212 printk(BIOS_ERR, "SPI-TPM: transfer error\n");
213 spi_release_bus(&spi_slave);
214 return 0;
215 }
216
217 if (header_resp.body[3] & 1)
218 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700219
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800220 /*
221 * Now poll the bus until TPM removes the stall bit. Give it up to 100
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100222 * ms to sort it out - it could be saving stuff in nvram at some point.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800223 */
224 stopwatch_init_msecs_expire(&sw, 100);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700225 do {
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800226 if (stopwatch_expired(&sw)) {
227 printk(BIOS_ERR, "TPM flow control failure\n");
Patrick Georgic9b13592019-11-29 11:47:47 +0100228 spi_release_bus(&spi_slave);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800229 return 0;
230 }
Patrick Georgic9b13592019-11-29 11:47:47 +0100231 spi_xfer(&spi_slave, NULL, 0, &byte, 1);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700232 } while (!(byte & 1));
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100233
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800234 return 1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700235}
236
237/*
238 * Print out the contents of a buffer, if debug is enabled. Skip registers
239 * other than FIFO, unless debug_level_ is 2.
240 */
241static void trace_dump(const char *prefix, uint32_t reg,
242 size_t bytes, const uint8_t *buffer,
243 int force)
244{
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100245 static char prev_prefix;
246 static unsigned int prev_reg;
247 static int current_char;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700248 const int BYTES_PER_LINE = 32;
249
250 if (!force) {
251 if (!debug_level_)
252 return;
253
254 if ((debug_level_ < 2) && (reg != TPM_DATA_FIFO_REG))
255 return;
256 }
257
258 /*
259 * Do not print register address again if the last dump print was for
260 * that register.
261 */
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100262 if (prev_prefix != *prefix || (prev_reg != reg)) {
263 prev_prefix = *prefix;
264 prev_reg = reg;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700265 printk(BIOS_DEBUG, "\n%s %2.2x:", prefix, reg);
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100266 current_char = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700267 }
268
269 if ((reg != TPM_DATA_FIFO_REG) && (bytes == 4)) {
270 /*
271 * This must be a regular register address, print the 32 bit
272 * value.
273 */
274 printk(BIOS_DEBUG, " %8.8x", *(const uint32_t *)buffer);
275 } else {
276 int i;
277
278 /*
279 * Data read from or written to FIFO or not in 4 byte
280 * quantiites is printed byte at a time.
281 */
282 for (i = 0; i < bytes; i++) {
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100283 if (current_char &&
284 !(current_char % BYTES_PER_LINE)) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700285 printk(BIOS_DEBUG, "\n ");
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100286 current_char = 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700287 }
Arthur Heymans0ca944b2019-11-20 19:51:06 +0100288 (current_char)++;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700289 printk(BIOS_DEBUG, " %2.2x", buffer[i]);
290 }
291 }
292}
293
294/*
295 * Once transaction is initiated and the TPM indicated that it is ready to go,
296 * write the actual bytes to the register.
297 */
298static void write_bytes(const void *buffer, size_t bytes)
299{
Patrick Georgic9b13592019-11-29 11:47:47 +0100300 spi_xfer(&spi_slave, buffer, bytes, NULL, 0);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700301}
302
303/*
304 * Once transaction is initiated and the TPM indicated that it is ready to go,
305 * read the actual bytes from the register.
306 */
307static void read_bytes(void *buffer, size_t bytes)
308{
Patrick Georgic9b13592019-11-29 11:47:47 +0100309 spi_xfer(&spi_slave, NULL, 0, buffer, bytes);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700310}
311
312/*
313 * To write a register, start transaction, transfer data to the TPM, deassert
314 * CS when done.
315 *
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800316 * Returns one to indicate success, zero to indicate failure.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700317 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600318static int tpm2_write_reg(unsigned int reg_number, const void *buffer, size_t bytes)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700319{
320 trace_dump("W", reg_number, bytes, buffer, 0);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800321 if (!start_transaction(false, bytes, reg_number))
322 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700323 write_bytes(buffer, bytes);
Patrick Georgic9b13592019-11-29 11:47:47 +0100324 spi_release_bus(&spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700325 return 1;
326}
327
328/*
329 * To read a register, start transaction, transfer data from the TPM, deassert
330 * CS when done.
331 *
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700332 * Returns one to indicate success, zero to indicate failure. In case of
333 * failure zero out the user buffer.
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700334 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600335static int tpm2_read_reg(unsigned int reg_number, void *buffer, size_t bytes)
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700336{
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800337 if (!start_transaction(true, bytes, reg_number)) {
338 memset(buffer, 0, bytes);
339 return 0;
340 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700341 read_bytes(buffer, bytes);
Patrick Georgic9b13592019-11-29 11:47:47 +0100342 spi_release_bus(&spi_slave);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700343 trace_dump("R", reg_number, bytes, buffer, 0);
344 return 1;
345}
346
347/*
348 * Status register is accessed often, wrap reading and writing it into
349 * dedicated functions.
350 */
351static int read_tpm_sts(uint32_t *status)
352{
353 return tpm2_read_reg(TPM_STS_REG, status, sizeof(*status));
354}
355
356static int write_tpm_sts(uint32_t status)
357{
358 return tpm2_write_reg(TPM_STS_REG, &status, sizeof(status));
359}
360
361/*
362 * The TPM may limit the transaction bytes count (burst count) below the 64
363 * bytes max. The current value is available as a field of the status
364 * register.
365 */
366static uint32_t get_burst_count(void)
367{
368 uint32_t status;
369
370 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700371 return (status & TPM_STS_BURST_COUNT_MASK) >> TPM_STS_BURST_COUNT_SHIFT;
372}
373
374static uint8_t tpm2_read_access_reg(void)
375{
376 uint8_t access;
377 tpm2_read_reg(TPM_ACCESS_REG, &access, sizeof(access));
378 /* We do not care about access establishment bit state. Ignore it. */
379 return access & ~TPM_ACCESS_ESTABLISHMENT;
380}
381
382static void tpm2_write_access_reg(uint8_t cmd)
383{
384 /* Writes to access register can set only 1 bit at a time. */
385 assert (!(cmd & (cmd - 1)));
386
387 tpm2_write_reg(TPM_ACCESS_REG, &cmd, sizeof(cmd));
388}
389
390static int tpm2_claim_locality(void)
391{
392 uint8_t access;
Shelley Chen85eb0312017-11-07 14:24:19 -0800393 struct stopwatch sw;
Furquan Shaikh260b2972017-04-07 13:26:01 -0700394
Furquan Shaikh260b2972017-04-07 13:26:01 -0700395 /*
Vadim Bendebury8727e642017-11-16 21:00:41 -0800396 * Locality is released by TPM reset.
397 *
398 * If locality is taken at this point, this could be due to the fact
399 * that the TPM is performing a long operation and has not processed
400 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
401 * it releases locality when reset is processed.
Shelley Chen85eb0312017-11-07 14:24:19 -0800402 */
403 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
Vadim Bendebury8727e642017-11-16 21:00:41 -0800404 do {
Shelley Chen85eb0312017-11-07 14:24:19 -0800405 access = tpm2_read_access_reg();
Vadim Bendebury8727e642017-11-16 21:00:41 -0800406 if (access & TPM_ACCESS_ACTIVE_LOCALITY) {
407 /*
408 * Don't bombard the chip with traffic, let it keep
409 * processing the command.
410 */
411 mdelay(2);
412 continue;
413 }
Furquan Shaikh260b2972017-04-07 13:26:01 -0700414
Vadim Bendebury8727e642017-11-16 21:00:41 -0800415 /*
416 * Ok, the locality is free, TPM must be reset, let's claim
417 * it.
418 */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700419
Vadim Bendebury8727e642017-11-16 21:00:41 -0800420 tpm2_write_access_reg(TPM_ACCESS_REQUEST_USE);
421 access = tpm2_read_access_reg();
422 if (access != (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
423 break;
424 }
425
426 printk(BIOS_INFO, "TPM ready after %ld ms\n",
427 stopwatch_duration_msecs(&sw));
428
429 return 1;
430 } while (!stopwatch_expired(&sw));
431
432 printk(BIOS_ERR,
433 "Failed to claim locality 0 after %ld ms, status: %#x\n",
434 stopwatch_duration_msecs(&sw), access);
435
436 return 0;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700437}
438
Jes Klinkedcae8072020-07-29 14:22:41 -0700439static int cr50_parse_fw_version(const char *version_str, struct cr50_firmware_version *ver)
440{
441 int epoch, major, minor;
442
443 char *number = strstr(version_str, " RW_A:");
444 if (!number)
445 number = strstr(version_str, " RW_B:");
446 if (!number)
447 return -1;
448 number += 6; /* Skip past the colon. */
449
450 epoch = skip_atoi(&number);
451 if (*number++ != '.')
452 return -2;
453 major = skip_atoi(&number);
454 if (*number++ != '.')
455 return -2;
456 minor = skip_atoi(&number);
457
458 ver->epoch = epoch;
459 ver->major = major;
460 ver->minor = minor;
461 return 0;
462}
463
464static int cr50_fw_supports_board_cfg(struct cr50_firmware_version *version)
465{
466 /* Cr50 supports the CR50_BOARD_CFG register from version 0.5.5 / 0.6.5
467 * and onwards. */
468 if (version->epoch > 0 || version->major >= 7
469 || (version->major >= 5 && version->minor >= 5))
470 return 1;
471 printk(BIOS_INFO, "Cr50 firmware does not support CR50_BOARD_CFG, version: %d.%d.%d\n",
472 version->epoch, version->major, version->minor);
473 return 0;
474}
475
476/**
477 * Set the BOARD_CFG register on the TPM chip to a particular compile-time constant value.
478 */
479static void cr50_set_board_cfg(void)
480{
481 uint32_t board_cfg_value;
482 if (!cr50_fw_supports_board_cfg(&cr50_firmware_version))
483 return;
484 /* Set the CR50_BOARD_CFG register, for e.g. asking cr50 to use longer ready pulses. */
485 if (!tpm2_read_reg(CR50_BOARD_CFG, &board_cfg_value, sizeof(board_cfg_value))) {
486 printk(BIOS_INFO, "Error reading from cr50\n");
487 return;
488 }
489 if ((board_cfg_value & CR50_BOARD_CFG_FEATUREBITS_MASK) == CR50_BOARD_CFG_VALUE) {
490 printk(BIOS_INFO,
491 "Current CR50_BOARD_CFG = 0x%08x, matches desired = 0x%08x\n",
492 board_cfg_value, CR50_BOARD_CFG_VALUE);
493 return;
494 }
495 if (board_cfg_value & CR50_BOARD_CFG_LOCKBIT_MASK) {
496 /* The high bit is set, meaning that the Cr50 is already locked on a particular
497 * value for the register, but not the one we wanted. */
498 printk(BIOS_ERR,
499 "ERROR: Current CR50_BOARD_CFG = 0x%08x, does not match desired = 0x%08x\n",
500 board_cfg_value, CR50_BOARD_CFG_VALUE);
501 return;
502 }
503 printk(BIOS_INFO, "Current CR50_BOARD_CFG = 0x%08x, setting to 0x%08x\n",
504 board_cfg_value, CR50_BOARD_CFG_VALUE);
505 board_cfg_value = CR50_BOARD_CFG_VALUE;
506 if (!tpm2_write_reg(CR50_BOARD_CFG, &board_cfg_value, sizeof(board_cfg_value)))
507 printk(BIOS_INFO, "Error writing to cr50\n");
508}
509
510/*
511 * Expose method to read the CR50_BOARD_CFG register, will return zero if
512 * register not supported by Cr50 firmware.
513 */
514static uint32_t cr50_get_board_cfg(void)
515{
516 uint32_t board_cfg_value;
517 if (!cr50_fw_supports_board_cfg(&cr50_firmware_version))
518 return 0;
519 if (!tpm2_read_reg(CR50_BOARD_CFG, &board_cfg_value, sizeof(board_cfg_value))) {
520 printk(BIOS_INFO, "Error reading from cr50\n");
521 return 0;
522 }
523 return board_cfg_value & CR50_BOARD_CFG_FEATUREBITS_MASK;
524}
525
526bool cr50_is_long_interrupt_pulse_enabled(void)
527{
528 return cr50_get_board_cfg() & CR50_BOARD_CFG_100US_READY_PULSE;
529}
530
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700531/* Device/vendor ID values of the TPM devices this driver supports. */
532static const uint32_t supported_did_vids[] = {
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100533 0x00281ae0, /* H1 based Cr50 security chip. */
534 0x0000104a /* ST33HTPH2E32 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700535};
536
Jes Klinkedcae8072020-07-29 14:22:41 -0700537static int first_access_this_boot(void)
538{
539 return ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK || !CONFIG(VBOOT);
540}
541
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700542int tpm2_init(struct spi_slave *spi_if)
543{
544 uint32_t did_vid, status;
545 uint8_t cmd;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700546 int retries;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700547
Patrick Georgic9b13592019-11-29 11:47:47 +0100548 memcpy(&spi_slave, spi_if, sizeof(*spi_if));
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700549
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200550 /* clear any pending IRQs */
Shelley Chenf2e7b372017-12-15 15:25:08 -0800551 tis_plat_irq_status();
552
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800553 /*
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700554 * 150 ms should be enough to synchronize with the TPM even under the
555 * worst nested reset request conditions. In vast majority of cases
556 * there would be no wait at all.
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800557 */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700558 printk(BIOS_INFO, "Probing TPM: ");
559 for (retries = 15; retries > 0; retries--) {
560 int i;
561
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200562 /* In case of failure to read div_vid is set to zero. */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700563 tpm2_read_reg(TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
564
565 for (i = 0; i < ARRAY_SIZE(supported_did_vids); i++)
566 if (did_vid == supported_did_vids[i])
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200567 break; /* TPM is up and ready. */
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700568
569 if (i < ARRAY_SIZE(supported_did_vids))
570 break;
571
572 /* TPM might be resetting, let's retry in a bit. */
573 mdelay(10);
574 printk(BIOS_INFO, ".");
575 }
576
577 if (!retries) {
578 printk(BIOS_ERR, "\n%s: Failed to connect to the TPM\n",
579 __func__);
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800580 return -1;
Vadim Bendebury9a506d52017-10-25 15:45:00 -0700581 }
582
583 printk(BIOS_INFO, " done!\n");
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700584
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100585 // FIXME: Move this to tpm_setup()
Jes Klinkedcae8072020-07-29 14:22:41 -0700586 if (first_access_this_boot())
Vadim Bendebury8727e642017-11-16 21:00:41 -0800587 /*
588 * Claim locality 0, do it only during the first
589 * initialization after reset.
590 */
591 if (!tpm2_claim_locality())
592 return -1;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700593
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +0100594 if (!read_tpm_sts(&status)) {
595 printk(BIOS_ERR, "Reading status reg failed\n");
596 return -1;
597 }
Furquan Shaikh260b2972017-04-07 13:26:01 -0700598 if ((status & TPM_STS_FAMILY_MASK) != TPM_STS_FAMILY_TPM_2_0) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700599 printk(BIOS_ERR, "unexpected TPM family value, status: %#x\n",
600 status);
601 return -1;
602 }
603
604 /*
605 * Locality claimed, read the revision value and set up the tpm_info
606 * structure.
607 */
608 tpm2_read_reg(TPM_RID_REG, &cmd, sizeof(cmd));
Patrick Georgic9b13592019-11-29 11:47:47 +0100609 tpm_info.vendor_id = did_vid & 0xffff;
610 tpm_info.device_id = did_vid >> 16;
611 tpm_info.revision = cmd;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700612
613 printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
Patrick Georgic9b13592019-11-29 11:47:47 +0100614 tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700615
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700616 /* Let's report device FW version if available. */
Jes Klinkedcae8072020-07-29 14:22:41 -0700617 if (CONFIG(TPM_CR50) && tpm_info.vendor_id == 0x1ae0) {
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700618 int chunk_count = 0;
Jes Klinkedcae8072020-07-29 14:22:41 -0700619 size_t chunk_size = 50;
620 char version_str[301];
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700621
622 /*
623 * Does not really matter what's written, this just makes sure
624 * the version is reported from the beginning.
625 */
Vadim Bendebury9e561f82016-07-31 11:19:20 -0700626 tpm2_write_reg(TPM_FW_VER, &chunk_size, 1);
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700627
Jes Klinkedcae8072020-07-29 14:22:41 -0700628 /*
629 * Read chunk_size bytes at a time, last chunk will be zero padded.
630 */
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700631 do {
Jes Klinkedcae8072020-07-29 14:22:41 -0700632 tpm2_read_reg(TPM_FW_VER,
633 version_str + chunk_count * chunk_size,
634 chunk_size);
635 if (!version_str[++chunk_count * chunk_size - 1])
636 /* Zero padding detected: end of string. */
637 break;
638 /* Check if there is enough room for reading one more chunk. */
639 } while (chunk_count * chunk_size < sizeof(version_str) - chunk_size);
640 version_str[chunk_count * chunk_size] = '\0';
641 printk(BIOS_INFO, "Firmware version: %s\n", version_str);
642 if (cr50_parse_fw_version(version_str, &cr50_firmware_version)) {
643 printk(BIOS_ERR, "Did not recognize Cr50 version format\n");
644 return -1;
645 }
646 if (CR50_BOARD_CFG_VALUE) {
647 if (first_access_this_boot())
648 cr50_set_board_cfg();
649 }
Vadim Bendebury58826fc2016-06-23 18:17:33 -0700650 }
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700651 return 0;
652}
653
654/*
655 * This is in seconds, certain TPM commands, like key generation, can take
656 * long time to complete.
657 *
658 * Returns one to indicate success, zero (not yet implemented) to indicate
659 * failure.
660 */
661#define MAX_STATUS_TIMEOUT 120
662static int wait_for_status(uint32_t status_mask, uint32_t status_expected)
663{
664 uint32_t status;
665 struct stopwatch sw;
666
667 stopwatch_init_usecs_expire(&sw, MAX_STATUS_TIMEOUT * 1000 * 1000);
668 do {
669 udelay(1000);
670 if (stopwatch_expired(&sw)) {
671 printk(BIOS_ERR, "failed to get expected status %x\n",
672 status_expected);
673 return false;
674 }
675 read_tpm_sts(&status);
676 } while ((status & status_mask) != status_expected);
677
678 return 1;
679}
680
681enum fifo_transfer_direction {
682 fifo_transmit = 0,
683 fifo_receive = 1
684};
685
686/* Union allows to avoid casting away 'const' on transmit buffers. */
687union fifo_transfer_buffer {
688 uint8_t *rx_buffer;
689 const uint8_t *tx_buffer;
690};
691
692/*
693 * Transfer requested number of bytes to or from TPM FIFO, accounting for the
694 * current burst count value.
695 */
696static void fifo_transfer(size_t transfer_size,
697 union fifo_transfer_buffer buffer,
698 enum fifo_transfer_direction direction)
699{
700 size_t transaction_size;
701 size_t burst_count;
702 size_t handled_so_far = 0;
703
704 do {
705 do {
706 /* Could be zero when TPM is busy. */
707 burst_count = get_burst_count();
708 } while (!burst_count);
709
710 transaction_size = transfer_size - handled_so_far;
711 transaction_size = MIN(transaction_size, burst_count);
712
713 /*
714 * The SPI frame header does not allow to pass more than 64
715 * bytes.
716 */
717 transaction_size = MIN(transaction_size, 64);
718
719 if (direction == fifo_receive)
720 tpm2_read_reg(TPM_DATA_FIFO_REG,
721 buffer.rx_buffer + handled_so_far,
722 transaction_size);
723 else
724 tpm2_write_reg(TPM_DATA_FIFO_REG,
725 buffer.tx_buffer + handled_so_far,
726 transaction_size);
727
728 handled_so_far += transaction_size;
729
730 } while (handled_so_far != transfer_size);
731}
732
733size_t tpm2_process_command(const void *tpm2_command, size_t command_size,
734 void *tpm2_response, size_t max_response)
735{
736 uint32_t status;
737 uint32_t expected_status_bits;
738 size_t payload_size;
739 size_t bytes_to_go;
740 const uint8_t *cmd_body = tpm2_command;
741 uint8_t *rsp_body = tpm2_response;
742 union fifo_transfer_buffer fifo_buffer;
743 const int HEADER_SIZE = 6;
744
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800745 /* Do not try using an uninitialized TPM. */
Patrick Georgic9b13592019-11-29 11:47:47 +0100746 if (!tpm_info.vendor_id)
Vadim Bendebury731ef9b2016-12-15 21:49:23 -0800747 return 0;
748
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700749 /* Skip the two byte tag, read the size field. */
750 payload_size = read_be32(cmd_body + 2);
751
752 /* Sanity check. */
753 if (payload_size != command_size) {
754 printk(BIOS_ERR,
755 "Command size mismatch: encoded %zd != requested %zd\n",
756 payload_size, command_size);
757 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
758 printk(BIOS_DEBUG, "\n");
759 return 0;
760 }
761
762 /* Let the TPM know that the command is coming. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700763 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700764
765 /*
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200766 * TPM commands and responses written to and read from the FIFO
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700767 * register (0x24) are datagrams of variable size, prepended by a 6
768 * byte header.
769 *
770 * The specification description of the state machine is a bit vague,
771 * but from experience it looks like there is no need to wait for the
772 * sts.expect bit to be set, at least with the 9670 and cr50 devices.
773 * Just write the command into FIFO, making sure not to exceed the
774 * burst count or the maximum PDU size, whatever is smaller.
775 */
776 fifo_buffer.tx_buffer = cmd_body;
777 fifo_transfer(command_size, fifo_buffer, fifo_transmit);
778
779 /* Now tell the TPM it can start processing the command. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700780 write_tpm_sts(TPM_STS_GO);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700781
782 /* Now wait for it to report that the response is ready. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700783 expected_status_bits = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700784 if (!wait_for_status(expected_status_bits, expected_status_bits)) {
785 /*
786 * If timed out, which should never happen, let's at least
787 * print out the offending command.
788 */
789 trace_dump("W", TPM_DATA_FIFO_REG, command_size, cmd_body, 1);
790 printk(BIOS_DEBUG, "\n");
791 return 0;
792 }
793
794 /*
795 * The response is ready, let's read it. First we read the FIFO
796 * payload header, to see how much data to expect. The response header
797 * size is fixed to six bytes, the total payload size is stored in
798 * network order in the last four bytes.
799 */
800 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body, HEADER_SIZE);
801
802 /* Find out the total payload size, skipping the two byte tag. */
803 payload_size = read_be32(rsp_body + 2);
804
805 if (payload_size > max_response) {
806 /*
807 * TODO(vbendeb): at least drain the FIFO here or somehow let
808 * the TPM know that the response can be dropped.
809 */
Elyes HAOUAS6688f462018-08-29 17:22:44 +0200810 printk(BIOS_ERR, " TPM response too long (%zd bytes)",
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700811 payload_size);
812 return 0;
813 }
814
815 /*
816 * Now let's read all but the last byte in the FIFO to make sure the
817 * status register is showing correct flow control bits: 'more data'
818 * until the last byte and then 'no more data' once the last byte is
819 * read.
820 */
821 bytes_to_go = payload_size - 1 - HEADER_SIZE;
822 fifo_buffer.rx_buffer = rsp_body + HEADER_SIZE;
823 fifo_transfer(bytes_to_go, fifo_buffer, fifo_receive);
824
825 /* Verify that there is still data to read. */
826 read_tpm_sts(&status);
827 if ((status & expected_status_bits) != expected_status_bits) {
828 printk(BIOS_ERR, "unexpected intermediate status %#x\n",
829 status);
830 return 0;
831 }
832
833 /* Read the last byte of the PDU. */
834 tpm2_read_reg(TPM_DATA_FIFO_REG, rsp_body + payload_size - 1, 1);
835
836 /* Terminate the dump, if enabled. */
837 if (debug_level_)
838 printk(BIOS_DEBUG, "\n");
839
840 /* Verify that 'data available' is not asseretd any more. */
841 read_tpm_sts(&status);
Furquan Shaikh260b2972017-04-07 13:26:01 -0700842 if ((status & expected_status_bits) != TPM_STS_VALID) {
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700843 printk(BIOS_ERR, "unexpected final status %#x\n", status);
844 return 0;
845 }
846
847 /* Move the TPM back to idle state. */
Furquan Shaikh260b2972017-04-07 13:26:01 -0700848 write_tpm_sts(TPM_STS_COMMAND_READY);
Vadim Bendeburye31d2432016-04-09 18:33:49 -0700849
850 return payload_size;
851}