Skip to content

Commit

Permalink
c908x: Add fastmem setting support
Browse files Browse the repository at this point in the history
Xuantie C908X supports fast memory configuration vector and scalar
memory access instructions to access external SRAM.
Configure the starting address and size through CSR_MFASTMBA. For
specific settings, please refer to the datasheet.
Dynamic configuration is achieved by parsing dts through opensbi.
The dts reference configuration is as follows:
```
/ {
	compatible = "xuantie,c908x";
	model = "xuantie,c908x";

	fastmem {
		mfastmba = <0x12 0x34567890>;
	};
};
```

Signed-off-by: hanyu.cp <[email protected]>
  • Loading branch information
cp0613 committed Dec 23, 2024
1 parent 9294bed commit eefc3f6
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/platform/thead-c9xx.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ Here is the simplest boot flow for a fpga prototype:

For more details, refer:
[zero stage boot](https://github.com/c-sky/zero_stage_boot)

DTS Example: (fastmem)
-----------------------------

```
/ {
compatible = "xuantie,c908x";
model = "xuantie,c908x";
fastmem {
mfastmba = <0x12 0x34567890>;
};
};
```
1 change: 1 addition & 0 deletions platform/generic/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ config PLATFORM_STARFIVE_JH7110
config PLATFORM_THEAD
bool "THEAD C9xx support"
select THEAD_C9XX_ERRATA
select THEAD_C9XX_EXTFUNC
select THEAD_C9XX_PMU
default n

Expand Down
3 changes: 3 additions & 0 deletions platform/generic/include/thead/c9xx_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define THEAD_C9XX_CSR_MEXSTATUS 0x7e1
#define THEAD_C9XX_CSR_MNMICAUSE 0x7e2
#define THEAD_C9XX_CSR_MNMIPC 0x7e3
#define THEAD_C9XX_CSR_MFASTMBA 0x7eb
#define THEAD_C9XX_CSR_MFASTMBAH 0x7ec
#define THEAD_C9XX_CSR_MHPMCR 0x7f0
#define THEAD_C9XX_CSR_MHPMSR 0x7f1
#define THEAD_C9XX_CSR_MHPMER 0x7f2
Expand Down Expand Up @@ -95,6 +97,7 @@

/* T-HEAD C9xx U mode CSR. */
#define THEAD_C9XX_CSR_FXCR 0x800
#define THEAD_C9XX_CSR_UTNMODE 0x8da

/* T-HEAD C9xx MMU extentions. */
#define THEAD_C9XX_CSR_SMIR 0x9c0
Expand Down
10 changes: 10 additions & 0 deletions platform/generic/include/thead/c9xx_extfunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#ifndef __RISCV_THEAD_C9XX_EXTFUNC_H____
#define __RISCV_THEAD_C9XX_EXTFUNC_H____

/* Fast Memory Setting */
#define THEAD_QUIRK_EXTFUNC_FASTMEM BIT(0)

void thead_c9xx_fastmem_init(void);

#endif // __RISCV_THEAD_C9XX_EXTFUNC_H____
4 changes: 4 additions & 0 deletions platform/generic/thead/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ config THEAD_C9XX_PMU
config THEAD_C9XX_ERRATA
bool "T-HEAD c9xx errata support"
default n

config THEAD_C9XX_EXTFUNC
bool "THEAD c9xx extfunc support"
default n
1 change: 1 addition & 0 deletions platform/generic/thead/objects.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ platform-objs-$(CONFIG_THEAD_C9XX_PMU) += thead/thead_c9xx_pmu.o

platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_tlb_trap_handler.o
platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_errata_tlb_flush.o
platform-objs-$(CONFIG_THEAD_C9XX_EXTFUNC) += thead/thead_c9xx_extfunc_fastmem.o

carray-platform_override_modules-$(CONFIG_PLATFORM_THEAD) += thead_generic
platform-objs-$(CONFIG_PLATFORM_THEAD) += thead/thead-generic.o
10 changes: 10 additions & 0 deletions platform/generic/thead/thead-generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <platform_override.h>
#include <thead/c9xx_errata.h>
#include <thead/c9xx_extfunc.h>
#include <thead/c9xx_pmu.h>
#include <sbi/sbi_const.h>
#include <sbi/sbi_platform.h>
Expand All @@ -16,6 +17,7 @@
#include <sbi_utils/fdt/fdt_helper.h>

struct thead_generic_quirks {
u64 extfunc;
u64 errata;
};

Expand All @@ -27,6 +29,9 @@ static int thead_generic_early_init(bool cold_boot,
if (quirks->errata & THEAD_QUIRK_ERRATA_TLB_FLUSH)
thead_register_tlb_flush_trap_handler();

if (quirks->extfunc & THEAD_QUIRK_EXTFUNC_FASTMEM)
thead_c9xx_fastmem_init();

return 0;
}

Expand All @@ -45,8 +50,13 @@ static struct thead_generic_quirks thead_th1520_quirks = {
.errata = THEAD_QUIRK_ERRATA_TLB_FLUSH | THEAD_QUIRK_ERRATA_THEAD_PMU,
};

static struct thead_generic_quirks xuantie_c908x_quirks = {
.extfunc = THEAD_QUIRK_EXTFUNC_FASTMEM,
};

static const struct fdt_match thead_generic_match[] = {
{ .compatible = "thead,th1520", .data = &thead_th1520_quirks },
{ .compatible = "xuantie,c908x", .data = &xuantie_c908x_quirks },
{ },
};

Expand Down
35 changes: 35 additions & 0 deletions platform/generic/thead/thead_c9xx_extfunc_fastmem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <libfdt.h>
#include <libfdt_env.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi/riscv_asm.h>
#include <thead/c9xx_encoding.h>

void thead_c9xx_fastmem_init(void)
{
int offset, len;
unsigned long mfastmbal, mfastmbah;
const fdt32_t *val;

void *fdt = fdt_get_address();

/* Find node offset */
offset = fdt_path_offset(fdt, "/fastmem");
if (offset < 0)
return;

val = fdt_getprop(fdt, offset, "mfastmba", &len);
if (val && len == 8) {
mfastmbah = fdt32_to_cpu(val[0]);
mfastmbal = fdt32_to_cpu(val[1]);
#if __riscv_xlen == 32
csr_write(THEAD_C9XX_CSR_MFASTMBA, mfastmbal);
csr_write(THEAD_C9XX_CSR_MFASTMBAH, mfastmbah);
#else
csr_write(THEAD_C9XX_CSR_MFASTMBA, mfastmbah << 32 | mfastmbal);
#endif
}
}

0 comments on commit eefc3f6

Please sign in to comment.