1/*
2 * Copyright (c) 2014 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/**
8 * @file
9 * @brief Watchdog initialization for kv5x platform
10 *
11 * This module initializes the watchdog for the kv5x platform.
12 */
13
14#include <soc.h>
15#include <zephyr/toolchain.h>
16#include <zephyr/linker/sections.h>
17
18_ASM_FILE_PROLOGUE
19
20GTEXT(z_arm_watchdog_init)
21
22/* watchdog register offsets */
23#define WDOG_SCTRL_HI_OFFSET  0x0
24#define WDOG_UNLOCK_OFFSET  0xE
25
26/* watchdog command words */
27#define WDOG_UNLOCK_1_CMD  0xC520
28#define WDOG_UNLOCK_2_CMD  0xD928
29
30/**
31 *
32 * @brief Watchdog timer disable routine
33 *
34 * This routine will disable the watchdog timer.
35 *
36 */
37
38SECTION_FUNC(TEXT,z_arm_watchdog_init)
39    /*
40     * NOTE: DO NOT SINGLE STEP THROUGH THIS FUNCTION!!!
41     * There are timing requirements for the execution of the unlock process.
42     * Single stepping through the code will cause the CPU to reset.
43     */
44
45    /*
46     * First unlock the watchdog so that we can write to registers.
47     *
48     * This sequence must execute within 20 clock cycles, so disable
49     * interrupts to keep the code atomic and ensure the timing.
50     */
51
52    ldr r0, =PERIPH_ADDR_BASE_WDOG
53
54    movw r1, #WDOG_UNLOCK_1_CMD
55    strh r1, [r0, #WDOG_UNLOCK_OFFSET]
56
57    movw r1, #WDOG_UNLOCK_2_CMD
58    strh r1, [r0, #WDOG_UNLOCK_OFFSET]
59
60    /*
61     * Disable the watchdog.
62     *
63     * Writes to control/configuration registers must execute within
64     * 256 clock cycles after unlocking.
65     */
66
67    ldrh r1, [r0, #WDOG_SCTRL_HI_OFFSET]
68    mov  r2, #1
69    bics r1, r2
70    strh r1, [r0, #WDOG_SCTRL_HI_OFFSET]
71
72    bx lr
73
74