blob: d3d88f6703d79ea8daaa685d8f10d5668c143aa1 [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
37static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
38{
39 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
40 u32 timeout;
41
42 timeout = wdt->rate * wdog->timeout;
43
44 writel(timeout, wdt->base + WDT_TIMEOUT_REG);
45}
46
47static int bcm7038_wdt_ping(struct watchdog_device *wdog)
48{
49 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
50
51 writel(WDT_START_1, wdt->base + WDT_CMD_REG);
52 writel(WDT_START_2, wdt->base + WDT_CMD_REG);
53
54 return 0;
55}
56
57static int bcm7038_wdt_start(struct watchdog_device *wdog)
58{
59 bcm7038_wdt_set_timeout_reg(wdog);
60 bcm7038_wdt_ping(wdog);
61
62 return 0;
63}
64
65static int bcm7038_wdt_stop(struct watchdog_device *wdog)
66{
67 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
68
69 writel(WDT_STOP_1, wdt->base + WDT_CMD_REG);
70 writel(WDT_STOP_2, wdt->base + WDT_CMD_REG);
71
72 return 0;
73}
74
75static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
76 unsigned int t)
77{
78 /* Can't modify timeout value if watchdog timer is running */
79 bcm7038_wdt_stop(wdog);
80 wdog->timeout = t;
81 bcm7038_wdt_start(wdog);
82
83 return 0;
84}
85
86static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
87{
88 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
89 u32 time_left;
90
91 time_left = readl(wdt->base + WDT_CMD_REG);
92
93 return time_left / wdt->rate;
94}
95
Bhumika Goyal6c368932016-12-26 22:35:11 +053096static const struct watchdog_info bcm7038_wdt_info = {
Justin Chen7a3629f2015-08-31 11:02:43 -070097 .identity = "Broadcom BCM7038 Watchdog Timer",
98 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
99 WDIOF_MAGICCLOSE
100};
101
Julia Lawall85f15cf2016-09-01 19:35:26 +0200102static const struct watchdog_ops bcm7038_wdt_ops = {
Justin Chen7a3629f2015-08-31 11:02:43 -0700103 .owner = THIS_MODULE,
104 .start = bcm7038_wdt_start,
105 .stop = bcm7038_wdt_stop,
106 .set_timeout = bcm7038_wdt_set_timeout,
107 .get_timeleft = bcm7038_wdt_get_timeleft,
108};
109
Guenter Roeck69656dc2019-04-08 12:38:30 -0700110static void bcm7038_clk_disable_unprepare(void *data)
111{
112 clk_disable_unprepare(data);
113}
114
Justin Chen7a3629f2015-08-31 11:02:43 -0700115static int bcm7038_wdt_probe(struct platform_device *pdev)
116{
117 struct device *dev = &pdev->dev;
118 struct bcm7038_watchdog *wdt;
Justin Chen7a3629f2015-08-31 11:02:43 -0700119 int err;
120
121 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
122 if (!wdt)
123 return -ENOMEM;
124
125 platform_set_drvdata(pdev, wdt);
126
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700127 wdt->base = devm_platform_ioremap_resource(pdev, 0);
Justin Chen7a3629f2015-08-31 11:02:43 -0700128 if (IS_ERR(wdt->base))
129 return PTR_ERR(wdt->base);
130
131 wdt->clk = devm_clk_get(dev, NULL);
132 /* If unable to get clock, use default frequency */
133 if (!IS_ERR(wdt->clk)) {
Fabio Estevamdd0a18c2017-07-22 17:22:59 -0300134 err = clk_prepare_enable(wdt->clk);
135 if (err)
136 return err;
Guenter Roeck69656dc2019-04-08 12:38:30 -0700137 err = devm_add_action_or_reset(dev,
138 bcm7038_clk_disable_unprepare,
139 wdt->clk);
140 if (err)
141 return err;
Justin Chen7a3629f2015-08-31 11:02:43 -0700142 wdt->rate = clk_get_rate(wdt->clk);
143 /* Prevent divide-by-zero exception */
144 if (!wdt->rate)
145 wdt->rate = WDT_DEFAULT_RATE;
146 } else {
147 wdt->rate = WDT_DEFAULT_RATE;
148 wdt->clk = NULL;
149 }
150
151 wdt->wdd.info = &bcm7038_wdt_info;
152 wdt->wdd.ops = &bcm7038_wdt_ops;
153 wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
154 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
155 wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
156 wdt->wdd.parent = dev;
157 watchdog_set_drvdata(&wdt->wdd, wdt);
158
Guenter Roeck69656dc2019-04-08 12:38:30 -0700159 watchdog_stop_on_reboot(&wdt->wdd);
160 watchdog_stop_on_unregister(&wdt->wdd);
161 err = devm_watchdog_register_device(dev, &wdt->wdd);
Justin Chen7a3629f2015-08-31 11:02:43 -0700162 if (err) {
163 dev_err(dev, "Failed to register watchdog device\n");
Justin Chen7a3629f2015-08-31 11:02:43 -0700164 return err;
165 }
166
167 dev_info(dev, "Registered BCM7038 Watchdog\n");
168
169 return 0;
170}
171
Justin Chen7a3629f2015-08-31 11:02:43 -0700172#ifdef CONFIG_PM_SLEEP
173static int bcm7038_wdt_suspend(struct device *dev)
174{
175 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
176
177 if (watchdog_active(&wdt->wdd))
178 return bcm7038_wdt_stop(&wdt->wdd);
179
180 return 0;
181}
182
183static int bcm7038_wdt_resume(struct device *dev)
184{
185 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
186
187 if (watchdog_active(&wdt->wdd))
188 return bcm7038_wdt_start(&wdt->wdd);
189
190 return 0;
191}
192#endif
193
194static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
195 bcm7038_wdt_resume);
196
Justin Chen7a3629f2015-08-31 11:02:43 -0700197static const struct of_device_id bcm7038_wdt_match[] = {
198 { .compatible = "brcm,bcm7038-wdt" },
199 {},
200};
Javier Martinez Canillas57d77c62016-10-14 12:23:50 -0300201MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
Justin Chen7a3629f2015-08-31 11:02:43 -0700202
203static struct platform_driver bcm7038_wdt_driver = {
204 .probe = bcm7038_wdt_probe,
Justin Chen7a3629f2015-08-31 11:02:43 -0700205 .driver = {
206 .name = "bcm7038-wdt",
207 .of_match_table = bcm7038_wdt_match,
208 .pm = &bcm7038_wdt_pm_ops,
209 }
210};
211module_platform_driver(bcm7038_wdt_driver);
212
213module_param(nowayout, bool, 0);
214MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
215 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Marcus Folkesson2e62c492018-03-16 16:14:11 +0100216MODULE_LICENSE("GPL");
Justin Chen7a3629f2015-08-31 11:02:43 -0700217MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
218MODULE_AUTHOR("Justin Chen");