blob: 0ea4444570078b2265a98ce987bef4f1cc386187 [file] [log] [blame]
Lionel Debieve47e022c2023-04-14 15:49:15 +02001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * ARM Secure Monitor Call watchdog driver
4 * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
5 * This file is based on Linux driver drivers/watchdog/arm_smc_wdt.c
6 */
7
8#define LOG_CATEGORY UCLASS_WDT
9
10#include <dm.h>
11#include <dm/device_compat.h>
12#include <linux/arm-smccc.h>
13#include <linux/psci.h>
14#include <wdt.h>
15
16#define DRV_NAME "arm_smc_wdt"
17
18#define WDT_TIMEOUT_SECS(TIMEOUT) ((TIMEOUT) / 1000)
19
20enum smcwd_call {
21 SMCWD_INIT = 0,
22 SMCWD_SET_TIMEOUT = 1,
23 SMCWD_ENABLE = 2,
24 SMCWD_PET = 3,
25 SMCWD_GET_TIMELEFT = 4,
26};
27
28struct smcwd_priv_data {
29 u32 smc_id;
30 unsigned int min_timeout;
31 unsigned int max_timeout;
32};
33
34static int smcwd_call(struct udevice *dev, enum smcwd_call call,
35 unsigned long arg, struct arm_smccc_res *res)
36{
37 struct smcwd_priv_data *priv = dev_get_priv(dev);
38 struct arm_smccc_res local_res;
39
40 if (!res)
41 res = &local_res;
42
43 arm_smccc_smc(priv->smc_id, call, arg, 0, 0, 0, 0, 0, res);
44
45 if (res->a0 == PSCI_RET_NOT_SUPPORTED)
46 return -ENODEV;
47 if (res->a0 == PSCI_RET_INVALID_PARAMS)
48 return -EINVAL;
49 if (res->a0 != PSCI_RET_SUCCESS)
50 return -EIO;
51
52 return 0;
53}
54
55static int smcwd_reset(struct udevice *dev)
56{
57 return smcwd_call(dev, SMCWD_PET, 0, NULL);
58}
59
60static int smcwd_stop(struct udevice *dev)
61{
62 return smcwd_call(dev, SMCWD_ENABLE, 0, NULL);
63}
64
65static int smcwd_start(struct udevice *dev, u64 timeout_ms, ulong flags)
66{
67 struct smcwd_priv_data *priv = dev_get_priv(dev);
68 u64 timeout_sec = WDT_TIMEOUT_SECS(timeout_ms);
69 int err;
70
71 if (timeout_sec < priv->min_timeout || timeout_sec > priv->max_timeout) {
72 dev_err(dev, "Timeout value not supported\n");
73 return -EINVAL;
74 }
75
76 err = smcwd_call(dev, SMCWD_SET_TIMEOUT, timeout_sec, NULL);
77 if (err) {
78 dev_err(dev, "Timeout out configuration failed\n");
79 return err;
80 }
81
82 return smcwd_call(dev, SMCWD_ENABLE, 1, NULL);
83}
84
85static int smcwd_probe(struct udevice *dev)
86{
87 struct smcwd_priv_data *priv = dev_get_priv(dev);
88 struct arm_smccc_res res;
89 int err;
90
91 priv->smc_id = dev_read_u32_default(dev, "arm,smc-id", 0x82003D06);
92
93 err = smcwd_call(dev, SMCWD_INIT, 0, &res);
94 if (err < 0) {
95 dev_err(dev, "Init failed %i\n", err);
96 return err;
97 }
98
99 priv->min_timeout = res.a1;
100 priv->max_timeout = res.a2;
101
102 return 0;
103}
104
105static const struct wdt_ops smcwd_ops = {
106 .start = smcwd_start,
107 .stop = smcwd_stop,
108 .reset = smcwd_reset,
109};
110
111static const struct udevice_id smcwd_dt_ids[] = {
112 { .compatible = "arm,smc-wdt" },
113 {}
114};
115
116U_BOOT_DRIVER(wdt_sandbox) = {
117 .name = "smcwd",
118 .id = UCLASS_WDT,
119 .of_match = smcwd_dt_ids,
120 .priv_auto = sizeof(struct smcwd_priv_data),
121 .probe = smcwd_probe,
122 .ops = &smcwd_ops,
123};