blob: 10fe3e28232313a35eb357a1dfcb37a65f73657f [file] [log] [blame]
Qiang Zhao3af19942019-05-07 03:16:09 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Watchdog driver for SP805 on some Layerscape SoC
4 *
5 * Copyright 2019 NXP
6 */
7
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Qiang Zhao3af19942019-05-07 03:16:09 +000010#include <asm/io.h>
Rayagonda Kokatanurf0799902020-04-06 13:29:52 +053011#include <clk.h>
Qiang Zhao3af19942019-05-07 03:16:09 +000012#include <dm/device.h>
13#include <dm/fdtaddr.h>
14#include <dm/read.h>
15#include <linux/bitops.h>
16#include <watchdog.h>
17#include <wdt.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Qiang Zhao3af19942019-05-07 03:16:09 +000019
20#define WDTLOAD 0x000
21#define WDTCONTROL 0x008
22#define WDTINTCLR 0x00C
23#define WDTLOCK 0xC00
24
25#define TIME_OUT_MIN_MSECS 1
26#define TIME_OUT_MAX_MSECS 120000
27#define SYS_FSL_WDT_CLK_DIV 16
28#define INT_ENABLE BIT(0)
29#define RESET_ENABLE BIT(1)
30#define DISABLE 0
31#define UNLOCK 0x1ACCE551
32#define LOCK 0x00000001
33#define INT_MASK BIT(0)
34
35DECLARE_GLOBAL_DATA_PTR;
36
37struct sp805_wdt_priv {
38 void __iomem *reg;
Rayagonda Kokatanurf0799902020-04-06 13:29:52 +053039 unsigned long clk_rate;
Qiang Zhao3af19942019-05-07 03:16:09 +000040};
41
42static int sp805_wdt_reset(struct udevice *dev)
43{
44 struct sp805_wdt_priv *priv = dev_get_priv(dev);
45
46 writel(UNLOCK, priv->reg + WDTLOCK);
47 writel(INT_MASK, priv->reg + WDTINTCLR);
48 writel(LOCK, priv->reg + WDTLOCK);
49 readl(priv->reg + WDTLOCK);
50
51 return 0;
52}
53
54static int sp805_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
55{
56 u32 load_value;
57 u32 load_time;
58 struct sp805_wdt_priv *priv = dev_get_priv(dev);
59
60 load_time = (u32)timeout;
61 if (timeout < TIME_OUT_MIN_MSECS)
62 load_time = TIME_OUT_MIN_MSECS;
63 else if (timeout > TIME_OUT_MAX_MSECS)
64 load_time = TIME_OUT_MAX_MSECS;
65 /* sp805 runs counter with given value twice, so when the max timeout is
66 * set 120s, the gd->bus_clk is less than 1145MHz, the load_value will
67 * not overflow.
68 */
Rayagonda Kokatanurf0799902020-04-06 13:29:52 +053069 if (gd->bus_clk) {
70 load_value = (gd->bus_clk) /
71 (2 * 1000 * SYS_FSL_WDT_CLK_DIV) * load_time;
72 } else {
73 /* platform provide clk */
74 load_value = (timeout / 2) * (priv->clk_rate / 1000);
75 }
Qiang Zhao3af19942019-05-07 03:16:09 +000076
77 writel(UNLOCK, priv->reg + WDTLOCK);
78 writel(load_value, priv->reg + WDTLOAD);
79 writel(INT_MASK, priv->reg + WDTINTCLR);
80 writel(INT_ENABLE | RESET_ENABLE, priv->reg + WDTCONTROL);
81 writel(LOCK, priv->reg + WDTLOCK);
82 readl(priv->reg + WDTLOCK);
83
84 return 0;
85}
86
87static int sp805_wdt_stop(struct udevice *dev)
88{
89 struct sp805_wdt_priv *priv = dev_get_priv(dev);
90
91 writel(UNLOCK, priv->reg + WDTLOCK);
92 writel(DISABLE, priv->reg + WDTCONTROL);
93 writel(LOCK, priv->reg + WDTLOCK);
94 readl(priv->reg + WDTLOCK);
95
96 return 0;
97}
98
Thomas Schaefer6345a342019-08-08 16:00:31 +080099static int sp805_wdt_expire_now(struct udevice *dev, ulong flags)
100{
101 sp805_wdt_start(dev, 0, flags);
102
103 return 0;
104}
105
Qiang Zhao3af19942019-05-07 03:16:09 +0000106static int sp805_wdt_probe(struct udevice *dev)
107{
Simon Glass75e534b2020-12-16 21:20:07 -0700108 debug("%s: Probing wdt%u (sp805-wdt)\n", __func__, dev_seq(dev));
Qiang Zhao3af19942019-05-07 03:16:09 +0000109
110 return 0;
111}
112
Simon Glassaad29ae2020-12-03 16:55:21 -0700113static int sp805_wdt_of_to_plat(struct udevice *dev)
Qiang Zhao3af19942019-05-07 03:16:09 +0000114{
115 struct sp805_wdt_priv *priv = dev_get_priv(dev);
Rayagonda Kokatanurf0799902020-04-06 13:29:52 +0530116 struct clk clk;
Qiang Zhao3af19942019-05-07 03:16:09 +0000117
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100118 priv->reg = dev_read_addr_ptr(dev);
119 if (!priv->reg)
120 return -EINVAL;
Qiang Zhao3af19942019-05-07 03:16:09 +0000121
Rayagonda Kokatanurf0799902020-04-06 13:29:52 +0530122 if (!clk_get_by_index(dev, 0, &clk))
123 priv->clk_rate = clk_get_rate(&clk);
124
Qiang Zhao3af19942019-05-07 03:16:09 +0000125 return 0;
126}
127
128static const struct wdt_ops sp805_wdt_ops = {
129 .start = sp805_wdt_start,
130 .reset = sp805_wdt_reset,
131 .stop = sp805_wdt_stop,
Thomas Schaefer6345a342019-08-08 16:00:31 +0800132 .expire_now = sp805_wdt_expire_now,
Qiang Zhao3af19942019-05-07 03:16:09 +0000133};
134
135static const struct udevice_id sp805_wdt_ids[] = {
Michael Walle1fe74462021-10-13 18:14:16 +0200136 { .compatible = "arm,sp805" },
Qiang Zhao3af19942019-05-07 03:16:09 +0000137 {}
138};
139
140U_BOOT_DRIVER(sp805_wdt) = {
141 .name = "sp805_wdt",
142 .id = UCLASS_WDT,
143 .of_match = sp805_wdt_ids,
144 .probe = sp805_wdt_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700145 .priv_auto = sizeof(struct sp805_wdt_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -0700146 .of_to_plat = sp805_wdt_of_to_plat,
Qiang Zhao3af19942019-05-07 03:16:09 +0000147 .ops = &sp805_wdt_ops,
148};