summaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/tangier_wdt.c
blob: ba265cf223d8c5b07bd792437a64e8314a5da006 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2017 Intel Corporation
 */
#include <common.h>
#include <dm.h>
#include <wdt.h>
#include <div64.h>
#include <asm/scu.h>

/* Hardware timeout in seconds */
#define WDT_PRETIMEOUT		15
#define WDT_TIMEOUT_MIN		(1 + WDT_PRETIMEOUT)
#define WDT_TIMEOUT_MAX		170

/*
 * Note, firmware chooses 90 seconds as a default timeout for watchdog on
 * Intel Tangier SoC. It means that without handling it in the running code
 * the reboot will happen.
 */

enum {
	SCU_WATCHDOG_START			= 0,
	SCU_WATCHDOG_STOP			= 1,
	SCU_WATCHDOG_KEEPALIVE			= 2,
	SCU_WATCHDOG_SET_ACTION_ON_TIMEOUT	= 3,
};

static int tangier_wdt_reset(struct udevice *dev)
{
	scu_ipc_simple_command(IPCMSG_WATCHDOG_TIMER, SCU_WATCHDOG_KEEPALIVE);
	return 0;
}

static int tangier_wdt_stop(struct udevice *dev)
{
	return scu_ipc_simple_command(IPCMSG_WATCHDOG_TIMER, SCU_WATCHDOG_STOP);
}

static int tangier_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
{
	u32 timeout_sec;
	int in_size;
	struct ipc_wd_start {
		u32 pretimeout;
		u32 timeout;
	} ipc_wd_start;

	/* Calculate timeout in seconds and restrict to min and max value */
	do_div(timeout_ms, 1000);
	timeout_sec = clamp_t(u32, timeout_ms, WDT_TIMEOUT_MIN, WDT_TIMEOUT_MAX);

	/* Update values in the IPC request */
	ipc_wd_start.pretimeout = timeout_sec - WDT_PRETIMEOUT;
	ipc_wd_start.timeout = timeout_sec;

	/*
	 * SCU expects the input size for watchdog IPC
	 * to be based on 4 bytes
	 */
	in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);

	scu_ipc_command(IPCMSG_WATCHDOG_TIMER, SCU_WATCHDOG_START,
			(u32 *)&ipc_wd_start, in_size, NULL, 0);

	return 0;
}

static const struct wdt_ops tangier_wdt_ops = {
	.reset = tangier_wdt_reset,
	.start = tangier_wdt_start,
	.stop = tangier_wdt_stop,
};

static const struct udevice_id tangier_wdt_ids[] = {
	{ .compatible = "intel,tangier-wdt" },
	{ /* sentinel */ }
};

static int tangier_wdt_probe(struct udevice *dev)
{
	debug("%s: Probing wdt%u\n", __func__, dev->seq);
	return 0;
}

U_BOOT_DRIVER(wdt_tangier) = {
	.name = "wdt_tangier",
	.id = UCLASS_WDT,
	.of_match = tangier_wdt_ids,
	.ops = &tangier_wdt_ops,
	.probe = tangier_wdt_probe,
};