blob: 064cb994fbeeaa0c26f7c70ca63fce2870d2dc12 [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>
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070030#include <console/console.h>
Stefan Reinauerd518c7a2013-11-04 17:38:32 -080031#include <tpm.h>
Stefan Reinauerfd4f4132013-06-19 12:25:44 -070032#include <arch/early_variables.h>
Duncan Lauriedd281ed2014-10-30 15:20:19 -070033#include <device/pnp.h>
34#include "chip.h"
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070035
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070036#define PREFIX "lpc_tpm: "
37
38/* coreboot wrapper for TPM driver (start) */
39#define TPM_DEBUG(fmt, args...) \
Stefan Reinauerdfb098d2011-11-17 12:50:54 -080040 if (CONFIG_DEBUG_TPM) { \
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070041 printk(BIOS_DEBUG, PREFIX); \
42 printk(BIOS_DEBUG, fmt , ##args); \
43 }
Aaron Durbinc4220022013-07-24 16:02:14 -050044#define TPM_DEBUG_IO_READ(reg_, val_) \
45 TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_))
46#define TPM_DEBUG_IO_WRITE(reg_, val_) \
47 TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_))
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070048#define printf(x...) printk(BIOS_ERR, x)
49
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070050/* coreboot wrapper for TPM driver (end) */
51
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070052/* the macro accepts the locality value, but only locality 0 is operational */
53#define TIS_REG(LOCALITY, REG) \
54 (void *)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
55
56/* hardware registers' offsets */
57#define TIS_REG_ACCESS 0x0
58#define TIS_REG_INT_ENABLE 0x8
59#define TIS_REG_INT_VECTOR 0xc
60#define TIS_REG_INT_STATUS 0x10
61#define TIS_REG_INTF_CAPABILITY 0x14
62#define TIS_REG_STS 0x18
Aaron Durbinc4220022013-07-24 16:02:14 -050063#define TIS_REG_BURST_COUNT 0x19
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070064#define TIS_REG_DATA_FIFO 0x24
65#define TIS_REG_DID_VID 0xf00
66#define TIS_REG_RID 0xf04
67
68/* Some registers' bit field definitions */
69#define TIS_STS_VALID (1 << 7) /* 0x80 */
70#define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
71#define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
72#define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
73#define TIS_STS_EXPECT (1 << 3) /* 0x08 */
74#define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
75
76#define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
77#define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
78#define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
79#define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
80#define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
81#define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
82#define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
83
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070084/*
85 * Error value returned if a tpm register does not enter the expected state
86 * after continuous polling. No actual TPM register reading ever returns ~0,
87 * so this value is a safe error indication to be mixed with possible status
88 * register values.
89 */
90#define TPM_TIMEOUT_ERR (~0)
91
92/* Error value returned on various TPM driver errors */
93#define TPM_DRIVER_ERR (~0)
94
95 /* 1 second is plenty for anything TPM does.*/
96#define MAX_DELAY_US (1000 * 1000)
97
Stefan Reinauer3008bbad2011-10-11 14:46:25 -070098/*
99 * Structures defined below allow creating descriptions of TPM vendor/device
100 * ID information for run time discovery. The only device the system knows
101 * about at this time is Infineon slb9635
102 */
103struct device_name {
104 u16 dev_id;
105 const char * const dev_name;
106};
107
108struct vendor_name {
109 u16 vendor_id;
110 const char * vendor_name;
Stefan Reinauerc668af72011-10-27 21:28:25 +0000111 const struct device_name* dev_names;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700112};
113
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700114static const struct device_name atmel_devices[] = {
115 {0x3204, "AT97SC3204"},
116 {0xffff}
117};
118
Stefan Reinauerc668af72011-10-27 21:28:25 +0000119static const struct device_name infineon_devices[] = {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700120 {0x000b, "SLB9635 TT 1.2"},
Subrataf4d65872015-05-14 14:38:07 +0530121 {0x001a, "SLB9660 TT 1.2"},
Wenkai Du641529b2015-05-29 10:54:27 -0700122 {0x001b, "SLB9670 TT 1.2"},
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700123 {0xffff}
124};
125
126static const struct device_name nuvoton_devices[] = {
127 {0x00fe, "NPCT420AA V2"},
128 {0xffff}
129};
130
131static const struct device_name stmicro_devices[] = {
132 {0x0000, "ST33ZP24" },
133 {0xffff}
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700134};
135
136static const struct vendor_name vendor_names[] = {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700137 {0x1114, "Atmel", atmel_devices},
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700138 {0x15d1, "Infineon", infineon_devices},
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700139 {0x1050, "Nuvoton", nuvoton_devices},
140 {0x104a, "ST Microelectronics", stmicro_devices},
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700141};
142
143/*
144 * Cached vendor/device ID pair to indicate that the device has been already
145 * discovered
146 */
Stefan Reinauerc668af72011-10-27 21:28:25 +0000147static u32 vendor_dev_id CAR_GLOBAL;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700148
Aaron Durbinc4220022013-07-24 16:02:14 -0500149static inline u8 tpm_read_status(int locality)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700150{
Julius Werner2f37bd62015-02-19 14:51:15 -0800151 u8 value = read8(TIS_REG(locality, TIS_REG_STS));
Aaron Durbinc4220022013-07-24 16:02:14 -0500152 TPM_DEBUG_IO_READ(TIS_REG_STS, value);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700153 return value;
154}
155
Aaron Durbinc4220022013-07-24 16:02:14 -0500156static inline void tpm_write_status(u8 sts, int locality)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700157{
Aaron Durbinc4220022013-07-24 16:02:14 -0500158 TPM_DEBUG_IO_WRITE(TIS_REG_STS, sts);
Julius Werner2f37bd62015-02-19 14:51:15 -0800159 write8(TIS_REG(locality, TIS_REG_STS), sts);
Aaron Durbinc4220022013-07-24 16:02:14 -0500160}
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700161
Aaron Durbinc4220022013-07-24 16:02:14 -0500162static inline u8 tpm_read_data(int locality)
163{
Julius Werner2f37bd62015-02-19 14:51:15 -0800164 u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO));
Aaron Durbinc4220022013-07-24 16:02:14 -0500165 TPM_DEBUG_IO_READ(TIS_REG_DATA_FIFO, value);
166 return value;
167}
168
169static inline void tpm_write_data(u8 data, int locality)
170{
171 TPM_DEBUG_IO_WRITE(TIS_REG_STS, data);
Julius Werner2f37bd62015-02-19 14:51:15 -0800172 write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data);
Aaron Durbinc4220022013-07-24 16:02:14 -0500173}
174
175static inline u16 tpm_read_burst_count(int locality)
176{
177 u16 count;
Julius Werner2f37bd62015-02-19 14:51:15 -0800178 count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT));
179 count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8;
Aaron Durbinc4220022013-07-24 16:02:14 -0500180 TPM_DEBUG_IO_READ(TIS_REG_BURST_COUNT, count);
181 return count;
182}
183
184static inline u8 tpm_read_access(int locality)
185{
Julius Werner2f37bd62015-02-19 14:51:15 -0800186 u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS));
Aaron Durbinc4220022013-07-24 16:02:14 -0500187 TPM_DEBUG_IO_READ(TIS_REG_ACCESS, value);
188 return value;
189}
190
191static inline void tpm_write_access(u8 data, int locality)
192{
193 TPM_DEBUG_IO_WRITE(TIS_REG_ACCESS, data);
Julius Werner2f37bd62015-02-19 14:51:15 -0800194 write8(TIS_REG(locality, TIS_REG_ACCESS), data);
Aaron Durbinc4220022013-07-24 16:02:14 -0500195}
196
197static inline u32 tpm_read_did_vid(int locality)
198{
Julius Werner2f37bd62015-02-19 14:51:15 -0800199 u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID));
Aaron Durbinc4220022013-07-24 16:02:14 -0500200 TPM_DEBUG_IO_READ(TIS_REG_DID_VID, value);
201 return value;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700202}
203
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700204static inline void tpm_write_int_vector(int vector, int locality)
205{
206 TPM_DEBUG_IO_WRITE(TIS_REG_INT_VECTOR, vector);
Julius Werner2f37bd62015-02-19 14:51:15 -0800207 write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf);
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700208}
209
210static inline void tpm_write_int_polarity(int polarity, int locality)
211{
212 /* Set polarity and leave all other bits at 0 */
213 u32 value = (polarity & 0x3) << 3;
214 TPM_DEBUG_IO_WRITE(TIS_REG_INT_ENABLE, value);
Julius Werner2f37bd62015-02-19 14:51:15 -0800215 write32(TIS_REG(locality, TIS_REG_INT_ENABLE), value);
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700216}
217
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700218/*
Aaron Durbinc4220022013-07-24 16:02:14 -0500219 * tis_wait_sts()
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700220 *
Aaron Durbinc4220022013-07-24 16:02:14 -0500221 * Wait for at least a second for a status to change its state to match the
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700222 * expected state. Normally the transition happens within microseconds.
223 *
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700224 * @locality - locality
225 * @mask - bitmask for the bitfield(s) to watch
226 * @expected - value the field(s) are supposed to be set to
227 *
Aaron Durbinc4220022013-07-24 16:02:14 -0500228 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700229 */
Aaron Durbinc4220022013-07-24 16:02:14 -0500230static int tis_wait_sts(int locality, u8 mask, u8 expected)
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700231{
232 u32 time_us = MAX_DELAY_US;
233 while (time_us > 0) {
Aaron Durbinc4220022013-07-24 16:02:14 -0500234 u8 value = tpm_read_status(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700235 if ((value & mask) == expected)
Aaron Durbinc4220022013-07-24 16:02:14 -0500236 return 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700237 udelay(1); /* 1 us */
238 time_us--;
239 }
240 return TPM_TIMEOUT_ERR;
241}
242
Aaron Durbinc4220022013-07-24 16:02:14 -0500243static inline int tis_wait_ready(int locality)
244{
245 return tis_wait_sts(locality, TIS_STS_COMMAND_READY,
246 TIS_STS_COMMAND_READY);
247}
248
249static inline int tis_wait_valid(int locality)
250{
251 return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID);
252}
253
254static inline int tis_wait_valid_data(int locality)
255{
256 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
257 return tis_wait_sts(locality, has_data, has_data);
258}
259
260static inline int tis_has_valid_data(int locality)
261{
262 const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
263 return (tpm_read_status(locality) & has_data) == has_data;
264}
265
266static inline int tis_expect_data(int locality)
267{
268 return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
269}
270
271/*
272 * tis_wait_access()
273 *
274 * Wait for at least a second for a access to change its state to match the
275 * expected state. Normally the transition happens within microseconds.
276 *
277 * @locality - locality
278 * @mask - bitmask for the bitfield(s) to watch
279 * @expected - value the field(s) are supposed to be set to
280 *
281 * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
282 */
283static int tis_wait_access(int locality, u8 mask, u8 expected)
284{
285 u32 time_us = MAX_DELAY_US;
286 while (time_us > 0) {
287 u8 value = tpm_read_access(locality);
288 if ((value & mask) == expected)
289 return 0;
290 udelay(1); /* 1 us */
291 time_us--;
292 }
293 return TPM_TIMEOUT_ERR;
294}
295
296static inline int tis_wait_dropped_access(int locality)
297{
298 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0);
299}
300
301static inline int tis_wait_received_access(int locality)
302{
303 return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY,
304 TIS_ACCESS_ACTIVE_LOCALITY);
305}
306
307static inline int tis_has_access(int locality)
308{
309 return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
310}
311
312static inline void tis_request_access(int locality)
313{
314 tpm_write_access(TIS_ACCESS_REQUEST_USE, locality);
315}
316
317static inline void tis_drop_access(int locality)
318{
319 tpm_write_access(TIS_ACCESS_ACTIVE_LOCALITY, locality);
320}
321
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700322/*
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700323 * PC Client Specific TPM Interface Specification section 11.2.12:
324 *
325 * Software must be prepared to send two writes of a "1" to command ready
326 * field: the first to indicate successful read of all the data, thus
327 * clearing the data from the ReadFIFO and freeing the TPM's resources,
328 * and the second to indicate to the TPM it is about to send a new command.
329 *
330 * In practice not all TPMs behave the same so it is necessary to be
331 * flexible when trying to set command ready.
332 *
333 * Returns 0 on success if the TPM is ready for transactions.
334 * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set.
335 */
336static int tis_command_ready(u8 locality)
337{
338 u32 status;
339
340 /* 1st attempt to set command ready */
Aaron Durbinc4220022013-07-24 16:02:14 -0500341 tpm_write_status(TIS_STS_COMMAND_READY, locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700342
343 /* Wait for response */
Aaron Durbinc4220022013-07-24 16:02:14 -0500344 status = tpm_read_status(locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700345
346 /* Check if command ready is set yet */
347 if (status & TIS_STS_COMMAND_READY)
348 return 0;
349
350 /* 2nd attempt to set command ready */
Aaron Durbinc4220022013-07-24 16:02:14 -0500351 tpm_write_status(TIS_STS_COMMAND_READY, locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700352
Aaron Durbinc4220022013-07-24 16:02:14 -0500353 return tis_wait_ready(locality);
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700354}
355
356/*
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700357 * Probe the TPM device and try determining its manufacturer/device name.
358 *
359 * Returns 0 on success (the device is found or was found during an earlier
360 * invocation) or TPM_DRIVER_ERR if the device is not found.
361 */
362static u32 tis_probe(void)
363{
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700364 const char *device_name = "unknown";
365 const char *vendor_name = device_name;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700366 const struct device_name *dev;
367 u32 didvid;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700368 u16 vid, did;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700369 int i;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700370
Aaron Durbincb997d32013-05-10 00:40:56 -0500371 if (car_get_var(vendor_dev_id))
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700372 return 0; /* Already probed. */
373
Aaron Durbinc4220022013-07-24 16:02:14 -0500374 didvid = tpm_read_did_vid(0);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700375 if (!didvid || (didvid == 0xffffffff)) {
376 printf("%s: No TPM device found\n", __FUNCTION__);
377 return TPM_DRIVER_ERR;
378 }
379
Aaron Durbincb997d32013-05-10 00:40:56 -0500380 car_set_var(vendor_dev_id, didvid);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700381
382 vid = didvid & 0xffff;
383 did = (didvid >> 16) & 0xffff;
384 for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
385 int j = 0;
386 u16 known_did;
387 if (vid == vendor_names[i].vendor_id) {
388 vendor_name = vendor_names[i].vendor_name;
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700389 } else {
390 continue;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700391 }
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700392 dev = &vendor_names[i].dev_names[j];
393 while ((known_did = dev->dev_id) != 0xffff) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700394 if (known_did == did) {
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700395 device_name = dev->dev_name;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700396 break;
397 }
398 j++;
Subrata41b08d92015-05-14 14:38:07 +0530399 dev = &vendor_names[i].dev_names[j];
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700400 }
401 break;
402 }
403 /* this will have to be converted into debug printout */
Duncan Laurie17ba9442015-09-03 16:00:49 -0700404 printk(BIOS_INFO, "Found TPM %s by %s\n", device_name, vendor_name);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700405 return 0;
406}
407
408/*
409 * tis_senddata()
410 *
411 * send the passed in data to the TPM device.
412 *
413 * @data - address of the data to send, byte by byte
414 * @len - length of the data to send
415 *
416 * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
417 * not accept the entire command).
418 */
419static u32 tis_senddata(const u8 * const data, u32 len)
420{
421 u32 offset = 0;
422 u16 burst = 0;
423 u32 max_cycles = 0;
424 u8 locality = 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700425
Aaron Durbinc4220022013-07-24 16:02:14 -0500426 if (tis_wait_ready(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700427 printf("%s:%d - failed to get 'command_ready' status\n",
428 __FILE__, __LINE__);
429 return TPM_DRIVER_ERR;
430 }
Aaron Durbinc4220022013-07-24 16:02:14 -0500431 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700432
433 while (1) {
434 unsigned count;
435
436 /* Wait till the device is ready to accept more data. */
437 while (!burst) {
438 if (max_cycles++ == MAX_DELAY_US) {
439 printf("%s:%d failed to feed %d bytes of %d\n",
440 __FILE__, __LINE__, len - offset, len);
441 return TPM_DRIVER_ERR;
442 }
443 udelay(1);
Aaron Durbinc4220022013-07-24 16:02:14 -0500444 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700445 }
446
447 max_cycles = 0;
448
449 /*
450 * Calculate number of bytes the TPM is ready to accept in one
451 * shot.
452 *
453 * We want to send the last byte outside of the loop (hence
454 * the -1 below) to make sure that the 'expected' status bit
455 * changes to zero exactly after the last byte is fed into the
456 * FIFO.
457 */
458 count = min(burst, len - offset - 1);
459 while (count--)
Aaron Durbinc4220022013-07-24 16:02:14 -0500460 tpm_write_data(data[offset++], locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700461
Aaron Durbinc4220022013-07-24 16:02:14 -0500462 if (tis_wait_valid(locality) || !tis_expect_data(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700463 printf("%s:%d TPM command feed overflow\n",
464 __FILE__, __LINE__);
465 return TPM_DRIVER_ERR;
466 }
467
Aaron Durbinc4220022013-07-24 16:02:14 -0500468 burst = tpm_read_burst_count(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700469 if ((offset == (len - 1)) && burst)
470 /*
471 * We need to be able to send the last byte to the
472 * device, so burst size must be nonzero before we
473 * break out.
474 */
475 break;
476 }
477
478 /* Send the last byte. */
Aaron Durbinc4220022013-07-24 16:02:14 -0500479 tpm_write_data(data[offset++], locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700480
481 /*
482 * Verify that TPM does not expect any more data as part of this
483 * command.
484 */
Aaron Durbinc4220022013-07-24 16:02:14 -0500485 if (tis_wait_valid(locality) || tis_expect_data(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700486 printf("%s:%d unexpected TPM status 0x%x\n",
Aaron Durbinc4220022013-07-24 16:02:14 -0500487 __FILE__, __LINE__, tpm_read_status(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700488 return TPM_DRIVER_ERR;
489 }
490
491 /* OK, sitting pretty, let's start the command execution. */
Aaron Durbinc4220022013-07-24 16:02:14 -0500492 tpm_write_status(TIS_STS_TPM_GO, locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700493
494 return 0;
495}
496
497/*
498 * tis_readresponse()
499 *
500 * read the TPM device response after a command was issued.
501 *
502 * @buffer - address where to read the response, byte by byte.
503 * @len - pointer to the size of buffer
504 *
505 * On success stores the number of received bytes to len and returns 0. On
506 * errors (misformatted TPM data or synchronization problems) returns
507 * TPM_DRIVER_ERR.
508 */
509static u32 tis_readresponse(u8 *buffer, size_t *len)
510{
511 u16 burst_count;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700512 u32 offset = 0;
513 u8 locality = 0;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700514 u32 expected_count = *len;
515 int max_cycles = 0;
516
517 /* Wait for the TPM to process the command */
Aaron Durbinc4220022013-07-24 16:02:14 -0500518 if (tis_wait_valid_data(locality)) {
519 printf("%s:%d failed processing command\n", __FILE__, __LINE__);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700520 return TPM_DRIVER_ERR;
521 }
522
523 do {
Aaron Durbinc4220022013-07-24 16:02:14 -0500524 while ((burst_count = tpm_read_burst_count(locality)) == 0) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700525 if (max_cycles++ == MAX_DELAY_US) {
526 printf("%s:%d TPM stuck on read\n",
527 __FILE__, __LINE__);
528 return TPM_DRIVER_ERR;
529 }
530 udelay(1);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700531 }
532
533 max_cycles = 0;
534
535 while (burst_count-- && (offset < expected_count)) {
Aaron Durbinc4220022013-07-24 16:02:14 -0500536 buffer[offset++] = tpm_read_data(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700537 if (offset == 6) {
538 /*
539 * We got the first six bytes of the reply,
540 * let's figure out how many bytes to expect
541 * total - it is stored as a 4 byte number in
542 * network order, starting with offset 2 into
543 * the body of the reply.
544 */
545 u32 real_length;
546 memcpy(&real_length,
547 buffer + 2,
548 sizeof(real_length));
549 expected_count = be32_to_cpu(real_length);
550
551 if ((expected_count < offset) ||
552 (expected_count > *len)) {
553 printf("%s:%d bad response size %d\n",
554 __FILE__, __LINE__,
555 expected_count);
556 return TPM_DRIVER_ERR;
557 }
558 }
559 }
560
561 /* Wait for the next portion */
Aaron Durbinc4220022013-07-24 16:02:14 -0500562 if (tis_wait_valid(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700563 printf("%s:%d failed to read response\n",
564 __FILE__, __LINE__);
565 return TPM_DRIVER_ERR;
566 }
567
568 if (offset == expected_count)
569 break; /* We got all we need */
570
Aaron Durbinc4220022013-07-24 16:02:14 -0500571 } while (tis_has_valid_data(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700572
Aaron Durbinc4220022013-07-24 16:02:14 -0500573 /* * Make sure we indeed read all there was. */
574 if (tis_has_valid_data(locality)) {
575 printf("%s:%d wrong receive status: %x %d bytes left\n",
576 __FILE__, __LINE__, tpm_read_status(locality),
577 tpm_read_burst_count(locality));
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700578 return TPM_DRIVER_ERR;
579 }
580
581 /* Tell the TPM that we are done. */
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700582 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
583 return TPM_DRIVER_ERR;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700584
585 *len = offset;
586 return 0;
587}
588
589/*
590 * tis_init()
591 *
592 * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on
593 * failure (in case device probing did not succeed).
594 */
595int tis_init(void)
596{
597 if (tis_probe())
598 return TPM_DRIVER_ERR;
599 return 0;
600}
601
602/*
603 * tis_open()
604 *
605 * Requests access to locality 0 for the caller. After all commands have been
606 * completed the caller is supposed to call tis_close().
607 *
608 * Returns 0 on success, TPM_DRIVER_ERR on failure.
609 */
610int tis_open(void)
611{
612 u8 locality = 0; /* we use locality zero for everything */
613
614 if (tis_close())
615 return TPM_DRIVER_ERR;
616
617 /* now request access to locality */
Aaron Durbinc4220022013-07-24 16:02:14 -0500618 tis_request_access(locality);
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700619
620 /* did we get a lock? */
Aaron Durbinc4220022013-07-24 16:02:14 -0500621 if (tis_wait_received_access(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700622 printf("%s:%d - failed to lock locality %d\n",
623 __FILE__, __LINE__, locality);
624 return TPM_DRIVER_ERR;
625 }
626
Stefan Reinauerc908fc72012-04-30 16:33:44 -0700627 /* Certain TPMs seem to need some delay here or they hang... */
628 udelay(10);
629
630 if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
631 return TPM_DRIVER_ERR;
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700632
633 return 0;
634}
635
636/*
637 * tis_close()
638 *
Martin Roth56889792013-07-09 21:39:46 -0600639 * terminate the current session with the TPM by releasing the locked
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700640 * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock
641 * removal did not succeed).
642 */
643int tis_close(void)
644{
645 u8 locality = 0;
Aaron Durbinc4220022013-07-24 16:02:14 -0500646 if (tis_has_access(locality)) {
647 tis_drop_access(locality);
648 if (tis_wait_dropped_access(locality)) {
Stefan Reinauer3008bbad2011-10-11 14:46:25 -0700649 printf("%s:%d - failed to release locality %d\n",
650 __FILE__, __LINE__, locality);
651 return TPM_DRIVER_ERR;
652 }
653 }
654 return 0;
655}
656
657/*
658 * tis_sendrecv()
659 *
660 * Send the requested data to the TPM and then try to get its response
661 *
662 * @sendbuf - buffer of the data to send
663 * @send_size size of the data to send
664 * @recvbuf - memory to save the response to
665 * @recv_len - pointer to the size of the response buffer
666 *
667 * Returns 0 on success (and places the number of response bytes at recv_len)
668 * or TPM_DRIVER_ERR on failure.
669 */
670int tis_sendrecv(const uint8_t *sendbuf, size_t send_size,
671 uint8_t *recvbuf, size_t *recv_len)
672{
673 if (tis_senddata(sendbuf, send_size)) {
674 printf("%s:%d failed sending data to TPM\n",
675 __FILE__, __LINE__);
676 return TPM_DRIVER_ERR;
677 }
678
679 return tis_readresponse(recvbuf, recv_len);
680}
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700681
682#ifdef __RAMSTAGE__
683
684/*
685 * tis_setup_interrupt()
686 *
687 * Set up the interrupt vector and polarity for locality 0 and
688 * disable all interrupts so they are unused in firmware but can
689 * be enabled by the OS.
690 *
691 * The values used here must match what is passed in the TPM ACPI
692 * device if ACPI is used on the platform.
693 *
694 * @vector - TPM interrupt vector
695 * @polarity - TPM interrupt polarity
696 *
697 * Returns 0 on success, TPM_DRIVER_ERR on failure.
698 */
699static int tis_setup_interrupt(int vector, int polarity)
700{
701 u8 locality = 0;
702 int has_access = tis_has_access(locality);
703
704 /* Open connection and request access if not already granted */
705 if (!has_access && tis_open() < 0)
706 return TPM_DRIVER_ERR;
707
708 /* Set TPM interrupt vector */
709 tpm_write_int_vector(vector, locality);
710
711 /* Set TPM interupt polarity and disable interrupts */
712 tpm_write_int_polarity(polarity, locality);
713
714 /* Close connection if it was opened */
715 if (!has_access && tis_close() < 0)
716 return TPM_DRIVER_ERR;
717
718 return 0;
719}
720
721static void lpc_tpm_read_resources(struct device *dev)
722{
723 /* Static 5K memory region specified in Kconfig */
724 mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10);
725}
726
727static void lpc_tpm_set_resources(struct device *dev)
728{
729 tpm_config_t *config = (tpm_config_t *)dev->chip_info;
730 struct resource *res;
731
732 for (res = dev->resource_list; res; res = res->next) {
733 if (!(res->flags & IORESOURCE_ASSIGNED))
734 continue;
735
736 if (res->flags & IORESOURCE_IRQ) {
737 /* Set interrupt vector */
738 tis_setup_interrupt((int)res->base,
739 config->irq_polarity);
740 } else {
Duncan Lauriedd281ed2014-10-30 15:20:19 -0700741 continue;
742 }
743
744 res->flags |= IORESOURCE_STORED;
745 report_resource_stored(dev, res, " <tpm>");
746 }
747}
748
749static struct device_operations lpc_tpm_ops = {
750 .read_resources = &lpc_tpm_read_resources,
751 .set_resources = &lpc_tpm_set_resources,
752};
753
754static struct pnp_info pnp_dev_info[] = {
755 { .flags = PNP_IRQ0 }
756};
757
758static void enable_dev(struct device *dev)
759{
760 pnp_enable_devices(dev, &lpc_tpm_ops,
761 ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
762}
763
764struct chip_operations drivers_pc80_tpm_ops = {
765 CHIP_NAME("LPC TPM")
766 .enable_dev = enable_dev
767};
768
769#endif /* __RAMSTAGE__ */