Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | // SPDX-License-Identifier: GPL-2.0+ /* * exar_wdt.c - Driver for the watchdog present in some * Exar/MaxLinear UART chips like the XR28V38x. * * (c) Copyright 2022 D. Müller <d.mueller@elsoft.ch>. * */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/io.h> #include <linux/list.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/watchdog.h> #define DRV_NAME "exar_wdt" static const unsigned short sio_config_ports[] = { 0x2e, 0x4e }; static const unsigned char sio_enter_keys[] = { 0x67, 0x77, 0x87, 0xA0 }; #define EXAR_EXIT_KEY 0xAA #define EXAR_LDN 0x07 #define EXAR_DID 0x20 #define EXAR_VID 0x23 #define EXAR_WDT 0x26 #define EXAR_ACT 0x30 #define EXAR_RTBASE 0x60 #define EXAR_WDT_LDEV 0x08 #define EXAR_VEN_ID 0x13A8 #define EXAR_DEV_382 0x0382 #define EXAR_DEV_384 0x0384 /* WDT runtime registers */ #define WDT_CTRL 0x00 #define WDT_VAL 0x01 #define WDT_UNITS_10MS 0x0 /* the 10 millisec unit of the HW is not used */ #define WDT_UNITS_SEC 0x2 #define WDT_UNITS_MIN 0x4 /* default WDT control for WDTOUT signal activ / rearm by read */ #define EXAR_WDT_DEF_CONF 0 struct wdt_pdev_node { struct list_head list; struct platform_device *pdev; const char name[16]; }; struct wdt_priv { /* the lock for WDT io operations */ spinlock_t io_lock; struct resource wdt_res; struct watchdog_device wdt_dev; unsigned short did; unsigned short config_port; unsigned char enter_key; unsigned char unit; unsigned char timeout; }; #define WATCHDOG_TIMEOUT 60 static int timeout = WATCHDOG_TIMEOUT; module_param(timeout, int, 0); MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<=timeout<=15300, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) "."); static bool nowayout = WATCHDOG_NOWAYOUT; module_param(nowayout, bool, 0); MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); static int exar_sio_enter(const unsigned short config_port, const unsigned char key) { if (!request_muxed_region(config_port, 2, DRV_NAME)) return -EBUSY; /* write the ENTER-KEY twice */ outb(key, config_port); outb(key, config_port); return 0; } static void exar_sio_exit(const unsigned short config_port) { outb(EXAR_EXIT_KEY, config_port); release_region(config_port, 2); } static unsigned char exar_sio_read(const unsigned short config_port, const unsigned char reg) { outb(reg, config_port); return inb(config_port + 1); } static void exar_sio_write(const unsigned short config_port, const unsigned char reg, const unsigned char val) { outb(reg, config_port); outb(val, config_port + 1); } static unsigned short exar_sio_read16(const unsigned short config_port, const unsigned char reg) { unsigned char msb, lsb; msb = exar_sio_read(config_port, reg); lsb = exar_sio_read(config_port, reg + 1); return (msb << 8) | lsb; } static void exar_sio_select_wdt(const unsigned short config_port) { exar_sio_write(config_port, EXAR_LDN, EXAR_WDT_LDEV); } static void exar_wdt_arm(const struct wdt_priv *priv) { unsigned short rt_base = priv->wdt_res.start; /* write timeout value twice to arm watchdog */ outb(priv->timeout, rt_base + WDT_VAL); outb(priv->timeout, rt_base + WDT_VAL); } static void exar_wdt_disarm(const struct wdt_priv *priv) { unsigned short rt_base = priv->wdt_res.start; /* * use two accesses with different values to make sure * that a combination of a previous single access and * the ones below with the same value are not falsely * interpreted as "arm watchdog" */ outb(0xFF, rt_base + WDT_VAL); outb(0, rt_base + WDT_VAL); } static int exar_wdt_start(struct watchdog_device *wdog) { struct wdt_priv *priv = watchdog_get_drvdata(wdog); unsigned short rt_base = priv->wdt_res.start; spin_lock(&priv->io_lock); exar_wdt_disarm(priv); outb(priv->unit, rt_base + WDT_CTRL); exar_wdt_arm(priv); spin_unlock(&priv->io_lock); return 0; } static int exar_wdt_stop(struct watchdog_device *wdog) { struct wdt_priv *priv = watchdog_get_drvdata(wdog); spin_lock(&priv->io_lock); exar_wdt_disarm(priv); spin_unlock(&priv->io_lock); return 0; } static int exar_wdt_keepalive(struct watchdog_device *wdog) { struct wdt_priv *priv = watchdog_get_drvdata(wdog); unsigned short rt_base = priv->wdt_res.start; spin_lock(&priv->io_lock); /* reading the WDT_VAL reg will feed the watchdog */ inb(rt_base + WDT_VAL); spin_unlock(&priv->io_lock); return 0; } static int exar_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t) { struct wdt_priv *priv = watchdog_get_drvdata(wdog); bool unit_min = false; /* * if new timeout is bigger then 255 seconds, change the * unit to minutes and round the timeout up to the next whole minute */ if (t > 255) { unit_min = true; t = DIV_ROUND_UP(t, 60); } /* save for later use in exar_wdt_start() */ priv->unit = unit_min ? WDT_UNITS_MIN : WDT_UNITS_SEC; priv->timeout = t; wdog->timeout = unit_min ? t * 60 : t; if (watchdog_hw_running(wdog)) exar_wdt_start(wdog); return 0; } static const struct watchdog_info exar_wdt_info = { .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, .identity = "Exar/MaxLinear XR28V38x Watchdog", }; static const struct watchdog_ops exar_wdt_ops = { .owner = THIS_MODULE, .start = exar_wdt_start, .stop = exar_wdt_stop, .ping = exar_wdt_keepalive, .set_timeout = exar_wdt_set_timeout, }; static int exar_wdt_config(struct watchdog_device *wdog, const unsigned char conf) { struct wdt_priv *priv = watchdog_get_drvdata(wdog); int ret; ret = exar_sio_enter(priv->config_port, priv->enter_key); if (ret) return ret; exar_sio_select_wdt(priv->config_port); exar_sio_write(priv->config_port, EXAR_WDT, conf); exar_sio_exit(priv->config_port); return 0; } static int __init exar_wdt_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; struct wdt_priv *priv = dev->platform_data; struct watchdog_device *wdt_dev = &priv->wdt_dev; struct resource *res; int ret; res = platform_get_resource(pdev, IORESOURCE_IO, 0); if (!res) return -ENXIO; spin_lock_init(&priv->io_lock); wdt_dev->info = &exar_wdt_info; wdt_dev->ops = &exar_wdt_ops; wdt_dev->min_timeout = 1; wdt_dev->max_timeout = 255 * 60; watchdog_init_timeout(wdt_dev, timeout, NULL); watchdog_set_nowayout(wdt_dev, nowayout); watchdog_stop_on_reboot(wdt_dev); watchdog_stop_on_unregister(wdt_dev); watchdog_set_drvdata(wdt_dev, priv); ret = exar_wdt_config(wdt_dev, EXAR_WDT_DEF_CONF); if (ret) return ret; exar_wdt_set_timeout(wdt_dev, timeout); /* Make sure that the watchdog is not running */ exar_wdt_stop(wdt_dev); ret = devm_watchdog_register_device(dev, wdt_dev); if (ret) return ret; dev_info(dev, "XR28V%X WDT initialized. timeout=%d sec (nowayout=%d)\n", priv->did, timeout, nowayout); return 0; } static unsigned short __init exar_detect(const unsigned short config_port, const unsigned char key, unsigned short *rt_base) { int ret; unsigned short base = 0; unsigned short vid, did; ret = exar_sio_enter(config_port, key); if (ret) return 0; vid = exar_sio_read16(config_port, EXAR_VID); did = exar_sio_read16(config_port, EXAR_DID); /* check for the vendor and device IDs we currently know about */ if (vid == EXAR_VEN_ID && (did == EXAR_DEV_382 || did == EXAR_DEV_384)) { exar_sio_select_wdt(config_port); /* is device active? */ if (exar_sio_read(config_port, EXAR_ACT) == 0x01) base = exar_sio_read16(config_port, EXAR_RTBASE); } exar_sio_exit(config_port); if (base) { pr_debug("Found a XR28V%X WDT (conf: 0x%x / rt: 0x%04x)\n", did, config_port, base); *rt_base = base; return did; } return 0; } static struct platform_driver exar_wdt_driver = { .driver = { .name = DRV_NAME, }, }; static LIST_HEAD(pdev_list); static int __init exar_wdt_register(struct wdt_priv *priv, const int idx) { struct wdt_pdev_node *n; n = kzalloc(sizeof(*n), GFP_KERNEL); if (!n) return -ENOMEM; INIT_LIST_HEAD(&n->list); scnprintf((char *)n->name, sizeof(n->name), DRV_NAME ".%d", idx); priv->wdt_res.name = n->name; n->pdev = platform_device_register_resndata(NULL, DRV_NAME, idx, &priv->wdt_res, 1, priv, sizeof(*priv)); if (IS_ERR(n->pdev)) { int err = PTR_ERR(n->pdev); kfree(n); return err; } list_add_tail(&n->list, &pdev_list); return 0; } static void exar_wdt_unregister(void) { struct wdt_pdev_node *n, *t; list_for_each_entry_safe(n, t, &pdev_list, list) { platform_device_unregister(n->pdev); list_del(&n->list); kfree(n); } } static int __init exar_wdt_init(void) { int ret, i, j, idx = 0; /* search for active Exar watchdogs on all possible locations */ for (i = 0; i < ARRAY_SIZE(sio_config_ports); i++) { for (j = 0; j < ARRAY_SIZE(sio_enter_keys); j++) { unsigned short did, rt_base = 0; did = exar_detect(sio_config_ports[i], sio_enter_keys[j], &rt_base); if (did) { struct wdt_priv priv = { .wdt_res = DEFINE_RES_IO(rt_base, 2), .did = did, .config_port = sio_config_ports[i], .enter_key = sio_enter_keys[j], }; ret = exar_wdt_register(&priv, idx); if (!ret) idx++; } } } if (!idx) return -ENODEV; ret = platform_driver_probe(&exar_wdt_driver, exar_wdt_probe); if (ret) exar_wdt_unregister(); return ret; } static void __exit exar_wdt_exit(void) { exar_wdt_unregister(); platform_driver_unregister(&exar_wdt_driver); } module_init(exar_wdt_init); module_exit(exar_wdt_exit); MODULE_AUTHOR("David Müller <d.mueller@elsoft.ch>"); MODULE_DESCRIPTION("Exar/MaxLinear Watchdog Driver"); MODULE_LICENSE("GPL"); |