summaryrefslogtreecommitdiff
path: root/chip/stm32/watchdog.c
blob: a25e2576ca8974d048521bbca2293e7d56106d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Watchdog driver */

#include "board.h"
#include "common.h"
#include "config.h"
#include "registers.h"
#include "gpio.h"
#include "task.h"
#include "timer.h"
#include "util.h"
#include "watchdog.h"

/* LSI oscillator frequency is typically 38 kHz
 * but might vary from 28 to 56kHz.
 * So let's pick 56kHz to ensure we reload
 * early enough.
 */
#define LSI_CLOCK 56000

/* Prescaler divider = /256 */
#define IWDG_PRESCALER 6
#define IWDG_PRESCALER_DIV (1 << ((IWDG_PRESCALER) + 2))

/*
 * We use the WWDG as an early warning for the real watchdog, which just
 * resets. Since it has a very short period, we need to allow several cycles
 * of this to make up one IWDG cycle. The WWDG's early warning kicks in
 * half way through the cycle, with a maximum time of 65.54ms at 32 MHz.
 */
#define WATCHDOG_CYCLES_BEFORE_RESET \
	(WATCHDOG_PERIOD_MS / (65540 * 32000 / CPU_CLOCK))

/* Keep a track of how many WWDG cycles we have had */
static unsigned int watchdog_count;


static void watchdog_reset_count(void)
{
	watchdog_count = WATCHDOG_CYCLES_BEFORE_RESET;
}


void watchdog_reload(void)
{
	/* Reload the watchdog */
	STM32_IWDG_KR = 0xaaaa;

	watchdog_reset_count();
}


/**
 * Chcek if a watchdog interrupt needs to be reported.
 *
 * If so, this function should call watchdog_trace()
 *
 * @param excep_lr	Value of lr to indicate caller return
 * @param excep_sp	Value of sp to indicate caller task id
 */
void watchdog_check(uint32_t excep_lr, uint32_t excep_sp)
{
	/* Reset the windowed watchdog here */
	STM32_WWDG_CR = 0xff;
	STM32_WWDG_SR = 0;

	/* If the count has expired, output a trace */
	if (!--watchdog_count) {
		/* Reset here, to give the UART enough time to send trace */
		watchdog_reset_count();
		watchdog_trace(excep_lr, excep_sp);
		watchdog_reset_count();
	}
}


void IRQ_HANDLER(STM32_IRQ_WWDG)(void) __attribute__((naked));
void IRQ_HANDLER(STM32_IRQ_WWDG)(void)
{
	/* Naked call so we can extract raw LR and SP */
	asm volatile("mov r0, lr\n"
		     "mov r1, sp\n"
		     /* Must push registers in pairs to keep 64-bit aligned
		      * stack for ARM EABI.  This also conveninently saves
		      * R0=LR so we can pass it to task_resched_if_needed. */
		     "push {r0, lr}\n"
		     "bl watchdog_check\n"
		     "pop {r0, lr}\n"
		     "b task_resched_if_needed\n");
}
const struct irq_priority IRQ_BUILD_NAME(prio_, STM32_IRQ_WWDG, )
	__attribute__((section(".rodata.irqprio")))
		= {STM32_IRQ_WWDG, 0}; /* put the watchdog at the highest
					    priority */

int watchdog_init(void)
{
	uint32_t watchdog_period;

	/* set the time-out period */
	watchdog_period = WATCHDOG_PERIOD_MS *
			(LSI_CLOCK / IWDG_PRESCALER_DIV) / 1000;

	/* Unlock watchdog registers */
	STM32_IWDG_KR = 0x5555;

	/* Set the prescaler between the LSI clock and the watchdog counter */
	STM32_IWDG_PR = IWDG_PRESCALER & 7;
	/* Set the reload value of the watchdog counter */
	STM32_IWDG_RLR = watchdog_period & 0x7FF ;

	/* Start the watchdog (and re-lock registers) */
	STM32_IWDG_KR = 0xcccc;

	watchdog_reset_count();

#ifdef CONFIG_WATCHDOG_HELP
	/* enable clock */
	STM32_RCC_APB1ENR |= 1 << 11;

	STM32_WWDG_SR = 0;
	STM32_WWDG_CR = 0xff;
	STM32_WWDG_CFR = 0x7f | STM32_WWDG_TB_8 | STM32_WWDG_EWI;

	/* Enable watchdog interrupt */
	task_enable_irq(IRQ_WATCHDOG);
#endif

	return EC_SUCCESS;
}