xref: /linux/drivers/watchdog/loongson1_wdt.c (revision cbac924200b838cfb8d8b1415113d788089dc50b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/watchdog.h>
10 #include <loongson1.h>
11 
12 #define DEFAULT_HEARTBEAT	30
13 
14 static bool nowayout = WATCHDOG_NOWAYOUT;
15 module_param(nowayout, bool, 0444);
16 
17 static unsigned int heartbeat;
18 module_param(heartbeat, uint, 0444);
19 
20 struct ls1x_wdt_drvdata {
21 	void __iomem *base;
22 	struct clk *clk;
23 	unsigned long clk_rate;
24 	struct watchdog_device wdt;
25 };
26 
27 static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
28 {
29 	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
30 
31 	writel(0x1, drvdata->base + WDT_SET);
32 
33 	return 0;
34 }
35 
36 static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
37 				unsigned int timeout)
38 {
39 	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
40 	unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
41 	unsigned int counts;
42 
43 	wdt_dev->timeout = timeout;
44 
45 	counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
46 	writel(counts, drvdata->base + WDT_TIMER);
47 
48 	return 0;
49 }
50 
51 static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
52 {
53 	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
54 
55 	writel(0x1, drvdata->base + WDT_EN);
56 
57 	return 0;
58 }
59 
60 static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
61 {
62 	struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
63 
64 	writel(0x0, drvdata->base + WDT_EN);
65 
66 	return 0;
67 }
68 
69 static const struct watchdog_info ls1x_wdt_info = {
70 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
71 	.identity = "Loongson1 Watchdog",
72 };
73 
74 static const struct watchdog_ops ls1x_wdt_ops = {
75 	.owner = THIS_MODULE,
76 	.start = ls1x_wdt_start,
77 	.stop = ls1x_wdt_stop,
78 	.ping = ls1x_wdt_ping,
79 	.set_timeout = ls1x_wdt_set_timeout,
80 };
81 
82 static void ls1x_clk_disable_unprepare(void *data)
83 {
84 	clk_disable_unprepare(data);
85 }
86 
87 static int ls1x_wdt_probe(struct platform_device *pdev)
88 {
89 	struct device *dev = &pdev->dev;
90 	struct ls1x_wdt_drvdata *drvdata;
91 	struct watchdog_device *ls1x_wdt;
92 	unsigned long clk_rate;
93 	int err;
94 
95 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
96 	if (!drvdata)
97 		return -ENOMEM;
98 
99 	drvdata->base = devm_platform_ioremap_resource(pdev, 0);
100 	if (IS_ERR(drvdata->base))
101 		return PTR_ERR(drvdata->base);
102 
103 	drvdata->clk = devm_clk_get(dev, pdev->name);
104 	if (IS_ERR(drvdata->clk))
105 		return PTR_ERR(drvdata->clk);
106 
107 	err = clk_prepare_enable(drvdata->clk);
108 	if (err) {
109 		dev_err(dev, "clk enable failed\n");
110 		return err;
111 	}
112 	err = devm_add_action_or_reset(dev, ls1x_clk_disable_unprepare,
113 				       drvdata->clk);
114 	if (err)
115 		return err;
116 
117 	clk_rate = clk_get_rate(drvdata->clk);
118 	if (!clk_rate)
119 		return -EINVAL;
120 	drvdata->clk_rate = clk_rate;
121 
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;
129 
130 	watchdog_init_timeout(ls1x_wdt, heartbeat, dev);
131 	watchdog_set_nowayout(ls1x_wdt, nowayout);
132 	watchdog_set_drvdata(ls1x_wdt, drvdata);
133 
134 	err = devm_watchdog_register_device(dev, &drvdata->wdt);
135 	if (err)
136 		return err;
137 
138 	platform_set_drvdata(pdev, drvdata);
139 
140 	dev_info(dev, "Loongson1 Watchdog driver registered\n");
141 
142 	return 0;
143 }
144 
145 static struct platform_driver ls1x_wdt_driver = {
146 	.probe = ls1x_wdt_probe,
147 	.driver = {
148 		.name = "ls1x-wdt",
149 	},
150 };
151 
152 module_platform_driver(ls1x_wdt_driver);
153 
154 MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
155 MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
156 MODULE_LICENSE("GPL");
157