1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Broadcom Corporation
8 #include <linux/init.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
14 #include <linux/watchdog.h>
16 #define WDT_START_1 0xff00
17 #define WDT_START_2 0x00ff
18 #define WDT_STOP_1 0xee00
19 #define WDT_STOP_2 0x00ee
21 #define WDT_TIMEOUT_REG 0x0
22 #define WDT_CMD_REG 0x4
24 #define WDT_MIN_TIMEOUT 1 /* seconds */
25 #define WDT_DEFAULT_TIMEOUT 30 /* seconds */
26 #define WDT_DEFAULT_RATE 27000000
28 struct bcm7038_watchdog {
30 struct watchdog_device wdd;
35 static bool nowayout = WATCHDOG_NOWAYOUT;
37 static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
39 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
42 timeout = wdt->rate * wdog->timeout;
44 writel(timeout, wdt->base + WDT_TIMEOUT_REG);
47 static int bcm7038_wdt_ping(struct watchdog_device *wdog)
49 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
51 writel(WDT_START_1, wdt->base + WDT_CMD_REG);
52 writel(WDT_START_2, wdt->base + WDT_CMD_REG);
57 static int bcm7038_wdt_start(struct watchdog_device *wdog)
59 bcm7038_wdt_set_timeout_reg(wdog);
60 bcm7038_wdt_ping(wdog);
65 static int bcm7038_wdt_stop(struct watchdog_device *wdog)
67 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
69 writel(WDT_STOP_1, wdt->base + WDT_CMD_REG);
70 writel(WDT_STOP_2, wdt->base + WDT_CMD_REG);
75 static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
78 /* Can't modify timeout value if watchdog timer is running */
79 bcm7038_wdt_stop(wdog);
81 bcm7038_wdt_start(wdog);
86 static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
88 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
91 time_left = readl(wdt->base + WDT_CMD_REG);
93 return time_left / wdt->rate;
96 static const struct watchdog_info bcm7038_wdt_info = {
97 .identity = "Broadcom BCM7038 Watchdog Timer",
98 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
102 static const struct watchdog_ops bcm7038_wdt_ops = {
103 .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,
110 static void bcm7038_clk_disable_unprepare(void *data)
112 clk_disable_unprepare(data);
115 static int bcm7038_wdt_probe(struct platform_device *pdev)
117 struct device *dev = &pdev->dev;
118 struct bcm7038_watchdog *wdt;
121 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
125 platform_set_drvdata(pdev, wdt);
127 wdt->base = devm_platform_ioremap_resource(pdev, 0);
128 if (IS_ERR(wdt->base))
129 return PTR_ERR(wdt->base);
131 wdt->clk = devm_clk_get(dev, NULL);
132 /* If unable to get clock, use default frequency */
133 if (!IS_ERR(wdt->clk)) {
134 err = clk_prepare_enable(wdt->clk);
137 err = devm_add_action_or_reset(dev,
138 bcm7038_clk_disable_unprepare,
142 wdt->rate = clk_get_rate(wdt->clk);
143 /* Prevent divide-by-zero exception */
145 wdt->rate = WDT_DEFAULT_RATE;
147 wdt->rate = WDT_DEFAULT_RATE;
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);
159 watchdog_stop_on_reboot(&wdt->wdd);
160 watchdog_stop_on_unregister(&wdt->wdd);
161 err = devm_watchdog_register_device(dev, &wdt->wdd);
165 dev_info(dev, "Registered BCM7038 Watchdog\n");
170 #ifdef CONFIG_PM_SLEEP
171 static int bcm7038_wdt_suspend(struct device *dev)
173 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
175 if (watchdog_active(&wdt->wdd))
176 return bcm7038_wdt_stop(&wdt->wdd);
181 static int bcm7038_wdt_resume(struct device *dev)
183 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
185 if (watchdog_active(&wdt->wdd))
186 return bcm7038_wdt_start(&wdt->wdd);
192 static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
195 static const struct of_device_id bcm7038_wdt_match[] = {
196 { .compatible = "brcm,bcm7038-wdt" },
199 MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
201 static struct platform_driver bcm7038_wdt_driver = {
202 .probe = bcm7038_wdt_probe,
204 .name = "bcm7038-wdt",
205 .of_match_table = bcm7038_wdt_match,
206 .pm = &bcm7038_wdt_pm_ops,
209 module_platform_driver(bcm7038_wdt_driver);
211 module_param(nowayout, bool, 0);
212 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
213 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
214 MODULE_LICENSE("GPL");
215 MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
216 MODULE_AUTHOR("Justin Chen");