blob: 5f586ef68379baba16817b3550b3d40dbd475573 [file] [log] [blame]
Suneel Garapatiaf6ba902019-10-21 16:09:36 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Marvell International Ltd.
4 *
5 * https://spdx.org/licenses
6 */
7
Suneel Garapati3981cdd2020-09-23 11:01:31 +02008#include <clk.h>
Suneel Garapatiaf6ba902019-10-21 16:09:36 -07009#include <dm.h>
10#include <errno.h>
11#include <wdt.h>
12#include <asm/io.h>
Suneel Garapati3981cdd2020-09-23 11:01:31 +020013#include <linux/bitfield.h>
Suneel Garapatiaf6ba902019-10-21 16:09:36 -070014
15DECLARE_GLOBAL_DATA_PTR;
16
Suneel Garapati3981cdd2020-09-23 11:01:31 +020017#define CORE0_WDOG_OFFSET 0x40000
Suneel Garapatiaf6ba902019-10-21 16:09:36 -070018#define CORE0_POKE_OFFSET 0x50000
19#define CORE0_POKE_OFFSET_MASK 0xfffffULL
20
Suneel Garapati3981cdd2020-09-23 11:01:31 +020021#define WDOG_MODE GENMASK_ULL(1, 0)
22#define WDOG_LEN GENMASK_ULL(19, 4)
23#define WDOG_CNT GENMASK_ULL(43, 20)
24
Suneel Garapatiaf6ba902019-10-21 16:09:36 -070025struct octeontx_wdt {
26 void __iomem *reg;
Suneel Garapati3981cdd2020-09-23 11:01:31 +020027 struct clk clk;
Suneel Garapatiaf6ba902019-10-21 16:09:36 -070028};
29
Suneel Garapati3981cdd2020-09-23 11:01:31 +020030static int octeontx_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
31{
32 struct octeontx_wdt *priv = dev_get_priv(dev);
33 u64 clk_rate, val;
34 u64 tout_wdog;
35
36 clk_rate = clk_get_rate(&priv->clk);
37 if (IS_ERR_VALUE(clk_rate))
38 return -EINVAL;
39
40 /* Watchdog counts in 1024 cycle steps */
41 tout_wdog = (clk_rate * timeout_ms / 1000) >> 10;
42
43 /*
44 * We can only specify the upper 16 bits of a 24 bit value.
45 * Round up
46 */
47 tout_wdog = (tout_wdog + 0xff) >> 8;
48
49 /* If the timeout overflows the hardware limit, set max */
50 if (tout_wdog >= 0x10000)
51 tout_wdog = 0xffff;
52
53 val = FIELD_PREP(WDOG_MODE, 0x3) |
54 FIELD_PREP(WDOG_LEN, tout_wdog) |
55 FIELD_PREP(WDOG_CNT, tout_wdog << 8);
56 writeq(val, priv->reg + CORE0_WDOG_OFFSET);
57
58 return 0;
59}
60
61static int octeontx_wdt_stop(struct udevice *dev)
62{
63 struct octeontx_wdt *priv = dev_get_priv(dev);
64
65 writeq(0, priv->reg + CORE0_WDOG_OFFSET);
66
67 return 0;
68}
69
70static int octeontx_wdt_expire_now(struct udevice *dev, ulong flags)
71{
72 octeontx_wdt_stop(dev);
73
74 /* Start with 100ms timeout to expire immediately */
75 octeontx_wdt_start(dev, 100, flags);
76
77 return 0;
78}
79
Suneel Garapatiaf6ba902019-10-21 16:09:36 -070080static int octeontx_wdt_reset(struct udevice *dev)
81{
82 struct octeontx_wdt *priv = dev_get_priv(dev);
83
Suneel Garapati3981cdd2020-09-23 11:01:31 +020084 writeq(~0ULL, priv->reg + CORE0_POKE_OFFSET);
85
86 return 0;
87}
88
89static int octeontx_wdt_remove(struct udevice *dev)
90{
91 octeontx_wdt_stop(dev);
Suneel Garapatiaf6ba902019-10-21 16:09:36 -070092
93 return 0;
94}
95
96static int octeontx_wdt_probe(struct udevice *dev)
97{
98 struct octeontx_wdt *priv = dev_get_priv(dev);
Suneel Garapati3981cdd2020-09-23 11:01:31 +020099 int ret;
Suneel Garapatiaf6ba902019-10-21 16:09:36 -0700100
101 priv->reg = dev_remap_addr(dev);
102 if (!priv->reg)
103 return -EINVAL;
104
105 /*
Suneel Garapati3981cdd2020-09-23 11:01:31 +0200106 * Save base register address in reg masking lower 20 bits
107 * as 0xa0000 appears when extracted from the DT
Suneel Garapatiaf6ba902019-10-21 16:09:36 -0700108 */
109 priv->reg = (void __iomem *)(((u64)priv->reg &
Suneel Garapati3981cdd2020-09-23 11:01:31 +0200110 ~CORE0_POKE_OFFSET_MASK));
111
112 ret = clk_get_by_index(dev, 0, &priv->clk);
113 if (ret < 0)
114 return ret;
115
116 ret = clk_enable(&priv->clk);
117 if (ret)
118 return ret;
Suneel Garapatiaf6ba902019-10-21 16:09:36 -0700119
120 return 0;
121}
122
123static const struct wdt_ops octeontx_wdt_ops = {
124 .reset = octeontx_wdt_reset,
Suneel Garapati3981cdd2020-09-23 11:01:31 +0200125 .start = octeontx_wdt_start,
126 .stop = octeontx_wdt_stop,
127 .expire_now = octeontx_wdt_expire_now,
Suneel Garapatiaf6ba902019-10-21 16:09:36 -0700128};
129
130static const struct udevice_id octeontx_wdt_ids[] = {
131 { .compatible = "arm,sbsa-gwdt" },
132 {}
133};
134
135U_BOOT_DRIVER(wdt_octeontx) = {
136 .name = "wdt_octeontx",
137 .id = UCLASS_WDT,
138 .of_match = octeontx_wdt_ids,
139 .ops = &octeontx_wdt_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700140 .priv_auto = sizeof(struct octeontx_wdt),
Suneel Garapatiaf6ba902019-10-21 16:09:36 -0700141 .probe = octeontx_wdt_probe,
Suneel Garapati3981cdd2020-09-23 11:01:31 +0200142 .remove = octeontx_wdt_remove,
143 .flags = DM_FLAG_OS_PREPARE,
Suneel Garapatiaf6ba902019-10-21 16:09:36 -0700144};