blob: 7ebcd2552668e73222b9c060caed5ec8b4718742 [file] [log] [blame]
Patrice Chotard473b2442019-04-30 17:26:22 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5
Patrick Delaunay0647db52020-11-06 19:01:41 +01006#define LOG_CATEGORY UCLASS_WDT
7
Patrice Chotard473b2442019-04-30 17:26:22 +02008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrice Chotard473b2442019-04-30 17:26:22 +020012#include <syscon.h>
13#include <wdt.h>
14#include <asm/io.h>
Patrick Delaunay0647db52020-11-06 19:01:41 +010015#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Patrice Chotard473b2442019-04-30 17:26:22 +020017#include <linux/iopoll.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060018#include <linux/printk.h>
Patrice Chotard473b2442019-04-30 17:26:22 +020019
20/* IWDG registers */
21#define IWDG_KR 0x00 /* Key register */
22#define IWDG_PR 0x04 /* Prescaler Register */
23#define IWDG_RLR 0x08 /* ReLoad Register */
24#define IWDG_SR 0x0C /* Status Register */
25
26/* IWDG_KR register bit mask */
27#define KR_KEY_RELOAD 0xAAAA /* Reload counter enable */
28#define KR_KEY_ENABLE 0xCCCC /* Peripheral enable */
29#define KR_KEY_EWA 0x5555 /* Write access enable */
30
31/* IWDG_PR register bit values */
32#define PR_256 0x06 /* Prescaler set to 256 */
33
34/* IWDG_RLR register values */
35#define RLR_MAX 0xFFF /* Max value supported by reload register */
36
37/* IWDG_SR register bit values */
38#define SR_PVU BIT(0) /* Watchdog prescaler value update */
39#define SR_RVU BIT(1) /* Watchdog counter reload value update */
40
41struct stm32mp_wdt_priv {
42 fdt_addr_t base; /* registers addr in physical memory */
43 unsigned long wdt_clk_rate; /* Watchdog dedicated clock rate */
44};
45
46static int stm32mp_wdt_reset(struct udevice *dev)
47{
48 struct stm32mp_wdt_priv *priv = dev_get_priv(dev);
49
50 writel(KR_KEY_RELOAD, priv->base + IWDG_KR);
51
52 return 0;
53}
54
55static int stm32mp_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
56{
57 struct stm32mp_wdt_priv *priv = dev_get_priv(dev);
58 int reload;
59 u32 val;
60 int ret;
61
62 /* Prescaler fixed to 256 */
63 reload = timeout_ms * priv->wdt_clk_rate / 256;
64 if (reload > RLR_MAX + 1)
65 /* Force to max watchdog counter reload value */
66 reload = RLR_MAX + 1;
67 else if (!reload)
68 /* Force to min watchdog counter reload value */
69 reload = priv->wdt_clk_rate / 256;
70
71 /* Set prescaler & reload registers */
72 writel(KR_KEY_EWA, priv->base + IWDG_KR);
73 writel(PR_256, priv->base + IWDG_PR);
74 writel(reload - 1, priv->base + IWDG_RLR);
75
76 /* Enable watchdog */
77 writel(KR_KEY_ENABLE, priv->base + IWDG_KR);
78
79 /* Wait for the registers to be updated */
80 ret = readl_poll_timeout(priv->base + IWDG_SR, val,
81 val & (SR_PVU | SR_RVU), CONFIG_SYS_HZ);
82
83 if (ret < 0) {
Patrick Delaunay0647db52020-11-06 19:01:41 +010084 dev_err(dev, "Updating IWDG registers timeout");
Patrice Chotard473b2442019-04-30 17:26:22 +020085 return -ETIMEDOUT;
86 }
87
88 return 0;
89}
90
91static int stm32mp_wdt_probe(struct udevice *dev)
92{
93 struct stm32mp_wdt_priv *priv = dev_get_priv(dev);
94 struct clk clk;
95 int ret;
96
Patrick Delaunay0647db52020-11-06 19:01:41 +010097 dev_dbg(dev, "IWDG init\n");
Patrice Chotard473b2442019-04-30 17:26:22 +020098
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090099 priv->base = dev_read_addr(dev);
Patrice Chotard473b2442019-04-30 17:26:22 +0200100 if (priv->base == FDT_ADDR_T_NONE)
101 return -EINVAL;
102
103 /* Enable clock */
104 ret = clk_get_by_name(dev, "pclk", &clk);
105 if (ret)
106 return ret;
107
108 ret = clk_enable(&clk);
109 if (ret)
110 return ret;
111
112 /* Get LSI clock */
113 ret = clk_get_by_name(dev, "lsi", &clk);
114 if (ret)
115 return ret;
116
117 priv->wdt_clk_rate = clk_get_rate(&clk);
118
Patrick Delaunay0647db52020-11-06 19:01:41 +0100119 dev_dbg(dev, "IWDG init done\n");
Patrice Chotard473b2442019-04-30 17:26:22 +0200120
121 return 0;
122}
123
124static const struct wdt_ops stm32mp_wdt_ops = {
125 .start = stm32mp_wdt_start,
126 .reset = stm32mp_wdt_reset,
127};
128
129static const struct udevice_id stm32mp_wdt_match[] = {
130 { .compatible = "st,stm32mp1-iwdg" },
131 { /* sentinel */ }
132};
133
134U_BOOT_DRIVER(stm32mp_wdt) = {
135 .name = "stm32mp-wdt",
136 .id = UCLASS_WDT,
137 .of_match = stm32mp_wdt_match,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700138 .priv_auto = sizeof(struct stm32mp_wdt_priv),
Patrice Chotard473b2442019-04-30 17:26:22 +0200139 .probe = stm32mp_wdt_probe,
140 .ops = &stm32mp_wdt_ops,
141};