From 447f767abc86b8319eea5d615006454d7196f2d5 Mon Sep 17 00:00:00 2001 From: Ning Yang Date: Wed, 7 Aug 2024 21:53:36 +0800 Subject: [PATCH] aarch64/raspberrypi: Add Watchdog Timer driver --- .../raspberrypi/include/bsp/raspberrypi.h | 5 +- .../raspberrypi/include/bsp/watchdog.h | 97 +++++++++++++++++++ bsps/aarch64/raspberrypi/start/watchdog.c | 73 ++++++++++++++ .../aarch64/raspberrypi/bspraspberrypi4.yml | 2 + .../bsps/aarch64/raspberrypi/objwatchdog.yml | 17 ++++ 5 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 bsps/aarch64/raspberrypi/include/bsp/watchdog.h create mode 100644 bsps/aarch64/raspberrypi/start/watchdog.c create mode 100644 spec/build/bsps/aarch64/raspberrypi/objwatchdog.yml diff --git a/bsps/aarch64/raspberrypi/include/bsp/raspberrypi.h b/bsps/aarch64/raspberrypi/include/bsp/raspberrypi.h index 3592d26126..a449f6febd 100644 --- a/bsps/aarch64/raspberrypi/include/bsp/raspberrypi.h +++ b/bsps/aarch64/raspberrypi/include/bsp/raspberrypi.h @@ -93,7 +93,7 @@ /** @} */ /** - * @name Power Management and Watchdog Registers + * @name Power Management and Watchdog Registers * * @{ */ @@ -124,10 +124,12 @@ #define BCM2711_PM_RSTC_DRCFG 0x00000003 #define BCM2711_PM_RSTC_WRCFG 0x00000030 #define BCM2711_PM_RSTC_WRCFG_FULL 0x00000020 +#define BCM2711_PM_RSTC_WRCFG_CLR 0xffffffcf #define BCM2711_PM_RSTC_SRCFG 0x00000300 #define BCM2711_PM_RSTC_QRCFG 0x00003000 #define BCM2711_PM_RSTC_FRCFG 0x00030000 #define BCM2711_PM_RSTC_HRCFG 0x00300000 +#define BCM2711_PM_RSTC_RESET 0x00000102 #define BCM2711_PM_RSTS (BCM2711_PM_BASE + 0x20) #define BCM2711_PM_RSTS_HADDRQ 0x00000001 @@ -142,6 +144,7 @@ #define BCM2711_PM_RSTS_HADPOR 0x00001000 #define BCM2711_PM_WDOG (BCM2711_PM_BASE + 0x24) +#define BCM2711_PM_WDOG_MASK 0x000fffff /** @} */ diff --git a/bsps/aarch64/raspberrypi/include/bsp/watchdog.h b/bsps/aarch64/raspberrypi/include/bsp/watchdog.h new file mode 100644 index 0000000000..533ace6eb9 --- /dev/null +++ b/bsps/aarch64/raspberrypi/include/bsp/watchdog.h @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup RTEMSBSPsAArch64RaspberryPi + * + * @brief API of the Watchdog driver for the raspberrypi4 bsp in RTEMS. + */ + +/* + * Copyright (C) 2024 Ning Yang + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LIBBSP_AARCH64_RASPBERRYPI_BSP_RPI_WATCHDOG_H +#define LIBBSP_AARCH64_RASPBERRYPI_BSP_RPI_WATCHDOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @note a brief example of expected usage. + * + * void raspberrypi_watchdog_example() + * { + * raspberrypi_watchdog_init(); + * raspberrypi_watchdog_start(15000); + * + * raspberrypi_watchdog_reload(); + * ... + * raspberrypi_watchdog_reload(); + * + * raspberrypi_watchdog_stop(); + * } + * + */ + +/** + * @brief Initialize BSP watchdog routines. + */ +void raspberrypi_watchdog_init(void); + +/** + * @brief Turn on the watchdog / begin the counter at the desired value. + * + * @param timeout Watchdog timeout value in ms. + * The watchdog device has 20 bits of timeout, so it only + * supports a maximum of 15999 ms for its timeout. + * This value should be between 0 and 15999. + */ +void raspberrypi_watchdog_start(uint32_t timeout_ms); + +/** + * @brief Turn off the watchdog. + */ +void raspberrypi_watchdog_stop(void); + +/** + * @brief Reload watchdog. + */ +void raspberrypi_watchdog_reload(void); + +/** + * @brief Get the remaining time of the watchdog. + * The return value is still valid when the watchdog has been stopped. + * + * @retval Watchdog remaining time in ms. + */ +uint32_t raspberrypi_watchdog_get_remaining_time(void); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/bsps/aarch64/raspberrypi/start/watchdog.c b/bsps/aarch64/raspberrypi/start/watchdog.c new file mode 100644 index 0000000000..20719d2452 --- /dev/null +++ b/bsps/aarch64/raspberrypi/start/watchdog.c @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +/** + * @file + * + * @ingroup RTEMSBSPsAArch64RaspberryPi + * + * @brief Watchdog Driver + */ + +/* + * Copyright (C) 2024 Ning Yang + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#define PM_WDOG BCM2835_REG(BCM2711_PM_WDOG) +#define PM_RSTC BCM2835_REG(BCM2711_PM_RSTC) + +uint32_t raspberrypi_watchdog_timeout; + +void raspberrypi_watchdog_init() +{ + raspberrypi_watchdog_timeout = 0; +} + +void raspberrypi_watchdog_start(uint32_t timeout_ms) +{ + raspberrypi_watchdog_timeout = timeout_ms; + + PM_WDOG = BCM2711_PM_PASSWD_MAGIC | + ((timeout_ms * 65536 / 1000) & BCM2711_PM_WDOG_MASK); + + PM_RSTC &= BCM2711_PM_RSTC_WRCFG_CLR; + PM_RSTC = (BCM2711_PM_PASSWD_MAGIC | BCM2711_PM_RSTC_WRCFG_FULL); +} + +void raspberrypi_watchdog_stop() +{ + PM_RSTC = BCM2711_PM_PASSWD_MAGIC | BCM2711_PM_RSTC_RESET; +} + +void raspberrypi_watchdog_reload() +{ + raspberrypi_watchdog_start(raspberrypi_watchdog_timeout); +} + +uint32_t raspberrypi_watchdog_get_remaining_time() +{ + return (PM_WDOG & BCM2711_PM_WDOG_MASK)*1000/65536; +} \ No newline at end of file diff --git a/spec/build/bsps/aarch64/raspberrypi/bspraspberrypi4.yml b/spec/build/bsps/aarch64/raspberrypi/bspraspberrypi4.yml index 484caa4fef..d1424ee920 100644 --- a/spec/build/bsps/aarch64/raspberrypi/bspraspberrypi4.yml +++ b/spec/build/bsps/aarch64/raspberrypi/bspraspberrypi4.yml @@ -57,6 +57,8 @@ links: uid: objgpio - role: build-dependency uid: objspi +- role: build-dependency + uid: objwatchdog source: - bsps/aarch64/raspberrypi/console/console.c - bsps/aarch64/raspberrypi/start/bspstart.c diff --git a/spec/build/bsps/aarch64/raspberrypi/objwatchdog.yml b/spec/build/bsps/aarch64/raspberrypi/objwatchdog.yml new file mode 100644 index 0000000000..08fc41ee4f --- /dev/null +++ b/spec/build/bsps/aarch64/raspberrypi/objwatchdog.yml @@ -0,0 +1,17 @@ +SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause +build-type: objects +cflags: [] +copyrights: + - Copyright (C) 2024 Ning Yang +cppflags: [] +cxxflags: [] +enabled-by: true +includes: [] +install: + - destination: ${BSP_INCLUDEDIR}/bsp + source: + - bsps/aarch64/raspberrypi/include/bsp/watchdog.h +links: [] +source: + - bsps/aarch64/raspberrypi/start/watchdog.c +type: build