blob: acaaa0005d5b28ae85382dcd6b9c729205963ead [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Justin Chen7a3629f2015-08-31 11:02:43 -07002/*
3 * Copyright (C) 2015 Broadcom Corporation
4 *
Justin Chen7a3629f2015-08-31 11:02:43 -07005 */
6
7#include <linux/clk.h>
8#include <linux/init.h>
9#include <linux/io.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/platform_device.h>
13#include <linux/pm.h>
14#include <linux/watchdog.h>
15
16#define WDT_START_1 0xff00
17#define WDT_START_2 0x00ff
18#define WDT_STOP_1 0xee00
19#define WDT_STOP_2 0x00ee
20
21#define WDT_TIMEOUT_REG 0x0
22#define WDT_CMD_REG 0x4
23
24#define WDT_MIN_TIMEOUT 1 /* seconds */
25#define WDT_DEFAULT_TIMEOUT 30 /* seconds */
26#define WDT_DEFAULT_RATE 27000000
27
28struct bcm7038_watchdog {
29 void __iomem *base;
30 struct watchdog_device wdd;
31 u32 rate;
32 struct clk *clk;
33};
34
35static bool nowayout = WATCHDOG_NOWAYOUT;
36
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010037static inline void bcm7038_wdt_write(u32 value, void __iomem *addr)
38{
39 /* MIPS chips strapped for BE will automagically configure the
40 * peripheral registers for CPU-native byte order.
41 */
42 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
43 __raw_writel(value, addr);
44 else
45 writel_relaxed(value, addr);
46}
47
48static inline u32 bcm7038_wdt_read(void __iomem *addr)
49{
50 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
51 return __raw_readl(addr);
52 else
53 return readl_relaxed(addr);
54}
55
Justin Chen7a3629f2015-08-31 11:02:43 -070056static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
57{
58 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
59 u32 timeout;
60
61 timeout = wdt->rate * wdog->timeout;
62
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010063 bcm7038_wdt_write(timeout, wdt->base + WDT_TIMEOUT_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -070064}
65
66static int bcm7038_wdt_ping(struct watchdog_device *wdog)
67{
68 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
69
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010070 bcm7038_wdt_write(WDT_START_1, wdt->base + WDT_CMD_REG);
71 bcm7038_wdt_write(WDT_START_2, wdt->base + WDT_CMD_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -070072
73 return 0;
74}
75
76static int bcm7038_wdt_start(struct watchdog_device *wdog)
77{
78 bcm7038_wdt_set_timeout_reg(wdog);
79 bcm7038_wdt_ping(wdog);
80
81 return 0;
82}
83
84static int bcm7038_wdt_stop(struct watchdog_device *wdog)
85{
86 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
87
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +010088 bcm7038_wdt_write(WDT_STOP_1, wdt->base + WDT_CMD_REG);
89 bcm7038_wdt_write(WDT_STOP_2, wdt->base + WDT_CMD_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -070090
91 return 0;
92}
93
94static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
95 unsigned int t)
96{
97 /* Can't modify timeout value if watchdog timer is running */
98 bcm7038_wdt_stop(wdog);
99 wdog->timeout = t;
100 bcm7038_wdt_start(wdog);
101
102 return 0;
103}
104
105static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
106{
107 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
108 u32 time_left;
109
Álvaro Fernández Rojase379c212021-02-23 09:00:42 +0100110 time_left = bcm7038_wdt_read(wdt->base + WDT_CMD_REG);
Justin Chen7a3629f2015-08-31 11:02:43 -0700111
112 return time_left / wdt->rate;
113}
114
Bhumika Goyal6c368932016-12-26 22:35:11 +0530115static const struct watchdog_info bcm7038_wdt_info = {
Justin Chen7a3629f2015-08-31 11:02:43 -0700116 .identity = "Broadcom BCM7038 Watchdog Timer",
117 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
118 WDIOF_MAGICCLOSE
119};
120
Julia Lawall85f15cf2016-09-01 19:35:26 +0200121static const struct watchdog_ops bcm7038_wdt_ops = {
Justin Chen7a3629f2015-08-31 11:02:43 -0700122 .owner = THIS_MODULE,
123 .start = bcm7038_wdt_start,
124 .stop = bcm7038_wdt_stop,
125 .set_timeout = bcm7038_wdt_set_timeout,
126 .get_timeleft = bcm7038_wdt_get_timeleft,
127};
128
Guenter Roeck69656dc2019-04-08 12:38:30 -0700129static void bcm7038_clk_disable_unprepare(void *data)
130{
131 clk_disable_unprepare(data);
132}
133
Justin Chen7a3629f2015-08-31 11:02:43 -0700134static int bcm7038_wdt_probe(struct platform_device *pdev)
135{
136 struct device *dev = &pdev->dev;
137 struct bcm7038_watchdog *wdt;
Justin Chen7a3629f2015-08-31 11:02:43 -0700138 int err;
139
140 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
141 if (!wdt)
142 return -ENOMEM;
143
144 platform_set_drvdata(pdev, wdt);
145
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700146 wdt->base = devm_platform_ioremap_resource(pdev, 0);
Justin Chen7a3629f2015-08-31 11:02:43 -0700147 if (IS_ERR(wdt->base))
148 return PTR_ERR(wdt->base);
149
150 wdt->clk = devm_clk_get(dev, NULL);
151 /* If unable to get clock, use default frequency */
152 if (!IS_ERR(wdt->clk)) {
Fabio Estevamdd0a18c2017-07-22 17:22:59 -0300153 err = clk_prepare_enable(wdt->clk);
154 if (err)
155 return err;
Guenter Roeck69656dc2019-04-08 12:38:30 -0700156 err = devm_add_action_or_reset(dev,
157 bcm7038_clk_disable_unprepare,
158 wdt->clk);
159 if (err)
160 return err;
Justin Chen7a3629f2015-08-31 11:02:43 -0700161 wdt->rate = clk_get_rate(wdt->clk);
162 /* Prevent divide-by-zero exception */
163 if (!wdt->rate)
164 wdt->rate = WDT_DEFAULT_RATE;
165 } else {
166 wdt->rate = WDT_DEFAULT_RATE;
167 wdt->clk = NULL;
168 }
169
170 wdt->wdd.info = &bcm7038_wdt_info;
171 wdt->wdd.ops = &bcm7038_wdt_ops;
172 wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
173 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
174 wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
175 wdt->wdd.parent = dev;
176 watchdog_set_drvdata(&wdt->wdd, wdt);
177
Guenter Roeck69656dc2019-04-08 12:38:30 -0700178 watchdog_stop_on_reboot(&wdt->wdd);
179 watchdog_stop_on_unregister(&wdt->wdd);
180 err = devm_watchdog_register_device(dev, &wdt->wdd);
Wolfram Sangf5429892019-05-18 23:27:20 +0200181 if (err)
Justin Chen7a3629f2015-08-31 11:02:43 -0700182 return err;
Justin Chen7a3629f2015-08-31 11:02:43 -0700183
184 dev_info(dev, "Registered BCM7038 Watchdog\n");
185
186 return 0;
187}
188
Justin Chen7a3629f2015-08-31 11:02:43 -0700189#ifdef CONFIG_PM_SLEEP
190static int bcm7038_wdt_suspend(struct device *dev)
191{
192 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
193
194 if (watchdog_active(&wdt->wdd))
195 return bcm7038_wdt_stop(&wdt->wdd);
196
197 return 0;
198}
199
200static int bcm7038_wdt_resume(struct device *dev)
201{
202 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
203
204 if (watchdog_active(&wdt->wdd))
205 return bcm7038_wdt_start(&wdt->wdd);
206
207 return 0;
208}
209#endif
210
211static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
212 bcm7038_wdt_resume);
213
Justin Chen7a3629f2015-08-31 11:02:43 -0700214static const struct of_device_id bcm7038_wdt_match[] = {
215 { .compatible = "brcm,bcm7038-wdt" },
216 {},
217};
Javier Martinez Canillas57d77c62016-10-14 12:23:50 -0300218MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
Justin Chen7a3629f2015-08-31 11:02:43 -0700219
220static struct platform_driver bcm7038_wdt_driver = {
221 .probe = bcm7038_wdt_probe,
Justin Chen7a3629f2015-08-31 11:02:43 -0700222 .driver = {
223 .name = "bcm7038-wdt",
224 .of_match_table = bcm7038_wdt_match,
225 .pm = &bcm7038_wdt_pm_ops,
226 }
227};
228module_platform_driver(bcm7038_wdt_driver);
229
230module_param(nowayout, bool, 0);
231MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
232 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Marcus Folkesson2e62c492018-03-16 16:14:11 +0100233MODULE_LICENSE("GPL");
Justin Chen7a3629f2015-08-31 11:02:43 -0700234MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
235MODULE_AUTHOR("Justin Chen");