• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
4  *  SMP86xx/SMP87xx Watchdog driver
5  */
6 
7 #include <linux/bitops.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/watchdog.h>
17 
18 #define DEFAULT_TIMEOUT 30
19 
20 static bool nowayout = WATCHDOG_NOWAYOUT;
21 module_param(nowayout, bool, 0);
22 MODULE_PARM_DESC(nowayout,
23 		 "Watchdog cannot be stopped once started (default="
24 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
25 
26 static unsigned int timeout;
27 module_param(timeout, int, 0);
28 MODULE_PARM_DESC(timeout, "Watchdog timeout");
29 
30 /*
31  * Counter counts down from programmed value.  Reset asserts when
32  * the counter reaches 1.
33  */
34 #define WD_COUNTER		0
35 
36 #define WD_CONFIG		4
37 #define WD_CONFIG_XTAL_IN	BIT(0)
38 #define WD_CONFIG_DISABLE	BIT(31)
39 
40 struct tangox_wdt_device {
41 	struct watchdog_device wdt;
42 	void __iomem *base;
43 	unsigned long clk_rate;
44 	struct clk *clk;
45 };
46 
tangox_wdt_set_timeout(struct watchdog_device * wdt,unsigned int new_timeout)47 static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
48 				  unsigned int new_timeout)
49 {
50 	wdt->timeout = new_timeout;
51 
52 	return 0;
53 }
54 
tangox_wdt_start(struct watchdog_device * wdt)55 static int tangox_wdt_start(struct watchdog_device *wdt)
56 {
57 	struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
58 	u32 ticks;
59 
60 	ticks = 1 + wdt->timeout * dev->clk_rate;
61 	writel(ticks, dev->base + WD_COUNTER);
62 
63 	return 0;
64 }
65 
tangox_wdt_stop(struct watchdog_device * wdt)66 static int tangox_wdt_stop(struct watchdog_device *wdt)
67 {
68 	struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
69 
70 	writel(0, dev->base + WD_COUNTER);
71 
72 	return 0;
73 }
74 
tangox_wdt_get_timeleft(struct watchdog_device * wdt)75 static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
76 {
77 	struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
78 	u32 count;
79 
80 	count = readl(dev->base + WD_COUNTER);
81 
82 	if (!count)
83 		return 0;
84 
85 	return (count - 1) / dev->clk_rate;
86 }
87 
88 static const struct watchdog_info tangox_wdt_info = {
89 	.options  = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
90 	.identity = "tangox watchdog",
91 };
92 
tangox_wdt_restart(struct watchdog_device * wdt,unsigned long action,void * data)93 static int tangox_wdt_restart(struct watchdog_device *wdt,
94 			      unsigned long action, void *data)
95 {
96 	struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
97 
98 	writel(1, dev->base + WD_COUNTER);
99 
100 	return 0;
101 }
102 
103 static const struct watchdog_ops tangox_wdt_ops = {
104 	.start		= tangox_wdt_start,
105 	.stop		= tangox_wdt_stop,
106 	.set_timeout	= tangox_wdt_set_timeout,
107 	.get_timeleft	= tangox_wdt_get_timeleft,
108 	.restart	= tangox_wdt_restart,
109 };
110 
tangox_wdt_probe(struct platform_device * pdev)111 static int tangox_wdt_probe(struct platform_device *pdev)
112 {
113 	struct tangox_wdt_device *dev;
114 	struct resource *res;
115 	u32 config;
116 	int err;
117 
118 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
119 	if (!dev)
120 		return -ENOMEM;
121 
122 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123 	dev->base = devm_ioremap_resource(&pdev->dev, res);
124 	if (IS_ERR(dev->base))
125 		return PTR_ERR(dev->base);
126 
127 	dev->clk = devm_clk_get(&pdev->dev, NULL);
128 	if (IS_ERR(dev->clk))
129 		return PTR_ERR(dev->clk);
130 
131 	err = clk_prepare_enable(dev->clk);
132 	if (err)
133 		return err;
134 
135 	dev->clk_rate = clk_get_rate(dev->clk);
136 	if (!dev->clk_rate) {
137 		err = -EINVAL;
138 		goto err;
139 	}
140 
141 	dev->wdt.parent = &pdev->dev;
142 	dev->wdt.info = &tangox_wdt_info;
143 	dev->wdt.ops = &tangox_wdt_ops;
144 	dev->wdt.timeout = DEFAULT_TIMEOUT;
145 	dev->wdt.min_timeout = 1;
146 	dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
147 
148 	watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
149 	watchdog_set_nowayout(&dev->wdt, nowayout);
150 	watchdog_set_drvdata(&dev->wdt, dev);
151 
152 	/*
153 	 * Deactivate counter if disable bit is set to avoid
154 	 * accidental reset.
155 	 */
156 	config = readl(dev->base + WD_CONFIG);
157 	if (config & WD_CONFIG_DISABLE)
158 		writel(0, dev->base + WD_COUNTER);
159 
160 	writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
161 
162 	/*
163 	 * Mark as active and restart with configured timeout if
164 	 * already running.
165 	 */
166 	if (readl(dev->base + WD_COUNTER)) {
167 		set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
168 		tangox_wdt_start(&dev->wdt);
169 	}
170 
171 	watchdog_set_restart_priority(&dev->wdt, 128);
172 
173 	err = watchdog_register_device(&dev->wdt);
174 	if (err)
175 		goto err;
176 
177 	platform_set_drvdata(pdev, dev);
178 
179 	dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
180 
181 	return 0;
182 
183  err:
184 	clk_disable_unprepare(dev->clk);
185 	return err;
186 }
187 
tangox_wdt_remove(struct platform_device * pdev)188 static int tangox_wdt_remove(struct platform_device *pdev)
189 {
190 	struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
191 
192 	tangox_wdt_stop(&dev->wdt);
193 	clk_disable_unprepare(dev->clk);
194 
195 	watchdog_unregister_device(&dev->wdt);
196 
197 	return 0;
198 }
199 
200 static const struct of_device_id tangox_wdt_dt_ids[] = {
201 	{ .compatible = "sigma,smp8642-wdt" },
202 	{ .compatible = "sigma,smp8759-wdt" },
203 	{ }
204 };
205 MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
206 
207 static struct platform_driver tangox_wdt_driver = {
208 	.probe	= tangox_wdt_probe,
209 	.remove	= tangox_wdt_remove,
210 	.driver	= {
211 		.name		= "tangox-wdt",
212 		.of_match_table	= tangox_wdt_dt_ids,
213 	},
214 };
215 
216 module_platform_driver(tangox_wdt_driver);
217 
218 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
219 MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
220 MODULE_LICENSE("GPL");
221