Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (C) 2022 Hewlett-Packard Enterprise Development Company, L.P. */
0003 
0004 #include <linux/delay.h>
0005 #include <linux/io.h>
0006 #include <linux/module.h>
0007 #include <linux/platform_device.h>
0008 #include <linux/types.h>
0009 #include <linux/watchdog.h>
0010 
0011 #define MASK_WDGCS_ENABLE   0x01
0012 #define MASK_WDGCS_RELOAD   0x04
0013 #define MASK_WDGCS_NMIEN    0x08
0014 #define MASK_WDGCS_WARN     0x80
0015 
0016 #define WDT_MAX_TIMEOUT_MS  655350
0017 #define WDT_DEFAULT_TIMEOUT 30
0018 #define SECS_TO_WDOG_TICKS(x) ((x) * 100)
0019 #define WDOG_TICKS_TO_SECS(x) ((x) / 100)
0020 
0021 #define GXP_WDT_CNT_OFS     0x10
0022 #define GXP_WDT_CTRL_OFS    0x16
0023 
0024 struct gxp_wdt {
0025     void __iomem *base;
0026     struct watchdog_device wdd;
0027 };
0028 
0029 static void gxp_wdt_enable_reload(struct gxp_wdt *drvdata)
0030 {
0031     u8 val;
0032 
0033     val = readb(drvdata->base + GXP_WDT_CTRL_OFS);
0034     val |= (MASK_WDGCS_ENABLE | MASK_WDGCS_RELOAD);
0035     writeb(val, drvdata->base + GXP_WDT_CTRL_OFS);
0036 }
0037 
0038 static int gxp_wdt_start(struct watchdog_device *wdd)
0039 {
0040     struct gxp_wdt *drvdata = watchdog_get_drvdata(wdd);
0041 
0042     writew(SECS_TO_WDOG_TICKS(wdd->timeout), drvdata->base + GXP_WDT_CNT_OFS);
0043     gxp_wdt_enable_reload(drvdata);
0044     return 0;
0045 }
0046 
0047 static int gxp_wdt_stop(struct watchdog_device *wdd)
0048 {
0049     struct gxp_wdt *drvdata = watchdog_get_drvdata(wdd);
0050     u8 val;
0051 
0052     val = readb_relaxed(drvdata->base + GXP_WDT_CTRL_OFS);
0053     val &= ~MASK_WDGCS_ENABLE;
0054     writeb(val, drvdata->base + GXP_WDT_CTRL_OFS);
0055     return 0;
0056 }
0057 
0058 static int gxp_wdt_set_timeout(struct watchdog_device *wdd,
0059                    unsigned int timeout)
0060 {
0061     struct gxp_wdt *drvdata = watchdog_get_drvdata(wdd);
0062     u32 actual;
0063 
0064     wdd->timeout = timeout;
0065     actual = min(timeout * 100, wdd->max_hw_heartbeat_ms / 10);
0066     writew(actual, drvdata->base + GXP_WDT_CNT_OFS);
0067 
0068     return 0;
0069 }
0070 
0071 static unsigned int gxp_wdt_get_timeleft(struct watchdog_device *wdd)
0072 {
0073     struct gxp_wdt *drvdata = watchdog_get_drvdata(wdd);
0074     u32 val = readw(drvdata->base + GXP_WDT_CNT_OFS);
0075 
0076     return WDOG_TICKS_TO_SECS(val);
0077 }
0078 
0079 static int gxp_wdt_ping(struct watchdog_device *wdd)
0080 {
0081     struct gxp_wdt *drvdata = watchdog_get_drvdata(wdd);
0082 
0083     gxp_wdt_enable_reload(drvdata);
0084     return 0;
0085 }
0086 
0087 static int gxp_restart(struct watchdog_device *wdd, unsigned long action,
0088                void *data)
0089 {
0090     struct gxp_wdt *drvdata = watchdog_get_drvdata(wdd);
0091 
0092     writew(1, drvdata->base + GXP_WDT_CNT_OFS);
0093     gxp_wdt_enable_reload(drvdata);
0094     mdelay(100);
0095     return 0;
0096 }
0097 
0098 static const struct watchdog_ops gxp_wdt_ops = {
0099     .owner =    THIS_MODULE,
0100     .start =    gxp_wdt_start,
0101     .stop =     gxp_wdt_stop,
0102     .ping =     gxp_wdt_ping,
0103     .set_timeout =  gxp_wdt_set_timeout,
0104     .get_timeleft = gxp_wdt_get_timeleft,
0105     .restart =  gxp_restart,
0106 };
0107 
0108 static const struct watchdog_info gxp_wdt_info = {
0109     .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
0110     .identity = "HPE GXP Watchdog timer",
0111 };
0112 
0113 static int gxp_wdt_probe(struct platform_device *pdev)
0114 {
0115     struct device *dev = &pdev->dev;
0116     struct gxp_wdt *drvdata;
0117     int err;
0118     u8 val;
0119 
0120     drvdata = devm_kzalloc(dev, sizeof(struct gxp_wdt), GFP_KERNEL);
0121     if (!drvdata)
0122         return -ENOMEM;
0123 
0124     /*
0125      * The register area where the timer and watchdog reside is disarranged.
0126      * Hence mapping individual register blocks for the timer and watchdog
0127      * is not recommended as they would have access to each others
0128      * registers. Based on feedback the watchdog is no longer part of the
0129      * device tree file and the timer driver now creates the watchdog as a
0130      * child device. During the watchdogs creation, the timer driver passes
0131      * the base address to the watchdog over the private interface.
0132      */
0133 
0134     drvdata->base = (void __iomem *)dev->platform_data;
0135 
0136     drvdata->wdd.info = &gxp_wdt_info;
0137     drvdata->wdd.ops = &gxp_wdt_ops;
0138     drvdata->wdd.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT_MS;
0139     drvdata->wdd.parent = dev;
0140     drvdata->wdd.timeout = WDT_DEFAULT_TIMEOUT;
0141 
0142     watchdog_set_drvdata(&drvdata->wdd, drvdata);
0143     watchdog_set_nowayout(&drvdata->wdd, WATCHDOG_NOWAYOUT);
0144 
0145     val = readb(drvdata->base + GXP_WDT_CTRL_OFS);
0146 
0147     if (val & MASK_WDGCS_ENABLE)
0148         set_bit(WDOG_HW_RUNNING, &drvdata->wdd.status);
0149 
0150     watchdog_set_restart_priority(&drvdata->wdd, 128);
0151 
0152     watchdog_stop_on_reboot(&drvdata->wdd);
0153     err = devm_watchdog_register_device(dev, &drvdata->wdd);
0154     if (err) {
0155         dev_err(dev, "Failed to register watchdog device");
0156         return err;
0157     }
0158 
0159     dev_info(dev, "HPE GXP watchdog timer");
0160 
0161     return 0;
0162 }
0163 
0164 static struct platform_driver gxp_wdt_driver = {
0165     .probe = gxp_wdt_probe,
0166     .driver = {
0167         .name = "gxp-wdt",
0168     },
0169 };
0170 module_platform_driver(gxp_wdt_driver);
0171 
0172 MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>");
0173 MODULE_AUTHOR("Jean-Marie Verdun <verdun@hpe.com>");
0174 MODULE_DESCRIPTION("Driver for GXP watchdog timer");
0175 MODULE_LICENSE("GPL");