diff options
Diffstat (limited to 'drivers/watchdog/mpc8xxx_wdt.c')
| -rw-r--r-- | drivers/watchdog/mpc8xxx_wdt.c | 143 |
1 files changed, 70 insertions, 73 deletions
diff --git a/drivers/watchdog/mpc8xxx_wdt.c b/drivers/watchdog/mpc8xxx_wdt.c index 366e5c7e650b..a4b497ecfa20 100644 --- a/drivers/watchdog/mpc8xxx_wdt.c +++ b/drivers/watchdog/mpc8xxx_wdt.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * mpc8xxx_wdt.c - MPC8xx/MPC83xx/MPC86xx watchdog userspace interface * @@ -10,31 +11,26 @@ * * Note: it appears that you can only actually ENABLE or DISABLE the thing * once after POR. Once enabled, you cannot disable, and vice versa. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. */ -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt - #include <linux/fs.h> #include <linux/init.h> #include <linux/kernel.h> -#include <linux/timer.h> -#include <linux/of_address.h> -#include <linux/of_platform.h> +#include <linux/of.h> +#include <linux/platform_device.h> #include <linux/module.h> #include <linux/watchdog.h> #include <linux/io.h> #include <linux/uaccess.h> #include <sysdev/fsl_soc.h> +#define WATCHDOG_TIMEOUT 10 + struct mpc8xxx_wdt { __be32 res0; __be32 swcrr; /* System watchdog control register */ #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */ +#define SWCRR_SWF 0x00000008 /* Software Watchdog Freeze (mpc8xx). */ #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */ #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/ #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */ @@ -47,19 +43,21 @@ struct mpc8xxx_wdt { struct mpc8xxx_wdt_type { int prescaler; bool hw_enabled; + u32 rsr_mask; }; struct mpc8xxx_wdt_ddata { struct mpc8xxx_wdt __iomem *base; struct watchdog_device wdd; - struct timer_list timer; spinlock_t lock; + u16 swtc; }; -static u16 timeout = 0xffff; +static u16 timeout; module_param(timeout, ushort, 0); MODULE_PARM_DESC(timeout, - "Watchdog timeout in ticks. (0<timeout<65536, default=65535)"); + "Watchdog timeout in seconds. (1<timeout<65535, default=" + __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); static bool reset = 1; module_param(reset, bool, 0); @@ -80,64 +78,52 @@ static void mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata *ddata) spin_unlock(&ddata->lock); } -static void mpc8xxx_wdt_timer_ping(unsigned long arg) -{ - struct mpc8xxx_wdt_ddata *ddata = (void *)arg; - - mpc8xxx_wdt_keepalive(ddata); - /* We're pinging it twice faster than needed, just to be sure. */ - mod_timer(&ddata->timer, jiffies + HZ * ddata->wdd.timeout / 2); -} - static int mpc8xxx_wdt_start(struct watchdog_device *w) { struct mpc8xxx_wdt_ddata *ddata = container_of(w, struct mpc8xxx_wdt_ddata, wdd); - - u32 tmp = SWCRR_SWEN | SWCRR_SWPR; + u32 tmp = in_be32(&ddata->base->swcrr); /* Good, fire up the show */ + tmp &= ~(SWCRR_SWTC | SWCRR_SWF | SWCRR_SWEN | SWCRR_SWRI | SWCRR_SWPR); + tmp |= SWCRR_SWEN | SWCRR_SWPR | (ddata->swtc << 16); + if (reset) tmp |= SWCRR_SWRI; - tmp |= timeout << 16; - out_be32(&ddata->base->swcrr, tmp); - del_timer_sync(&ddata->timer); - - return 0; -} + tmp = in_be32(&ddata->base->swcrr); + if (!(tmp & SWCRR_SWEN)) + return -EOPNOTSUPP; -static int mpc8xxx_wdt_ping(struct watchdog_device *w) -{ - struct mpc8xxx_wdt_ddata *ddata = - container_of(w, struct mpc8xxx_wdt_ddata, wdd); + ddata->swtc = tmp >> 16; + set_bit(WDOG_HW_RUNNING, &ddata->wdd.status); mpc8xxx_wdt_keepalive(ddata); + return 0; } -static int mpc8xxx_wdt_stop(struct watchdog_device *w) +static int mpc8xxx_wdt_ping(struct watchdog_device *w) { struct mpc8xxx_wdt_ddata *ddata = container_of(w, struct mpc8xxx_wdt_ddata, wdd); - mod_timer(&ddata->timer, jiffies); + mpc8xxx_wdt_keepalive(ddata); return 0; } static struct watchdog_info mpc8xxx_wdt_info = { - .options = WDIOF_KEEPALIVEPING, + .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT, .firmware_version = 1, .identity = "MPC8xxx", }; -static struct watchdog_ops mpc8xxx_wdt_ops = { +static const struct watchdog_ops mpc8xxx_wdt_ops = { .owner = THIS_MODULE, .start = mpc8xxx_wdt_start, .ping = mpc8xxx_wdt_ping, - .stop = mpc8xxx_wdt_stop, }; static int mpc8xxx_wdt_probe(struct platform_device *ofdev) @@ -148,52 +134,59 @@ static int mpc8xxx_wdt_probe(struct platform_device *ofdev) struct mpc8xxx_wdt_ddata *ddata; u32 freq = fsl_get_sys_freq(); bool enabled; - unsigned int timeout_sec; + struct device *dev = &ofdev->dev; - wdt_type = of_device_get_match_data(&ofdev->dev); + wdt_type = of_device_get_match_data(dev); if (!wdt_type) return -EINVAL; if (!freq || freq == -1) return -EINVAL; - ddata = devm_kzalloc(&ofdev->dev, sizeof(*ddata), GFP_KERNEL); + ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); if (!ddata) return -ENOMEM; - res = platform_get_resource(ofdev, IORESOURCE_MEM, 0); - ddata->base = devm_ioremap_resource(&ofdev->dev, res); + ddata->base = devm_platform_ioremap_resource(ofdev, 0); if (IS_ERR(ddata->base)) return PTR_ERR(ddata->base); enabled = in_be32(&ddata->base->swcrr) & SWCRR_SWEN; if (!enabled && wdt_type->hw_enabled) { - pr_info("could not be enabled in software\n"); + dev_info(dev, "could not be enabled in software\n"); return -ENODEV; } - spin_lock_init(&ddata->lock); - setup_timer(&ddata->timer, mpc8xxx_wdt_timer_ping, - (unsigned long)ddata); + res = platform_get_resource(ofdev, IORESOURCE_MEM, 1); + if (res) { + bool status; + u32 __iomem *rsr = ioremap(res->start, resource_size(res)); - ddata->wdd.info = &mpc8xxx_wdt_info, - ddata->wdd.ops = &mpc8xxx_wdt_ops, + if (!rsr) + return -ENOMEM; - /* Calculate the timeout in seconds */ - timeout_sec = (timeout * wdt_type->prescaler) / freq; + status = in_be32(rsr) & wdt_type->rsr_mask; + ddata->wdd.bootstatus = status ? WDIOF_CARDRESET : 0; + /* clear reset status bits related to watchdog timer */ + out_be32(rsr, wdt_type->rsr_mask); + iounmap(rsr); - ddata->wdd.timeout = timeout_sec; + dev_info(dev, "Last boot was %scaused by watchdog\n", + status ? "" : "not "); + } - watchdog_set_nowayout(&ddata->wdd, nowayout); + spin_lock_init(&ddata->lock); - ret = watchdog_register_device(&ddata->wdd); - if (ret) { - pr_err("cannot register watchdog device (err=%d)\n", ret); - return ret; - } + ddata->wdd.info = &mpc8xxx_wdt_info; + ddata->wdd.ops = &mpc8xxx_wdt_ops; - pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d (%d seconds)\n", - reset ? "reset" : "interrupt", timeout, timeout_sec); + ddata->wdd.timeout = WATCHDOG_TIMEOUT; + watchdog_init_timeout(&ddata->wdd, timeout, dev); + + watchdog_set_nowayout(&ddata->wdd, nowayout); + + ddata->swtc = min(ddata->wdd.timeout * freq / wdt_type->prescaler, + 0xffffU); /* * If the watchdog was previously enabled or we're running on @@ -201,21 +194,23 @@ static int mpc8xxx_wdt_probe(struct platform_device *ofdev) * userspace handles it. */ if (enabled) - mod_timer(&ddata->timer, jiffies); + mpc8xxx_wdt_start(&ddata->wdd); - platform_set_drvdata(ofdev, ddata); - return 0; -} + ddata->wdd.max_hw_heartbeat_ms = (ddata->swtc * wdt_type->prescaler) / + (freq / 1000); + ddata->wdd.min_timeout = ddata->wdd.max_hw_heartbeat_ms / 1000; + if (ddata->wdd.timeout < ddata->wdd.min_timeout) + ddata->wdd.timeout = ddata->wdd.min_timeout; -static int mpc8xxx_wdt_remove(struct platform_device *ofdev) -{ - struct mpc8xxx_wdt_ddata *ddata = platform_get_drvdata(ofdev); + ret = devm_watchdog_register_device(dev, &ddata->wdd); + if (ret) + return ret; - pr_crit("Watchdog removed, expect the %s soon!\n", - reset ? "reset" : "machine check exception"); - del_timer_sync(&ddata->timer); - watchdog_unregister_device(&ddata->wdd); + dev_info(dev, + "WDT driver for MPC8xxx initialized. mode:%s timeout=%d sec\n", + reset ? "reset" : "interrupt", ddata->wdd.timeout); + platform_set_drvdata(ofdev, ddata); return 0; } @@ -224,6 +219,7 @@ static const struct of_device_id mpc8xxx_wdt_match[] = { .compatible = "mpc83xx_wdt", .data = &(struct mpc8xxx_wdt_type) { .prescaler = 0x10000, + .rsr_mask = BIT(3), /* RSR Bit SWRS */ }, }, { @@ -231,6 +227,7 @@ static const struct of_device_id mpc8xxx_wdt_match[] = { .data = &(struct mpc8xxx_wdt_type) { .prescaler = 0x10000, .hw_enabled = true, + .rsr_mask = BIT(20), /* RSTRSCR Bit WDT_RR */ }, }, { @@ -238,6 +235,7 @@ static const struct of_device_id mpc8xxx_wdt_match[] = { .data = &(struct mpc8xxx_wdt_type) { .prescaler = 0x800, .hw_enabled = true, + .rsr_mask = BIT(28), /* RSR Bit SWRS */ }, }, {}, @@ -246,7 +244,6 @@ MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match); static struct platform_driver mpc8xxx_wdt_driver = { .probe = mpc8xxx_wdt_probe, - .remove = mpc8xxx_wdt_remove, .driver = { .name = "mpc8xxx_wdt", .of_match_table = mpc8xxx_wdt_match, |
