Skip to content

Commit fb7ced1

Browse files
committed
soc: Make BL60x independant from SDK
Reorganize and update soc folder files for SDK-independance Signed-off-by: Camille BAUD <mail@massdriver.space>
1 parent 2c43a00 commit fb7ced1

File tree

15 files changed

+204
-392
lines changed

15 files changed

+204
-392
lines changed

soc/bflb/Kconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
config SOC_FAMILY_BFLB
6-
select HAS_BFLB_HAL
7-
85
if SOC_FAMILY_BFLB
96

107
rsource "*/Kconfig"

soc/bflb/bl60x/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Copyright (c) 2021-2025 ATL Electronics
2+
# Copyright (c) 2024-2025 MASSDRIVER EI (massdriver.space)
23
#
34
# SPDX-License-Identifier: Apache-2.0
45

56
zephyr_include_directories(.)
67
zephyr_sources(soc.c)
78

8-
zephyr_linker_sources_ifdef(CONFIG_SOC_SERIES_BL60X RODATA rodata.ld)
9+
set(SOC_LINKER_SCRIPT ${ZEPHYR_BASE}/include/zephyr/arch/riscv/common/linker.ld CACHE INTERNAL "")
910

10-
set(SOC_LINKER_SCRIPT
11-
${ZEPHYR_BASE}/include/zephyr/arch/riscv/common/linker.ld
12-
CACHE INTERNAL ""
13-
)
11+
zephyr_code_relocate_ifdef(CONFIG_UART_BFLB LIBRARY drivers__serial LOCATION ITCM NOKEEP)
12+
zephyr_code_relocate_ifdef(CONFIG_RISCV_MACHINE_TIMER LIBRARY drivers__timer LOCATION ITCM NOKEEP)
13+
zephyr_code_relocate_ifdef(CONFIG_PINCTRL_BFLB LIBRARY drivers__pinctrl LOCATION ITCM NOKEEP)

soc/bflb/bl60x/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
# Copyright (c) 2021-2025 ATL Electronics
2+
# Copyright (c) 2024-2025 MASSDRIVER EI (massdriver.space)
23
#
34
# SPDX-License-Identifier: Apache-2.0
45

56
config SOC_SERIES_BL60X
67
select ATOMIC_OPERATIONS_C
78
select CLOCK_CONTROL
9+
select CODE_DATA_RELOCATION
810
select CPU_HAS_FPU
11+
select FLOAT_HARD
12+
select FPU
13+
select GEN_IRQ_VECTOR_TABLE
914
select INCLUDE_RESET_VECTOR
1015
select RISCV
16+
select RISCV_HAS_CLIC
1117
select RISCV_MACHINE_TIMER
18+
select RISCV_SOC_HAS_CUSTOM_IRQ_HANDLING
19+
select RISCV_PRIVILEGED
1220
select RISCV_ISA_RV32I
1321
select RISCV_ISA_EXT_M
1422
select RISCV_ISA_EXT_A
23+
select RISCV_ISA_EXT_F
1524
select RISCV_ISA_EXT_C
1625
select RISCV_ISA_EXT_ZICSR
1726
select RISCV_ISA_EXT_ZIFENCEI
27+
select RISCV_VECTORED_MODE
1828
select SOC_EARLY_INIT_HOOK
1929
select SYSCON
2030
select XIP

soc/bflb/bl60x/Kconfig.defconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44

55
if SOC_SERIES_BL60X
66

7+
config SYS_CLOCK_TICKS_PER_SEC
8+
default 1000
9+
710
config NUM_IRQS
811
default 80
912

13+
config ARCH_SW_ISR_TABLE_ALIGN
14+
default 64
15+
16+
config RISCV_MCAUSE_EXCEPTION_MASK
17+
default 0x3FF
18+
1019
endif # SOC_SERIES_BL60X

soc/bflb/bl60x/rodata.ld

Lines changed: 0 additions & 17 deletions
This file was deleted.

soc/bflb/bl60x/soc.c

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2021-2025 ATL Electronics
3+
* Copyright (c) 2024-2025 MASSDRIVER EI (massdriver.space)
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -13,37 +14,31 @@
1314
#include <zephyr/init.h>
1415
#include <zephyr/irq.h>
1516

16-
#include <bflb_hbn.h>
17-
#include <bflb_glb.h>
1817
#include <clic.h>
18+
#include <bflb_soc.h>
19+
#include <glb_reg.h>
20+
#include <hbn_reg.h>
21+
#include <pds_reg.h>
1922

