1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/char/watchdog/ixp4xx_wdt.c
5 * Watchdog driver for Intel IXP4xx network processors
7 * Author: Deepak Saxena <dsaxena@plexity.net>
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * Copyright 2004 (c) MontaVista, Software, Inc.
11 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/watchdog.h>
18 #include <linux/bits.h>
19 #include <linux/platform_device.h>
20 #include <linux/clk.h>
21 #include <linux/soc/ixp4xx/cpu.h>
24 struct watchdog_device wdd;
29 /* Fallback if we do not have a clock for this */
30 #define IXP4XX_TIMER_FREQ 66666000
32 /* Registers after the timer registers */
33 #define IXP4XX_OSWT_OFFSET 0x14 /* Watchdog Timer */
34 #define IXP4XX_OSWE_OFFSET 0x18 /* Watchdog Enable */
35 #define IXP4XX_OSWK_OFFSET 0x1C /* Watchdog Key */
36 #define IXP4XX_OSST_OFFSET 0x20 /* Timer Status */
38 #define IXP4XX_OSST_TIMER_WDOG_PEND 0x00000008
39 #define IXP4XX_OSST_TIMER_WARM_RESET 0x00000010
40 #define IXP4XX_WDT_KEY 0x0000482E
41 #define IXP4XX_WDT_RESET_ENABLE 0x00000001
42 #define IXP4XX_WDT_IRQ_ENABLE 0x00000002
43 #define IXP4XX_WDT_COUNT_ENABLE 0x00000004
46 struct ixp4xx_wdt *to_ixp4xx_wdt(struct watchdog_device *wdd)
48 return container_of(wdd, struct ixp4xx_wdt, wdd);
51 static int ixp4xx_wdt_start(struct watchdog_device *wdd)
53 struct ixp4xx_wdt *iwdt = to_ixp4xx_wdt(wdd);
55 __raw_writel(IXP4XX_WDT_KEY, iwdt->base + IXP4XX_OSWK_OFFSET);
56 __raw_writel(0, iwdt->base + IXP4XX_OSWE_OFFSET);
57 __raw_writel(wdd->timeout * iwdt->rate,
58 iwdt->base + IXP4XX_OSWT_OFFSET);
59 __raw_writel(IXP4XX_WDT_COUNT_ENABLE | IXP4XX_WDT_RESET_ENABLE,
60 iwdt->base + IXP4XX_OSWE_OFFSET);
61 __raw_writel(0, iwdt->base + IXP4XX_OSWK_OFFSET);
66 static int ixp4xx_wdt_stop(struct watchdog_device *wdd)
68 struct ixp4xx_wdt *iwdt = to_ixp4xx_wdt(wdd);
70 __raw_writel(IXP4XX_WDT_KEY, iwdt->base + IXP4XX_OSWK_OFFSET);
71 __raw_writel(0, iwdt->base + IXP4XX_OSWE_OFFSET);
72 __raw_writel(0, iwdt->base + IXP4XX_OSWK_OFFSET);
77 static int ixp4xx_wdt_set_timeout(struct watchdog_device *wdd,
80 wdd->timeout = timeout;
81 if (watchdog_active(wdd))
82 ixp4xx_wdt_start(wdd);
87 static const struct watchdog_ops ixp4xx_wdt_ops = {
88 .start = ixp4xx_wdt_start,
89 .stop = ixp4xx_wdt_stop,
90 .set_timeout = ixp4xx_wdt_set_timeout,
94 static const struct watchdog_info ixp4xx_wdt_info = {
95 .options = WDIOF_KEEPALIVEPING
98 .identity = KBUILD_MODNAME,
101 /* Devres-handled clock disablement */
102 static void ixp4xx_clock_action(void *d)
104 clk_disable_unprepare(d);
107 static int ixp4xx_wdt_probe(struct platform_device *pdev)
109 struct device *dev = &pdev->dev;
110 struct ixp4xx_wdt *iwdt;
114 if (!(read_cpuid_id() & 0xf) && !cpu_is_ixp46x()) {
115 dev_err(dev, "Rev. A0 IXP42x CPU detected - watchdog disabled\n");
119 iwdt = devm_kzalloc(dev, sizeof(*iwdt), GFP_KERNEL);
122 iwdt->base = (void __iomem *)dev->platform_data;
125 * Retrieve rate from a fixed clock from the device tree if
126 * the parent has that, else use the default clock rate.
128 clk = devm_clk_get(dev->parent, NULL);
130 ret = clk_prepare_enable(clk);
133 ret = devm_add_action_or_reset(dev, ixp4xx_clock_action, clk);
136 iwdt->rate = clk_get_rate(clk);
139 iwdt->rate = IXP4XX_TIMER_FREQ;
141 iwdt->wdd.info = &ixp4xx_wdt_info;
142 iwdt->wdd.ops = &ixp4xx_wdt_ops;
143 iwdt->wdd.min_timeout = 1;
144 iwdt->wdd.max_timeout = U32_MAX / iwdt->rate;
145 iwdt->wdd.parent = dev;
146 /* Default to 60 seconds */
147 iwdt->wdd.timeout = 60U;
148 watchdog_init_timeout(&iwdt->wdd, 0, dev);
150 if (__raw_readl(iwdt->base + IXP4XX_OSST_OFFSET) &
151 IXP4XX_OSST_TIMER_WARM_RESET)
152 iwdt->wdd.bootstatus = WDIOF_CARDRESET;
154 ret = devm_watchdog_register_device(dev, &iwdt->wdd);
158 dev_info(dev, "IXP4xx watchdog available\n");
163 static struct platform_driver ixp4xx_wdt_driver = {
164 .probe = ixp4xx_wdt_probe,
166 .name = "ixp4xx-watchdog",
169 module_platform_driver(ixp4xx_wdt_driver);
171 MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
172 MODULE_DESCRIPTION("IXP4xx Network Processor Watchdog");
173 MODULE_LICENSE("GPL");