Skip to content

Commit d93b1d2

Browse files
committed
Added SPI TPM support for STM32H5. Added non-secure callable TPM API's. Cleanup some of the STM32H5 register mappings.
1 parent cc80965 commit d93b1d2

File tree

16 files changed

+395
-136
lines changed

16 files changed

+395
-136
lines changed

arch.mk

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ ifeq ($(ARCH),ARM)
250250
WOLFBOOT_ORIGIN=0x10000000
251251
ifeq ($(TZEN),1)
252252
LSCRIPT_IN=hal/$(TARGET).ld
253-
CFLAGS+=-DTZEN
254253
else
255254
LSCRIPT_IN=hal/$(TARGET)-ns.ld
256255
endif
@@ -400,10 +399,6 @@ endif
400399
endif
401400
endif
402401

403-
ifeq ($(TZEN),1)
404-
CFLAGS+=-DTZEN
405-
endif
406-
407402

408403
## Renesas RX
409404
ifeq ($(ARCH),RENESAS_RX)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
ARCH?=ARM
2+
TZEN?=1
3+
TARGET?=stm32h5
4+
SIGN?=ECC256
5+
HASH?=SHA256
6+
DEBUG?=0
7+
VTOR?=1
8+
CORTEX_M0?=0
9+
CORTEX_M33?=1
10+
NO_ASM?=0
11+
NO_MPU=1
12+
EXT_FLASH?=0
13+
SPI_FLASH?=0
14+
ALLOW_DOWNGRADE?=0
15+
NVM_FLASH_WRITEONCE?=1
16+
WOLFBOOT_VERSION?=1
17+
V?=0
18+
SPMATH?=1
19+
RAM_CODE?=1
20+
DUALBANK_SWAP?=0
21+
WOLFBOOT_PARTITION_SIZE?=0xA0000
22+
WOLFBOOT_SECTOR_SIZE?=0x2000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C100000
25+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x0C1A0000
26+
FLAGS_HOME=0
27+
DISABLE_BACKUP=0
28+
WOLFCRYPT_TZ=1
29+
WOLFCRYPT_TZ_PKCS11=1
30+
IMAGE_HEADER_SIZE?=1024
31+
ARMORED=1
32+
WOLFTPM=1