20-
#define ROOT_FCLK_DIV (0)
21-
#define ROOT_BCLK_DIV (1)
22-
#define ROOT_UART_CLOCK_DIV (0)
23-
23+
/* Set Embedded Flash Pullup */
2424
static void system_bor_init(void)
2525
{
26-
HBN_BOR_CFG_Type borCfg = { 1 /* pu_bor */, 0 /* irq_bor_en */,
27-
1 /* bor_vth */, 1 /* bor_sel */ };
28-
HBN_Set_BOR_Cfg(&borCfg);
29-
}
26+
uint32_t tmp = 0;
3027

31-
static uint32_t mtimer_get_clk_src_div(void)
32-
{
33-
return ((SystemCoreClockGet() / (GLB_Get_BCLK_Div() + 1))
34-
/ 1000 / 1000 - 1);
35-
}
28+
tmp = sys_read32(HBN_BASE + HBN_BOR_CFG_OFFSET);
29+
/* borThreshold = 1 */
30+
tmp = (tmp & HBN_BOR_VTH_UMSK) | ((uint32_t)(1) << HBN_BOR_VTH_POS);
31+
/* enablePorInBor true*/
32+
tmp = (tmp & HBN_BOR_SEL_UMSK) | ((uint32_t)(1) << HBN_BOR_SEL_POS);
33+
/* enableBor true*/
34+
tmp = (tmp & HBN_PU_BOR_UMSK) | ((uint32_t)(1) << HBN_PU_BOR_POS);
35+
sys_write32(tmp, HBN_BASE + HBN_BOR_CFG_OFFSET);
3636

37-
static void system_clock_init(void)
38-
{
39-
GLB_Set_System_CLK(GLB_PLL_XTAL_40M, GLB_SYS_CLK_PLL160M);
40-
GLB_Set_System_CLK_Div(ROOT_FCLK_DIV, ROOT_BCLK_DIV);
41-
GLB_Set_MTimer_CLK(1, GLB_MTIMER_CLK_BCLK, mtimer_get_clk_src_div());
42-
}
4337

44-
static void peripheral_clock_init(void)
45-
{
46-
GLB_Set_UART_CLK(1, HBN_UART_CLK_160M, ROOT_UART_CLOCK_DIV);
38+
/* enableBorInt false */
39+
tmp = sys_read32(HBN_BASE + HBN_IRQ_MODE_OFFSET);
40+
tmp = tmp & HBN_IRQ_BOR_EN_UMSK;
41+
sys_write32(tmp, HBN_BASE + HBN_IRQ_MODE_OFFSET);
4742
}
4843

4944
void soc_early_init_hook(void)
@@ -55,37 +50,37 @@ void soc_early_init_hook(void)
5550

5651
key = irq_lock();
5752

58-
__disable_irq();
5953

6054
/* disable hardware_pullup_pull_down (reg_en_hw_pu_pd = 0) */
61-
tmp = BL_RD_REG(HBN_BASE, HBN_IRQ_MODE);
62-
tmp = BL_CLR_REG_BIT(tmp, HBN_REG_EN_HW_PU_PD);
63-
BL_WR_REG(HBN_BASE, HBN_IRQ_MODE, tmp);
55+
tmp = sys_read32(HBN_BASE + HBN_IRQ_MODE_OFFSET);
56+
/* "BL_CLR_REG_BIT" */
57+
tmp = tmp & HBN_REG_EN_HW_PU_PD_UMSK;
58+
sys_write32(tmp, HBN_BASE + HBN_IRQ_MODE_OFFSET);
6459

65-
/* GLB_Set_EM_Sel(GLB_EM_0KB); */
66-
tmp = BL_RD_REG(GLB_BASE, GLB_SEAM_MISC);
67-
tmp = BL_SET_REG_BITS_VAL(tmp, GLB_EM_SEL, GLB_EM_0KB);
68-
BL_WR_REG(GLB_BASE, GLB_SEAM_MISC, tmp);
60+
/* 'seam' 0kb, undocumented */
61+
tmp = sys_read32(GLB_BASE + GLB_SEAM_MISC_OFFSET);
62+
tmp = (tmp & GLB_EM_SEL_UMSK) | ((uint32_t)(0) << GLB_EM_SEL_POS);
63+
sys_write32(tmp, GLB_BASE + GLB_SEAM_MISC_OFFSET);
6964

