2 * drivers/watchdog/orion_wdt.c
4 * Watchdog driver for Orion/Kirkwood processors
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/watchdog.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
27 #include <linux/of_device.h>
29 /* RSTOUT mask register physical address for Orion5x, Kirkwood and Dove */
30 #define ORION_RSTOUT_MASK_OFFSET 0x20108
32 /* Internal registers can be configured at any 1 MiB aligned address */
33 #define INTERNAL_REGS_MASK ~(SZ_1M - 1)
36 * Watchdog timer block registers.
38 #define TIMER_CTRL 0x0000
40 #define WDT_MAX_CYCLE_COUNT 0xffffffff
42 static bool nowayout = WATCHDOG_NOWAYOUT;
43 static int heartbeat = -1; /* module parameter (seconds) */
45 struct orion_watchdog;
47 struct orion_watchdog_data {
48 int wdt_counter_offset;
50 int rstout_enable_bit;
51 int (*clock_init)(struct platform_device *,
52 struct orion_watchdog *);
55 struct orion_watchdog {
56 struct watchdog_device wdt;
59 unsigned long clk_rate;
61 const struct orion_watchdog_data *data;
64 static int orion_wdt_clock_init(struct platform_device *pdev,
65 struct orion_watchdog *dev)
69 dev->clk = devm_clk_get(&pdev->dev, NULL);
71 return PTR_ERR(dev->clk);
72 ret = clk_prepare_enable(dev->clk);
76 dev->clk_rate = clk_get_rate(dev->clk);
80 static int orion_wdt_ping(struct watchdog_device *wdt_dev)
82 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
83 /* Reload watchdog duration */
84 writel(dev->clk_rate * wdt_dev->timeout,
85 dev->reg + dev->data->wdt_counter_offset);
89 static int orion_wdt_start(struct watchdog_device *wdt_dev)
91 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
93 /* Set watchdog duration */
94 writel(dev->clk_rate * wdt_dev->timeout,
95 dev->reg + dev->data->wdt_counter_offset);
97 /* Enable watchdog timer */
98 atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit,
99 dev->data->wdt_enable_bit);
101 /* Enable reset on watchdog */
102 atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit,
103 dev->data->rstout_enable_bit);
108 static int orion_wdt_stop(struct watchdog_device *wdt_dev)
110 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
112 /* Disable reset on watchdog */
113 atomic_io_modify(dev->rstout, dev->data->rstout_enable_bit, 0);
115 /* Disable watchdog timer */
116 atomic_io_modify(dev->reg + TIMER_CTRL, dev->data->wdt_enable_bit, 0);
121 static int orion_wdt_enabled(struct orion_watchdog *dev)
123 bool enabled, running;
125 enabled = readl(dev->rstout) & dev->data->rstout_enable_bit;
126 running = readl(dev->reg + TIMER_CTRL) & dev->data->wdt_enable_bit;
128 return enabled && running;
131 static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
133 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
134 return readl(dev->reg + dev->data->wdt_counter_offset) / dev->clk_rate;
137 static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
138 unsigned int timeout)
140 wdt_dev->timeout = timeout;
144 static const struct watchdog_info orion_wdt_info = {
145 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
146 .identity = "Orion Watchdog",
149 static const struct watchdog_ops orion_wdt_ops = {
150 .owner = THIS_MODULE,
151 .start = orion_wdt_start,
152 .stop = orion_wdt_stop,
153 .ping = orion_wdt_ping,
154 .set_timeout = orion_wdt_set_timeout,
155 .get_timeleft = orion_wdt_get_timeleft,
158 static irqreturn_t orion_wdt_irq(int irq, void *devid)
160 panic("Watchdog Timeout");
165 * The original devicetree binding for this driver specified only
166 * one memory resource, so in order to keep DT backwards compatibility
167 * we try to fallback to a hardcoded register address, if the resource
168 * is missing from the devicetree.
170 static void __iomem *orion_wdt_ioremap_rstout(struct platform_device *pdev,
171 phys_addr_t internal_regs)
173 struct resource *res;
176 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
178 return devm_ioremap(&pdev->dev, res->start,
181 /* This workaround works only for "orion-wdt", DT-enabled */
182 if (!of_device_is_compatible(pdev->dev.of_node, "marvell,orion-wdt"))
185 rstout = internal_regs + ORION_RSTOUT_MASK_OFFSET;
187 WARN(1, FW_BUG "falling back to harcoded RSTOUT reg 0x%x\n", rstout);
188 return devm_ioremap(&pdev->dev, rstout, 0x4);
191 static const struct orion_watchdog_data orion_data = {
192 .rstout_enable_bit = BIT(1),
193 .wdt_enable_bit = BIT(4),
194 .wdt_counter_offset = 0x24,
195 .clock_init = orion_wdt_clock_init,
198 static const struct of_device_id orion_wdt_of_match_table[] = {
200 .compatible = "marvell,orion-wdt",
205 MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
207 static int orion_wdt_probe(struct platform_device *pdev)
209 struct orion_watchdog *dev;
210 const struct of_device_id *match;
211 unsigned int wdt_max_duration; /* (seconds) */
212 struct resource *res;
215 dev = devm_kzalloc(&pdev->dev, sizeof(struct orion_watchdog),
220 match = of_match_device(orion_wdt_of_match_table, &pdev->dev);
222 /* Default legacy match */
223 match = &orion_wdt_of_match_table[0];
225 dev->wdt.info = &orion_wdt_info;
226 dev->wdt.ops = &orion_wdt_ops;
227 dev->wdt.min_timeout = 1;
228 dev->data = match->data;
230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
234 dev->reg = devm_ioremap(&pdev->dev, res->start,
239 dev->rstout = orion_wdt_ioremap_rstout(pdev, res->start &
244 ret = dev->data->clock_init(pdev, dev);
246 dev_err(&pdev->dev, "cannot initialize clock\n");
250 wdt_max_duration = WDT_MAX_CYCLE_COUNT / dev->clk_rate;
252 dev->wdt.timeout = wdt_max_duration;
253 dev->wdt.max_timeout = wdt_max_duration;
254 watchdog_init_timeout(&dev->wdt, heartbeat, &pdev->dev);
256 platform_set_drvdata(pdev, &dev->wdt);
257 watchdog_set_drvdata(&dev->wdt, dev);
260 * Let's make sure the watchdog is fully stopped, unless it's
261 * explicitly enabled. This may be the case if the module was
262 * removed and re-insterted, or if the bootloader explicitly
263 * set a running watchdog before booting the kernel.
265 if (!orion_wdt_enabled(dev))
266 orion_wdt_stop(&dev->wdt);
268 /* Request the IRQ only after the watchdog is disabled */
269 irq = platform_get_irq(pdev, 0);
272 * Not all supported platforms specify an interrupt for the
273 * watchdog, so let's make it optional.
275 ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
278 dev_err(&pdev->dev, "failed to request IRQ\n");
283 watchdog_set_nowayout(&dev->wdt, nowayout);
284 ret = watchdog_register_device(&dev->wdt);
288 pr_info("Initial timeout %d sec%s\n",
289 dev->wdt.timeout, nowayout ? ", nowayout" : "");
293 clk_disable_unprepare(dev->clk);
297 static int orion_wdt_remove(struct platform_device *pdev)
299 struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
300 struct orion_watchdog *dev = watchdog_get_drvdata(wdt_dev);
302 watchdog_unregister_device(wdt_dev);
303 clk_disable_unprepare(dev->clk);
307 static void orion_wdt_shutdown(struct platform_device *pdev)
309 struct watchdog_device *wdt_dev = platform_get_drvdata(pdev);
310 orion_wdt_stop(wdt_dev);
313 static struct platform_driver orion_wdt_driver = {
314 .probe = orion_wdt_probe,
315 .remove = orion_wdt_remove,
316 .shutdown = orion_wdt_shutdown,
318 .owner = THIS_MODULE,
320 .of_match_table = orion_wdt_of_match_table,
324 module_platform_driver(orion_wdt_driver);
326 MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
327 MODULE_DESCRIPTION("Orion Processor Watchdog");
329 module_param(heartbeat, int, 0);
330 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
332 module_param(nowayout, bool, 0);
333 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
334 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
336 MODULE_LICENSE("GPL");
337 MODULE_ALIAS("platform:orion_wdt");