coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
tis.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 /*
4  * The code in this file has been heavily based on the article "Writing a TPM
5  * Device Driver" published on http://ptgmedia.pearsoncmg.com and the
6  * submission by Stefan Berger on Qemu-devel mailing list.
7  *
8  * One principal difference is that in the simplest config the other than 0
9  * TPM localities do not get mapped by some devices (for instance, by
10  * Infineon slb9635), so this driver provides access to locality 0 only.
11  */
12 
13 #include <commonlib/helpers.h>
14 #include <string.h>
15 #include <delay.h>
16 #include <device/mmio.h>
17 #include <acpi/acpi.h>
18 #include <acpi/acpigen.h>
19 #include <acpi/acpi_device.h>
20 #include <device/device.h>
21 #include <console/console.h>
22 #include <security/tpm/tis.h>
23 #include <device/pnp.h>
24 #include <drivers/tpm/tpm_ppi.h>
25 #include <timer.h>
26 #include "chip.h"
27 
28 #define PREFIX "lpc_tpm: "
29 
30 /* coreboot wrapper for TPM driver (start) */
31 #define TPM_DEBUG(fmt, args...) \
32  if (CONFIG(DEBUG_TPM)) { \
33  printk(BIOS_DEBUG, PREFIX); \
34  printk(BIOS_DEBUG, fmt, ##args); \
35  }
36 #define TPM_DEBUG_IO_READ(reg_, val_) \
37  TPM_DEBUG("Read reg 0x%x returns 0x%x\n", (reg_), (val_))
38 #define TPM_DEBUG_IO_WRITE(reg_, val_) \
39  TPM_DEBUG("Write reg 0x%x with 0x%x\n", (reg_), (val_))
40 #define printf(x...) printk(BIOS_ERR, x)
41 
42 /* coreboot wrapper for TPM driver (end) */
43 
44 /* the macro accepts the locality value, but only locality 0 is operational */
45 #define TIS_REG(LOCALITY, REG) \
46  (void *)(uintptr_t)(CONFIG_TPM_TIS_BASE_ADDRESS + (LOCALITY << 12) + REG)
47 
48 /* hardware registers' offsets */
49 #define TIS_REG_ACCESS 0x0
50 #define TIS_REG_INT_ENABLE 0x8
51 #define TIS_REG_INT_VECTOR 0xc
52 #define TIS_REG_INT_STATUS 0x10
53 #define TIS_REG_INTF_CAPABILITY 0x14
54 #define TIS_REG_STS 0x18
55 #define TIS_REG_BURST_COUNT 0x19
56 #define TIS_REG_DATA_FIFO 0x24
57 #define TIS_REG_DID_VID 0xf00
58 #define TIS_REG_RID 0xf04
59 
60 /* Some registers' bit field definitions */
61 #define TIS_STS_VALID (1 << 7) /* 0x80 */
62 #define TIS_STS_COMMAND_READY (1 << 6) /* 0x40 */
63 #define TIS_STS_TPM_GO (1 << 5) /* 0x20 */
64 #define TIS_STS_DATA_AVAILABLE (1 << 4) /* 0x10 */
65 #define TIS_STS_EXPECT (1 << 3) /* 0x08 */
66 #define TIS_STS_RESPONSE_RETRY (1 << 1) /* 0x02 */
67 
68 #define TIS_ACCESS_TPM_REG_VALID_STS (1 << 7) /* 0x80 */
69 #define TIS_ACCESS_ACTIVE_LOCALITY (1 << 5) /* 0x20 */
70 #define TIS_ACCESS_BEEN_SEIZED (1 << 4) /* 0x10 */
71 #define TIS_ACCESS_SEIZE (1 << 3) /* 0x08 */
72 #define TIS_ACCESS_PENDING_REQUEST (1 << 2) /* 0x04 */
73 #define TIS_ACCESS_REQUEST_USE (1 << 1) /* 0x02 */
74 #define TIS_ACCESS_TPM_ESTABLISHMENT (1 << 0) /* 0x01 */
75 
76 /*
77  * Error value returned if a tpm register does not enter the expected state
78  * after continuous polling. No actual TPM register reading ever returns ~0,
79  * so this value is a safe error indication to be mixed with possible status
80  * register values.
81  */
82 #define TPM_TIMEOUT_ERR (~0)
83 
84 /* Error value returned on various TPM driver errors */
85 #define TPM_DRIVER_ERR (~0)
86 
87  /* 1 second is plenty for anything TPM does.*/
88 #define MAX_DELAY_US USECS_PER_SEC
89 
90 /*
91  * Structures defined below allow creating descriptions of TPM vendor/device
92  * ID information for run time discovery. The only device the system knows
93  * about at this time is Infineon slb9635
94  */
95 struct device_name {
97  const char *const dev_name;
98 };
99 
100 struct vendor_name {
102  const char *vendor_name;
103  const struct device_name *dev_names;
104 };
105 
106 static const struct device_name atmel_devices[] = {
107  {0x3204, "AT97SC3204"},
108  {0xffff}
109 };
110 
111 static const struct device_name infineon_devices[] = {
112  {0x000b, "SLB9635 TT 1.2"},
113 #if CONFIG(TPM2)
114  {0x001a, "SLB9665 TT 2.0"},
115  {0x001b, "SLB9670 TT 2.0"},
116 #else
117  {0x001a, "SLB9660 TT 1.2"},
118  {0x001b, "SLB9670 TT 1.2"},
119 #endif
120  {0xffff}
121 };
122 
123 static const struct device_name nuvoton_devices[] = {
124  {0x00fe, "NPCT420AA V2"},
125  {0xffff}
126 };
127 
128 static const struct device_name stmicro_devices[] = {
129  {0x0000, "ST33ZP24" },
130  {0xffff}
131 };
132 
133 static const struct device_name swtpm_devices[] = {
134 #if CONFIG(TPM2)
135  {0x0001, "SwTPM 2.0" },
136 #endif
137  {0xffff}
138 };
139 
140 static const struct vendor_name vendor_names[] = {
141  {0x1114, "Atmel", atmel_devices},
142  {0x15d1, "Infineon", infineon_devices},
143  {0x1050, "Nuvoton", nuvoton_devices},
144  {0x1014, "TPM Emulator", swtpm_devices},
145  {0x104a, "ST Microelectronics", stmicro_devices},
146 };
147 
148 /*
149  * Cached vendor/device ID pair to indicate that the device has been already
150  * discovered
151  */
153 
154 static inline u8 tpm_read_status(int locality)
155 {
156  u8 value = read8(TIS_REG(locality, TIS_REG_STS));
158  return value;
159 }
160 
161 static inline void tpm_write_status(u8 sts, int locality)
162 {
164  write8(TIS_REG(locality, TIS_REG_STS), sts);
165 }
166 
167 static inline u8 tpm_read_data(int locality)
168 {
169  u8 value = read8(TIS_REG(locality, TIS_REG_DATA_FIFO));
171  return value;
172 }
173 
174 static inline void tpm_write_data(u8 data, int locality)
175 {
177  write8(TIS_REG(locality, TIS_REG_DATA_FIFO), data);
178 }
179 
180 static inline u16 tpm_read_burst_count(int locality)
181 {
182  u16 count;
183  count = read8(TIS_REG(locality, TIS_REG_BURST_COUNT));
184  count |= read8(TIS_REG(locality, TIS_REG_BURST_COUNT + 1)) << 8;
186  return count;
187 }
188 
189 static inline u8 tpm_read_access(int locality)
190 {
191  u8 value = read8(TIS_REG(locality, TIS_REG_ACCESS));
193  return value;
194 }
195 
196 static inline void tpm_write_access(u8 data, int locality)
197 {
199  write8(TIS_REG(locality, TIS_REG_ACCESS), data);
200 }
201 
202 static inline u32 tpm_read_did_vid(int locality)
203 {
204  u32 value = read32(TIS_REG(locality, TIS_REG_DID_VID));
206  return value;
207 }
208 
209 static inline void tpm_write_int_vector(int vector, int locality)
210 {
212  write8(TIS_REG(locality, TIS_REG_INT_VECTOR), vector & 0xf);
213 }
214 
215 static inline u8 tpm_read_int_vector(int locality)
216 {
217  u8 value = read8(TIS_REG(locality, TIS_REG_INT_VECTOR));
219  return value;
220 }
221 
222 static inline void tpm_write_int_polarity(int polarity, int locality)
223 {
224  /* Set polarity and leave all other bits at 0 */
225  u32 value = (polarity & 0x3) << 3;
228 }
229 
230 static inline u32 tpm_read_int_polarity(int locality)
231 {
232  /* Get polarity and leave all other bits */
233  u32 value = read8(TIS_REG(locality, TIS_REG_INT_ENABLE));
234  value = (value >> 3) & 0x3;
236  return value;
237 }
238 
239 /*
240  * tis_wait_sts()
241  *
242  * Wait for at most a second for a status to change its state to match the
243  * expected state. Normally the transition happens within microseconds.
244  *
245  * @locality - locality
246  * @mask - bitmask for the bitfield(s) to watch
247  * @expected - value the field(s) are supposed to be set to
248  *
249  * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
250  */
251 static int tis_wait_sts(int locality, u8 mask, u8 expected)
252 {
253  struct stopwatch sw;
254 
256  do {
257  u8 value = tpm_read_status(locality);
258  if ((value & mask) == expected)
259  return 0;
260  udelay(1);
261  } while (!stopwatch_expired(&sw));
262  return TPM_TIMEOUT_ERR;
263 }
264 
265 static inline int tis_wait_ready(int locality)
266 {
267  return tis_wait_sts(locality, TIS_STS_COMMAND_READY,
269 }
270 
271 static inline int tis_wait_valid(int locality)
272 {
273  return tis_wait_sts(locality, TIS_STS_VALID, TIS_STS_VALID);
274 }
275 
276 static inline int tis_wait_valid_data(int locality)
277 {
278  const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
279  return tis_wait_sts(locality, has_data, has_data);
280 }
281 
282 static inline int tis_has_valid_data(int locality)
283 {
284  const u8 has_data = TIS_STS_DATA_AVAILABLE | TIS_STS_VALID;
285  return (tpm_read_status(locality) & has_data) == has_data;
286 }
287 
288 static inline int tis_expect_data(int locality)
289 {
290  return !!(tpm_read_status(locality) & TIS_STS_EXPECT);
291 }
292 
293 /*
294  * tis_wait_access()
295  *
296  * Wait for at most a second for a access to change its state to match the
297  * expected state. Normally the transition happens within microseconds.
298  *
299  * @locality - locality
300  * @mask - bitmask for the bitfield(s) to watch
301  * @expected - value the field(s) are supposed to be set to
302  *
303  * Returns 0 on success or TPM_TIMEOUT_ERR on timeout.
304  */
305 static int tis_wait_access(int locality, u8 mask, u8 expected)
306 {
307  struct stopwatch sw;
308 
310  do {
311  u8 value = tpm_read_access(locality);
312  if ((value & mask) == expected)
313  return 0;
314  udelay(1);
315  } while (!stopwatch_expired(&sw));
316  return TPM_TIMEOUT_ERR;
317 }
318 
319 static inline int tis_wait_dropped_access(int locality)
320 {
321  return tis_wait_access(locality, TIS_ACCESS_ACTIVE_LOCALITY, 0);
322 }
323 
324 static inline int tis_wait_received_access(int locality)
325 {
328 }
329 
330 static inline int tis_has_access(int locality)
331 {
332  return !!(tpm_read_access(locality) & TIS_ACCESS_ACTIVE_LOCALITY);
333 }
334 
335 static inline void tis_request_access(int locality)
336 {
338 }
339 
340 static inline void tis_drop_access(int locality)
341 {
343 }
344 
345 /*
346  * PC Client Specific TPM Interface Specification section 11.2.12:
347  *
348  * Software must be prepared to send two writes of a "1" to command ready
349  * field: the first to indicate successful read of all the data, thus
350  * clearing the data from the ReadFIFO and freeing the TPM's resources,
351  * and the second to indicate to the TPM it is about to send a new command.
352  *
353  * In practice not all TPMs behave the same so it is necessary to be
354  * flexible when trying to set command ready.
355  *
356  * Returns 0 on success if the TPM is ready for transactions.
357  * Returns TPM_TIMEOUT_ERR if the command ready bit does not get set.
358  */
359 static int tis_command_ready(u8 locality)
360 {
361  u32 status;
362 
363  /* 1st attempt to set command ready */
365 
366  /* Wait for response */
367  status = tpm_read_status(locality);
368 
369  /* Check if command ready is set yet */
370  if (status & TIS_STS_COMMAND_READY)
371  return 0;
372 
373  /* 2nd attempt to set command ready */
375 
376  return tis_wait_ready(locality);
377 }
378 
379 /*
380  * Probe the TPM device and try determining its manufacturer/device name.
381  *
382  * Returns 0 on success (the device is found or was found during an earlier
383  * invocation) or TPM_DRIVER_ERR if the device is not found.
384  */
385 static u32 tis_probe(void)
386 {
387  const char *device_name = "unknown";
388  const char *vendor_name = device_name;
389  const struct device_name *dev;
390  u32 didvid;
391  u16 vid, did;
392  int i;
393 
394  if (vendor_dev_id)
395  return 0; /* Already probed. */
396 
397  didvid = tpm_read_did_vid(0);
398  if (!didvid || (didvid == 0xffffffff)) {
399  printf("%s: No TPM device found\n", __func__);
400  return TPM_DRIVER_ERR;
401  }
402 
403  vendor_dev_id = didvid;
404 
405  vid = didvid & 0xffff;
406  did = (didvid >> 16) & 0xffff;
407  for (i = 0; i < ARRAY_SIZE(vendor_names); i++) {
408  int j = 0;
409  u16 known_did;
410  if (vid == vendor_names[i].vendor_id) {
412  } else {
413  continue;
414  }
415  dev = &vendor_names[i].dev_names[j];
416  while ((known_did = dev->dev_id) != 0xffff) {
417  if (known_did == did) {
418  device_name = dev->dev_name;
419  break;
420  }
421  j++;
422  dev = &vendor_names[i].dev_names[j];
423  }
424  break;
425  }
426  /* this will have to be converted into debug printout */
427  printk(BIOS_INFO, "Found TPM %s by %s\n", device_name, vendor_name);
428  return 0;
429 }
430 
431 /*
432  * tis_senddata()
433  *
434  * send the passed in data to the TPM device.
435  *
436  * @data - address of the data to send, byte by byte
437  * @len - length of the data to send
438  *
439  * Returns 0 on success, TPM_DRIVER_ERR on error (in case the device does
440  * not accept the entire command).
441  */
442 static u32 tis_senddata(const u8 *const data, u32 len)
443 {
444  u32 offset = 0;
445  u16 burst = 0;
446  u8 locality = 0;
447 
448  if (tis_wait_ready(locality)) {
449  printf("%s:%d - failed to get 'command_ready' status\n",
450  __FILE__, __LINE__);
451  return TPM_DRIVER_ERR;
452  }
453  burst = tpm_read_burst_count(locality);
454 
455  while (1) {
456  unsigned int count;
457  struct stopwatch sw;
458 
459  /* Wait till the device is ready to accept more data. */
461  while (!burst) {
462  if (stopwatch_expired(&sw)) {
463  printf("%s:%d failed to feed %u bytes of %u\n",
464  __FILE__, __LINE__, len - offset, len);
465  return TPM_DRIVER_ERR;
466  }
467  udelay(1);
468  burst = tpm_read_burst_count(locality);
469  }
470 
471  /*
472  * Calculate number of bytes the TPM is ready to accept in one
473  * shot.
474  *
475  * We want to send the last byte outside of the loop (hence
476  * the -1 below) to make sure that the 'expected' status bit
477  * changes to zero exactly after the last byte is fed into the
478  * FIFO.
479  */
480  count = MIN(burst, len - offset - 1);
481  while (count--)
482  tpm_write_data(data[offset++], locality);
483 
484  if (tis_wait_valid(locality) || !tis_expect_data(locality)) {
485  printf("%s:%d TPM command feed overflow\n",
486  __FILE__, __LINE__);
487  return TPM_DRIVER_ERR;
488  }
489 
490  burst = tpm_read_burst_count(locality);
491  if ((offset == (len - 1)) && burst)
492  /*
493  * We need to be able to send the last byte to the
494  * device, so burst size must be nonzero before we
495  * break out.
496  */
497  break;
498  }
499 
500  /* Send the last byte. */
501  tpm_write_data(data[offset++], locality);
502 
503  /*
504  * Verify that TPM does not expect any more data as part of this
505  * command.
506  */
507  if (tis_wait_valid(locality) || tis_expect_data(locality)) {
508  printf("%s:%d unexpected TPM status 0x%x\n",
509  __FILE__, __LINE__, tpm_read_status(locality));
510  return TPM_DRIVER_ERR;
511  }
512 
513  /* OK, sitting pretty, let's start the command execution. */
514  tpm_write_status(TIS_STS_TPM_GO, locality);
515 
516  return 0;
517 }
518 
519 /*
520  * tis_readresponse()
521  *
522  * read the TPM device response after a command was issued.
523  *
524  * @buffer - address where to read the response, byte by byte.
525  * @len - pointer to the size of buffer
526  *
527  * On success stores the number of received bytes to len and returns 0. On
528  * errors (misformatted TPM data or synchronization problems) returns
529  * TPM_DRIVER_ERR.
530  */
531 static u32 tis_readresponse(u8 *buffer, size_t *len)
532 {
533  u16 burst_count;
534  u32 offset = 0;
535  u8 locality = 0;
536  u32 expected_count = *len;
537  int max_cycles = 0;
538 
539  /* Wait for the TPM to process the command */
540  if (tis_wait_valid_data(locality)) {
541  printf("%s:%d failed processing command\n", __FILE__, __LINE__);
542  return TPM_DRIVER_ERR;
543  }
544 
545  do {
546  while ((burst_count = tpm_read_burst_count(locality)) == 0) {
547  if (max_cycles++ == MAX_DELAY_US) {
548  printf("%s:%d TPM stuck on read\n",
549  __FILE__, __LINE__);
550  return TPM_DRIVER_ERR;
551  }
552  udelay(1);
553  }
554 
555  max_cycles = 0;
556 
557  while (burst_count-- && (offset < expected_count)) {
558  buffer[offset++] = tpm_read_data(locality);
559  if (offset == 6) {
560  /*
561  * We got the first six bytes of the reply,
562  * let's figure out how many bytes to expect
563  * total - it is stored as a 4 byte number in
564  * network order, starting with offset 2 into
565  * the body of the reply.
566  */
567  u32 real_length;
568  memcpy(&real_length,
569  buffer + 2,
570  sizeof(real_length));
571  expected_count = be32_to_cpu(real_length);
572 
573  if ((expected_count < offset) ||
574  (expected_count > *len)) {
575  printf("%s:%d bad response size %u\n",
576  __FILE__, __LINE__,
577  expected_count);
578  return TPM_DRIVER_ERR;
579  }
580  }
581  }
582 
583  /* Wait for the next portion */
584  if (tis_wait_valid(locality)) {
585  printf("%s:%d failed to read response\n",
586  __FILE__, __LINE__);
587  return TPM_DRIVER_ERR;
588  }
589 
590  if (offset == expected_count)
591  break; /* We got all we need */
592 
593  /*
594  * Certain TPMs seem to need some delay between tis_wait_valid()
595  * and tis_has_valid_data(), or some race-condition-related
596  * issue will occur.
597  */
598  if (CONFIG(TPM_RDRESP_NEED_DELAY))
599  udelay(10);
600 
601  } while (tis_has_valid_data(locality));
602 
603  /* * Make sure we indeed read all there was. */
604  if (tis_has_valid_data(locality)) {
605  printf("%s:%d wrong receive status: %x %u bytes left\n",
606  __FILE__, __LINE__, tpm_read_status(locality),
607  tpm_read_burst_count(locality));
608  return TPM_DRIVER_ERR;
609  }
610 
611  /* Tell the TPM that we are done. */
612  if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
613  return TPM_DRIVER_ERR;
614 
615  *len = offset;
616  return 0;
617 }
618 
619 /*
620  * tis_init()
621  *
622  * Initialize the TPM device. Returns 0 on success or TPM_DRIVER_ERR on
623  * failure (in case device probing did not succeed).
624  */
625 int tis_init(void)
626 {
627  if (tis_probe())
628  return TPM_DRIVER_ERR;
629  return 0;
630 }
631 
632 /*
633  * tis_open()
634  *
635  * Requests access to locality 0 for the caller. After all commands have been
636  * completed the caller is supposed to call tis_close().
637  *
638  * Returns 0 on success, TPM_DRIVER_ERR on failure.
639  */
640 int tis_open(void)
641 {
642  u8 locality = 0; /* we use locality zero for everything */
643 
644  if (tis_close())
645  return TPM_DRIVER_ERR;
646 
647  /* now request access to locality */
648  tis_request_access(locality);
649 
650  /* did we get a lock? */
651  if (tis_wait_received_access(locality)) {
652  printf("%s:%d - failed to lock locality %u\n",
653  __FILE__, __LINE__, locality);
654  return TPM_DRIVER_ERR;
655  }
656 
657  /* Certain TPMs seem to need some delay here or they hang... */
658  udelay(10);
659 
660  if (tis_command_ready(locality) == TPM_TIMEOUT_ERR)
661  return TPM_DRIVER_ERR;
662 
663  return 0;
664 }
665 
666 /*
667  * tis_close()
668  *
669  * terminate the current session with the TPM by releasing the locked
670  * locality. Returns 0 on success of TPM_DRIVER_ERR on failure (in case lock
671  * removal did not succeed).
672  */
673 int tis_close(void)
674 {
675  u8 locality = 0;
676  if (tis_has_access(locality)) {
677  tis_drop_access(locality);
678  if (tis_wait_dropped_access(locality)) {
679  printf("%s:%d - failed to release locality %u\n",
680  __FILE__, __LINE__, locality);
681  return TPM_DRIVER_ERR;
682  }
683  }
684  return 0;
685 }
686 
687 /*
688  * tis_sendrecv()
689  *
690  * Send the requested data to the TPM and then try to get its response
691  *
692  * @sendbuf - buffer of the data to send
693  * @send_size size of the data to send
694  * @recvbuf - memory to save the response to
695  * @recv_len - pointer to the size of the response buffer
696  *
697  * Returns 0 on success (and places the number of response bytes at recv_len)
698  * or TPM_DRIVER_ERR on failure.
699  */
700 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size,
701  uint8_t *recvbuf, size_t *recv_len)
702 {
703  if (tis_senddata(sendbuf, send_size)) {
704  printf("%s:%d failed sending data to TPM\n",
705  __FILE__, __LINE__);
706  return TPM_DRIVER_ERR;
707  }
708 
709  return tis_readresponse(recvbuf, recv_len);
710 }
711 
712 /*
713  * tis_setup_interrupt()
714  *
715  * Set up the interrupt vector and polarity for locality 0 and
716  * disable all interrupts so they are unused in firmware but can
717  * be enabled by the OS.
718  *
719  * The values used here must match what is passed in the TPM ACPI
720  * device if ACPI is used on the platform.
721  *
722  * @vector - TPM interrupt vector
723  * @polarity - TPM interrupt polarity
724  *
725  * Returns 0 on success, TPM_DRIVER_ERR on failure.
726  */
727 static int tis_setup_interrupt(int vector, int polarity)
728 {
729  u8 locality = 0;
730  int has_access = tis_has_access(locality);
731 
732  /* Open connection and request access if not already granted */
733  if (!has_access && tis_open() < 0)
734  return TPM_DRIVER_ERR;
735 
736  /* Set TPM interrupt vector */
737  tpm_write_int_vector(vector, locality);
738 
739  /* Set TPM interrupt polarity and disable interrupts */
740  tpm_write_int_polarity(polarity, locality);
741 
742  /* Close connection if it was opened */
743  if (!has_access && tis_close() < 0)
744  return TPM_DRIVER_ERR;
745 
746  return 0;
747 }
748 
749 static void lpc_tpm_read_resources(struct device *dev)
750 {
751  /* Static 5K memory region specified in Kconfig */
752  mmio_resource(dev, 0, CONFIG_TPM_TIS_BASE_ADDRESS >> 10, 0x5000 >> 10);
753 }
754 
755 static void lpc_tpm_set_resources(struct device *dev)
756 {
758  DEVTREE_CONST struct resource *res;
759 
760  for (res = dev->resource_list; res; res = res->next) {
761  if (!(res->flags & IORESOURCE_ASSIGNED))
762  continue;
763 
764  if (res->flags & IORESOURCE_IRQ) {
765  /* Set interrupt vector */
766  tis_setup_interrupt((int)res->base,
767  config->irq_polarity);
768  } else {
769  continue;
770  }
771 
772 #if !DEVTREE_EARLY
773  res->flags |= IORESOURCE_STORED;
774  report_resource_stored(dev, res, " <tpm>");
775 #endif
776  }
777 }
778 
779 #if CONFIG(HAVE_ACPI_TABLES)
780 static void lpc_tpm_fill_ssdt(const struct device *dev)
781 {
782  /* Windows 11 requires the following path for TPM to be detected */
783  const char *path = "\\_SB_.PCI0";
784 
785  /* Device */
786  acpigen_write_scope(path);
788 
789  if (CONFIG(TPM2)) {
790  acpigen_write_name_string("_HID", "MSFT0101");
791  acpigen_write_name_string("_CID", "MSFT0101");
792  } else {
793  acpigen_write_name("_HID");
794  acpigen_emit_eisaid("PNP0C31");
795 
796  acpigen_write_name("_CID");
797  acpigen_emit_eisaid("PNP0C31");
798  }
799 
801 
802  u32 did_vid = tpm_read_did_vid(0);
803  if (did_vid > 0 && did_vid < 0xffffffff)
805  else
807 
808  u16 port = dev->path.pnp.port;
809 
810  /* Resources */
811  acpigen_write_name("_CRS");
813  acpigen_write_mem32fixed(1, CONFIG_TPM_TIS_BASE_ADDRESS, 0x5000);
814  if (port)
815  acpigen_write_io16(port, port, 1, 2, 1);
816 
817  if (CONFIG_TPM_PIRQ) {
818  /*
819  * PIRQ: Update interrupt vector with configured PIRQ
820  * Active-Low Level-Triggered Shared
821  */
822  struct acpi_irq tpm_irq_a = ACPI_IRQ_LEVEL_LOW(CONFIG_TPM_PIRQ);
823  acpi_device_write_interrupt(&tpm_irq_a);
824  } else if (tpm_read_int_vector(0) > 0) {
825  u8 int_vec = tpm_read_int_vector(0);
826  u8 int_pol = tpm_read_int_polarity(0);
827  struct acpi_irq tpm_irq = ACPI_IRQ_LEVEL_LOW(int_vec);
828 
829  if (int_pol & 1)
830  tpm_irq.polarity = ACPI_IRQ_ACTIVE_LOW;
831  else
832  tpm_irq.polarity = ACPI_IRQ_ACTIVE_HIGH;
833 
834  if (int_pol & 2)
835  tpm_irq.mode = ACPI_IRQ_EDGE_TRIGGERED;
836  else
837  tpm_irq.mode = ACPI_IRQ_LEVEL_TRIGGERED;
838 
839  acpi_device_write_interrupt(&tpm_irq);
840  }
841 
842 
844 
845  if (!CONFIG(CHROMEOS))
847 
848  acpigen_pop_len(); /* Device */
849  acpigen_pop_len(); /* Scope */
850 
851 #if !DEVTREE_EARLY
852  printk(BIOS_INFO, "%s.%s: %s %s\n", path, acpi_device_name(dev),
853  dev->chip_ops->name, dev_path(dev));
854 #endif
855 }
856 
857 static const char *lpc_tpm_acpi_name(const struct device *dev)
858 {
859  return "TPM";
860 }
861 #endif
862 
863 static struct device_operations lpc_tpm_ops = {
865  .set_resources = lpc_tpm_set_resources,
866 #if CONFIG(HAVE_ACPI_TABLES)
867  .acpi_name = lpc_tpm_acpi_name,
868  .acpi_fill_ssdt = lpc_tpm_fill_ssdt,
869 #endif
870 };
871 
872 static struct pnp_info pnp_dev_info[] = {
873  { .flags = PNP_IRQ0 }
874 };
875 
876 static void enable_dev(struct device *dev)
877 {
878  if (CONFIG(TPM))
881 }
882 
884  CHIP_NAME("LPC TPM")
885  .enable_dev = enable_dev
886 };
void acpi_device_write_uid(const struct device *dev)
Definition: device.c:203
void acpi_device_write_interrupt(const struct acpi_irq *irq)
Definition: device.c:209
const char * acpi_device_name(const struct device *dev)
Definition: device.c:49
@ ACPI_IRQ_ACTIVE_HIGH
Definition: acpi_device.h:74
@ ACPI_IRQ_ACTIVE_LOW
Definition: acpi_device.h:73
@ ACPI_IRQ_EDGE_TRIGGERED
Definition: acpi_device.h:68
@ ACPI_IRQ_LEVEL_TRIGGERED
Definition: acpi_device.h:69
#define ACPI_IRQ_LEVEL_LOW(x)
Definition: acpi_device.h:111
void acpigen_pop_len(void)
Definition: acpigen.c:37
void acpigen_write_scope(const char *name)
Definition: acpigen.c:326
void acpigen_write_resourcetemplate_footer(void)
Definition: acpigen.c:1165
void acpigen_write_STA(uint8_t status)
Definition: acpigen.c:783
void acpigen_write_resourcetemplate_header(void)
Definition: acpigen.c:1147
void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Definition: acpigen.c:1071
void acpigen_emit_eisaid(const char *eisaid)
Definition: acpigen.c:1242
void acpigen_write_device(const char *name)
Definition: acpigen.c:769
void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Definition: acpigen.c:1123
void acpigen_write_name(const char *name)
Definition: acpigen.c:320
void acpigen_write_name_string(const char *name, const char *string)
Definition: acpigen.c:176
pte_t value
Definition: mmu.c:91
static void write8(void *addr, uint8_t val)
Definition: mmio.h:30
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
static uint8_t read8(const void *addr)
Definition: mmio.h:12
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define MIN(a, b)
Definition: helpers.h:37
#define printk(level,...)
Definition: stdlib.h:16
int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, uint8_t *recvbuf, size_t *rbuf_len)
Definition: tis.c:81
const char * device_name
Definition: tis.c:17
uint16_t vid
Definition: tis.c:15
int tis_close(void)
Definition: tis.c:51
uint16_t did
Definition: tis.c:16
int tis_open(void)
Definition: tis.c:33
int tis_init(void)
Definition: tis.c:65
void report_resource_stored(struct device *dev, const struct resource *resource, const char *comment)
Print the resource that was just stored.
Definition: device_util.c:508
const char * dev_path(const struct device *dev)
Definition: device_util.c:149
@ CONFIG
Definition: dsi_common.h:201
static size_t offset
Definition: flashconsole.c:16
port
Definition: i915.h:29
@ TPM2
Definition: acpi.h:76
#define ACPI_STATUS_DEVICE_ALL_ON
Definition: acpigen.h:20
#define ACPI_STATUS_DEVICE_ALL_OFF
Definition: acpigen.h:19
#define CHIP_NAME(X)
Definition: device.h:32
#define mmio_resource(dev, idx, basek, sizek)
Definition: device.h:334
static int stopwatch_expired(struct stopwatch *sw)
Definition: timer.h:152
static void stopwatch_init_usecs_expire(struct stopwatch *sw, long us)
Definition: timer.h:127
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
enum board_config config
Definition: memory.c:448
#define TIS_REG(LOCALITY, REG)
Definition: tis.c:45
static void lpc_tpm_set_resources(struct device *dev)
Definition: tis.c:755
static int tis_wait_ready(int locality)
Definition: tis.c:265
#define TIS_REG_DATA_FIFO
Definition: tis.c:56
#define TPM_DEBUG_IO_READ(reg_, val_)
Definition: tis.c:36
static u8 tpm_read_access(int locality)
Definition: tis.c:189
static void tis_drop_access(int locality)
Definition: tis.c:340
static void tpm_write_int_vector(int vector, int locality)
Definition: tis.c:209
#define TIS_STS_TPM_GO
Definition: tis.c:63
#define printf(x...)
Definition: tis.c:40
static int tis_setup_interrupt(int vector, int polarity)
Definition: tis.c:727
static void lpc_tpm_read_resources(struct device *dev)
Definition: tis.c:749
static u32 tis_readresponse(u8 *buffer, size_t *len)
Definition: tis.c:531
static const struct device_name stmicro_devices[]
Definition: tis.c:128
#define TIS_REG_INT_ENABLE
Definition: tis.c:50
static int tis_wait_sts(int locality, u8 mask, u8 expected)
Definition: tis.c:251
#define TIS_REG_INT_VECTOR
Definition: tis.c:51
static int tis_wait_access(int locality, u8 mask, u8 expected)
Definition: tis.c:305
static struct device_operations lpc_tpm_ops
Definition: tis.c:863
#define TIS_REG_ACCESS
Definition: tis.c:49
#define TIS_STS_EXPECT
Definition: tis.c:65
#define TIS_STS_VALID
Definition: tis.c:61
static int tis_wait_dropped_access(int locality)
Definition: tis.c:319
#define TPM_DRIVER_ERR
Definition: tis.c:85
#define TIS_REG_DID_VID
Definition: tis.c:57
static const struct device_name nuvoton_devices[]
Definition: tis.c:123
static void enable_dev(struct device *dev)
Definition: tis.c:876
#define TIS_ACCESS_REQUEST_USE
Definition: tis.c:73
static u16 tpm_read_burst_count(int locality)
Definition: tis.c:180
static int tis_expect_data(int locality)
Definition: tis.c:288
static int tis_command_ready(u8 locality)
Definition: tis.c:359
static int tis_has_access(int locality)
Definition: tis.c:330
#define TIS_STS_COMMAND_READY
Definition: tis.c:62
#define TPM_TIMEOUT_ERR
Definition: tis.c:82
static struct pnp_info pnp_dev_info[]
Definition: tis.c:872
static void tpm_write_status(u8 sts, int locality)
Definition: tis.c:161
#define MAX_DELAY_US
Definition: tis.c:88
static const struct vendor_name vendor_names[]
Definition: tis.c:140
static const struct device_name infineon_devices[]
Definition: tis.c:111
static u8 tpm_read_int_vector(int locality)
Definition: tis.c:215
#define TIS_STS_DATA_AVAILABLE
Definition: tis.c:64
#define TIS_REG_STS
Definition: tis.c:54
static u8 tpm_read_data(int locality)
Definition: tis.c:167
static void tis_request_access(int locality)
Definition: tis.c:335
static u32 tis_senddata(const u8 *const data, u32 len)
Definition: tis.c:442
static int tis_has_valid_data(int locality)
Definition: tis.c:282
static int tis_wait_valid_data(int locality)
Definition: tis.c:276
struct chip_operations drivers_pc80_tpm_ops
Definition: tis.c:883
static u8 tpm_read_status(int locality)
Definition: tis.c:154
static void tpm_write_access(u8 data, int locality)
Definition: tis.c:196
#define TPM_DEBUG_IO_WRITE(reg_, val_)
Definition: tis.c:38
static int tis_wait_valid(int locality)
Definition: tis.c:271
static int tis_wait_received_access(int locality)
Definition: tis.c:324
static void tpm_write_int_polarity(int polarity, int locality)
Definition: tis.c:222
static u32 tpm_read_int_polarity(int locality)
Definition: tis.c:230
static const struct device_name atmel_devices[]
Definition: tis.c:106
#define TIS_REG_BURST_COUNT
Definition: tis.c:55
static void tpm_write_data(u8 data, int locality)
Definition: tis.c:174
#define TIS_ACCESS_ACTIVE_LOCALITY
Definition: tis.c:69
static u32 tis_probe(void)
Definition: tis.c:385
static u32 tpm_read_did_vid(int locality)
Definition: tis.c:202
static const struct device_name swtpm_devices[]
Definition: tis.c:133
static u32 vendor_dev_id
Definition: tis.c:152
#define PNP_IRQ0
Definition: pnp.h:47
void pnp_enable_devices(struct device *base_dev, struct device_operations *ops, unsigned int functions, struct pnp_info *info)
Definition: pnp_device.c:371
void tpm_ppi_acpi_fill_ssdt(const struct device *dev)
Definition: ppi.c:493
u8 buffer[C2P_BUFFER_MAXSIZE]
Definition: psp_smm.c:18
#define IORESOURCE_IRQ
Definition: resource.h:11
#define IORESOURCE_STORED
Definition: resource.h:32
#define IORESOURCE_ASSIGNED
Definition: resource.h:34
static const int mask[4]
Definition: gpio.c:308
#define DEVTREE_CONST
Definition: stddef.h:30
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
enum acpi_irq_polarity polarity
Definition: acpi_device.h:91
enum acpi_irq_mode mode
Definition: acpi_device.h:90
const char * name
Definition: device.h:29
u16 dev_id
Definition: tis.c:96
const char *const dev_name
Definition: tis.c:97
void(* read_resources)(struct device *dev)
Definition: device.h:39
struct pnp_path pnp
Definition: path.h:117
Definition: device.h:107
struct chip_operations * chip_ops
Definition: device.h:144
struct device_path path
Definition: device.h:115
DEVTREE_CONST struct resource * resource_list
Definition: device.h:134
DEVTREE_CONST void * chip_info
Definition: device.h:164
Definition: pnp.h:37
unsigned int flags
Definition: pnp.h:41
unsigned int port
Definition: path.h:58
DEVTREE_CONST struct resource * next
Definition: resource.h:48
u16 vendor_id
Definition: tis.c:101
const char * vendor_name
Definition: tis.c:102
const struct device_name * dev_names
Definition: tis.c:103
void udelay(uint32_t us)
Definition: udelay.c:15
#define count