Skip to content

Commit 01a2bb2

Browse files
committed
Add hardware SPI support for Raspberry Pi 4
- Change default TPM_SPI_CS to 1 (CE1/GPIO7) to match official Raspberry Pi tpm-slb9670 overlay used by LetsTrust and Infineon HATs - Use _spi_get_bus_and_cs() with explicit speed/mode parameters for proper SPI device initialization - Add required includes for asm/io.h and dm/device-internal.h - Improve debug message prefixes for clarity Tested on Raspberry Pi 4 with Infineon SLB9672 TPM HAT.
1 parent 1955938 commit 01a2bb2

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

hal/tpm_io_uboot.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2020
*/
2121

22-
/* This example shows IO interfaces for U-boot using raw SPI */
22+
/* This example shows IO interfaces for U-boot using raw SPI
23+
* For Raspberry Pi 4 with Infineon SLB9672 TPM HAT
24+
* Reference: https://github.com/wolfSSL/wolfTPM/pull/451
25+
*/
2326

2427
#include <wolftpm/tpm2.h>
2528
#include <wolftpm/tpm2_tis.h>
@@ -44,16 +47,19 @@
4447
#if defined(__UBOOT__)
4548
#include <config.h>
4649
#include <spi.h>
50+
#include <asm/io.h>
4751
#include <dm/device.h>
52+
#include <dm/device-internal.h>
4853
#include <dm/uclass.h>
4954

5055
/* SPI bus and chip select configuration for TPM
51-
* These can be overridden in user_settings.h or board config */
56+
* These can be overridden in user_settings.h or board config
57+
* Official Raspberry Pi tpm-slb9670 overlay uses CE1 (GPIO7) */
5258
#ifndef TPM_SPI_BUS
5359
#define TPM_SPI_BUS 0
5460
#endif
5561
#ifndef TPM_SPI_CS
56-
#define TPM_SPI_CS 0
62+
#define TPM_SPI_CS 1 /* CE1 (GPIO7) - matches Linux tpm-slb9670 overlay */
5763
#endif
5864
#ifndef TPM_SPI_MAX_HZ
5965
#define TPM_SPI_MAX_HZ 1000000 /* 1 MHz - safe default */
@@ -74,24 +80,30 @@
7480
int ret;
7581

7682
if (g_spi_initialized) {
77-
return 0; /* Already initialized */
83+
return 0;
7884
}
7985

80-
/* Get SPI bus and slave device */
81-
ret = spi_get_bus_and_cs(TPM_SPI_BUS, TPM_SPI_CS,
82-
&g_spi_bus, &g_spi_slave);
86+
#ifdef DEBUG_WOLFTPM
87+
printf("wolfTPM: Initializing SPI bus=%d, cs=%d, hz=%d\n",
88+
TPM_SPI_BUS, TPM_SPI_CS, TPM_SPI_MAX_HZ);
89+
#endif
90+
91+
/* Get or create SPI bus and slave device */
92+
ret = _spi_get_bus_and_cs(TPM_SPI_BUS, TPM_SPI_CS,
93+
TPM_SPI_MAX_HZ, TPM_SPI_MODE,
94+
"spi_generic_drv", "wolftpm_spi",
95+
&g_spi_bus, &g_spi_slave);
8396
if (ret != 0) {
8497
#ifdef DEBUG_WOLFTPM
85-
printf("Failed to get SPI bus %d cs %d: %d\n",
86-
TPM_SPI_BUS, TPM_SPI_CS, ret);
98+
printf("wolfTPM: SPI init failed: %d\n", ret);
8799
#endif
88100
return ret;
89101
}
90102

91103
g_spi_initialized = 1;
92104

93105
#ifdef DEBUG_WOLFTPM
94-
printf("TPM SPI initialized: bus %d, cs %d\n", TPM_SPI_BUS, TPM_SPI_CS);
106+
printf("wolfTPM: SPI initialized successfully\n");
95107
#endif
96108

97109
return 0;
@@ -123,7 +135,7 @@
123135
ret = spi_claim_bus(g_spi_slave);
124136
if (ret != 0) {
125137
#ifdef DEBUG_WOLFTPM
126-
printf("Failed to claim SPI bus: %d\n", ret);
138+
printf("wolfTPM: Failed to claim SPI bus: %d\n", ret);
127139
#endif
128140
return TPM_RC_FAILURE;
129141
}
@@ -134,7 +146,7 @@
134146
txBuf, rxBuf, SPI_XFER_BEGIN);
135147
if (ret != 0) {
136148
#ifdef DEBUG_WOLFTPM
137-
printf("SPI header xfer failed: %d\n", ret);
149+
printf("wolfTPM: SPI header xfer failed: %d\n", ret);
138150
#endif
139151
goto cleanup;
140152
}
@@ -154,7 +166,7 @@
154166

155167
if (timeout <= 0 || ret != 0) {
156168
#ifdef DEBUG_WOLFTPM
157-
printf("SPI wait state timeout\n");
169+
printf("wolfTPM: SPI wait state timeout\n");
158170
#endif
159171
/* Deassert CS */
160172
spi_xfer(g_spi_slave, 0, NULL, NULL, SPI_XFER_END);
@@ -175,17 +187,12 @@
175187
}
176188

177189
#else
178-
/* No wait state handling - send entire message at once
179-
* This works for Infineon TPMs (SLB9670/SLB9672) which guarantee
180-
* no wait states */
190+
/* No wait state handling - send entire message at once */
181191
ret = spi_xfer(g_spi_slave, xferSz * 8, txBuf, rxBuf,
182192
SPI_XFER_BEGIN | SPI_XFER_END);
183193
#endif /* WOLFTPM_CHECK_WAIT_STATE */
184194

185195
if (ret != 0) {
186-
#ifdef DEBUG_WOLFTPM
187-
printf("SPI xfer failed: %d\n", ret);
188-
#endif
189196
ret = TPM_RC_FAILURE;
190197
} else {
191198
ret = TPM_RC_SUCCESS;

0 commit comments

Comments
 (0)