diff --git a/arch/risc-v/src/k230/CMakeLists.txt b/arch/risc-v/src/k230/CMakeLists.txt index 23c6361c2e..ccb4a1e49a 100644 --- a/arch/risc-v/src/k230/CMakeLists.txt +++ b/arch/risc-v/src/k230/CMakeLists.txt @@ -26,6 +26,7 @@ list( k230_start.c k230_irq_dispatch.c k230_irq.c + k230_hart.c k230_timerisr.c k230_allocateheap.c) diff --git a/arch/risc-v/src/k230/Make.defs b/arch/risc-v/src/k230/Make.defs index f5ae55c491..adfa2a6c04 100644 --- a/arch/risc-v/src/k230/Make.defs +++ b/arch/risc-v/src/k230/Make.defs @@ -24,7 +24,7 @@ CHIP_ASRCS += k230_head.S # Specify our C code within this directory to be included CHIP_CSRCS = k230_start.c k230_irq_dispatch.c k230_irq.c -CHIP_CSRCS += k230_timerisr.c k230_allocateheap.c +CHIP_CSRCS += k230_timerisr.c k230_allocateheap.c k230_hart.c ifeq ($(CONFIG_BUILD_KERNEL),y) CHIP_CSRCS += k230_mm_init.c diff --git a/arch/risc-v/src/k230/chip.h b/arch/risc-v/src/k230/chip.h index cd863e38bf..fbca4b78ea 100644 --- a/arch/risc-v/src/k230/chip.h +++ b/arch/risc-v/src/k230/chip.h @@ -30,9 +30,11 @@ #include #include "k230_memorymap.h" +#include "k230_hart.h" #include "hardware/k230_memorymap.h" #include "hardware/k230_plic.h" +#include "hardware/k230_clint.h" #include "riscv_internal.h" #include "riscv_percpu.h" diff --git a/arch/risc-v/src/k230/hardware/k230_memorymap.h b/arch/risc-v/src/k230/hardware/k230_memorymap.h index d928f15aa0..4086f5a4ea 100644 --- a/arch/risc-v/src/k230/hardware/k230_memorymap.h +++ b/arch/risc-v/src/k230/hardware/k230_memorymap.h @@ -25,9 +25,21 @@ * Pre-processor Definitions ****************************************************************************/ -/* Register Base Address ****************************************************/ +/* PLIC/CLINT Base Address **************************************************/ #define K230_PLIC_BASE 0xF00000000 #define K230_CLINT_BASE (K230_PLIC_BASE + 0x04000000) +/* T-Head c908 specific CSR */ + +#define CSR_MCOR 0x7c2 +#define CSR_MHCR 0x7c1 +#define CSR_MCCR2 0x7c3 +#define CSR_MHINT 0x7c5 +#define CSR_MXSTATUS 0x7c0 +#define CSR_MRMR 0x7c6 +#define CSR_MRVBR 0x7c7 +#define CSR_MSMPR 0x7f3 +#define CSR_PLIC_BASE 0xfc1 + #endif /* __ARCH_RISCV_SRC_K230_HARDWARE_K230_MEMORYMAP_H */ diff --git a/arch/risc-v/src/k230/k230_hart.c b/arch/risc-v/src/k230/k230_hart.c new file mode 100644 index 0000000000..b340315deb --- /dev/null +++ b/arch/risc-v/src/k230/k230_hart.c @@ -0,0 +1,91 @@ +/**************************************************************************** + * arch/risc-v/src/k230/k230_hart.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include +#include + +#include "riscv_internal.h" +#include "chip.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#if !defined(CONFIG_BUILD_KERNEL) || defined(CONFIG_NUTTSBI) + +/**************************************************************************** + * Name: hart_has_vec_ext() + * Description: returns Non-zero if CPU has vector extension + ****************************************************************************/ + +int hart_has_vec_ext(void) +{ + #define MISA_VECTOR_BIT ('V'-'A') + #define MISA_VECOTR_MASK ( 1 << MISA_VECTOR_BIT ) + + return (READ_CSR(CSR_MISA) & MISA_VECOTR_MASK); +} + +/**************************************************************************** + * Name: k230_hart_init() + * Description: K230 M-mode HART initialization + ****************************************************************************/ + +void k230_hart_init(void) +{ + if (hart_has_vec_ext()) + { + WRITE_CSR(CSR_MHCR, 0x11ff); + WRITE_CSR(CSR_MCOR, 0x70013); + WRITE_CSR(CSR_MSMPR, 0x1); + WRITE_CSR(CSR_MCCR2, 0xe0410009); + WRITE_CSR(CSR_MHINT, 0x16e30c); + } + + /* turn off MAEE to have standard PTE format */ + + WRITE_CSR(CSR_MXSTATUS, 0x438000); +} +#endif /* !defined(CONFIG_BUILD_KERNEL) || defined(CONFIG_NUTTSBI) */ + +#ifdef CONFIG_NUTTSBI +/**************************************************************************** + * Name: sbi_late_initialize runs in M-mode + ****************************************************************************/ + +void sbi_late_initialize(void) +{ + /* delegate K230 plic enable to S-mode */ + + *((volatile uint32_t *)K230_PLIC_CTRL) = 1; + k230_hart_init(); +} +#endif diff --git a/arch/risc-v/src/k230/k230_hart.h b/arch/risc-v/src/k230/k230_hart.h new file mode 100644 index 0000000000..2c718ad0bb --- /dev/null +++ b/arch/risc-v/src/k230/k230_hart.h @@ -0,0 +1,40 @@ +/**************************************************************************** + * arch/risc-v/src/k230/k230_hart.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_K230_K230_HART_H +#define __ARCH_RISCV_SRC_K230_K230_HART_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Public functions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ +#if !defined(CONFIG_BUILD_KERNEL) || defined(CONFIG_NUTTSBI) + +int hart_has_vec_ext(void); /* checks for vector extension */ +void k230_hart_init(void); /* M-mode initialization */ + +#endif /* !defined(CONFIG_BUILD_KERNEL) || defined(CONFIG_NUTTSBI) */ +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RISCV_SRC_K230_K230_HART_H */ diff --git a/arch/risc-v/src/k230/k230_irq.c b/arch/risc-v/src/k230/k230_irq.c index 868df4d92c..cf31c9bb59 100644 --- a/arch/risc-v/src/k230/k230_irq.c +++ b/arch/risc-v/src/k230/k230_irq.c @@ -41,19 +41,6 @@ * Public Functions ****************************************************************************/ -#ifdef CONFIG_NUTTSBI -/**************************************************************************** - * Name: sbi_late_initialize runs in M-mode - ****************************************************************************/ - -void sbi_late_initialize(void) -{ - /* delegate K230 plic enable to S-mode */ - - *((volatile uint32_t *)K230_PLIC_CTRL) = 1; -} -#endif - /**************************************************************************** * Name: up_irqinitialize ****************************************************************************/ diff --git a/arch/risc-v/src/k230/k230_start.c b/arch/risc-v/src/k230/k230_start.c index 9b998f500b..ff6d74c484 100644 --- a/arch/risc-v/src/k230/k230_start.c +++ b/arch/risc-v/src/k230/k230_start.c @@ -129,6 +129,10 @@ void k230_start(int mhartid, const char *dtb) #endif } +#ifndef CONFIG_BUILD_KERNEL + k230_hart_init(); +#endif + /* Disable MMU */ WRITE_CSR(satp, 0x0);