blob: 7fa9b582990ae56a0bed7c415328fe22fac93b02 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070018 */
19
20/*
21 * The code in this file has been heavily based on the article "Writing a TPM
22 * Device Driver" published on http://ptgmedia.pearsoncmg.com and the
23 * submission by Stefan Berger on Qemu-devel mailing list.
24 *
25 * One principal difference is that in the simplest config the other than 0
26 * TPM localities do not get mapped by some devices (for instance, by
27 * Infineon slb9635), so this driver provides access to locality 0 only.
28 */
29
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070030#include <stdlib.h>
31#include <string.h>
32#include <delay.h>
33#include <arch/io.h>
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070034#include <console/console.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080035#include <tpm.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: "
41
42/* coreboot wrapper for TPM driver (start) */
43#define TPM_DEBUG(fmt, args...) \
Stefan Reinauerdfb098d2011-11-17 12:50:54 -080044 if (CONFIG_DEBUG_TPM) { \
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070045 printk(BIOS_DEBUG, PREFIX); \
46 printk(BIOS_DEBUG, fmt , ##args); \
47 }
Aaron Durbinc4220022013-07-24 16:02:14 -050048#define TPM_DEBUG_IO_READ(reg_, val_) \
49 TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_))
50#define TPM_DEBUG_IO_WRITE(reg_, val_) \
51 TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_))
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070052#define printf(x...) printk(BIOS_ERR, x)
53
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070054/* coreboot wrapper for TPM driver (end) */
55
56#ifndef CONFIG_TPM_TIS_BASE_ADDRESS
57/* Base TPM address standard for x86 systems */
58#define CONFIG_TPM_TIS_BASE_ADDRESS 0xfed40000
59#endif
60
61/* the macro accepts the locality value, but only locality 0 is operational */
62#define TIS_REG(LOCALITY, REG) \
63 (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
64
65/* hardware registers' offsets */
66#define TIS_REG_ACCESS 0x0
67#define TIS_REG_INT_ENABLE 0x8
68#define TIS_REG_INT_VECTOR 0xc
69#define TIS_REG_INT_STATUS 0x10
70#define TIS_REG_INTF_CAPABILITY 0x14
71#define TIS_REG_STS 0x18
Aaron Durbinc4220022013-07-24 16:02:14 -050072#define TIS_REG_BURST_COUNT 0x19
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070073#define TIS_REG_DATA_FIFO 0x24
74#define TIS_REG_DID_VID 0xf00
75#define TIS_REG_RID 0xf04
76
77/* Some registers' bit field definitions */
78#define TIS_STS_VALID (1 << 7) /* 0x80 */
79#define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
80#define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
81#define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
82#define TIS_STS_EXPECT (1 << 3) /* 0x08 */
83#define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
84
85#define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
86#define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
87#define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
88#define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
89#define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
90#define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
91#define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
92
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070093/*
94 * Error value returned if a tpm register does not enter the expected state
95 * after continuous polling. No actual TPM register reading ever returns ~0,
96 * so this value is a safe error indication to be mixed with possible status
97 * register values.
98 */
99#define TPM_TIMEOUT_ERR (~0)
100
101/* Error value returned on various TPM driver errors */
102#define TPM_DRIVER_ERR (~0)
103
104 /* 1 second is plenty for anything TPM does.*/
105#define MAX_DELAY_US (1000 * 1000)
106
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700107/*
108 * Structures defined below allow creating descriptions of TPM vendor/device
109 * ID information for run time discovery. The only device the system knows
110 * about at this time is Infineon slb9635
111 */
112struct device_name {
113 u16 dev_id;
114 const char * const dev_name;
115};
116
117struct vendor_name {
118 u16 vendor_id;
119 const char * vendor_name;
Stefan Reinauerc668af72011-10-27 21:28:25 +0000120 const struct device_name* dev_names;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700121};
122
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700123static const struct device_name atmel_devices[] = {
124 {0x3204, "AT97SC3204"},
125 {0xffff}
126};
127
Stefan Reinauerc668af72011-10-27 21:28:25 +0000128static const struct device_name infineon_devices[] = {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700129 {0x000b, "SLB9635 TT 1.2"},
130 {0xffff}
131};
132
133static const struct device_name nuvoton_devices[] = {
134 {0x00fe, "NPCT420AA V2"},
135 {0xffff}
136};
137
138static const struct device_name stmicro_devices[] = {
139 {0x0000, "ST33ZP24" },
140 {0xffff}
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700141};
142
143static const struct vendor_name vendor_names[] = {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700144 {0x1114, "Atmel", atmel_devices},
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700145 {0x15d1, "Infineon", infineon_devices},
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700146 {0x1050, "Nuvoton", nuvoton_devices},
147 {0x104a, "ST Microelectronics", stmicro_devices},
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700148};
149
150/*
151 * Cached vendor/device ID pair to indicate that the device has been already
152 * discovered
153 */
Stefan Reinauerc668af72011-10-27 21:28:25 +0000154static u32 vendor_dev_id CAR_GLOBAL;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700155
Aaron Durbinc4220022013-07-24 16:02:14 -0500156static inline u8 tpm_read_status(int locality)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700157{
Julius Werner2f37bd62015-02-19 14:51:15 -0800158 u8 value = read8(TIS_REG(locality, TIS_REG_STS));
Aaron Durbinc4220022013-07-24 16:02:14 -0500159 TPM_DEBUG_IO_READ(TIS_REG_STS, value);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700160 return value;
161}
162
Aaron Durbinc4220022013-07-24 16:02:14 -0500163static inline void tpm_write_status(u8 sts, int locality)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700164{
Aaron Durbinc4220022013-07-24 16:02:14 -0500165 TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts);
Julius Werner2f37bd62015-02-19 14:51:15 -0800166 write8(TIS_REG(locality, TIS_REG_STS), sts);
Aaron Durbinc4220022013-07-24 16:02:14 -0500167}
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700168
Aaron Durbinc4220022013-07-24 16:02:14 -0500169static inline u8 tpm_read_data(int locality)
170{
Julius Werner2f37bd62015-02-19 14:51:15 -0800171 u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO));
Aaron Durbinc4220022013-07-24 16:02:14 -0500172 TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value);
173 return value;
174}
175
176static inline void tpm_write_data(u8 data, int locality)
177{
178 TPM_DEBUG_IO_WRITE(TIS_REG_STS, data);
Julius Werner2f37bd62015-02-19 14:51:15 -0800179 write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data);
Aaron Durbinc4220022013-07-24 16:02:14 -0500180}
181
182static inline u16 tpm_read_burst_count(int locality)
183{
184 u16 count;
Julius Werner2f37bd62015-02-19 14:51:15 -0800185 count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT));
186 count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8;
Aaron Durbinc4220022013-07-24 16:02:14 -0500187 TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count);
188 return count;
189}
190
191static inline u8 tpm_read_access(int locality)
192{
Julius Werner2f37bd62015-02-19 14:51:15 -0800193 u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS));
Aaron Durbinc4220022013-07-24 16:02:14 -0500194 TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value);
195 return value;
196}
197
198static inline void tpm_write_access(u8 data, int locality)
199{
200 TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data);
Julius Werner2f37bd62015-02-19 14:51:15 -0800201 write8(TIS_REG(locality, TIS_REG_ACCESS), data);
Aaron Durbinc4220022013-07-24 16:02:14 -0500202}
203
204static inline u32 tpm_read_did_vid(int locality)
205{
Julius Werner2f37bd62015-02-19 14:51:15 -0800206 u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID));
Aaron Durbinc4220022013-07-24 16:02:14 -0500207 TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value);
208 return value;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700209}
210
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700211static inline void tpm_write_int_vector(int vector, int locality)
212{
213 TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector);
Julius Werner2f37bd62015-02-19 14:51:15 -0800214 write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf);
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700215}
216
217static inline void tpm_write_int_polarity(int polarity, int locality)
218{
219 /* Set polarity and leave all other bits at 0 */
220 u32 value = (polarity & 0x3) << 3;
221 TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value);
Julius Werner2f37bd62015-02-19 14:51:15 -0800222 write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value);
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700223}
224
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700225/*
Aaron Durbinc4220022013-07-24 16:02:14 -0500226 * tis_wait_sts()
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700227 *
Aaron Durbinc4220022013-07-24 16:02:14 -0500228 * Wait for at least a second for a status to change its state to match the
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700229 * expected state. Normally the transition happens within microseconds.
230 *
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700231 * @locality - locality
232 * @mask - bitmask for the bitfield(s) to watch
233 * @expected - value the field(s) are supposed to be set to
234 *
Aaron Durbinc4220022013-07-24 16:02:14 -0500235 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700236 */
Aaron Durbinc4220022013-07-24 16:02:14 -0500237static int tis_wait_sts(int locality, u8 mask, u8 expected)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700238{
239 u32 time_us = MAX_DELAY_US;
240 while (time_us > 0) {
Aaron Durbinc4220022013-07-24 16:02:14 -0500241 u8 value = tpm_read_status(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700242 if ((value & mask) == expected)
Aaron Durbinc4220022013-07-24 16:02:14 -0500243 return 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700244 udelay(1); /* 1 us */
245 time_us--;
246 }
247 return TPM_TIMEOUT_ERR;
248}
249
Aaron Durbinc4220022013-07-24 16:02:14 -0500250static inline int tis_wait_ready(int locality)
251{
252 return tis_wait_sts(locality, TIS_STS_COMMAND_READY,
253 TIS_STS_COMMAND_READY);
254}
255
256static inline int tis_wait_valid(int locality)
257{
258 return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID);
259}
260
261static inline int tis_wait_valid_data(int locality)
262{
263 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
264 return tis_wait_sts(locality, has_data, has_data);
265}
266
267static inline int tis_has_valid_data(int locality)
268{
269 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
270 return (tpm_read_status(locality) & has_data) == has_data;
271}
272
273static inline int tis_expect_data(int locality)
274{
275 return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
276}
277
278/*
279 * tis_wait_access()
280 *
281 * Wait for at least a second for a access to change its state to match the
282 * expected state. Normally the transition happens within microseconds.
283 *
284 * @locality - locality
285 * @mask - bitmask for the bitfield(s) to watch
286 * @expected - value the field(s) are supposed to be set to
287 *
288 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
289 */
290static int tis_wait_access(int locality, u8 mask, u8 expected)
291{
292 u32 time_us = MAX_DELAY_US;
293 while (time_us > 0) {
294 u8 value = tpm_read_access(locality);
295 if ((value & mask) == expected)
296 return 0;
297 udelay(1); /* 1 us */
298 time_us--;
299 }
300 return TPM_TIMEOUT_ERR;
301}
302
303static inline int tis_wait_dropped_access(int locality)
304{
305 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0);
306}
307
308static inline int tis_wait_received_access(int locality)
309{
310 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY,
311 TIS_ACCESS_ACTIVE_LOCALITY);
312}
313
314static inline int tis_has_access(int locality)
315{
316 return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
317}
318
319static inline void tis_request_access(int locality)
320{
321 tpm_write_access(TIS_ACCESS_REQUEST_USE, locality);
322}
323
324static inline void tis_drop_access(int locality)
325{
326 tpm_write_access(TIS_ACCESS_ACTIVE_LOCALITY, locality);
327}
328
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700329/*
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700330 * PC Client Specific TPM Interface Specification section 11.2.12:
331 *
332 * Software must be prepared to send two writes of a "1" to command ready
333 * field: the first to indicate successful read of all the data, thus
334 * clearing the data from the ReadFIFO and freeing the TPM's resources,
335 * and the second to indicate to the TPM it is about to send a new command.
336 *
337 * In practice not all TPMs behave the same so it is necessary to be
338 * flexible when trying to set command ready.
339 *
340 * Returns 0 on success if the TPM is ready for transactions.
341 * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set.
342 */
343static int tis_command_ready(u8 locality)
344{
345 u32 status;
346
347 /* 1st attempt to set command ready */
Aaron Durbinc4220022013-07-24 16:02:14 -0500348 tpm_write_status(TIS_STS_COMMAND_READY, locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700349
350 /* Wait for response */
Aaron Durbinc4220022013-07-24 16:02:14 -0500351 status = tpm_read_status(locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700352
353 /* Check if command ready is set yet */
354 if (status & TIS_STS_COMMAND_READY)
355 return 0;
356
357 /* 2nd attempt to set command ready */
Aaron Durbinc4220022013-07-24 16:02:14 -0500358 tpm_write_status(TIS_STS_COMMAND_READY, locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700359
Aaron Durbinc4220022013-07-24 16:02:14 -0500360 return tis_wait_ready(locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700361}
362
363/*
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700364 * Probe the TPM device and try determining its manufacturer/device name.
365 *
366 * Returns 0 on success (the device is found or was found during an earlier
367 * invocation) or TPM_DRIVER_ERR if the device is not found.
368 */
369static u32 tis_probe(void)
370{
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700371 const char *device_name = "unknown";
372 const char *vendor_name = device_name;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700373 const struct device_name *dev;
374 u32 didvid;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700375 u16 vid, did;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700376 int i;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700377
Aaron Durbincb997d32013-05-10 00:40:56 -0500378 if (car_get_var(vendor_dev_id))
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700379 return 0; /* Already probed. */
380
Aaron Durbinc4220022013-07-24 16:02:14 -0500381 didvid = tpm_read_did_vid(0);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700382 if (!didvid || (didvid == 0xffffffff)) {
383 printf("%s: No TPM device found\n", __FUNCTION__);
384 return TPM_DRIVER_ERR;
385 }
386
Aaron Durbincb997d32013-05-10 00:40:56 -0500387 car_set_var(vendor_dev_id, didvid);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700388
389 vid = didvid & 0xffff;
390 did = (didvid >> 16) & 0xffff;
391 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
392 int j = 0;
393 u16 known_did;
394 if (vid == vendor_names[i].vendor_id) {
395 vendor_name = vendor_names[i].vendor_name;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700396 } else {
397 continue;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700398 }
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700399 dev = &vendor_names[i].dev_names[j];
400 while ((known_did = dev->dev_id) != 0xffff) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700401 if (known_did == did) {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700402 device_name = dev->dev_name;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700403 break;
404 }
405 j++;
Subrata41b08d92015-05-14 14:38:07 +0530406 dev = &vendor_names[i].dev_names[j];
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700407 }
408 break;
409 }
410 /* this will have to be converted into debug printout */
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700411 printf("Found TPM %s by %s\n", device_name, vendor_name);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700412 return 0;
413}
414
415/*
416 * tis_senddata()
417 *
418 * send the passed in data to the TPM device.
419 *
420 * @data - address of the data to send, byte by byte
421 * @len - length of the data to send
422 *
423 * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
424 * not accept the entire command).
425 */
426static u32 tis_senddata(const u8 * const data, u32 len)
427{
428 u32 offset = 0;
429 u16 burst = 0;
430 u32 max_cycles = 0;
431 u8 locality = 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700432
Aaron Durbinc4220022013-07-24 16:02:14 -0500433 if (tis_wait_ready(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700434 printf("%s:%d - failed to get 'command_ready' status\n",
435 __FILE__, __LINE__);
436 return TPM_DRIVER_ERR;
437 }
Aaron Durbinc4220022013-07-24 16:02:14 -0500438 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700439
440 while (1) {
441 unsigned count;
442
443 /* Wait till the device is ready to accept more data. */
444 while (!burst) {
445 if (max_cycles++ == MAX_DELAY_US) {
446 printf("%s:%d failed to feed %d bytes of %d\n",
447 __FILE__, __LINE__, len - offset, len);
448 return TPM_DRIVER_ERR;
449 }
450 udelay(1);
Aaron Durbinc4220022013-07-24 16:02:14 -0500451 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700452 }
453
454 max_cycles = 0;
455
456 /*
457 * Calculate number of bytes the TPM is ready to accept in one
458 * shot.
459 *
460 * We want to send the last byte outside of the loop (hence
461 * the -1 below) to make sure that the 'expected' status bit
462 * changes to zero exactly after the last byte is fed into the
463 * FIFO.
464 */
465 count = min(burst, len - offset - 1);
466 while (count--)
Aaron Durbinc4220022013-07-24 16:02:14 -0500467 tpm_write_data(data[offset++], locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700468
Aaron Durbinc4220022013-07-24 16:02:14 -0500469 if (tis_wait_valid(locality) || !tis_expect_data(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700470 printf("%s:%d TPM command feed overflow\n",
471 __FILE__, __LINE__);
472 return TPM_DRIVER_ERR;
473 }
474
Aaron Durbinc4220022013-07-24 16:02:14 -0500475 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700476 if ((offset == (len - 1)) && burst)
477 /*
478 * We need to be able to send the last byte to the
479 * device, so burst size must be nonzero before we
480 * break out.
481 */
482 break;
483 }
484
485 /* Send the last byte. */
Aaron Durbinc4220022013-07-24 16:02:14 -0500486 tpm_write_data(data[offset++], locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700487
488 /*
489 * Verify that TPM does not expect any more data as part of this
490 * command.
491 */
Aaron Durbinc4220022013-07-24 16:02:14 -0500492 if (tis_wait_valid(locality) || tis_expect_data(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700493 printf("%s:%d unexpected TPM status 0x%x\n",
Aaron Durbinc4220022013-07-24 16:02:14 -0500494 __FILE__, __LINE__, tpm_read_status(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700495 return TPM_DRIVER_ERR;
496 }
497
498 /* OK, sitting pretty, let's start the command execution. */
Aaron Durbinc4220022013-07-24 16:02:14 -0500499 tpm_write_status(TIS_STS_TPM_GO, locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700500
501 return 0;
502}
503
504/*
505 * tis_readresponse()
506 *
507 * read the TPM device response after a command was issued.
508 *
509 * @buffer - address where to read the response, byte by byte.
510 * @len - pointer to the size of buffer
511 *
512 * On success stores the number of received bytes to len and returns 0. On
513 * errors (misformatted TPM data or synchronization problems) returns
514 * TPM_DRIVER_ERR.
515 */
516static u32 tis_readresponse(u8 *buffer, size_t *len)
517{
518 u16 burst_count;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700519 u32 offset = 0;
520 u8 locality = 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700521 u32 expected_count = *len;
522 int max_cycles = 0;
523
524 /* Wait for the TPM to process the command */
Aaron Durbinc4220022013-07-24 16:02:14 -0500525 if (tis_wait_valid_data(locality)) {
526 printf("%s:%d failed processing command\n", __FILE__, __LINE__);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700527 return TPM_DRIVER_ERR;
528 }
529
530 do {
Aaron Durbinc4220022013-07-24 16:02:14 -0500531 while ((burst_count = tpm_read_burst_count(locality)) == 0) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700532 if (max_cycles++ == MAX_DELAY_US) {
533 printf("%s:%d TPM stuck on read\n",
534 __FILE__, __LINE__);
535 return TPM_DRIVER_ERR;
536 }
537 udelay(1);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700538 }
539
540 max_cycles = 0;
541
542 while (burst_count-- && (offset < expected_count)) {
Aaron Durbinc4220022013-07-24 16:02:14 -0500543 buffer[offset++] = tpm_read_data(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700544 if (offset == 6) {
545 /*
546 * We got the first six bytes of the reply,
547 * let's figure out how many bytes to expect
548 * total - it is stored as a 4 byte number in
549 * network order, starting with offset 2 into
550 * the body of the reply.
551 */
552 u32 real_length;
553 memcpy(&real_length,
554 buffer + 2,
555 sizeof(real_length));
556 expected_count = be32_to_cpu(real_length);
557
558 if ((expected_count < offset) ||
559 (expected_count > *len)) {
560 printf("%s:%d bad response size %d\n",
561 __FILE__, __LINE__,
562 expected_count);
563 return TPM_DRIVER_ERR;
564 }
565 }
566 }
567
568 /* Wait for the next portion */
Aaron Durbinc4220022013-07-24 16:02:14 -0500569 if (tis_wait_valid(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700570 printf("%s:%d failed to read response\n",
571 __FILE__, __LINE__);
572 return TPM_DRIVER_ERR;
573 }
574
575 if (offset == expected_count)
576 break; /* We got all we need */
577
Aaron Durbinc4220022013-07-24 16:02:14 -0500578 } while (tis_has_valid_data(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700579
Aaron Durbinc4220022013-07-24 16:02:14 -0500580 /* * Make sure we indeed read all there was. */
581 if (tis_has_valid_data(locality)) {
582 printf("%s:%d wrong receive status: %x %d bytes left\n",
583 __FILE__, __LINE__, tpm_read_status(locality),
584 tpm_read_burst_count(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700585 return TPM_DRIVER_ERR;
586 }
587
588 /* Tell the TPM that we are done. */
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700589 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
590 return TPM_DRIVER_ERR;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700591
592 *len = offset;
593 return 0;
594}
595
596/*
597 * tis_init()
598 *
599 * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on
600 * failure (in case device probing did not succeed).
601 */
602int tis_init(void)
603{
604 if (tis_probe())
605 return TPM_DRIVER_ERR;
606 return 0;
607}
608
609/*
610 * tis_open()
611 *
612 * Requests access to locality 0 for the caller. After all commands have been
613 * completed the caller is supposed to call tis_close().
614 *
615 * Returns 0 on success, TPM_DRIVER_ERR on failure.
616 */
617int tis_open(void)
618{
619 u8 locality = 0; /* we use locality zero for everything */
620
621 if (tis_close())
622 return TPM_DRIVER_ERR;
623
624 /* now request access to locality */
Aaron Durbinc4220022013-07-24 16:02:14 -0500625 tis_request_access(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700626
627 /* did we get a lock? */
Aaron Durbinc4220022013-07-24 16:02:14 -0500628 if (tis_wait_received_access(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700629 printf("%s:%d - failed to lock locality %d\n",
630 __FILE__, __LINE__, locality);
631 return TPM_DRIVER_ERR;
632 }
633
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700634 /* Certain TPMs seem to need some delay here or they hang... */
635 udelay(10);
636
637 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
638 return TPM_DRIVER_ERR;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700639
640 return 0;
641}
642
643/*
644 * tis_close()
645 *
Martin Roth56889792013-07-09 21:39:46 -0600646 * terminate the current session with the TPM by releasing the locked
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700647 * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock
648 * removal did not succeed).
649 */
650int tis_close(void)
651{
652 u8 locality = 0;
Aaron Durbinc4220022013-07-24 16:02:14 -0500653 if (tis_has_access(locality)) {
654 tis_drop_access(locality);
655 if (tis_wait_dropped_access(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700656 printf("%s:%d - failed to release locality %d\n",
657 __FILE__, __LINE__, locality);
658 return TPM_DRIVER_ERR;
659 }
660 }
661 return 0;
662}
663
664/*
665 * tis_sendrecv()
666 *
667 * Send the requested data to the TPM and then try to get its response
668 *
669 * @sendbuf - buffer of the data to send
670 * @send_size size of the data to send
671 * @recvbuf - memory to save the response to
672 * @recv_len - pointer to the size of the response buffer
673 *
674 * Returns 0 on success (and places the number of response bytes at recv_len)
675 * or TPM_DRIVER_ERR on failure.
676 */
677int tis_sendrecv(const uint8_t *sendbuf, size_t send_size,
678 uint8_t *recvbuf, size_t *recv_len)
679{
680 if (tis_senddata(sendbuf, send_size)) {
681 printf("%s:%d failed sending data to TPM\n",
682 __FILE__, __LINE__);
683 return TPM_DRIVER_ERR;
684 }
685
686 return tis_readresponse(recvbuf, recv_len);
687}
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700688
689#ifdef __RAMSTAGE__
690
691/*
692 * tis_setup_interrupt()
693 *
694 * Set up the interrupt vector and polarity for locality 0 and
695 * disable all interrupts so they are unused in firmware but can
696 * be enabled by the OS.
697 *
698 * The values used here must match what is passed in the TPM ACPI
699 * device if ACPI is used on the platform.
700 *
701 * @vector - TPM interrupt vector
702 * @polarity - TPM interrupt polarity
703 *
704 * Returns 0 on success, TPM_DRIVER_ERR on failure.
705 */
706static int tis_setup_interrupt(int vector, int polarity)
707{
708 u8 locality = 0;
709 int has_access = tis_has_access(locality);
710
711 /* Open connection and request access if not already granted */
712 if (!has_access && tis_open() < 0)
713 return TPM_DRIVER_ERR;
714
715 /* Set TPM interrupt vector */
716 tpm_write_int_vector(vector, locality);
717
718 /* Set TPM interupt polarity and disable interrupts */
719 tpm_write_int_polarity(polarity, locality);
720
721 /* Close connection if it was opened */
722 if (!has_access && tis_close() < 0)
723 return TPM_DRIVER_ERR;
724
725 return 0;
726}
727
728static void lpc_tpm_read_resources(struct device *dev)
729{
730 /* Static 5K memory region specified in Kconfig */
731 mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10);
732}
733
734static void lpc_tpm_set_resources(struct device *dev)
735{
736 tpm_config_t *config = (tpm_config_t *)dev->chip_info;
737 struct resource *res;
738
739 for (res = dev->resource_list; res; res = res->next) {
740 if (!(res->flags & IORESOURCE_ASSIGNED))
741 continue;
742
743 if (res->flags & IORESOURCE_IRQ) {
744 /* Set interrupt vector */
745 tis_setup_interrupt((int)res->base,
746 config->irq_polarity);
747 } else {
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700748 continue;
749 }
750
751 res->flags |= IORESOURCE_STORED;
752 report_resource_stored(dev, res, " <tpm>");
753 }
754}
755
756static struct device_operations lpc_tpm_ops = {
757 .read_resources = &lpc_tpm_read_resources,
758 .set_resources = &lpc_tpm_set_resources,
759};
760
761static struct pnp_info pnp_dev_info[] = {
762 { .flags = PNP_IRQ0 }
763};
764
765static void enable_dev(struct device *dev)
766{
767 pnp_enable_devices(dev, &lpc_tpm_ops,
768 ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
769}
770
771struct chip_operations drivers_pc80_tpm_ops = {
772 CHIP_NAME("LPC TPM")
773 .enable_dev = enable_dev
774};
775
776#endif /* __RAMSTAGE__ */