Marcus Folkesson | 2e62c49 | 2018-03-16 16:14:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog timer driver for the WinSystems EBC-C384 |
| 4 | * Copyright (C) 2016 William Breathitt Gray |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/device.h> |
| 7 | #include <linux/dmi.h> |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/ioport.h> |
William Breathitt Gray | 4ef1bec | 2016-05-01 18:44:26 -0400 | [diff] [blame] | 11 | #include <linux/isa.h> |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/watchdog.h> |
| 17 | |
| 18 | #define MODULE_NAME "ebc-c384_wdt" |
| 19 | #define WATCHDOG_TIMEOUT 60 |
| 20 | /* |
| 21 | * The timeout value in minutes must fit in a single byte when sent to the |
| 22 | * watchdog timer; the maximum timeout possible is 15300 (255 * 60) seconds. |
| 23 | */ |
| 24 | #define WATCHDOG_MAX_TIMEOUT 15300 |
| 25 | #define BASE_ADDR 0x564 |
| 26 | #define ADDR_EXTENT 5 |
| 27 | #define CFG_ADDR (BASE_ADDR + 1) |
| 28 | #define PET_ADDR (BASE_ADDR + 2) |
| 29 | |
| 30 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 31 | module_param(nowayout, bool, 0); |
| 32 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 33 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 34 | |
| 35 | static unsigned timeout; |
| 36 | module_param(timeout, uint, 0); |
| 37 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default=" |
| 38 | __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); |
| 39 | |
| 40 | static int ebc_c384_wdt_start(struct watchdog_device *wdev) |
| 41 | { |
| 42 | unsigned t = wdev->timeout; |
| 43 | |
| 44 | /* resolution is in minutes for timeouts greater than 255 seconds */ |
| 45 | if (t > 255) |
| 46 | t = DIV_ROUND_UP(t, 60); |
| 47 | |
| 48 | outb(t, PET_ADDR); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int ebc_c384_wdt_stop(struct watchdog_device *wdev) |
| 54 | { |
| 55 | outb(0x00, PET_ADDR); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t) |
| 61 | { |
| 62 | /* resolution is in minutes for timeouts greater than 255 seconds */ |
| 63 | if (t > 255) { |
| 64 | /* round second resolution up to minute granularity */ |
| 65 | wdev->timeout = roundup(t, 60); |
| 66 | |
| 67 | /* set watchdog timer for minutes */ |
| 68 | outb(0x00, CFG_ADDR); |
| 69 | } else { |
| 70 | wdev->timeout = t; |
| 71 | |
| 72 | /* set watchdog timer for seconds */ |
| 73 | outb(0x80, CFG_ADDR); |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static const struct watchdog_ops ebc_c384_wdt_ops = { |
| 80 | .start = ebc_c384_wdt_start, |
| 81 | .stop = ebc_c384_wdt_stop, |
| 82 | .set_timeout = ebc_c384_wdt_set_timeout |
| 83 | }; |
| 84 | |
| 85 | static const struct watchdog_info ebc_c384_wdt_info = { |
| 86 | .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT, |
| 87 | .identity = MODULE_NAME |
| 88 | }; |
| 89 | |
William Breathitt Gray | 4ef1bec | 2016-05-01 18:44:26 -0400 | [diff] [blame] | 90 | static int ebc_c384_wdt_probe(struct device *dev, unsigned int id) |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 91 | { |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 92 | struct watchdog_device *wdd; |
| 93 | |
| 94 | if (!devm_request_region(dev, BASE_ADDR, ADDR_EXTENT, dev_name(dev))) { |
| 95 | dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n", |
| 96 | BASE_ADDR, BASE_ADDR + ADDR_EXTENT); |
| 97 | return -EBUSY; |
| 98 | } |
| 99 | |
| 100 | wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL); |
| 101 | if (!wdd) |
| 102 | return -ENOMEM; |
| 103 | |
| 104 | wdd->info = &ebc_c384_wdt_info; |
| 105 | wdd->ops = &ebc_c384_wdt_ops; |
| 106 | wdd->timeout = WATCHDOG_TIMEOUT; |
| 107 | wdd->min_timeout = 1; |
| 108 | wdd->max_timeout = WATCHDOG_MAX_TIMEOUT; |
| 109 | |
| 110 | watchdog_set_nowayout(wdd, nowayout); |
Wolfram Sang | cccbf8b | 2019-04-19 20:15:50 +0200 | [diff] [blame] | 111 | watchdog_init_timeout(wdd, timeout, dev); |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 112 | |
William Breathitt Gray | 540aea3 | 2017-01-24 17:02:33 -0500 | [diff] [blame] | 113 | return devm_watchdog_register_device(dev, wdd); |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 114 | } |
| 115 | |
William Breathitt Gray | 4ef1bec | 2016-05-01 18:44:26 -0400 | [diff] [blame] | 116 | static struct isa_driver ebc_c384_wdt_driver = { |
| 117 | .probe = ebc_c384_wdt_probe, |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 118 | .driver = { |
| 119 | .name = MODULE_NAME |
| 120 | }, |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 123 | static int __init ebc_c384_wdt_init(void) |
| 124 | { |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 125 | if (!dmi_match(DMI_BOARD_NAME, "EBC-C384 SBC")) |
| 126 | return -ENODEV; |
| 127 | |
William Breathitt Gray | 4ef1bec | 2016-05-01 18:44:26 -0400 | [diff] [blame] | 128 | return isa_register_driver(&ebc_c384_wdt_driver, 1); |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void __exit ebc_c384_wdt_exit(void) |
| 132 | { |
William Breathitt Gray | 4ef1bec | 2016-05-01 18:44:26 -0400 | [diff] [blame] | 133 | isa_unregister_driver(&ebc_c384_wdt_driver); |
William Breathitt Gray | c36a483 | 2016-02-28 23:54:46 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | module_init(ebc_c384_wdt_init); |
| 137 | module_exit(ebc_c384_wdt_exit); |
| 138 | |
| 139 | MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); |
| 140 | MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver"); |
| 141 | MODULE_LICENSE("GPL v2"); |
William Breathitt Gray | 4ef1bec | 2016-05-01 18:44:26 -0400 | [diff] [blame] | 142 | MODULE_ALIAS("isa:" MODULE_NAME); |