diff --git a/arch/risc-v/Kconfig b/arch/risc-v/Kconfig index 275c28639a..a0ded26b0b 100644 --- a/arch/risc-v/Kconfig +++ b/arch/risc-v/Kconfig @@ -45,6 +45,7 @@ config ARCH_CHIP_LITEX select ARCH_RV_ISA_A select ARCH_DCACHE select ARCH_HAVE_TICKLESS + select ARCH_HAVE_RESET ---help--- Enjoy Digital LITEX VEXRISCV softcore processor (RV32IMA). diff --git a/arch/risc-v/src/litex/Make.defs b/arch/risc-v/src/litex/Make.defs index ed868915b0..f3bfabac1b 100644 --- a/arch/risc-v/src/litex/Make.defs +++ b/arch/risc-v/src/litex/Make.defs @@ -30,6 +30,7 @@ endif # Specify our C code within this directory to be included CHIP_CSRCS = litex_allocateheap.c litex_clockconfig.c +CHIP_CSRCS += litex_ctrl.c litex_systemreset.c CHIP_CSRCS += litex_irq.c litex_irq_dispatch.c CHIP_CSRCS += litex_lowputc.c litex_serial.c CHIP_CSRCS += litex_start.c diff --git a/arch/risc-v/src/litex/hardware/litex_memorymap.h b/arch/risc-v/src/litex/hardware/litex_memorymap.h index fc9423e199..98584f5d40 100644 --- a/arch/risc-v/src/litex/hardware/litex_memorymap.h +++ b/arch/risc-v/src/litex/hardware/litex_memorymap.h @@ -60,6 +60,10 @@ #define LITEX_SDPHY_BASE 0xf0005000 #endif +/* Litex core control block */ + +#define LITEX_CTRL_BASE 0xf0000800 + /* GPIO peripheral definitions. * - LITEX_GPIO_BASE is the first 32-bit address which contains a block * of GPIO registers (peripheral). Each block can contain up to 32 pins. diff --git a/arch/risc-v/src/litex/litex_ctrl.c b/arch/risc-v/src/litex/litex_ctrl.c new file mode 100644 index 0000000000..c5293bddc1 --- /dev/null +++ b/arch/risc-v/src/litex/litex_ctrl.c @@ -0,0 +1,108 @@ +/**************************************************************************** + * arch/risc-v/src/litex/litex_ctrl.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 "hardware/litex_memorymap.h" +#include "riscv_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define LITEX_CTRL_RST_OFFSET 0x00 +#define LITEX_CTRL_SCRATCH_OFFSET 0x04 +#define LITEX_CTRL_BUS_ERROR_OFFSET 0x08 + +#define LITEX_CTRL_RST_RESET_BIT 0x00 + +#define CTRL_REG(X) (LITEX_CTRL_BASE + X) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: litex_ctrl_reset + * + * Description: + * Request a core reset. + * + * Returned Value: + * None: This function should not return. + * + ****************************************************************************/ + +void litex_ctrl_reset(void) +{ + putreg32(0x1 << LITEX_CTRL_RST_RESET_BIT, CTRL_REG(LITEX_CTRL_RST_OFFSET)); +} + +/**************************************************************************** + * Name: litex_ctrl_write_scratch + * + * Description: + * Write a value to the litex control block scratch register. + * + * Input Parameters: + * value - The value to store in the register. + * + ****************************************************************************/ + +void litex_ctrl_write_scratch(uint32_t value) +{ + putreg32(value, CTRL_REG(LITEX_CTRL_SCRATCH_OFFSET)); +} + +/**************************************************************************** + * Name: litex_ctrl_read_scratch + * + * Description: + * Read a stored value from the litex control block scratch register. + * + * Returned Value: + * The value stored in the scratch register. + * + ****************************************************************************/ + +uint32_t litex_ctrl_read_scratch(void) +{ + return getreg32(CTRL_REG(LITEX_CTRL_SCRATCH_OFFSET)); +} + +/**************************************************************************** + * Name: litex_ctrl_read_bus_error + * + * Description: + * Read any bus error reported in the control block. + * + * Returned Value: + * The bus error number stored. + * + ****************************************************************************/ + +uint32_t litex_ctrl_read_bus_error(void) +{ + return getreg32(CTRL_REG(LITEX_CTRL_BUS_ERROR_OFFSET)); +} diff --git a/arch/risc-v/src/litex/litex_ctrl.h b/arch/risc-v/src/litex/litex_ctrl.h new file mode 100644 index 0000000000..4e4fd7618f --- /dev/null +++ b/arch/risc-v/src/litex/litex_ctrl.h @@ -0,0 +1,105 @@ +/**************************************************************************** + * arch/risc-v/src/litex/litex_ctrl.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_LITEX_LITEX_CTRL_H +#define __ARCH_RISCV_SRC_LITEX_LITEX_CTRL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "chip.h" + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: litex_ctrl_reset + * + * Description: + * Request a core reset. + * + * Returned Value: + * None: This function should not return. + * + ****************************************************************************/ + +void litex_ctrl_reset(void) noreturn_function; + +/**************************************************************************** + * Name: litex_ctrl_write_scratch + * + * Description: + * Write a value to the litex control block scratch register. + * + * Input Parameters: + * value - The value to store in the register. + * + ****************************************************************************/ + +void litex_ctrl_write_scratch(uint32_t value); + +/**************************************************************************** + * Name: litex_ctrl_read_scratch + * + * Description: + * Read a stored value from the litex control block scratch register. + * + * Returned Value: + * The value stored in the scratch register. + * + ****************************************************************************/ + +uint32_t litex_ctrl_read_scratch(void); + +/**************************************************************************** + * Name: litex_ctrl_read_bus_error + * + * Description: + * Read any bus error reported in the control block. + * + * Returned Value: + * The bus error number stored. + * + ****************************************************************************/ + +uint32_t litex_ctrl_read_bus_error(void); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_RISCV_SRC_LITEX_LITEX_CTRL_H */ diff --git a/arch/risc-v/src/litex/litex_systemreset.c b/arch/risc-v/src/litex/litex_systemreset.c new file mode 100644 index 0000000000..ae13c7c89d --- /dev/null +++ b/arch/risc-v/src/litex/litex_systemreset.c @@ -0,0 +1,52 @@ +/**************************************************************************** + * arch/risc-v/src/litex/litex_systemreset.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 "litex_ctrl.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_systemreset + * + * Description: + * Does a warm reset of the core through litex ctrl register block. + * + ****************************************************************************/ + +void up_systemreset(void) +{ + /* Should not return */ + + litex_ctrl_reset(); + + for (; ; ) + { + up_mdelay(250); + } +} diff --git a/boards/risc-v/litex/arty_a7/configs/knsh-tickless/defconfig b/boards/risc-v/litex/arty_a7/configs/knsh-tickless/defconfig index 5b38853179..83a307a6cf 100644 --- a/boards/risc-v/litex/arty_a7/configs/knsh-tickless/defconfig +++ b/boards/risc-v/litex/arty_a7/configs/knsh-tickless/defconfig @@ -32,6 +32,7 @@ CONFIG_ARCH_USE_MMU=y CONFIG_ARCH_USE_MPU=y CONFIG_ARCH_USE_S_MODE=y CONFIG_BINFMT_ELF_EXECUTABLE=y +CONFIG_BOARDCTL_RESET=y CONFIG_BOARDCTL_ROMDISK=y CONFIG_BOARD_LATE_INITIALIZE=y CONFIG_BOARD_LOOPSPERMSEC=10000 diff --git a/boards/risc-v/litex/arty_a7/configs/knsh/defconfig b/boards/risc-v/litex/arty_a7/configs/knsh/defconfig index 013081976f..eb58bb67aa 100644 --- a/boards/risc-v/litex/arty_a7/configs/knsh/defconfig +++ b/boards/risc-v/litex/arty_a7/configs/knsh/defconfig @@ -32,6 +32,7 @@ CONFIG_ARCH_USE_MMU=y CONFIG_ARCH_USE_MPU=y CONFIG_ARCH_USE_S_MODE=y CONFIG_BINFMT_ELF_EXECUTABLE=y +CONFIG_BOARDCTL_RESET=y CONFIG_BOARDCTL_ROMDISK=y CONFIG_BOARD_LATE_INITIALIZE=y CONFIG_BOARD_LOOPSPERMSEC=10000 diff --git a/boards/risc-v/litex/arty_a7/configs/nsh-tickless/defconfig b/boards/risc-v/litex/arty_a7/configs/nsh-tickless/defconfig index 55a1de68c7..2c88bdcff4 100644 --- a/boards/risc-v/litex/arty_a7/configs/nsh-tickless/defconfig +++ b/boards/risc-v/litex/arty_a7/configs/nsh-tickless/defconfig @@ -28,6 +28,7 @@ CONFIG_ARCH_CHIP_LITEX=y CONFIG_ARCH_INTERRUPTSTACK=8192 CONFIG_ARCH_RISCV=y CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_RESET=y CONFIG_BOARD_LOOPSPERMSEC=10000 CONFIG_BUILTIN=y CONFIG_DEBUG_FULLOPT=y diff --git a/boards/risc-v/litex/arty_a7/configs/nsh/defconfig b/boards/risc-v/litex/arty_a7/configs/nsh/defconfig index 3e481de8da..a02e7037c6 100644 --- a/boards/risc-v/litex/arty_a7/configs/nsh/defconfig +++ b/boards/risc-v/litex/arty_a7/configs/nsh/defconfig @@ -28,6 +28,7 @@ CONFIG_ARCH_CHIP_LITEX=y CONFIG_ARCH_INTERRUPTSTACK=8192 CONFIG_ARCH_RISCV=y CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_RESET=y CONFIG_BOARD_LOOPSPERMSEC=10000 CONFIG_BUILTIN=y CONFIG_DEBUG_FULLOPT=y diff --git a/boards/risc-v/litex/arty_a7/src/Makefile b/boards/risc-v/litex/arty_a7/src/Makefile index ae8f0e7456..65518bffa4 100644 --- a/boards/risc-v/litex/arty_a7/src/Makefile +++ b/boards/risc-v/litex/arty_a7/src/Makefile @@ -42,4 +42,8 @@ ifeq ($(CONFIG_LITEX_APPLICATION_RAMDISK),y) CSRCS += litex_ramdisk.c endif +ifeq ($(CONFIG_BOARDCTL_RESET),y) +CSRCS += litex_reset.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/boards/risc-v/litex/arty_a7/src/litex_reset.c b/boards/risc-v/litex/arty_a7/src/litex_reset.c new file mode 100644 index 0000000000..2f3445fae7 --- /dev/null +++ b/boards/risc-v/litex/arty_a7/src/litex_reset.c @@ -0,0 +1,78 @@ +/**************************************************************************** + * boards/risc-v/litex/arty_a7/src/litex_reset.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 + +#ifdef CONFIG_BOARDCTL_RESET + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_reset + * + * Description: + * Reset board. Support for this function is required by board-level + * logic if CONFIG_BOARDCTL_RESET is selected. + * + * Input Parameters: + * status - Status information provided with the reset event. This + * meaning of this status information is board-specific. If not + * used by a board, the value zero may be provided in calls to + * board_reset(). + * + * Returned Value: + * If this function returns, then it was not possible to power-off the + * board due to some constraints. The return value in this case is a + * board-specific reason for the failure to shutdown. + * + * Additionally: This function is called from assert if + * CONFIG_BOARD_RESET_ON_ASSERT >= 1. In this case status is set to + * CONFIG_BOARD_ASSERT_RESET_VALUE + * + ****************************************************************************/ + +int board_reset(int status) +{ + switch (status) + { + case CONFIG_BOARD_ASSERT_RESET_VALUE: + default: + up_systemreset(); + } + + /* up_systemreset can't actually return. However, let's try to catch the + * case that this changes in the future, or the logic is extended to a case + * which can fail. + */ + + return -ENODEV; +} + +#endif /* CONFIG_BOARDCTL_RESET */