1 // SPDX-License-Identifier: GPL-2.0-only
3 * MOXA ART SoCs watchdog driver.
5 * Copyright (C) 2013 Jonas Jensen
7 * Jonas Jensen <jonas.jensen@gmail.com>
11 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
19 #include <linux/moduleparam.h>
23 #define REG_ENABLE 0xC
25 struct moxart_wdt_dev {
26 struct watchdog_device dev;
28 unsigned int clock_frequency;
33 static int moxart_wdt_restart(struct watchdog_device *wdt_dev,
34 unsigned long action, void *data)
36 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
38 writel(1, moxart_wdt->base + REG_COUNT);
39 writel(0x5ab9, moxart_wdt->base + REG_MODE);
40 writel(0x03, moxart_wdt->base + REG_ENABLE);
45 static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
47 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
49 writel(0, moxart_wdt->base + REG_ENABLE);
54 static int moxart_wdt_start(struct watchdog_device *wdt_dev)
56 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
58 writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
59 moxart_wdt->base + REG_COUNT);
60 writel(0x5ab9, moxart_wdt->base + REG_MODE);
61 writel(0x03, moxart_wdt->base + REG_ENABLE);
66 static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
69 wdt_dev->timeout = timeout;
74 static const struct watchdog_info moxart_wdt_info = {
75 .identity = "moxart-wdt",
76 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
80 static const struct watchdog_ops moxart_wdt_ops = {
82 .start = moxart_wdt_start,
83 .stop = moxart_wdt_stop,
84 .set_timeout = moxart_wdt_set_timeout,
85 .restart = moxart_wdt_restart,
88 static int moxart_wdt_probe(struct platform_device *pdev)
90 struct moxart_wdt_dev *moxart_wdt;
91 struct device *dev = &pdev->dev;
94 unsigned int max_timeout;
95 bool nowayout = WATCHDOG_NOWAYOUT;
97 moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
101 platform_set_drvdata(pdev, moxart_wdt);
103 moxart_wdt->base = devm_platform_ioremap_resource(pdev, 0);
104 if (IS_ERR(moxart_wdt->base))
105 return PTR_ERR(moxart_wdt->base);
107 clk = devm_clk_get(dev, NULL);
109 pr_err("%s: of_clk_get failed\n", __func__);
113 moxart_wdt->clock_frequency = clk_get_rate(clk);
114 if (moxart_wdt->clock_frequency == 0) {
115 pr_err("%s: incorrect clock frequency\n", __func__);
119 max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
121 moxart_wdt->dev.info = &moxart_wdt_info;
122 moxart_wdt->dev.ops = &moxart_wdt_ops;
123 moxart_wdt->dev.timeout = max_timeout;
124 moxart_wdt->dev.min_timeout = 1;
125 moxart_wdt->dev.max_timeout = max_timeout;
126 moxart_wdt->dev.parent = dev;
128 watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
129 watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
130 watchdog_set_restart_priority(&moxart_wdt->dev, 128);
132 watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
134 watchdog_stop_on_unregister(&moxart_wdt->dev);
135 err = devm_watchdog_register_device(dev, &moxart_wdt->dev);
139 dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
140 moxart_wdt->dev.timeout, nowayout);
145 static const struct of_device_id moxart_watchdog_match[] = {
146 { .compatible = "moxa,moxart-watchdog" },
149 MODULE_DEVICE_TABLE(of, moxart_watchdog_match);
151 static struct platform_driver moxart_wdt_driver = {
152 .probe = moxart_wdt_probe,
154 .name = "moxart-watchdog",
155 .of_match_table = moxart_watchdog_match,
158 module_platform_driver(moxart_wdt_driver);
160 module_param(heartbeat, int, 0);
161 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
163 MODULE_DESCRIPTION("MOXART watchdog driver");
164 MODULE_LICENSE("GPL");
165 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");