blob: 9802654ee1d30040c12ba5c5c81f1b755b3ad580 [file] [log] [blame]
Stefan Reinauer3008bbad2011-10-11 14:46:25 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
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 Reinauer3008bbad2011-10-11 14:46:25 -070014 */
15
16/*
17 * The code in this file has been heavily based on the article "Writing a TPM
18 * Device Driver" published on http://ptgmedia.pearsoncmg.com and the
19 * submission by Stefan Berger on Qemu-devel mailing list.
20 *
21 * One principal difference is that in the simplest config the other than 0
22 * TPM localities do not get mapped by some devices (for instance, by
23 * Infineon slb9635), so this driver provides access to locality 0 only.
24 */
25
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070026#include <stdlib.h>
27#include <string.h>
28#include <delay.h>
29#include <arch/io.h>
Naresh G Solanki80ff0382016-11-15 11:01:33 +053030#include <arch/acpi.h>
31#include <arch/acpigen.h>
32#include <arch/acpi_device.h>
33#include <device/device.h>
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070034#include <console/console.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +020035#include <security/tpm/tis.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070036#include <arch/early_variables.h>
Duncan Lauriedd281ed2014-10-30 15:20:19 -070037#include <device/pnp.h>
38#include "chip.h"
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070039
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070040#define PREFIX "lpc_tpm: "
Naresh G Solanki80ff0382016-11-15 11:01:33 +053041/* TCG Physical Presence Interface */
42#define TPM_PPI_UUID "3dddfaa6-361b-4eb4-a424-8d10089d1653"
43/* TCG Memory Clear Interface */
44#define TPM_MCI_UUID "376054ed-cc13-4675-901c-4756d7f2d45d"
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070045/* coreboot wrapper for TPM driver (start) */
46#define TPM_DEBUG(fmt, args...) \
Denis 'GNUtoo' Carikli0e92bb02016-02-20 17:32:03 +010047 if (IS_ENABLED(CONFIG_DEBUG_TPM)) { \
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070048 printk(BIOS_DEBUG, PREFIX); \
49 printk(BIOS_DEBUG, fmt , ##args); \
50 }
Aaron Durbinc4220022013-07-24 16:02:14 -050051#define TPM_DEBUG_IO_READ(reg_, val_) \
52 TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_))
53#define TPM_DEBUG_IO_WRITE(reg_, val_) \
54 TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_))
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070055#define printf(x...) printk(BIOS_ERR, x)
56
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070057/* coreboot wrapper for TPM driver (end) */
58
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070059/* the macro accepts the locality value, but only locality 0 is operational */
60#define TIS_REG(LOCALITY, REG) \
Martin Rothb9810a42017-07-23 20:00:04 -060061 (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070062
63/* hardware registers' offsets */
64#define TIS_REG_ACCESS 0x0
65#define TIS_REG_INT_ENABLE 0x8
66#define TIS_REG_INT_VECTOR 0xc
67#define TIS_REG_INT_STATUS 0x10
68#define TIS_REG_INTF_CAPABILITY 0x14
69#define TIS_REG_STS 0x18
Aaron Durbinc4220022013-07-24 16:02:14 -050070#define TIS_REG_BURST_COUNT 0x19
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070071#define TIS_REG_DATA_FIFO 0x24
72#define TIS_REG_DID_VID 0xf00
73#define TIS_REG_RID 0xf04
74
75/* Some registers' bit field definitions */
76#define TIS_STS_VALID (1 << 7) /* 0x80 */
77#define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
78#define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
79#define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
80#define TIS_STS_EXPECT (1 << 3) /* 0x08 */
81#define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
82
83#define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
84#define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
85#define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
86#define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
87#define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
88#define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
89#define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
90
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070091/*
92 * Error value returned if a tpm register does not enter the expected state
93 * after continuous polling. No actual TPM register reading ever returns ~0,
94 * so this value is a safe error indication to be mixed with possible status
95 * register values.
96 */
97#define TPM_TIMEOUT_ERR (~0)
98
99/* Error value returned on various TPM driver errors */
100#define TPM_DRIVER_ERR (~0)
101
102 /* 1 second is plenty for anything TPM does.*/
103#define MAX_DELAY_US (1000 * 1000)
104
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700105/*
106 * Structures defined below allow creating descriptions of TPM vendor/device
107 * ID information for run time discovery. The only device the system knows
108 * about at this time is Infineon slb9635
109 */
110struct device_name {
111 u16 dev_id;
112 const char * const dev_name;
113};
114
115struct vendor_name {
116 u16 vendor_id;
117 const char * vendor_name;
Stefan Reinauerc668af72011-10-27 21:28:25 +0000118 const struct device_name* dev_names;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700119};
120
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700121static const struct device_name atmel_devices[] = {
122 {0x3204, "AT97SC3204"},
123 {0xffff}
124};
125
Stefan Reinauerc668af72011-10-27 21:28:25 +0000126static const struct device_name infineon_devices[] = {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700127 {0x000b, "SLB9635 TT 1.2"},
Subrataf4d65872015-05-14 14:38:07 +0530128 {0x001a, "SLB9660 TT 1.2"},
Subrata Banik5b8c4a72016-11-11 09:28:45 +0530129#if IS_ENABLED(CONFIG_TPM2)
130 {0x001b, "SLB9670 TT 2.0"},
131#else
Wenkai Du641529b2015-05-29 10:54:27 -0700132 {0x001b, "SLB9670 TT 1.2"},
Subrata Banik5b8c4a72016-11-11 09:28:45 +0530133#endif
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700134 {0xffff}
135};
136
137static const struct device_name nuvoton_devices[] = {
138 {0x00fe, "NPCT420AA V2"},
139 {0xffff}
140};
141
142static const struct device_name stmicro_devices[] = {
143 {0x0000, "ST33ZP24" },
144 {0xffff}
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700145};
146
147static const struct vendor_name vendor_names[] = {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700148 {0x1114, "Atmel", atmel_devices},
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700149 {0x15d1, "Infineon", infineon_devices},
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700150 {0x1050, "Nuvoton", nuvoton_devices},
151 {0x104a, "ST Microelectronics", stmicro_devices},
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700152};
153
154/*
155 * Cached vendor/device ID pair to indicate that the device has been already
156 * discovered
157 */
Stefan Reinauerc668af72011-10-27 21:28:25 +0000158static u32 vendor_dev_id CAR_GLOBAL;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700159
Aaron Durbinc4220022013-07-24 16:02:14 -0500160static inline u8 tpm_read_status(int locality)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700161{
Julius Werner2f37bd62015-02-19 14:51:15 -0800162 u8 value = read8(TIS_REG(locality, TIS_REG_STS));
Aaron Durbinc4220022013-07-24 16:02:14 -0500163 TPM_DEBUG_IO_READ(TIS_REG_STS, value);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700164 return value;
165}
166
Aaron Durbinc4220022013-07-24 16:02:14 -0500167static inline void tpm_write_status(u8 sts, int locality)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700168{
Aaron Durbinc4220022013-07-24 16:02:14 -0500169 TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts);
Julius Werner2f37bd62015-02-19 14:51:15 -0800170 write8(TIS_REG(locality, TIS_REG_STS), sts);
Aaron Durbinc4220022013-07-24 16:02:14 -0500171}
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700172
Aaron Durbinc4220022013-07-24 16:02:14 -0500173static inline u8 tpm_read_data(int locality)
174{
Julius Werner2f37bd62015-02-19 14:51:15 -0800175 u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO));
Aaron Durbinc4220022013-07-24 16:02:14 -0500176 TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value);
177 return value;
178}
179
180static inline void tpm_write_data(u8 data, int locality)
181{
182 TPM_DEBUG_IO_WRITE(TIS_REG_STS, data);
Julius Werner2f37bd62015-02-19 14:51:15 -0800183 write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data);
Aaron Durbinc4220022013-07-24 16:02:14 -0500184}
185
186static inline u16 tpm_read_burst_count(int locality)
187{
188 u16 count;
Julius Werner2f37bd62015-02-19 14:51:15 -0800189 count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT));
190 count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8;
Aaron Durbinc4220022013-07-24 16:02:14 -0500191 TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count);
192 return count;
193}
194
195static inline u8 tpm_read_access(int locality)
196{
Julius Werner2f37bd62015-02-19 14:51:15 -0800197 u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS));
Aaron Durbinc4220022013-07-24 16:02:14 -0500198 TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value);
199 return value;
200}
201
202static inline void tpm_write_access(u8 data, int locality)
203{
204 TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data);
Julius Werner2f37bd62015-02-19 14:51:15 -0800205 write8(TIS_REG(locality, TIS_REG_ACCESS), data);
Aaron Durbinc4220022013-07-24 16:02:14 -0500206}
207
208static inline u32 tpm_read_did_vid(int locality)
209{
Julius Werner2f37bd62015-02-19 14:51:15 -0800210 u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID));
Aaron Durbinc4220022013-07-24 16:02:14 -0500211 TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value);
212 return value;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700213}
214
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700215static inline void tpm_write_int_vector(int vector, int locality)
216{
217 TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector);
Julius Werner2f37bd62015-02-19 14:51:15 -0800218 write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf);
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700219}
220
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530221static inline u8 tpm_read_int_vector(int locality)
222{
223 u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR));
224 TPM_DEBUG_IO_READ(TIS_REG_INT_VECTOR, value);
225 return value;
226}
227
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700228static inline void tpm_write_int_polarity(int polarity, int locality)
229{
230 /* Set polarity and leave all other bits at 0 */
231 u32 value = (polarity & 0x3) << 3;
232 TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value);
Julius Werner2f37bd62015-02-19 14:51:15 -0800233 write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value);
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700234}
235
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530236static inline u32 tpm_read_int_polarity(int locality)
237{
238 /* Get polarity and leave all other bits */
239 u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE));
240 value = (value >> 3) & 0x3;
241 TPM_DEBUG_IO_READ(TIS_REG_INT_ENABLE, value);
242 return value;
243}
244
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700245/*
Aaron Durbinc4220022013-07-24 16:02:14 -0500246 * tis_wait_sts()
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700247 *
Aaron Durbinc4220022013-07-24 16:02:14 -0500248 * Wait for at least a second for a status to change its state to match the
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700249 * expected state. Normally the transition happens within microseconds.
250 *
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700251 * @locality - locality
252 * @mask - bitmask for the bitfield(s) to watch
253 * @expected - value the field(s) are supposed to be set to
254 *
Aaron Durbinc4220022013-07-24 16:02:14 -0500255 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700256 */
Aaron Durbinc4220022013-07-24 16:02:14 -0500257static int tis_wait_sts(int locality, u8 mask, u8 expected)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700258{
259 u32 time_us = MAX_DELAY_US;
260 while (time_us > 0) {
Aaron Durbinc4220022013-07-24 16:02:14 -0500261 u8 value = tpm_read_status(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700262 if ((value & mask) == expected)
Aaron Durbinc4220022013-07-24 16:02:14 -0500263 return 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700264 udelay(1); /* 1 us */
265 time_us--;
266 }
267 return TPM_TIMEOUT_ERR;
268}
269
Aaron Durbinc4220022013-07-24 16:02:14 -0500270static inline int tis_wait_ready(int locality)
271{
272 return tis_wait_sts(locality, TIS_STS_COMMAND_READY,
273 TIS_STS_COMMAND_READY);
274}
275
276static inline int tis_wait_valid(int locality)
277{
278 return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID);
279}
280
281static inline int tis_wait_valid_data(int locality)
282{
283 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
284 return tis_wait_sts(locality, has_data, has_data);
285}
286
287static inline int tis_has_valid_data(int locality)
288{
289 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
290 return (tpm_read_status(locality) & has_data) == has_data;
291}
292
293static inline int tis_expect_data(int locality)
294{
295 return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
296}
297
298/*
299 * tis_wait_access()
300 *
301 * Wait for at least a second for a access to change its state to match the
302 * expected state. Normally the transition happens within microseconds.
303 *
304 * @locality - locality
305 * @mask - bitmask for the bitfield(s) to watch
306 * @expected - value the field(s) are supposed to be set to
307 *
308 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
309 */
310static int tis_wait_access(int locality, u8 mask, u8 expected)
311{
312 u32 time_us = MAX_DELAY_US;
313 while (time_us > 0) {
314 u8 value = tpm_read_access(locality);
315 if ((value & mask) == expected)
316 return 0;
317 udelay(1); /* 1 us */
318 time_us--;
319 }
320 return TPM_TIMEOUT_ERR;
321}
322
323static inline int tis_wait_dropped_access(int locality)
324{
325 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0);
326}
327
328static inline int tis_wait_received_access(int locality)
329{
330 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY,
331 TIS_ACCESS_ACTIVE_LOCALITY);
332}
333
334static inline int tis_has_access(int locality)
335{
336 return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
337}
338
339static inline void tis_request_access(int locality)
340{
341 tpm_write_access(TIS_ACCESS_REQUEST_USE, locality);
342}
343
344static inline void tis_drop_access(int locality)
345{
346 tpm_write_access(TIS_ACCESS_ACTIVE_LOCALITY, locality);
347}
348
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700349/*
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700350 * PC Client Specific TPM Interface Specification section 11.2.12:
351 *
352 * Software must be prepared to send two writes of a "1" to command ready
353 * field: the first to indicate successful read of all the data, thus
354 * clearing the data from the ReadFIFO and freeing the TPM's resources,
355 * and the second to indicate to the TPM it is about to send a new command.
356 *
357 * In practice not all TPMs behave the same so it is necessary to be
358 * flexible when trying to set command ready.
359 *
360 * Returns 0 on success if the TPM is ready for transactions.
361 * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set.
362 */
363static int tis_command_ready(u8 locality)
364{
365 u32 status;
366
367 /* 1st attempt to set command ready */
Aaron Durbinc4220022013-07-24 16:02:14 -0500368 tpm_write_status(TIS_STS_COMMAND_READY, locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700369
370 /* Wait for response */
Aaron Durbinc4220022013-07-24 16:02:14 -0500371 status = tpm_read_status(locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700372
373 /* Check if command ready is set yet */
374 if (status & TIS_STS_COMMAND_READY)
375 return 0;
376
377 /* 2nd attempt to set command ready */
Aaron Durbinc4220022013-07-24 16:02:14 -0500378 tpm_write_status(TIS_STS_COMMAND_READY, locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700379
Aaron Durbinc4220022013-07-24 16:02:14 -0500380 return tis_wait_ready(locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700381}
382
383/*
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700384 * Probe the TPM device and try determining its manufacturer/device name.
385 *
386 * Returns 0 on success (the device is found or was found during an earlier
387 * invocation) or TPM_DRIVER_ERR if the device is not found.
388 */
389static u32 tis_probe(void)
390{
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700391 const char *device_name = "unknown";
392 const char *vendor_name = device_name;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700393 const struct device_name *dev;
394 u32 didvid;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700395 u16 vid, did;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700396 int i;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700397
Aaron Durbincb997d32013-05-10 00:40:56 -0500398 if (car_get_var(vendor_dev_id))
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700399 return 0; /* Already probed. */
400
Aaron Durbinc4220022013-07-24 16:02:14 -0500401 didvid = tpm_read_did_vid(0);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700402 if (!didvid || (didvid == 0xffffffff)) {
403 printf("%s: No TPM device found\n", __FUNCTION__);
404 return TPM_DRIVER_ERR;
405 }
406
Aaron Durbincb997d32013-05-10 00:40:56 -0500407 car_set_var(vendor_dev_id, didvid);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700408
409 vid = didvid & 0xffff;
410 did = (didvid >> 16) & 0xffff;
411 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
412 int j = 0;
413 u16 known_did;
414 if (vid == vendor_names[i].vendor_id) {
415 vendor_name = vendor_names[i].vendor_name;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700416 } else {
417 continue;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700418 }
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700419 dev = &vendor_names[i].dev_names[j];
420 while ((known_did = dev->dev_id) != 0xffff) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700421 if (known_did == did) {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700422 device_name = dev->dev_name;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700423 break;
424 }
425 j++;
Subrata41b08d92015-05-14 14:38:07 +0530426 dev = &vendor_names[i].dev_names[j];
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700427 }
428 break;
429 }
430 /* this will have to be converted into debug printout */
Duncan Laurie17ba9442015-09-03 16:00:49 -0700431 printk(BIOS_INFO, "Found TPM %s by %s\n", device_name, vendor_name);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700432 return 0;
433}
434
435/*
436 * tis_senddata()
437 *
438 * send the passed in data to the TPM device.
439 *
440 * @data - address of the data to send, byte by byte
441 * @len - length of the data to send
442 *
443 * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
444 * not accept the entire command).
445 */
446static u32 tis_senddata(const u8 * const data, u32 len)
447{
448 u32 offset = 0;
449 u16 burst = 0;
450 u32 max_cycles = 0;
451 u8 locality = 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700452
Aaron Durbinc4220022013-07-24 16:02:14 -0500453 if (tis_wait_ready(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700454 printf("%s:%d - failed to get 'command_ready' status\n",
455 __FILE__, __LINE__);
456 return TPM_DRIVER_ERR;
457 }
Aaron Durbinc4220022013-07-24 16:02:14 -0500458 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700459
460 while (1) {
461 unsigned count;
462
463 /* Wait till the device is ready to accept more data. */
464 while (!burst) {
465 if (max_cycles++ == MAX_DELAY_US) {
466 printf("%s:%d failed to feed %d bytes of %d\n",
467 __FILE__, __LINE__, len - offset, len);
468 return TPM_DRIVER_ERR;
469 }
470 udelay(1);
Aaron Durbinc4220022013-07-24 16:02:14 -0500471 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700472 }
473
474 max_cycles = 0;
475
476 /*
477 * Calculate number of bytes the TPM is ready to accept in one
478 * shot.
479 *
480 * We want to send the last byte outside of the loop (hence
481 * the -1 below) to make sure that the 'expected' status bit
482 * changes to zero exactly after the last byte is fed into the
483 * FIFO.
484 */
485 count = min(burst, len - offset - 1);
486 while (count--)
Aaron Durbinc4220022013-07-24 16:02:14 -0500487 tpm_write_data(data[offset++], locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700488
Aaron Durbinc4220022013-07-24 16:02:14 -0500489 if (tis_wait_valid(locality) || !tis_expect_data(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700490 printf("%s:%d TPM command feed overflow\n",
491 __FILE__, __LINE__);
492 return TPM_DRIVER_ERR;
493 }
494
Aaron Durbinc4220022013-07-24 16:02:14 -0500495 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700496 if ((offset == (len - 1)) && burst)
497 /*
498 * We need to be able to send the last byte to the
499 * device, so burst size must be nonzero before we
500 * break out.
501 */
502 break;
503 }
504
505 /* Send the last byte. */
Aaron Durbinc4220022013-07-24 16:02:14 -0500506 tpm_write_data(data[offset++], locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700507
508 /*
509 * Verify that TPM does not expect any more data as part of this
510 * command.
511 */
Aaron Durbinc4220022013-07-24 16:02:14 -0500512 if (tis_wait_valid(locality) || tis_expect_data(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700513 printf("%s:%d unexpected TPM status 0x%x\n",
Aaron Durbinc4220022013-07-24 16:02:14 -0500514 __FILE__, __LINE__, tpm_read_status(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700515 return TPM_DRIVER_ERR;
516 }
517
518 /* OK, sitting pretty, let's start the command execution. */
Aaron Durbinc4220022013-07-24 16:02:14 -0500519 tpm_write_status(TIS_STS_TPM_GO, locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700520
521 return 0;
522}
523
524/*
525 * tis_readresponse()
526 *
527 * read the TPM device response after a command was issued.
528 *
529 * @buffer - address where to read the response, byte by byte.
530 * @len - pointer to the size of buffer
531 *
532 * On success stores the number of received bytes to len and returns 0. On
533 * errors (misformatted TPM data or synchronization problems) returns
534 * TPM_DRIVER_ERR.
535 */
536static u32 tis_readresponse(u8 *buffer, size_t *len)
537{
538 u16 burst_count;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700539 u32 offset = 0;
540 u8 locality = 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700541 u32 expected_count = *len;
542 int max_cycles = 0;
543
544 /* Wait for the TPM to process the command */
Aaron Durbinc4220022013-07-24 16:02:14 -0500545 if (tis_wait_valid_data(locality)) {
546 printf("%s:%d failed processing command\n", __FILE__, __LINE__);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700547 return TPM_DRIVER_ERR;
548 }
549
550 do {
Aaron Durbinc4220022013-07-24 16:02:14 -0500551 while ((burst_count = tpm_read_burst_count(locality)) == 0) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700552 if (max_cycles++ == MAX_DELAY_US) {
553 printf("%s:%d TPM stuck on read\n",
554 __FILE__, __LINE__);
555 return TPM_DRIVER_ERR;
556 }
557 udelay(1);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700558 }
559
560 max_cycles = 0;
561
562 while (burst_count-- && (offset < expected_count)) {
Aaron Durbinc4220022013-07-24 16:02:14 -0500563 buffer[offset++] = tpm_read_data(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700564 if (offset == 6) {
565 /*
566 * We got the first six bytes of the reply,
567 * let's figure out how many bytes to expect
568 * total - it is stored as a 4 byte number in
569 * network order, starting with offset 2 into
570 * the body of the reply.
571 */
572 u32 real_length;
573 memcpy(&real_length,
574 buffer + 2,
575 sizeof(real_length));
576 expected_count = be32_to_cpu(real_length);
577
578 if ((expected_count < offset) ||
579 (expected_count > *len)) {
580 printf("%s:%d bad response size %d\n",
581 __FILE__, __LINE__,
582 expected_count);
583 return TPM_DRIVER_ERR;
584 }
585 }
586 }
587
588 /* Wait for the next portion */
Aaron Durbinc4220022013-07-24 16:02:14 -0500589 if (tis_wait_valid(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700590 printf("%s:%d failed to read response\n",
591 __FILE__, __LINE__);
592 return TPM_DRIVER_ERR;
593 }
594
595 if (offset == expected_count)
596 break; /* We got all we need */
597
Aaron Durbinc4220022013-07-24 16:02:14 -0500598 } while (tis_has_valid_data(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700599
Aaron Durbinc4220022013-07-24 16:02:14 -0500600 /* * Make sure we indeed read all there was. */
601 if (tis_has_valid_data(locality)) {
602 printf("%s:%d wrong receive status: %x %d bytes left\n",
603 __FILE__, __LINE__, tpm_read_status(locality),
604 tpm_read_burst_count(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700605 return TPM_DRIVER_ERR;
606 }
607
608 /* Tell the TPM that we are done. */
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700609 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
610 return TPM_DRIVER_ERR;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700611
612 *len = offset;
613 return 0;
614}
615
616/*
617 * tis_init()
618 *
619 * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on
620 * failure (in case device probing did not succeed).
621 */
622int tis_init(void)
623{
624 if (tis_probe())
625 return TPM_DRIVER_ERR;
626 return 0;
627}
628
629/*
630 * tis_open()
631 *
632 * Requests access to locality 0 for the caller. After all commands have been
633 * completed the caller is supposed to call tis_close().
634 *
635 * Returns 0 on success, TPM_DRIVER_ERR on failure.
636 */
637int tis_open(void)
638{
639 u8 locality = 0; /* we use locality zero for everything */
640
641 if (tis_close())
642 return TPM_DRIVER_ERR;
643
644 /* now request access to locality */
Aaron Durbinc4220022013-07-24 16:02:14 -0500645 tis_request_access(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700646
647 /* did we get a lock? */
Aaron Durbinc4220022013-07-24 16:02:14 -0500648 if (tis_wait_received_access(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700649 printf("%s:%d - failed to lock locality %d\n",
650 __FILE__, __LINE__, locality);
651 return TPM_DRIVER_ERR;
652 }
653
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700654 /* Certain TPMs seem to need some delay here or they hang... */
655 udelay(10);
656
657 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
658 return TPM_DRIVER_ERR;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700659
660 return 0;
661}
662
663/*
664 * tis_close()
665 *
Martin Roth56889792013-07-09 21:39:46 -0600666 * terminate the current session with the TPM by releasing the locked
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700667 * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock
668 * removal did not succeed).
669 */
670int tis_close(void)
671{
672 u8 locality = 0;
Aaron Durbinc4220022013-07-24 16:02:14 -0500673 if (tis_has_access(locality)) {
674 tis_drop_access(locality);
675 if (tis_wait_dropped_access(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700676 printf("%s:%d - failed to release locality %d\n",
677 __FILE__, __LINE__, locality);
678 return TPM_DRIVER_ERR;
679 }
680 }
681 return 0;
682}
683
684/*
685 * tis_sendrecv()
686 *
687 * Send the requested data to the TPM and then try to get its response
688 *
689 * @sendbuf - buffer of the data to send
690 * @send_size size of the data to send
691 * @recvbuf - memory to save the response to
692 * @recv_len - pointer to the size of the response buffer
693 *
694 * Returns 0 on success (and places the number of response bytes at recv_len)
695 * or TPM_DRIVER_ERR on failure.
696 */
697int tis_sendrecv(const uint8_t *sendbuf, size_t send_size,
698 uint8_t *recvbuf, size_t *recv_len)
699{
700 if (tis_senddata(sendbuf, send_size)) {
701 printf("%s:%d failed sending data to TPM\n",
702 __FILE__, __LINE__);
703 return TPM_DRIVER_ERR;
704 }
705
706 return tis_readresponse(recvbuf, recv_len);
707}
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700708
709#ifdef __RAMSTAGE__
710
711/*
712 * tis_setup_interrupt()
713 *
714 * Set up the interrupt vector and polarity for locality 0 and
715 * disable all interrupts so they are unused in firmware but can
716 * be enabled by the OS.
717 *
718 * The values used here must match what is passed in the TPM ACPI
719 * device if ACPI is used on the platform.
720 *
721 * @vector - TPM interrupt vector
722 * @polarity - TPM interrupt polarity
723 *
724 * Returns 0 on success, TPM_DRIVER_ERR on failure.
725 */
726static int tis_setup_interrupt(int vector, int polarity)
727{
728 u8 locality = 0;
729 int has_access = tis_has_access(locality);
730
731 /* Open connection and request access if not already granted */
732 if (!has_access && tis_open() < 0)
733 return TPM_DRIVER_ERR;
734
735 /* Set TPM interrupt vector */
736 tpm_write_int_vector(vector, locality);
737
738 /* Set TPM interupt polarity and disable interrupts */
739 tpm_write_int_polarity(polarity, locality);
740
741 /* Close connection if it was opened */
742 if (!has_access && tis_close() < 0)
743 return TPM_DRIVER_ERR;
744
745 return 0;
746}
747
748static void lpc_tpm_read_resources(struct device *dev)
749{
750 /* Static 5K memory region specified in Kconfig */
751 mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10);
752}
753
754static void lpc_tpm_set_resources(struct device *dev)
755{
756 tpm_config_t *config = (tpm_config_t *)dev->chip_info;
757 struct resource *res;
758
759 for (res = dev->resource_list; res; res = res->next) {
760 if (!(res->flags & IORESOURCE_ASSIGNED))
761 continue;
762
763 if (res->flags & IORESOURCE_IRQ) {
764 /* Set interrupt vector */
765 tis_setup_interrupt((int)res->base,
766 config->irq_polarity);
767 } else {
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700768 continue;
769 }
770
771 res->flags |= IORESOURCE_STORED;
772 report_resource_stored(dev, res, " <tpm>");
773 }
774}
775
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530776#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
777
778static void tpm_ppi_func0_cb(void *arg)
779{
780 /* Functions 1-8. */
781 u8 buf[] = {0xff, 0x01};
782 acpigen_write_return_byte_buffer(buf, 2);
783}
784
785static void tpm_ppi_func1_cb(void *arg)
786{
787 if (IS_ENABLED(CONFIG_TPM2))
788 /* Interface version: 2.0 */
789 acpigen_write_return_string("2.0");
790 else
791 /* Interface version: 1.2 */
792 acpigen_write_return_string("1.2");
793}
794
795static void tpm_ppi_func2_cb(void *arg)
796{
797 /* Submit operations: drop on the floor and return success. */
798 acpigen_write_return_byte(0);
799}
800
801static void tpm_ppi_func3_cb(void *arg)
802{
803 /* Pending operation: none. */
804 acpigen_emit_byte(RETURN_OP);
805 acpigen_write_package(2);
806 acpigen_write_byte(0);
807 acpigen_write_byte(0);
808 acpigen_pop_len();
809}
810static void tpm_ppi_func4_cb(void *arg)
811{
812 /* Pre-OS transition method: reboot. */
813 acpigen_write_return_byte(2);
814}
815static void tpm_ppi_func5_cb(void *arg)
816{
817 /* Operation response: no operation executed. */
818 acpigen_emit_byte(RETURN_OP);
819 acpigen_write_package(3);
820 acpigen_write_byte(0);
821 acpigen_write_byte(0);
822 acpigen_write_byte(0);
823 acpigen_pop_len();
824}
825static void tpm_ppi_func6_cb(void *arg)
826{
827 /*
828 * Set preferred user language: deprecated and must return 3 aka
829 * "not implemented".
830 */
831 acpigen_write_return_byte(3);
832}
833static void tpm_ppi_func7_cb(void *arg)
834{
835 /* Submit operations: deny. */
836 acpigen_write_return_byte(3);
837}
838static void tpm_ppi_func8_cb(void *arg)
839{
840 /* All actions are forbidden. */
841 acpigen_write_return_byte(1);
842}
843static void (*tpm_ppi_callbacks[])(void *) = {
844 tpm_ppi_func0_cb,
845 tpm_ppi_func1_cb,
846 tpm_ppi_func2_cb,
847 tpm_ppi_func3_cb,
848 tpm_ppi_func4_cb,
849 tpm_ppi_func5_cb,
850 tpm_ppi_func6_cb,
851 tpm_ppi_func7_cb,
852 tpm_ppi_func8_cb,
853};
854
855static void tpm_mci_func0_cb(void *arg)
856{
857 /* Function 1. */
858 acpigen_write_return_singleton_buffer(0x3);
859}
860static void tpm_mci_func1_cb(void *arg)
861{
862 /* Just return success. */
863 acpigen_write_return_byte(0);
864}
865
866static void (*tpm_mci_callbacks[])(void *) = {
867 tpm_mci_func0_cb,
868 tpm_mci_func1_cb,
869};
870
871static void lpc_tpm_fill_ssdt(struct device *dev)
872{
873 const char *path = acpi_device_path(dev->bus->dev);
874 u32 arg;
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530875
Philipp Deppenwiese3a1fbea2016-12-14 01:06:55 +0100876 if (!path) {
Tobias Diedrich36537f12017-02-10 00:28:45 +0100877 path = "\\_SB_.PCI0.LPCB";
Philipp Deppenwiese3a1fbea2016-12-14 01:06:55 +0100878 printk(BIOS_DEBUG, "Using default TPM ACPI path: '%s'\n", path);
879 }
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530880
881 /* Device */
882 acpigen_write_scope(path);
883 acpigen_write_device(acpi_device_name(dev));
884
885 acpigen_write_name("_HID");
886 acpigen_emit_eisaid("PNP0C31");
887
888 acpigen_write_name("_CID");
889 acpigen_emit_eisaid("PNP0C31");
890
891 acpigen_write_name_integer("_UID", 1);
892
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530893 u32 did_vid = tpm_read_did_vid(0);
894 if (did_vid > 0 && did_vid < 0xffffffff)
895 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON);
896 else
897 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_OFF);
898
899 /* Resources */
900 acpigen_write_name("_CRS");
901 acpigen_write_resourcetemplate_header();
902 acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000);
903 acpigen_write_io16(0x2e, 0x2e, 1, 2, 1);
904
905 if (CONFIG_TPM_PIRQ) {
906 /*
907 * PIRQ: Update interrupt vector with configured PIRQ
908 * Active-Low Level-Triggered Shared
909 */
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800910 struct acpi_irq tpm_irq_a = ACPI_IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ);
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530911 acpi_device_write_interrupt(&tpm_irq_a);
912 } else if (tpm_read_int_vector(0) > 0) {
913 u8 int_vec = tpm_read_int_vector(0);
914 u8 int_pol = tpm_read_int_polarity(0);
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800915 struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec);
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530916
917 if (int_pol & 1)
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800918 tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW;
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530919 else
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800920 tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH;
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530921
922 if (int_pol & 2)
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800923 tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED;
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530924 else
Furquan Shaikh5b9b5932017-02-21 13:16:30 -0800925 tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED;
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530926
927 acpi_device_write_interrupt(&tpm_irq);
928 }
929
930 acpigen_write_resourcetemplate_footer();
931
932 if (!IS_ENABLED(CONFIG_CHROMEOS)) {
933 /*
934 * _DSM method
935 */
936 struct dsm_uuid ids[] = {
937 /* Physical presence interface.
938 * This is used to submit commands like "Clear TPM" to
939 * be run at next reboot provided that user confirms
940 * them. Spec allows user to cancel all commands and/or
941 * configure BIOS to reject commands. So we pretend that
942 * user did just this: cancelled everything. If user
943 * really wants to clear TPM the only option now is to
944 * do it manually in payload.
945 */
946 DSM_UUID(TPM_PPI_UUID, &tpm_ppi_callbacks[0],
947 ARRAY_SIZE(tpm_ppi_callbacks), (void *) &arg),
948 /* Memory clearing on boot: just a dummy. */
949 DSM_UUID(TPM_MCI_UUID, &tpm_mci_callbacks[0],
950 ARRAY_SIZE(tpm_mci_callbacks), (void *) &arg),
951 };
952
953 acpigen_write_dsm_uuid_arr(ids, ARRAY_SIZE(ids));
954 }
955 acpigen_pop_len(); /* Device */
956 acpigen_pop_len(); /* Scope */
957
958 printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
959 dev->chip_ops->name, dev_path(dev));
960}
961
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600962static const char *lpc_tpm_acpi_name(const struct device *dev)
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530963{
964 return "TPM";
965}
966#endif
967
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700968static struct device_operations lpc_tpm_ops = {
969 .read_resources = &lpc_tpm_read_resources,
970 .set_resources = &lpc_tpm_set_resources,
Naresh G Solanki80ff0382016-11-15 11:01:33 +0530971#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
972 .acpi_name = &lpc_tpm_acpi_name,
973 .acpi_fill_ssdt_generator = &lpc_tpm_fill_ssdt,
974#endif
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700975};
976
977static struct pnp_info pnp_dev_info[] = {
978 { .flags = PNP_IRQ0 }
979};
980
981static void enable_dev(struct device *dev)
982{
983 pnp_enable_devices(dev, &lpc_tpm_ops,
984 ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
985}
986
987struct chip_operations drivers_pc80_tpm_ops = {
988 CHIP_NAME("LPC TPM")
989 .enable_dev = enable_dev
990};
991
992#endif /* __RAMSTAGE__ */