7065
/* Fix 26M xtal clkpll_sdmin */
71-
tmp = BL_RD_REG(PDS_BASE, PDS_CLKPLL_SDM);
66+
tmp = sys_read32(PDS_BASE + PDS_CLKPLL_SDM_OFFSET);
7267

73-
if (BL_GET_REG_BITS_VAL(tmp, PDS_CLKPLL_SDMIN) == 0x49D39D) {
74-
tmp = BL_SET_REG_BITS_VAL(tmp, PDS_CLKPLL_SDMIN, 0x49D89E);
75-
BL_WR_REG(PDS_BASE, PDS_CLKPLL_SDM, tmp);
68+
if ((tmp & PDS_CLKPLL_SDMIN_MSK) == 0x49D39D) {
69+
tmp = (tmp & PDS_CLKPLL_SDMIN_UMSK) | (uint32_t)(0x49D89E);
70+
sys_write32(tmp, PDS_BASE + PDS_CLKPLL_SDM_OFFSET);
7671
}
7772

78-
/* Restore default setting*/
79-
80-
/* GLB_UART_Sig_Swap_Set(UART_SIG_SWAP_NONE); */
81-
tmp = BL_RD_REG(GLB_BASE, GLB_PARM);
82-
tmp = BL_SET_REG_BITS_VAL(tmp, GLB_UART_SWAP_SET, UART_SIG_SWAP_NONE);
83-
BL_WR_REG(GLB_BASE, GLB_PARM, tmp);
84-
73+
tmp = sys_read32(GLB_BASE + GLB_PARM_OFFSET);
74+
/* GLB_UART_Sig_Swap_Set(UART_SIG_SWAP_NONE);
75+
* no swap = 0
76+
* see bl602_glb.h for other possible values
77+
*/
78+
tmp = (tmp & GLB_UART_SWAP_SET_UMSK) | ((uint32_t)(0) <<
79+
GLB_UART_SWAP_SET_POS);
8580
/* GLB_JTAG_Sig_Swap_Set(JTAG_SIG_SWAP_NONE); */
86-
tmp = BL_RD_REG(GLB_BASE, GLB_PARM);
87-
tmp = BL_SET_REG_BITS_VAL(tmp, GLB_JTAG_SWAP_SET, JTAG_SIG_SWAP_NONE);
88-
BL_WR_REG(GLB_BASE, GLB_PARM, tmp);
81+
tmp = (tmp & GLB_JTAG_SWAP_SET_UMSK) | ((uint32_t)(0) <<
82+
GLB_JTAG_SWAP_SET_POS);
83+
sys_write32(tmp, GLB_BASE + GLB_PARM_OFFSET);
8984

9085
/* CLear all interrupt */
9186
p = (uint32_t *)(CLIC_HART0_ADDR + CLIC_INTIE);
@@ -102,21 +97,6 @@ void soc_early_init_hook(void)
10297

10398
/* init bor for all platform */
10499
system_bor_init();
105-
/* global IRQ enable */
106-
__enable_irq();
107-
108-
system_clock_init();
109-
peripheral_clock_init();
110100

111101
irq_unlock(key);
112102
}
113-
114-
/* identify flash config automatically */
115-
extern BL_Err_Type flash_init(void);
116-
117-
void System_Post_Init(void)
118-
{
119-
PDS_Trim_RC32M();
120-
HBN_Trim_RC32K();
121-
flash_init();
122-
}

soc/bflb/bl60x/soc.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,12 @@
1515
#define _SOC__H_
1616

1717
#include <zephyr/sys/util.h>
18-
#include <../common/soc_common.h>
1918

2019
#ifndef _ASMLANGUAGE
2120

2221
/* Add include for DTS generated information */
2322
#include <zephyr/devicetree.h>
2423

25-
#if defined(CONFIG_SOC_SERIES_BL60X)
26-
#include <bl602.h>
27-
#else
28-
#error Library does not support the specified device.
29-
#endif
30-
31-
/* clang-format off */
32-
33-
/* RISC-V Machine Timer configuration */
34-
#define RISCV_MTIME_BASE 0x0200BFF8
35-
#define RISCV_MTIMECMP_BASE 0x02004000
36-
37-
/* lib-c hooks required RAM defined variables */
38-
#define RISCV_RAM_BASE DT_SRAM_BASE_ADDRESS
39-
#define RISCV_RAM_SIZE KB(DT_SRAM_SIZE)
40-
41-
#define SOC_BOUFFALOLAB_BL_PLL160_FREQ_HZ (160000000)
42-
#define SOC_BOUFFALOLAB_BL_HCLK_FREQ_HZ \
43-
DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency)
44-
45-
/* clang-format on */
46-
4724
#endif /* !_ASMLANGUAGE */
4825

