2 * Copyright (C) 2016 National Instruments Corp.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/acpi.h>
16 #include <linux/bitops.h>
17 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/watchdog.h>
26 #define WDT_CTRL_RESET_EN BIT(7)
27 #define WDT_RELOAD_PORT_EN BIT(7)
30 #define WDT_RELOAD_CTRL 2
31 #define WDT_PRESET_PRESCALE 4
32 #define WDT_REG_LOCK 5
34 #define WDT_RELOAD_PORT 7
36 #define WDT_MIN_TIMEOUT 1
37 #define WDT_MAX_TIMEOUT 464
38 #define WDT_DEFAULT_TIMEOUT 80
40 #define WDT_MAX_COUNTER 15
42 static unsigned int timeout
;
43 module_param(timeout
, uint
, 0);
44 MODULE_PARM_DESC(timeout
,
45 "Watchdog timeout in seconds. (default="
46 __MODULE_STRING(WDT_DEFAULT_TIMEOUT
) ")");
48 static bool nowayout
= WATCHDOG_NOWAYOUT
;
49 module_param(nowayout
, bool, 0);
50 MODULE_PARM_DESC(nowayout
,
51 "Watchdog cannot be stopped once started. (default="
52 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
57 struct watchdog_device wdd
;
60 struct nic7018_config
{
65 static const struct nic7018_config nic7018_configs
[] = {
70 static inline u32
nic7018_timeout(u32 period
, u8 counter
)
72 return period
* counter
- period
/ 2;
75 static const struct nic7018_config
*nic7018_get_config(u32 timeout
,
78 const struct nic7018_config
*config
;
81 if (timeout
< 30 && timeout
!= 16) {
82 config
= &nic7018_configs
[0];
83 count
= timeout
/ 2 + 1;
85 config
= &nic7018_configs
[1];
86 count
= DIV_ROUND_UP(timeout
+ 16, 32);
88 if (count
> WDT_MAX_COUNTER
)
89 count
= WDT_MAX_COUNTER
;
95 static int nic7018_set_timeout(struct watchdog_device
*wdd
,
98 struct nic7018_wdt
*wdt
= watchdog_get_drvdata(wdd
);
99 const struct nic7018_config
*config
;
102 config
= nic7018_get_config(timeout
, &counter
);
104 outb(counter
<< 4 | config
->divider
,
105 wdt
->io_base
+ WDT_PRESET_PRESCALE
);
107 wdd
->timeout
= nic7018_timeout(config
->period
, counter
);
108 wdt
->period
= config
->period
;
113 static int nic7018_start(struct watchdog_device
*wdd
)
115 struct nic7018_wdt
*wdt
= watchdog_get_drvdata(wdd
);
118 nic7018_set_timeout(wdd
, wdd
->timeout
);
120 control
= inb(wdt
->io_base
+ WDT_RELOAD_CTRL
);
121 outb(control
| WDT_RELOAD_PORT_EN
, wdt
->io_base
+ WDT_RELOAD_CTRL
);
123 outb(1, wdt
->io_base
+ WDT_RELOAD_PORT
);
125 control
= inb(wdt
->io_base
+ WDT_CTRL
);
126 outb(control
| WDT_CTRL_RESET_EN
, wdt
->io_base
+ WDT_CTRL
);
131 static int nic7018_stop(struct watchdog_device
*wdd
)
133 struct nic7018_wdt
*wdt
= watchdog_get_drvdata(wdd
);
135 outb(0, wdt
->io_base
+ WDT_CTRL
);
136 outb(0, wdt
->io_base
+ WDT_RELOAD_CTRL
);
137 outb(0xF0, wdt
->io_base
+ WDT_PRESET_PRESCALE
);
142 static int nic7018_ping(struct watchdog_device
*wdd
)
144 struct nic7018_wdt
*wdt
= watchdog_get_drvdata(wdd
);
146 outb(1, wdt
->io_base
+ WDT_RELOAD_PORT
);
151 static unsigned int nic7018_get_timeleft(struct watchdog_device
*wdd
)
153 struct nic7018_wdt
*wdt
= watchdog_get_drvdata(wdd
);
156 count
= inb(wdt
->io_base
+ WDT_COUNT
) & 0xF;
160 return nic7018_timeout(wdt
->period
, count
);
163 static const struct watchdog_info nic7018_wdd_info
= {
164 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
165 .identity
= "NIC7018 Watchdog",
168 static const struct watchdog_ops nic7018_wdd_ops
= {
169 .owner
= THIS_MODULE
,
170 .start
= nic7018_start
,
171 .stop
= nic7018_stop
,
172 .ping
= nic7018_ping
,
173 .set_timeout
= nic7018_set_timeout
,
174 .get_timeleft
= nic7018_get_timeleft
,
177 static int nic7018_probe(struct platform_device
*pdev
)
179 struct device
*dev
= &pdev
->dev
;
180 struct watchdog_device
*wdd
;
181 struct nic7018_wdt
*wdt
;
182 struct resource
*io_rc
;
185 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
189 platform_set_drvdata(pdev
, wdt
);
191 io_rc
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
193 dev_err(dev
, "missing IO resources\n");
197 if (!devm_request_region(dev
, io_rc
->start
, resource_size(io_rc
),
199 dev_err(dev
, "failed to get IO region\n");
203 wdt
->io_base
= io_rc
->start
;
205 wdd
->info
= &nic7018_wdd_info
;
206 wdd
->ops
= &nic7018_wdd_ops
;
207 wdd
->min_timeout
= WDT_MIN_TIMEOUT
;
208 wdd
->max_timeout
= WDT_MAX_TIMEOUT
;
209 wdd
->timeout
= WDT_DEFAULT_TIMEOUT
;
212 watchdog_set_drvdata(wdd
, wdt
);
213 watchdog_set_nowayout(wdd
, nowayout
);
215 ret
= watchdog_init_timeout(wdd
, timeout
, dev
);
217 dev_warn(dev
, "unable to set timeout value, using default\n");
219 /* Unlock WDT register */
220 outb(UNLOCK
, wdt
->io_base
+ WDT_REG_LOCK
);
222 ret
= watchdog_register_device(wdd
);
224 outb(LOCK
, wdt
->io_base
+ WDT_REG_LOCK
);
225 dev_err(dev
, "failed to register watchdog\n");
229 dev_dbg(dev
, "io_base=0x%04X, timeout=%d, nowayout=%d\n",
230 wdt
->io_base
, timeout
, nowayout
);
234 static int nic7018_remove(struct platform_device
*pdev
)
236 struct nic7018_wdt
*wdt
= platform_get_drvdata(pdev
);
238 watchdog_unregister_device(&wdt
->wdd
);
240 /* Lock WDT register */
241 outb(LOCK
, wdt
->io_base
+ WDT_REG_LOCK
);
246 static const struct acpi_device_id nic7018_device_ids
[] = {
250 MODULE_DEVICE_TABLE(acpi
, nic7018_device_ids
);
252 static struct platform_driver watchdog_driver
= {
253 .probe
= nic7018_probe
,
254 .remove
= nic7018_remove
,
256 .name
= KBUILD_MODNAME
,
257 .acpi_match_table
= ACPI_PTR(nic7018_device_ids
),
261 module_platform_driver(watchdog_driver
);
263 MODULE_DESCRIPTION("National Instruments NIC7018 Watchdog driver");
264 MODULE_AUTHOR("Hui Chun Ong <hui.chun.ong@ni.com>");
265 MODULE_LICENSE("GPL");