|
19 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20 | 20 | */ |
21 | 21 |
|
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 | + */ |
23 | 26 |
|
24 | 27 | #include <wolftpm/tpm2.h> |
25 | 28 | #include <wolftpm/tpm2_tis.h> |
|
44 | 47 | #if defined(__UBOOT__) |
45 | 48 | #include <config.h> |
46 | 49 | #include <spi.h> |
| 50 | + #include <asm/io.h> |
47 | 51 | #include <dm/device.h> |
| 52 | + #include <dm/device-internal.h> |
48 | 53 | #include <dm/uclass.h> |
49 | 54 |
|
50 | 55 | /* 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) */ |
52 | 58 | #ifndef TPM_SPI_BUS |
53 | 59 | #define TPM_SPI_BUS 0 |
54 | 60 | #endif |
55 | 61 | #ifndef TPM_SPI_CS |
56 | | - #define TPM_SPI_CS 0 |
| 62 | + #define TPM_SPI_CS 1 /* CE1 (GPIO7) - matches Linux tpm-slb9670 overlay */ |
57 | 63 | #endif |
58 | 64 | #ifndef TPM_SPI_MAX_HZ |
59 | 65 | #define TPM_SPI_MAX_HZ 1000000 /* 1 MHz - safe default */ |
|
74 | 80 | int ret; |
75 | 81 |
|
76 | 82 | if (g_spi_initialized) { |
77 | | - return 0; /* Already initialized */ |
| 83 | + return 0; |
78 | 84 | } |
79 | 85 |
|
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); |
83 | 96 | if (ret != 0) { |
84 | 97 | #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); |
87 | 99 | #endif |
88 | 100 | return ret; |
89 | 101 | } |
90 | 102 |
|
91 | 103 | g_spi_initialized = 1; |
92 | 104 |
|
93 | 105 | #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"); |
95 | 107 | #endif |
96 | 108 |
|
97 | 109 | return 0; |
|
123 | 135 | ret = spi_claim_bus(g_spi_slave); |
124 | 136 | if (ret != 0) { |
125 | 137 | #ifdef DEBUG_WOLFTPM |
126 | | - printf("Failed to claim SPI bus: %d\n", ret); |
| 138 | + printf("wolfTPM: Failed to claim SPI bus: %d\n", ret); |
127 | 139 | #endif |
128 | 140 | return TPM_RC_FAILURE; |
129 | 141 | } |
|
134 | 146 | txBuf, rxBuf, SPI_XFER_BEGIN); |
135 | 147 | if (ret != 0) { |
136 | 148 | #ifdef DEBUG_WOLFTPM |
137 | | - printf("SPI header xfer failed: %d\n", ret); |
| 149 | + printf("wolfTPM: SPI header xfer failed: %d\n", ret); |
138 | 150 | #endif |
139 | 151 | goto cleanup; |
140 | 152 | } |
|
154 | 166 |
|
155 | 167 | if (timeout <= 0 || ret != 0) { |
156 | 168 | #ifdef DEBUG_WOLFTPM |
157 | | - printf("SPI wait state timeout\n"); |
| 169 | + printf("wolfTPM: SPI wait state timeout\n"); |
158 | 170 | #endif |
159 | 171 | /* Deassert CS */ |
160 | 172 | spi_xfer(g_spi_slave, 0, NULL, NULL, SPI_XFER_END); |
|
175 | 187 | } |
176 | 188 |
|
177 | 189 | #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 */ |
181 | 191 | ret = spi_xfer(g_spi_slave, xferSz * 8, txBuf, rxBuf, |
182 | 192 | SPI_XFER_BEGIN | SPI_XFER_END); |
183 | 193 | #endif /* WOLFTPM_CHECK_WAIT_STATE */ |
184 | 194 |
|
185 | 195 | if (ret != 0) { |
186 | | - #ifdef DEBUG_WOLFTPM |
187 | | - printf("SPI xfer failed: %d\n", ret); |
188 | | - #endif |
189 | 196 | ret = TPM_RC_FAILURE; |
190 | 197 | } else { |
191 | 198 | ret = TPM_RC_SUCCESS; |
|
0 commit comments