1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Intel OC Watchdog driver 4 * 5 * Copyright (C) 2025, Siemens 6 * Author: Diogo Ivo <diogo.ivo@siemens.com> 7 */ 8 9 #define DRV_NAME "intel_oc_wdt" 10 11 #include <linux/acpi.h> 12 #include <linux/bits.h> 13 #include <linux/io.h> 14 #include <linux/module.h> 15 #include <linux/moduleparam.h> 16 #include <linux/platform_device.h> 17 #include <linux/stringify.h> 18 #include <linux/watchdog.h> 19 20 #define INTEL_OC_WDT_TOV GENMASK(9, 0) 21 #define INTEL_OC_WDT_MIN_TOV 1 22 #define INTEL_OC_WDT_MAX_TOV 1024 23 #define INTEL_OC_WDT_DEF_TOV 60 24 25 /* 26 * One-time writable lock bit. If set forbids 27 * modification of itself, _TOV and _EN until 28 * next reboot. 29 */ 30 #define INTEL_OC_WDT_CTL_LCK BIT(12) 31 32 #define INTEL_OC_WDT_EN BIT(14) 33 #define INTEL_OC_WDT_NO_ICCSURV_STS BIT(24) 34 #define INTEL_OC_WDT_ICCSURV_STS BIT(25) 35 #define INTEL_OC_WDT_RLD BIT(31) 36 37 #define INTEL_OC_WDT_STS_BITS (INTEL_OC_WDT_NO_ICCSURV_STS | \ 38 INTEL_OC_WDT_ICCSURV_STS) 39 40 #define INTEL_OC_WDT_CTRL_REG(wdt) ((wdt)->ctrl_res->start) 41 42 struct intel_oc_wdt { 43 struct watchdog_device wdd; 44 struct resource *ctrl_res; 45 struct watchdog_info info; 46 bool locked; 47 bool suspended; 48 }; 49 50 static int heartbeat; 51 module_param(heartbeat, uint, 0); 52 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. (default=" 53 __stringify(WDT_HEARTBEAT) ")"); 54 55 static bool nowayout = WATCHDOG_NOWAYOUT; 56 module_param(nowayout, bool, 0); 57 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 58 __stringify(WATCHDOG_NOWAYOUT) ")"); 59 60 static int intel_oc_wdt_start(struct watchdog_device *wdd) 61 { 62 struct intel_oc_wdt *oc_wdt = watchdog_get_drvdata(wdd); 63 64 if (oc_wdt->locked) 65 return 0; 66 67 outl(inl(INTEL_OC_WDT_CTRL_REG(oc_wdt)) | INTEL_OC_WDT_EN, 68 INTEL_OC_WDT_CTRL_REG(oc_wdt)); 69 70 return 0; 71 } 72 73 static int intel_oc_wdt_stop(struct watchdog_device *wdd) 74 { 75 struct intel_oc_wdt *oc_wdt = watchdog_get_drvdata(wdd); 76 77 outl(inl(INTEL_OC_WDT_CTRL_REG(oc_wdt)) & ~INTEL_OC_WDT_EN, 78 INTEL_OC_WDT_CTRL_REG(oc_wdt)); 79 80 return 0; 81 } 82 83 static int intel_oc_wdt_ping(struct watchdog_device *wdd) 84 { 85 struct intel_oc_wdt *oc_wdt = watchdog_get_drvdata(wdd); 86 87 outl(inl(INTEL_OC_WDT_CTRL_REG(oc_wdt)) | INTEL_OC_WDT_RLD, 88 INTEL_OC_WDT_CTRL_REG(oc_wdt)); 89 90 return 0; 91 } 92 93 static int intel_oc_wdt_set_timeout(struct watchdog_device *wdd, 94 unsigned int t) 95 { 96 struct intel_oc_wdt *oc_wdt = watchdog_get_drvdata(wdd); 97 98 outl((inl(INTEL_OC_WDT_CTRL_REG(oc_wdt)) & ~INTEL_OC_WDT_TOV) | (t - 1), 99 INTEL_OC_WDT_CTRL_REG(oc_wdt)); 100 101 wdd->timeout = t; 102 103 return 0; 104 } 105 106 static const struct watchdog_info intel_oc_wdt_info = { 107 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, 108 .identity = DRV_NAME, 109 }; 110 111 static const struct watchdog_ops intel_oc_wdt_ops = { 112 .owner = THIS_MODULE, 113 .start = intel_oc_wdt_start, 114 .stop = intel_oc_wdt_stop, 115 .ping = intel_oc_wdt_ping, 116 .set_timeout = intel_oc_wdt_set_timeout, 117 }; 118 119 static int intel_oc_wdt_setup(struct intel_oc_wdt *oc_wdt) 120 { 121 unsigned long val; 122 123 val = inl(INTEL_OC_WDT_CTRL_REG(oc_wdt)); 124 125 if (val & INTEL_OC_WDT_STS_BITS) 126 oc_wdt->wdd.bootstatus |= WDIOF_CARDRESET; 127 128 oc_wdt->locked = !!(val & INTEL_OC_WDT_CTL_LCK); 129 130 if (val & INTEL_OC_WDT_EN) { 131 /* 132 * No need to issue a ping here to "commit" the new timeout 133 * value to hardware as the watchdog core schedules one 134 * immediately when registering the watchdog. 135 */ 136 set_bit(WDOG_HW_RUNNING, &oc_wdt->wdd.status); 137 138 if (oc_wdt->locked) { 139 /* 140 * Set nowayout unconditionally as we cannot stop 141 * the watchdog. 142 */ 143 nowayout = true; 144 /* 145 * If we are locked read the current timeout value 146 * and inform the core we can't change it. 147 */ 148 oc_wdt->wdd.timeout = (val & INTEL_OC_WDT_TOV) + 1; 149 oc_wdt->info.options &= ~WDIOF_SETTIMEOUT; 150 151 dev_info(oc_wdt->wdd.parent, 152 "Register access locked, heartbeat fixed at: %u s\n", 153 oc_wdt->wdd.timeout); 154 } 155 } else if (oc_wdt->locked) { 156 /* 157 * In case the watchdog is disabled and locked there 158 * is nothing we can do with it so just fail probing. 159 */ 160 return -EACCES; 161 } 162 163 val &= ~INTEL_OC_WDT_TOV; 164 outl(val | (oc_wdt->wdd.timeout - 1), INTEL_OC_WDT_CTRL_REG(oc_wdt)); 165 166 return 0; 167 } 168 169 static int intel_oc_wdt_probe(struct platform_device *pdev) 170 { 171 struct device *dev = &pdev->dev; 172 struct intel_oc_wdt *oc_wdt; 173 struct watchdog_device *wdd; 174 int ret; 175 176 oc_wdt = devm_kzalloc_obj(&pdev->dev, *oc_wdt); 177 if (!oc_wdt) 178 return -ENOMEM; 179 180 oc_wdt->ctrl_res = platform_get_resource(pdev, IORESOURCE_IO, 0); 181 if (!oc_wdt->ctrl_res) { 182 dev_err(&pdev->dev, "missing I/O resource\n"); 183 return -ENODEV; 184 } 185 186 if (!devm_request_region(&pdev->dev, oc_wdt->ctrl_res->start, 187 resource_size(oc_wdt->ctrl_res), pdev->name)) { 188 dev_err(dev, "resource %pR already in use, device disabled\n", 189 oc_wdt->ctrl_res); 190 return -EBUSY; 191 } 192 193 wdd = &oc_wdt->wdd; 194 wdd->min_timeout = INTEL_OC_WDT_MIN_TOV; 195 wdd->max_timeout = INTEL_OC_WDT_MAX_TOV; 196 wdd->timeout = INTEL_OC_WDT_DEF_TOV; 197 oc_wdt->info = intel_oc_wdt_info; 198 wdd->info = &oc_wdt->info; 199 wdd->ops = &intel_oc_wdt_ops; 200 wdd->parent = dev; 201 202 watchdog_init_timeout(wdd, heartbeat, dev); 203 204 ret = intel_oc_wdt_setup(oc_wdt); 205 if (ret) 206 return ret; 207 208 platform_set_drvdata(pdev, oc_wdt); 209 watchdog_set_drvdata(wdd, oc_wdt); 210 watchdog_set_nowayout(wdd, nowayout); 211 watchdog_stop_on_reboot(wdd); 212 watchdog_stop_on_unregister(wdd); 213 214 return devm_watchdog_register_device(dev, wdd); 215 } 216 217 /* 218 * Neither user space nor the watchdog core can ping the watchdog while the 219 * system sleeps, and in suspend-to-idle the counter keeps running, so stop the 220 * watchdog late in suspend and restart it early in resume. Check the hardware 221 * enable bit, as the watchdog may also have been started by the firmware. 222 */ 223 static int intel_oc_wdt_suspend_noirq(struct device *dev) 224 { 225 struct intel_oc_wdt *oc_wdt = dev_get_drvdata(dev); 226 227 oc_wdt->suspended = false; 228 229 if (oc_wdt->locked || 230 !(inl(INTEL_OC_WDT_CTRL_REG(oc_wdt)) & INTEL_OC_WDT_EN)) 231 return 0; 232 233 intel_oc_wdt_stop(&oc_wdt->wdd); 234 oc_wdt->suspended = true; 235 236 return 0; 237 } 238 239 static int intel_oc_wdt_resume_noirq(struct device *dev) 240 { 241 struct intel_oc_wdt *oc_wdt = dev_get_drvdata(dev); 242 243 if (oc_wdt->locked) 244 return 0; 245 246 /* 247 * The timeout in hardware may have been reset by the platform firmware 248 * or, when resuming from hibernation, set by the boot kernel, and 249 * intel_oc_wdt_start() does not program it. 250 */ 251 intel_oc_wdt_set_timeout(&oc_wdt->wdd, oc_wdt->wdd.timeout); 252 253 if (!oc_wdt->suspended) 254 return 0; 255 256 intel_oc_wdt_start(&oc_wdt->wdd); 257 intel_oc_wdt_ping(&oc_wdt->wdd); 258 oc_wdt->suspended = false; 259 260 return 0; 261 } 262 263 static DEFINE_NOIRQ_DEV_PM_OPS(intel_oc_wdt_pm_ops, intel_oc_wdt_suspend_noirq, 264 intel_oc_wdt_resume_noirq); 265 266 static const struct acpi_device_id intel_oc_wdt_match[] = { 267 { "INT3F0D" }, 268 { "INTC1099" }, 269 { }, 270 }; 271 MODULE_DEVICE_TABLE(acpi, intel_oc_wdt_match); 272 273 static struct platform_driver intel_oc_wdt_platform_driver = { 274 .driver = { 275 .name = DRV_NAME, 276 .acpi_match_table = intel_oc_wdt_match, 277 .pm = pm_sleep_ptr(&intel_oc_wdt_pm_ops), 278 }, 279 .probe = intel_oc_wdt_probe, 280 }; 281 282 module_platform_driver(intel_oc_wdt_platform_driver); 283 284 MODULE_AUTHOR("Diogo Ivo <diogo.ivo@siemens.com>"); 285 MODULE_LICENSE("GPL"); 286 MODULE_DESCRIPTION("Intel OC Watchdog driver"); 287