blob: d5fcce062920e0d1739e85b2956bd9d30b436daa [file] [log] [blame]
Mans Rullgarddca536c2015-11-19 22:09:05 +00001/*
2 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
3 * SMP86xx/SMP87xx Watchdog driver
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 */
10
11#include <linux/bitops.h>
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
Mans Rullgarddca536c2015-11-19 22:09:05 +000018#include <linux/platform_device.h>
Mans Rullgarddca536c2015-11-19 22:09:05 +000019#include <linux/watchdog.h>
20
21#define DEFAULT_TIMEOUT 30
22
23static bool nowayout = WATCHDOG_NOWAYOUT;
24module_param(nowayout, bool, 0);
25MODULE_PARM_DESC(nowayout,
26 "Watchdog cannot be stopped once started (default="
27 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
28
29static unsigned int timeout;
30module_param(timeout, int, 0);
31MODULE_PARM_DESC(timeout, "Watchdog timeout");
32
33/*
34 * Counter counts down from programmed value. Reset asserts when
35 * the counter reaches 1.
36 */
37#define WD_COUNTER 0
38
39#define WD_CONFIG 4
40#define WD_CONFIG_XTAL_IN BIT(0)
41#define WD_CONFIG_DISABLE BIT(31)
42
43struct tangox_wdt_device {
44 struct watchdog_device wdt;
45 void __iomem *base;
46 unsigned long clk_rate;
47 struct clk *clk;
Mans Rullgarddca536c2015-11-19 22:09:05 +000048};
49
50static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
51 unsigned int new_timeout)
52{
53 wdt->timeout = new_timeout;
54
55 return 0;
56}
57
58static int tangox_wdt_start(struct watchdog_device *wdt)
59{
60 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
61 u32 ticks;
62
63 ticks = 1 + wdt->timeout * dev->clk_rate;
64 writel(ticks, dev->base + WD_COUNTER);
65
66 return 0;
67}
68
69static int tangox_wdt_stop(struct watchdog_device *wdt)
70{
71 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
72
73 writel(0, dev->base + WD_COUNTER);
74
75 return 0;
76}
77
78static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
79{
80 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
81 u32 count;
82
83 count = readl(dev->base + WD_COUNTER);
84
85 if (!count)
86 return 0;
87
88 return (count - 1) / dev->clk_rate;
89}
90
91static const struct watchdog_info tangox_wdt_info = {
92 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
93 .identity = "tangox watchdog",
94};
95
Guenter Roeck0397c5d2017-01-04 12:28:01 -080096static int tangox_wdt_restart(struct watchdog_device *wdt,
97 unsigned long action, void *data)
98{
99 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
100
101 writel(1, dev->base + WD_COUNTER);
102
103 return 0;
104}
105
Mans Rullgarddca536c2015-11-19 22:09:05 +0000106static const struct watchdog_ops tangox_wdt_ops = {
107 .start = tangox_wdt_start,
108 .stop = tangox_wdt_stop,
109 .set_timeout = tangox_wdt_set_timeout,
110 .get_timeleft = tangox_wdt_get_timeleft,
Guenter Roeck0397c5d2017-01-04 12:28:01 -0800111 .restart = tangox_wdt_restart,
Mans Rullgarddca536c2015-11-19 22:09:05 +0000112};
113
Mans Rullgarddca536c2015-11-19 22:09:05 +0000114static int tangox_wdt_probe(struct platform_device *pdev)
115{
116 struct tangox_wdt_device *dev;
117 struct resource *res;
118 u32 config;
119 int err;
120
121 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
122 if (!dev)
123 return -ENOMEM;
124
125 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
126 dev->base = devm_ioremap_resource(&pdev->dev, res);
127 if (IS_ERR(dev->base))
128 return PTR_ERR(dev->base);
129
130 dev->clk = devm_clk_get(&pdev->dev, NULL);
131 if (IS_ERR(dev->clk))
132 return PTR_ERR(dev->clk);
133
134 err = clk_prepare_enable(dev->clk);
135 if (err)
136 return err;
137
138 dev->clk_rate = clk_get_rate(dev->clk);
Wolfram Sang84b84bc2016-03-03 09:24:12 +0100139 if (!dev->clk_rate) {
140 err = -EINVAL;
141 goto err;
142 }
Mans Rullgarddca536c2015-11-19 22:09:05 +0000143
144 dev->wdt.parent = &pdev->dev;
145 dev->wdt.info = &tangox_wdt_info;
146 dev->wdt.ops = &tangox_wdt_ops;
147 dev->wdt.timeout = DEFAULT_TIMEOUT;
148 dev->wdt.min_timeout = 1;
Guenter Roeckc7ef68c32016-07-17 13:47:47 -0700149 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
Mans Rullgarddca536c2015-11-19 22:09:05 +0000150
151 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
152 watchdog_set_nowayout(&dev->wdt, nowayout);
153 watchdog_set_drvdata(&dev->wdt, dev);
154
155 /*
156 * Deactivate counter if disable bit is set to avoid
157 * accidental reset.
158 */
159 config = readl(dev->base + WD_CONFIG);
160 if (config & WD_CONFIG_DISABLE)
161 writel(0, dev->base + WD_COUNTER);
162
163 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
164
165 /*
166 * Mark as active and restart with configured timeout if
167 * already running.
168 */
169 if (readl(dev->base + WD_COUNTER)) {
Guenter Roecka3e376d2016-05-27 15:28:00 -0700170 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
Mans Rullgarddca536c2015-11-19 22:09:05 +0000171 tangox_wdt_start(&dev->wdt);
172 }
173
Guenter Roeck0397c5d2017-01-04 12:28:01 -0800174 watchdog_set_restart_priority(&dev->wdt, 128);
175
Mans Rullgarddca536c2015-11-19 22:09:05 +0000176 err = watchdog_register_device(&dev->wdt);
Wolfram Sang84b84bc2016-03-03 09:24:12 +0100177 if (err)
178 goto err;
Mans Rullgarddca536c2015-11-19 22:09:05 +0000179
180 platform_set_drvdata(pdev, dev);
181
Guenter Roeck3d29f802015-12-24 14:22:01 -0800182 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
Mans Rullgarddca536c2015-11-19 22:09:05 +0000183
184 return 0;
Wolfram Sang84b84bc2016-03-03 09:24:12 +0100185
186 err:
187 clk_disable_unprepare(dev->clk);
188 return err;
Mans Rullgarddca536c2015-11-19 22:09:05 +0000189}
190
191static int tangox_wdt_remove(struct platform_device *pdev)
192{
193 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
194
195 tangox_wdt_stop(&dev->wdt);
196 clk_disable_unprepare(dev->clk);
197
Mans Rullgarddca536c2015-11-19 22:09:05 +0000198 watchdog_unregister_device(&dev->wdt);
199
200 return 0;
201}
202
203static const struct of_device_id tangox_wdt_dt_ids[] = {
204 { .compatible = "sigma,smp8642-wdt" },
205 { .compatible = "sigma,smp8759-wdt" },
206 { }
207};
208MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
209
210static struct platform_driver tangox_wdt_driver = {
211 .probe = tangox_wdt_probe,
212 .remove = tangox_wdt_remove,
213 .driver = {
214 .name = "tangox-wdt",
215 .of_match_table = tangox_wdt_dt_ids,
216 },
217};
218
219module_platform_driver(tangox_wdt_driver);
220
221MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
222MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
223MODULE_LICENSE("GPL");