Skip to content

Commit f6a884c

Browse files
committed
Fix for BOOT_BENCHMARK=1 and timer in M-Mode. Peer review fixes.
1 parent a052f91 commit f6a884c

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

hal/mpfs250.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ static void mpfs_config_l2_cache(void)
117117

118118
/* Microsecond delay using busy loop.
119119
* MTIME counter is not running in bare-metal M-mode (no HSS), so use a
120-
* calibrated loop. E51 runs at ~40 MHz on reset; with volatile load/store
121-
* overhead each iteration is ~10 cycles → 4 iterations per microsecond.
120+
* calibrated loop. E51 CPU core runs at 80 MHz on reset (2x the 40 MHz APB
121+
* bus clock); with volatile load/store overhead each iteration is ~10 cycles
122+
* → 8 iterations per microsecond.
122123
* noinline prevents the compiler from collapsing the loop at call sites. */
123124
static __attribute__((noinline)) void udelay(uint32_t us)
124125
{
125126
volatile uint32_t i;
126-
for (i = 0; i < us * 4; i++)
127+
for (i = 0; i < us * 8; i++)
127128
;
128129
}
129130

hal/mpfs250.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
/* PolarFire SoC MPFS250T board specific configuration */
2929

30-
/* APB/AHB Clock Frequency
31-
* M-mode (out of reset): 40 MHz
32-
* S-mode (after HSS): 150 MHz
30+
/* APB/AHB Peripheral Bus Clock Frequency (used for UART baud rate divisor)
31+
* M-mode (out of reset, no PLL): 40 MHz
32+
* S-mode (after HSS configures PLL): 150 MHz
3333
*/
3434
#ifndef MSS_APB_AHB_CLK
3535
#ifdef WOLFBOOT_RISCV_MMODE
@@ -39,6 +39,17 @@
3939
#endif
4040
#endif
4141

42+
/* CPU Core Clock Frequency (used for mcycle-based benchmarking)
43+
* The E51 core runs at 2x the APB bus clock on reset.
44+
* After HSS configures the PLL, CPU runs at 600 MHz. */
45+
#ifndef MSS_CPU_CLK
46+
#ifdef WOLFBOOT_RISCV_MMODE
47+
#define MSS_CPU_CLK (MSS_APB_AHB_CLK * 2)
48+
#else
49+
#define MSS_CPU_CLK 600000000
50+
#endif
51+
#endif
52+
4253
/* Hardware Base Address */
4354
#define SYSREG_BASE 0x20002000
4455

@@ -327,6 +338,14 @@ int mpfs_read_serial_number(uint8_t *serial);
327338
/* RTC Clock Frequency (1 MHz after divisor) */
328339
#define RTC_CLOCK_FREQ 1000000UL
329340

341+
/* Timer frequency for hal_get_timer_us().
342+
* In M-mode: CLINT MTIME is not running without HSS, so hal_get_timer()
343+
* reads mcycle (CPU cycle counter) instead. Set frequency to CPU clock.
344+
* In S-mode: MTIME runs at 1 MHz (default RISCV_SMODE_TIMER_FREQ). */
345+
#if defined(WOLFBOOT_RISCV_MMODE) && !defined(RISCV_SMODE_TIMER_FREQ)
346+
#define RISCV_SMODE_TIMER_FREQ MSS_CPU_CLK
347+
#endif
348+
330349

331350
/* ============================================================================
332351
* Hart Local Storage (HLS) - Per-hart communication structure

src/boot_riscv.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,14 @@ unsigned long WEAKFUNCTION handle_trap(unsigned long cause, unsigned long epc,
207207

208208
uint64_t hal_get_timer(void)
209209
{
210-
#if __riscv_xlen == 64
211-
/* For RV64, CSR time contains full 64-bit value */
210+
#ifdef WOLFBOOT_RISCV_MMODE
211+
/* In M-mode, use mcycle (CPU cycle counter) instead of rdtime/CLINT MTIME.
212+
* rdtime CSR causes illegal instruction on E51 in bare-metal M-mode, and
213+
* CLINT MTIME is not running without HSS. mcycle always increments at the
214+
* CPU clock rate (MSS_APB_AHB_CLK). */
215+
return csr_read(mcycle);
216+
#elif __riscv_xlen == 64
217+
/* For RV64 S-mode, CSR time contains full 64-bit value */
212218
return csr_read(time);
213219
#else
214220
/* For RV32, read both timeh and time with wrap-around protection */

test-app/startup_riscv.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,6 @@ void __attribute__((naked,section(".init"))) _reset(void) {
4242
asm volatile("la gp, _global_pointer");
4343
asm volatile("la sp, _end_stack");
4444

45-
/* Direct UART diagnostic: write "!\r\n" to confirm test-app is running.
46-
* MPFS MMUART: THR at offset 0x100, LSR at offset 0x14, THRE = bit 5. */
47-
asm volatile(
48-
"li a0, 0x20000000\n" /* UART0 base */
49-
/* write '!' */
50-
"1: lbu a1, 0x14(a0)\n" /* read LSR */
51-
"andi a1, a1, 0x20\n" /* check THRE (bit 5) */
52-
"beqz a1, 1b\n"
53-
"li a2, 0x21\n" /* '!' */
54-
"sb a2, 0x100(a0)\n" /* write to THR */
55-
/* write '\r' */
56-
"2: lbu a1, 0x14(a0)\n"
57-
"andi a1, a1, 0x20\n"
58-
"beqz a1, 2b\n"
59-
"li a2, 0x0d\n" /* '\r' */
60-
"sb a2, 0x100(a0)\n"
61-
/* write '\n' */
62-
"3: lbu a1, 0x14(a0)\n"
63-
"andi a1, a1, 0x20\n"
64-
"beqz a1, 3b\n"
65-
"li a2, 0x0a\n" /* '\n' */
66-
"sb a2, 0x100(a0)\n"
67-
::: "a0", "a1", "a2"
68-
);
69-
7045
/* Set up M-mode vectored interrupt table.
7146
* wolfBoot M-mode does a direct jr (no enter_smode), so payload runs in M-mode.
7247
* Use mtvec. The +1 sets MODE=1 (vectored). */

0 commit comments

Comments
 (0)