1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
7 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/watchdog.h>
12 /* Loongson 1 Watchdog Register Definitions */
17 #define DEFAULT_HEARTBEAT 30
19 static bool nowayout = WATCHDOG_NOWAYOUT;
20 module_param(nowayout, bool, 0444);
22 static unsigned int heartbeat;
23 module_param(heartbeat, uint, 0444);
25 struct ls1x_wdt_drvdata {
28 unsigned long clk_rate;
29 struct watchdog_device wdt;
32 static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
34 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
36 writel(0x1, drvdata->base + WDT_SET);
41 static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
44 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
45 unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
48 wdt_dev->timeout = timeout;
50 counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
51 writel(counts, drvdata->base + WDT_TIMER);
56 static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
58 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
60 writel(0x1, drvdata->base + WDT_EN);
65 static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
67 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
69 writel(0x0, drvdata->base + WDT_EN);
74 static int ls1x_wdt_restart(struct watchdog_device *wdt_dev,
75 unsigned long action, void *data)
77 struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
79 writel(0x1, drvdata->base + WDT_EN);
80 writel(0x1, drvdata->base + WDT_TIMER);
81 writel(0x1, drvdata->base + WDT_SET);
86 static const struct watchdog_info ls1x_wdt_info = {
87 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
88 .identity = "Loongson1 Watchdog",
91 static const struct watchdog_ops ls1x_wdt_ops = {
93 .start = ls1x_wdt_start,
94 .stop = ls1x_wdt_stop,
95 .ping = ls1x_wdt_ping,
96 .set_timeout = ls1x_wdt_set_timeout,
97 .restart = ls1x_wdt_restart,
100 static int ls1x_wdt_probe(struct platform_device *pdev)
102 struct device *dev = &pdev->dev;
103 struct ls1x_wdt_drvdata *drvdata;
104 struct watchdog_device *ls1x_wdt;
105 unsigned long clk_rate;
108 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
112 drvdata->base = devm_platform_ioremap_resource(pdev, 0);
113 if (IS_ERR(drvdata->base))
114 return PTR_ERR(drvdata->base);
116 drvdata->clk = devm_clk_get_enabled(dev, NULL);
117 if (IS_ERR(drvdata->clk))
118 return PTR_ERR(drvdata->clk);
120 clk_rate = clk_get_rate(drvdata->clk);
123 drvdata->clk_rate = clk_rate;
125 ls1x_wdt = &drvdata->wdt;
126 ls1x_wdt->info = &ls1x_wdt_info;
127 ls1x_wdt->ops = &ls1x_wdt_ops;
128 ls1x_wdt->timeout = DEFAULT_HEARTBEAT;
129 ls1x_wdt->min_timeout = 1;
130 ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000;
131 ls1x_wdt->parent = dev;
133 watchdog_init_timeout(ls1x_wdt, heartbeat, dev);
134 watchdog_set_nowayout(ls1x_wdt, nowayout);
135 watchdog_set_drvdata(ls1x_wdt, drvdata);
137 err = devm_watchdog_register_device(dev, &drvdata->wdt);
141 platform_set_drvdata(pdev, drvdata);
143 dev_info(dev, "Loongson1 Watchdog driver registered\n");
149 static const struct of_device_id ls1x_wdt_dt_ids[] = {
150 { .compatible = "loongson,ls1b-wdt", },
151 { .compatible = "loongson,ls1c-wdt", },
154 MODULE_DEVICE_TABLE(of, ls1x_wdt_dt_ids);
157 static struct platform_driver ls1x_wdt_driver = {
158 .probe = ls1x_wdt_probe,
161 .of_match_table = of_match_ptr(ls1x_wdt_dt_ids),
165 module_platform_driver(ls1x_wdt_driver);
167 MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
168 MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
169 MODULE_LICENSE("GPL");