4926
#endif /* _SOC__H_ */

soc/bflb/common/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
zephyr_include_directories(.)
66

7+
if(CONFIG_SOC_SERIES_BL60X)
8+
zephyr_include_directories(e24)
79
zephyr_sources(
8-
soc_irq.S
9-
soc_common_irq.c
10-
vector.S
11-
)
10+
e24/soc_irq_privileged.c
11+
e24/intc_clic.S)
12+
endif()

soc/bflb/common/clic.h renamed to soc/bflb/common/e24/clic.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@
2323
#define CLIC_INTCFG 0x800
2424
#define CLIC_CFG 0xc00
2525

26+
/* CLIC relative CSR number */
27+
#define CSR_MTVT (0x307)
28+
#define CSR_MNXTI (0x345)
29+
#define CSR_MINTTHRESH (0x347)
30+
#define CSR_MISELECT (0x350)
31+
#define CSR_MIREG (0x351)
32+
#define CSR_MIREG2 (0x352)
33+
2634
#endif /* _SIFIVE_CLIC_H */

soc/bflb/common/e24/intc_clic.S

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2024 Baumer Electric AG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @brief Assembler-hooks specific to RISC-V Core Local Interrupt Controller
9+
*/
10+
11+
#include <zephyr/arch/cpu.h>
12+
#include "clic.h"
13+
14+
15+
/* register-wide load/store based on lw/sw (XLEN = 32) */
16+
17+
.macro lr, rd, mem
18+
lw \rd, \mem
19+
.endm
20+
21+
.macro sr, rs, mem
22+
sw \rs, \mem
23+
.endm
24+
25+
26+
GTEXT(__soc_handle_irq)
27+
/*
28+
* In an CLIC, pending interrupts don't have to be cleared by hand.
29+
* In vectored mode, interrupts are cleared automatically.
30+
* In non-vectored mode, interrupts are cleared when writing the mnxti register (done in
31+
* __soc_handle_all_irqs).
32+
* Thus this function can directly return.
33+
*/
34+
SECTION_FUNC(exception.other, __soc_handle_irq)
35+
ret
36+
37+
GTEXT(__soc_handle_all_irqs)
38+
39+
#ifdef CONFIG_TRACING
40+
/* imports */
41+
GTEXT(sys_trace_isr_enter)
42+
GTEXT(sys_trace_isr_exit)
43+
#endif
44+
45+
/*
46+
* This function services and clears all pending interrupts for an CLIC in non-vectored mode.
47+
*/
48+
SECTION_FUNC(exception.other, __soc_handle_all_irqs)
49+
addi sp, sp, -16
50+
sr ra, 0(sp)
51+
52+
/* Read and clear mnxti to get highest current interrupt and enable interrupts. Will return
53+
* original interrupt if no others appear. */
54+
csrrci a0, CSR_MNXTI, MSTATUS_IEN
55+
beqz a0, irq_done /* Check if original interrupt vanished. */
56+
57+
irq_loop:
58+
59+
#ifdef CONFIG_TRACING_ISR
60+
call sys_trace_isr_enter
61+
#endif
62+
63+
/* Call corresponding registered function in _sw_isr_table. a0 is offset in pointer with
64+
* the mtvt, sw irq table is 2-pointer wide -> shift by one. */
65+
csrr t0, CSR_MTVT
66+
sub a0, a0, t0
67+
la t0, _sw_isr_table
68+
slli a0, a0, (1)
69+
add t0, t0, a0
70+
71+
/* Load argument in a0 register */
72+
lr a0, 0(t0)
73+
74+
/* Load ISR function address in register t1 */
75+
lr t1, RV_REGSIZE(t0)
76+
77+
/* Call ISR function */
78+
jalr ra, t1, 0
79+
80+
#ifdef CONFIG_TRACING_ISR
81+
call sys_trace_isr_exit
82+
#endif
83+
84+
/* Read and clear mnxti to get highest current interrupt and enable interrupts. */
85+
csrrci a0, CSR_MNXTI, MSTATUS_IEN
86+
bnez a0, irq_loop
87+
88+
irq_done:
89+
lr ra, 0(sp)
90+
addi sp, sp, 16
91+
ret

0 commit comments

Comments
 (0)