1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/watchdog.h>
10 #include <loongson1.h>
12 #define DEFAULT_HEARTBEAT 30
14 static bool nowayout = WATCHDOG_NOWAYOUT;
15 module_param(nowayout, bool, 0444);
17 static unsigned int heartbeat;
18 module_param(heartbeat, uint, 0444);
20 struct ls1x_wdt_drvdata {
23 unsigned long clk_rate;
24 struct watchdog_device wdt;
27 static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
29 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
31 writel(0x1, drvdata->base + WDT_SET);
36 static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
39 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
40 unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
43 wdt_dev->timeout = timeout;
45 counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
46 writel(counts, drvdata->base + WDT_TIMER);
51 static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
53 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
55 writel(0x1, drvdata->base + WDT_EN);
60 static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
62 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
64 writel(0x0, drvdata->base + WDT_EN);
69 static const struct watchdog_info ls1x_wdt_info = {
70 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
71 .identity = "Loongson1 Watchdog",
74 static const struct watchdog_ops ls1x_wdt_ops = {
76 .start = ls1x_wdt_start,
77 .stop = ls1x_wdt_stop,
78 .ping = ls1x_wdt_ping,
79 .set_timeout = ls1x_wdt_set_timeout,
82 static void ls1x_clk_disable_unprepare(void *data)
84 clk_disable_unprepare(data);
87 static int ls1x_wdt_probe(struct platform_device *pdev)
89 struct device *dev = &pdev->dev;
90 struct ls1x_wdt_drvdata *drvdata;
91 struct watchdog_device *ls1x_wdt;
92 unsigned long clk_rate;
95 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
99 drvdata->base = devm_platform_ioremap_resource(pdev, 0);
100 if (IS_ERR(drvdata->base))
101 return PTR_ERR(drvdata->base);
103 drvdata->clk = devm_clk_get(dev, pdev->name);
104 if (IS_ERR(drvdata->clk))
105 return PTR_ERR(drvdata->clk);
107 err = clk_prepare_enable(drvdata->clk);
109 dev_err(dev, "clk enable failed\n");
112 err = devm_add_action_or_reset(dev, ls1x_clk_disable_unprepare,
117 clk_rate = clk_get_rate(drvdata->clk);
120 drvdata->clk_rate = clk_rate;
122 ls1x_wdt = &drvdata->wdt;
123 ls1x_wdt->info = &ls1x_wdt_info;
124 ls1x_wdt->ops = &ls1x_wdt_ops;
125 ls1x_wdt->timeout = DEFAULT_HEARTBEAT;
126 ls1x_wdt->min_timeout = 1;
127 ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000;
128 ls1x_wdt->parent = dev;
130 watchdog_init_timeout(ls1x_wdt, heartbeat, dev);
131 watchdog_set_nowayout(ls1x_wdt, nowayout);
132 watchdog_set_drvdata(ls1x_wdt, drvdata);
134 err = devm_watchdog_register_device(dev, &drvdata->wdt);
138 platform_set_drvdata(pdev, drvdata);
140 dev_info(dev, "Loongson1 Watchdog driver registered\n");
145 static struct platform_driver ls1x_wdt_driver = {
146 .probe = ls1x_wdt_probe,
152 module_platform_driver(ls1x_wdt_driver);
154 MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
155 MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
156 MODULE_LICENSE("GPL");