hal/spi/spi_drv_stm32.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,23 @@ int qspi_transfer(uint8_t fmode, const uint8_t cmd,
369369
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
370370
uint8_t RAMFUNCTION spi_read(void)
371371
{
372-
volatile uint32_t reg;
373-
do {
374-
reg = SPI1_SR;
375-
} while(!(reg & SPI_SR_RX_NOTEMPTY));
376-
return (uint8_t)SPI1_DR;
372+
while ((SPI1_SR & SPI_SR_RX_NOTEMPTY) == 0);
373+
#ifdef SPI1_RXDR
374+
return SPI1_RXDR;
375+
#else
376+
return SPI1_DR
377+
#endif
377378
}
378379

379380
void RAMFUNCTION spi_write(const char byte)
380381
{
381382
int i;
382-
volatile uint32_t reg;
383-
do {
384-
reg = SPI1_SR;
385-
} while ((reg & SPI_SR_TX_EMPTY) == 0);
383+
while ((SPI1_SR & SPI_SR_TX_EMPTY) == 0);
384+
#ifdef SPI1_TXDR
385+
SPI1_TXDR = byte;
386+
#else
386387
SPI1_DR = byte;
387-
do {
388-
reg = SPI1_SR;
389-
} while ((reg & SPI_SR_TX_EMPTY) == 0);
388+
#endif
390389
}
391390
#endif /* SPI_FLASH || WOLFBOOT_TPM */
392391

@@ -398,7 +397,6 @@ void RAMFUNCTION spi_init(int polarity, int phase)
398397

399398
/* Setup clocks */
400399
#if defined(QSPI_FLASH) || defined(OCTOSPI_FLASH)
401-
402400
#ifdef TARGET_stm32u5
403401
/* Clock configuration for QSPI defaults to SYSCLK
404402
* (RM0456 section 11.8.47)
@@ -413,6 +411,10 @@ void RAMFUNCTION spi_init(int polarity, int phase)
413411

414412
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
415413
APB2_CLOCK_ER |= SPI1_APB2_CLOCK_ER_VAL;
414+
#ifdef TARGET_stm32h5
415+
RCC_CCIPR3 &= ~ (RCC_CCIPR3_SPI1SEL_MASK << RCC_CCIPR3_SPI1SEL_SHIFT);
416+
RCC_CCIPR3 |= (0 << RCC_CCIPR3_SPI1SEL_SHIFT); /* PLL1_Q */
417+
#endif
416418
#endif
417419

418420
/* reset peripheral before setting up GPIO pins */
@@ -486,14 +488,30 @@ void RAMFUNCTION spi_init(int polarity, int phase)
486488
#endif
487489
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
488490
/* Configure SPI1 for master mode */
489-
# ifdef TARGET_stm32l0
490-
SPI1_CR1 = SPI_CR1_MASTER | (polarity << 1) | (phase << 0);
491-
# else
492-
/* baud rate 5 (hclk/6) */
493-
SPI1_CR1 = SPI_CR1_MASTER | (5 << 3) | (polarity << 1) | (phase << 0);
494-
# endif
491+
SPI1_CR1 &= ~SPI_CR1_SPI_EN;
492+
#if defined(TARGET_stm32h5)
493+
/* baud rate 2 (hclk/8), data size 8 bits, FIFO threshold level (8-data) */
494+
SPI1_CFG1 = (
495+
((7 & SPI_CFG1_FTHLV_MASK) << SPI_CFG1_FTHLV_SHIFT) |
496+
((2 & SPI_CFG1_BAUDRATE_MASK) << SPI_CFG1_BAUDRATE_SHIFT) |
497+
((7 & SPI_CFG1_DSIZE_MASK) << SPI_CFG1_DSIZE_SHIFT));
498+
SPI1_CFG2 = SPI_CRF2_MASTER | SPI_CFG2_SSOE |
499+
(polarity << SPI_CFG2_CLOCK_POL_SHIFT) |
500+
(phase << SPI_CFG2_CLOCK_PHASE_SHIFT);
501+
SPI1_CR1 |= SPI_CR1_CSTART; /* use continuous start mode */
502+
#else
503+
#ifndef TARGET_stm32l0 /* use existing/default baud for L0 */
504+
/* Baud rate 5 (hclk/6), data size 8 bits */
505+
SPI1_CR1 |= ((5 & SPI_CR1_BAUDRATE_MASK) << SPI_CR1_BAUDRATE_SHIFT);
506+
#endif
507+
SPI1_CR1 &= ~((1 << SPI_CR1_CLOCK_POL_SHIFT) | (1 << SPI_CR1_CLOCK_PHASE_SHIFT));
508+
SPI1_CR1 |= SPI_CR1_MASTER |
509+
(polarity << SPI_CR1_CLOCK_POL_SHIFT) |
510+
(phase << SPI_CR1_CLOCK_PHASE_SHIFT);
495511
SPI1_CR2 |= SPI_CR2_SSOE;
496-
SPI1_CR1 |= SPI_CR1_SPI_EN;
512+
#endif
513+
514+
SPI1_CR1 |= SPI_CR1_SPI_EN; /* Enable SPI */
497515
#endif /* SPI_FLASH || WOLFBOOOT_TPM */
498516
}
499517
}
@@ -505,8 +523,12 @@ void RAMFUNCTION spi_release(void)
505523
}
506524
if (initialized == 0) {
507525
spi_reset();
508-
#if defined (SPI_FLASH) || defined(WOLFBOOT_TPM)
526+
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
527+
#if defined(TARGET_stm32h5)
528+
SPI1_CFG2 &= ~SPI_CFG2_SSOE;
529+
#else
509530
SPI1_CR2 &= ~SPI_CR2_SSOE;
531+
#endif
510532
SPI1_CR1 = 0;
511533
#endif
512534
stm_pins_release();

hal/spi/spi_drv_stm32.h

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@
4040
#define SPI_MOSI_PIN 5 /* SPI_MOSI PB5 */
4141
#endif /* TARGET_stm32f4 */
4242

43+
44+
#ifdef TARGET_stm32h5
45+
#include "hal/stm32h5.h"
46+
47+
#define RCC_GPIO_CLOCK_ER RCC_AHB2ENR_CLOCK_ER
48+
#define APB2_CLOCK_RST RCC_APB2_CLOCK_RSTR
49+
#define APB2_CLOCK_ER RCC_APB2_CLOCK_ER
50+
51+
/* Nucleo STM32H573ZI SPI_A Port (SPI1) */
52+
#define SPI_CLOCK_PIO_BASE GPIOA_BASE
53+
#define SPI_MISO_PIO_BASE GPIOG_BASE
54+
#define SPI_MOSI_PIO_BASE GPIOB_BASE
55+
#define SPI_CS_TPM_PIO_BASE GPIOD_BASE
56+
57+
#define SPI_PIN_AF 5 /* Alternate function for SPI pins */
58+
#define SPI_CLOCK_PIN 5 /* SPI_SCK: PA5 */
59+
#define SPI_MISO_PIN 9 /* SPI_MISO PG9 */
60+
#define SPI_MOSI_PIN 5 /* SPI_MOSI PB5 */
61+
#define SPI_CS_TPM 14 /* TPM CS connected to PD14 */
62+
#endif /* TARGET_stm32h5 */
63+
64+
4365
#ifdef TARGET_stm32u5
4466

4567
#ifdef TZEN
@@ -405,15 +427,53 @@
405427

406428
#define SPI1_APB2_CLOCK_ER_VAL (1 << 12)
407429

430+
#if defined(TARGET_stm32h5)
431+
/* newer SPI/I2S peripheral */
432+
#define SPI1_CR1 (*(volatile uint32_t *)(SPI1_BASE))
433+
#define SPI1_CR2 (*(volatile uint32_t *)(SPI1_BASE + 0x04))
434+
#define SPI1_CFG1 (*(volatile uint32_t *)(SPI1_BASE + 0x08))
435+
#define SPI1_CFG2 (*(volatile uint32_t *)(SPI1_BASE + 0x0C))
436+
#define SPI1_SR (*(volatile uint32_t *)(SPI1_BASE + 0x14))
437+
#define SPI1_TXDR (*(volatile uint8_t *)(SPI1_BASE + 0x20))
438+
#define SPI1_RXDR (*(volatile uint8_t *)(SPI1_BASE + 0x30))
439+
440+
#define SPI_CR1_SPI_EN (1 << 6)
441+
#define SPI_CR1_CSTART (1 << 9) /* Continous start */
442+
#define SPI_CFG1_DSIZE_MASK (0x1F)
443+
#define SPI_CFG1_DSIZE_SHIFT (0)
444+
445+
#define SPI_CFG1_FTHLV_MASK (0x1F)
446+
#define SPI_CFG1_FTHLV_SHIFT (5)
447+
448+
#define SPI_CFG1_BAUDRATE_MASK (0x07)
449+
#define SPI_CFG1_BAUDRATE_SHIFT (28)
450+
451+
#define SPI_CRF2_MASTER (1 << 22)
452+
#define SPI_CFG2_LSBFIRST (1 << 23)
453+
#define SPI_CFG2_CLOCK_PHASE_SHIFT (24)
454+
#define SPI_CFG2_CLOCK_POL_SHIFT (25)
455+
#define SPI_CFG2_SSM (1 << 26)
456+
#define SPI_CFG2_SSOE (1 << 29)
457+
#define SPI_CFG2_HW_CRC_EN (1 << 29)
458+
#define SPI_CFG2_COMM_MASK (0x3) /* 0=full duplex, 1=simplex tx, 2=simplex rx, 3=half duplex */
459+
#define SPI_CFG2_COMM_SHIFT (17)
460+
461+
#define SPI_SR_RX_NOTEMPTY (1 << 0)
462+
#define SPI_SR_TX_EMPTY (1 << 1)
463+
464+
#else
465+
466+
/* older SPI peripheral */
408467
#define SPI1_CR1 (*(volatile uint32_t *)(SPI1_BASE))
409468
#define SPI1_CR2 (*(volatile uint32_t *)(SPI1_BASE + 0x04))
410469
#define SPI1_SR (*(volatile uint32_t *)(SPI1_BASE + 0x08))
411-
#define SPI1_DR (*(volatile uint32_t *)(SPI1_BASE + 0x0c))
470+
#define SPI1_DR (*(volatile uint8_t *)(SPI1_BASE + 0x0c))
412471

413-
#define SPI_CR1_CLOCK_PHASE (1 << 0)
414-
#define SPI_CR1_CLOCK_POLARITY (1 << 1)
472+
#define SPI_CR1_CLOCK_PHASE_SHIFT (0)
473+
#define SPI_CR1_CLOCK_POL_SHIFT (1)
415474
#define SPI_CR1_MASTER (1 << 2)
416-
#define SPI_CR1_BAUDRATE (0x07 << 3)
475+
#define SPI_CR1_BAUDRATE_MASK (0x07)
476+
#define SPI_CR1_BAUDRATE_SHIFT (3)
417477
#define SPI_CR1_SPI_EN (1 << 6)
418478
#define SPI_CR1_LSBFIRST (1 << 7)
419479
#define SPI_CR1_SSI (1 << 8)
@@ -428,6 +488,7 @@
428488
#define SPI_SR_TX_EMPTY (1 << 1)
429489
#define SPI_SR_BUSY (1 << 7)
430490

491+
#endif
431492

432493
/* GPIO */
433494
#define GPIO_MODE(base) (*(volatile uint32_t *)(base + 0x00)) /* GPIOx_MODER */

hal/stm32h5.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ static void clock_pll_on(void)
279279
uint32_t reg32;
280280
uint32_t plln, pllm, pllq, pllp, pllr, hpre, apb1pre, apb2pre, apb3pre, flash_waitstates;
281281

282-
283282
#if PLL_SRC_HSE
284283
pllm = 4;
285284
plln = 250;
@@ -348,16 +347,16 @@ static void clock_pll_on(void)
348347
#endif
349348
DMB();
350349

351-
RCC_PLL1DIVR = ((plln - 1) << RCC_PLLDIVR_DIVN_SHIFT) | ((pllp - 1) << RCC_PLLDIVR_DIVP_SHIFT) |
352-
((pllq - 1) << RCC_PLLDIVR_DIVQ_SHIFT) | ((pllr - 1) << RCC_PLLDIVR_DIVR_SHIFT);
350+
RCC_PLL1DIVR = ((plln - 1) << RCC_PLLDIVR_DIVN_SHIFT) |
351+
((pllp - 1) << RCC_PLLDIVR_DIVP_SHIFT) |
352+
((pllq - 1) << RCC_PLLDIVR_DIVQ_SHIFT) |
353+
((pllr - 1) << RCC_PLLDIVR_DIVR_SHIFT);
353354
DMB();
354355

355-
356356
/* Disable Fractional PLL */
357357
RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLLFRACEN;
358358
DMB();
359359

360-
361360
/* Configure Fractional PLL factor */
362361
RCC_PLL1FRACR = 0x00000000;
363362
DMB();
@@ -373,8 +372,8 @@ static void clock_pll_on(void)
373372
RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLLVCOSEL;
374373
DMB();
375374

376-
/* Enable PLL1 system clock out (DIV: P) */
377-
RCC_PLL1CFGR |= RCC_PLLCFGR_PLL1PEN;
375+
/* Enable PLL1 system clock out (DIV: P and Q) */
376+
RCC_PLL1CFGR |= RCC_PLLCFGR_PLL1PEN | RCC_PLLCFGR_PLL1QEN;
378377

379378
/* Enable PLL1 */
380379
RCC_CR |= RCC_CR_PLL1ON;
@@ -386,11 +385,13 @@ static void clock_pll_on(void)
386385
apb3pre = RCC_APB_PRESCALER_DIV_NONE;
387386
reg32 = RCC_CFGR2;
388387
reg32 &= ~( (0x0F << RCC_CFGR2_HPRE_SHIFT) |
389-
(0x07 << RCC_CFGR2_PPRE1_SHIFT) |
390-
(0x07 << RCC_CFGR2_PPRE2_SHIFT) |
391-
(0x07 << RCC_CFGR2_PPRE3_SHIFT));
392-
reg32 |= ((hpre) << RCC_CFGR2_HPRE_SHIFT) | ((apb1pre) << RCC_CFGR2_PPRE1_SHIFT) |
393-
((apb2pre) << RCC_CFGR2_PPRE2_SHIFT) | ((apb3pre) << RCC_CFGR2_PPRE3_SHIFT);
388+
(0x07 << RCC_CFGR2_PPRE1_SHIFT) |
389+
(0x07 << RCC_CFGR2_PPRE2_SHIFT) |
390+
(0x07 << RCC_CFGR2_PPRE3_SHIFT));
391+
reg32 |= ( (hpre) << RCC_CFGR2_HPRE_SHIFT) |
392+
((apb1pre) << RCC_CFGR2_PPRE1_SHIFT) |
393+
((apb2pre) << RCC_CFGR2_PPRE2_SHIFT) |
394+
((apb3pre) << RCC_CFGR2_PPRE3_SHIFT);
394395
RCC_CFGR2 = reg32;
395396
DMB();
396397

@@ -595,8 +596,6 @@ void hal_init(void)
595596
#if defined(DUALBANK_SWAP) && defined(__WOLFBOOT)
596597
fork_bootloader();
597598
#endif
598-
599-
600599
}
601600

602601

0 commit comments

Comments
 (0)