blob: a793b03a785db1f6fded8fb8670d57f21b24a4c8 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * PC Watchdog Driver
4 * by Ken Hollis (khollis@bitgate.com)
5 *
Wim Van Sebroeckf9146f22007-01-09 22:38:54 +01006 * Permission granted from Simon Machell (smachell@berkprod.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Written for the Linux Kernel, and GPLed by Ken Hollis
8 *
9 * 960107 Added request_region routines, modulized the whole thing.
10 * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
11 * WD_TIMEOUT define.
12 * 960216 Added eof marker on the file, and changed verbose messages.
13 * 960716 Made functional and cosmetic changes to the source for
14 * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
15 * 960717 Removed read/seek routines, replaced with ioctl. Also, added
16 * check_region command due to Alan's suggestion.
17 * 960821 Made changes to compile in newer 2.0.x kernels. Added
18 * "cold reboot sense" entry.
19 * 960825 Made a few changes to code, deleted some defines and made
20 * typedefs to replace them. Made heartbeat reset only available
21 * via ioctl, and removed the write routine.
22 * 960828 Added new items for PC Watchdog Rev.C card.
23 * 960829 Changed around all of the IOCTLs, added new features,
24 * added watchdog disable/re-enable routines. Added firmware
25 * version reporting. Added read routine for temperature.
26 * Removed some extra defines, added an autodetect Revision
27 * routine.
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000028 * 961006 Revised some documentation, fixed some cosmetic bugs. Made
29 * drivers to panic the system if it's overheating at bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 961118 Changed some verbiage on some of the output, tidied up
31 * code bits, and added compatibility to 2.1.x.
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000032 * 970912 Enabled board on open and disable on close.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * 971107 Took account of recent VFS changes (broke read).
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000034 * 971210 Disable board on initialisation in case board already ticking.
35 * 971222 Changed open/close for temperature handling
36 * Michael Meskes <meskes@debian.org>.
37 * 980112 Used minor numbers from include/linux/miscdevice.h
38 * 990403 Clear reset status after reading control status register in
39 * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * 990605 Made changes to code to support Firmware 1.22a, added
41 * fairly useless proc entry.
42 * 990610 removed said useless proc code for the merge <alan>
43 * 000403 Removed last traces of proc code. <davej>
Alan Cox261dcc72008-05-19 14:07:43 +010044 * 011214 Added nowayout module option to override
45 * CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000046 * Added timeout module option to override default
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48
49/*
50 * A bells and whistles driver is available from http://www.pcwd.de/
Alan Cox261dcc72008-05-19 14:07:43 +010051 * More info available at http://www.berkprod.com/ or
52 * http://www.pcwatchdog.com/
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
Joe Perches27c766a2012-02-15 15:06:19 -080055#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010057#include <linux/module.h> /* For module specific items */
58#include <linux/moduleparam.h> /* For new moduleparam's */
59#include <linux/types.h> /* For standard types (like size_t) */
60#include <linux/errno.h> /* For the -ENODEV/... values */
61#include <linux/kernel.h> /* For printk/panic/... */
62#include <linux/delay.h> /* For mdelay function */
63#include <linux/timer.h> /* For timer related operations */
64#include <linux/jiffies.h> /* For jiffies stuff */
Jean Delvare487722c2013-10-21 17:38:49 +020065#include <linux/miscdevice.h> /* For struct miscdevice */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010066#include <linux/watchdog.h> /* For the watchdog specific items */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000067#include <linux/reboot.h> /* For kernel_power_off() */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010068#include <linux/init.h> /* For __init/__exit/... */
69#include <linux/fs.h> /* For file operations */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000070#include <linux/isa.h> /* For isa devices */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010071#include <linux/ioport.h> /* For io-port access */
72#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Alan Cox261dcc72008-05-19 14:07:43 +010073#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
74#include <linux/io.h> /* For inb/outb/... */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010075
76/* Module and version information */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000077#define WATCHDOG_VERSION "1.20"
78#define WATCHDOG_DATE "18 Feb 2007"
Wim Van Sebroecka7122f92006-01-09 22:07:22 +010079#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
80#define WATCHDOG_NAME "pcwd"
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000081#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * It should be noted that PCWD_REVISION_B was removed because A and B
85 * are essentially the same types of card, with the exception that B
86 * has temperature reporting. Since I didn't receive a Rev.B card,
87 * the Rev.B card is not supported. (It's a good thing too, as they
88 * are no longer in production.)
89 */
90#define PCWD_REVISION_A 1
91#define PCWD_REVISION_C 2
92
93/*
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000094 * These are the auto-probe addresses available.
95 *
96 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
97 * Revision A has an address range of 2 addresses, while Revision C has 4.
98 */
99#define PCWD_ISA_NR_CARDS 3
100static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
101
102/*
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100103 * These are the defines that describe the control status bits for the
104 * PCI-PC Watchdog card.
105*/
106/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100107#define WD_WDRST 0x01 /* Previously reset state */
108#define WD_T110 0x02 /* Temperature overheat sense */
109#define WD_HRTBT 0x04 /* Heartbeat sense */
110#define WD_RLY2 0x08 /* External relay triggered */
111#define WD_SRLY2 0x80 /* Software external relay triggered */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100112/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100113#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
114#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
115#define WD_REVC_TTRP 0x04 /* Temperature Trip status */
Alan Cox261dcc72008-05-19 14:07:43 +0100116#define WD_REVC_RL2A 0x08 /* Relay 2 activated by
117 on-board processor */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100118#define WD_REVC_RL1A 0x10 /* Relay 1 active */
119#define WD_REVC_R2DS 0x40 /* Relay 2 disable */
120#define WD_REVC_RLY2 0x80 /* Relay 2 activated? */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100121/* Port 2 : Control Status #2 */
122#define WD_WDIS 0x10 /* Watchdog Disabled */
123#define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
Alan Cox261dcc72008-05-19 14:07:43 +0100124#define WD_SSEL 0x40 /* Watchdog Switch Select
125 (1:SW1 <-> 0:SW2) */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100126#define WD_WCMD 0x80 /* Watchdog Command Mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* max. time we give an ISA watchdog card to process a command */
129/* 500ms for each 4 bit response (according to spec.) */
130#define ISA_COMMAND_TIMEOUT 1000
131
132/* Watchdog's internal commands */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100133#define CMD_ISA_IDLE 0x00
134#define CMD_ISA_VERSION_INTEGER 0x01
135#define CMD_ISA_VERSION_TENTH 0x02
136#define CMD_ISA_VERSION_HUNDRETH 0x03
137#define CMD_ISA_VERSION_MINOR 0x04
138#define CMD_ISA_SWITCH_SETTINGS 0x05
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100139#define CMD_ISA_RESET_PC 0x06
140#define CMD_ISA_ARM_0 0x07
141#define CMD_ISA_ARM_30 0x08
142#define CMD_ISA_ARM_60 0x09
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100143#define CMD_ISA_DELAY_TIME_2SECS 0x0A
144#define CMD_ISA_DELAY_TIME_4SECS 0x0B
145#define CMD_ISA_DELAY_TIME_8SECS 0x0C
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100146#define CMD_ISA_RESET_RELAYS 0x0D
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100148/* Watchdog's Dip Switch heartbeat values */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000149static const int heartbeat_tbl[] = {
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100150 20, /* OFF-OFF-OFF = 20 Sec */
151 40, /* OFF-OFF-ON = 40 Sec */
152 60, /* OFF-ON-OFF = 1 Min */
153 300, /* OFF-ON-ON = 5 Min */
154 600, /* ON-OFF-OFF = 10 Min */
155 1800, /* ON-OFF-ON = 30 Min */
156 3600, /* ON-ON-OFF = 1 Hour */
157 7200, /* ON-ON-ON = 2 hour */
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * We are using an kernel timer to do the pinging of the watchdog
162 * every ~500ms. We try to set the internal heartbeat of the
163 * watchdog to 2 ms.
164 */
165
166#define WDT_INTERVAL (HZ/2+1)
167
168/* We can only use 1 card due to the /dev/watchdog restriction */
169static int cards_found;
170
171/* internal variables */
Wim Van Sebroeck36cbaa82008-08-10 21:57:03 +0000172static unsigned long open_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static char expect_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int temp_panic;
Alan Cox261dcc72008-05-19 14:07:43 +0100175
176/* this is private data for each ISA-PC watchdog card */
177static struct {
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100178 char fw_ver_str[6]; /* The cards firmware version */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100179 int revision; /* The card's revision */
Alan Cox261dcc72008-05-19 14:07:43 +0100180 int supports_temp; /* Whether or not the card has
181 a temperature device */
182 int command_mode; /* Whether or not the card is in
183 command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100184 int boot_status; /* The card's boot status */
185 int io_addr; /* The cards I/O address */
186 spinlock_t io_lock; /* the lock for io operations */
187 struct timer_list timer; /* The timer that pings the watchdog */
188 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
189} pcwd_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* module parameters */
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100192#define QUIET 0 /* Default */
193#define VERBOSE 1 /* Verbose */
194#define DEBUG 2 /* print fancy stuff too */
195static int debug = QUIET;
196module_param(debug, int, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100197MODULE_PARM_DESC(debug,
198 "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100199
Alan Cox261dcc72008-05-19 14:07:43 +0100200/* default heartbeat = delay-time from dip-switches */
201#define WATCHDOG_HEARTBEAT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int heartbeat = WATCHDOG_HEARTBEAT;
203module_param(heartbeat, int, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000204MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
205 "(2 <= heartbeat <= 7200 or 0=delay-time from dip-switches, default="
206 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100208static bool nowayout = WATCHDOG_NOWAYOUT;
209module_param(nowayout, bool, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100210MODULE_PARM_DESC(nowayout,
211 "Watchdog cannot be stopped once started (default="
212 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214/*
215 * Internal functions
216 */
217
218static int send_isa_command(int cmd)
219{
220 int i;
221 int control_status;
222 int port0, last_port0; /* Double read for stabilising */
223
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100224 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800225 pr_debug("sending following data cmd=0x%02x\n", cmd);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* The WCMD bit must be 1 and the command is only 4 bits in size */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100228 control_status = (cmd & 0x0F) | WD_WCMD;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100229 outb_p(control_status, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 udelay(ISA_COMMAND_TIMEOUT);
231
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100232 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 for (i = 0; i < 25; ++i) {
234 last_port0 = port0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100235 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (port0 == last_port0)
238 break; /* Data is stable */
239
Alan Cox261dcc72008-05-19 14:07:43 +0100240 udelay(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100243 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800244 pr_debug("received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
245 cmd, port0, last_port0);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return port0;
248}
249
250static int set_command_mode(void)
251{
Alan Cox261dcc72008-05-19 14:07:43 +0100252 int i, found = 0, count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Set the card into command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100255 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 while ((!found) && (count < 3)) {
257 i = send_isa_command(CMD_ISA_IDLE);
258
259 if (i == 0x00)
260 found = 1;
261 else if (i == 0xF3) {
262 /* Card does not like what we've done to it */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100263 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 udelay(1200); /* Spec says wait 1ms */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100265 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 udelay(ISA_COMMAND_TIMEOUT);
267 }
268 count++;
269 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100270 spin_unlock(&pcwd_private.io_lock);
271 pcwd_private.command_mode = found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100273 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800274 pr_debug("command_mode=%d\n", pcwd_private.command_mode);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100275
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000276 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static void unset_command_mode(void)
280{
281 /* Set the card into normal mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100282 spin_lock(&pcwd_private.io_lock);
283 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100285 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100287 pcwd_private.command_mode = 0;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100288
289 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800290 pr_debug("command_mode=%d\n", pcwd_private.command_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100293static inline void pcwd_check_temperature_support(void)
294{
295 if (inb(pcwd_private.io_addr) != 0xF0)
296 pcwd_private.supports_temp = 1;
297}
298
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100299static inline void pcwd_get_firmware(void)
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100300{
301 int one, ten, hund, minor;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100302
Wim Van Sebroeck6bbc20b2006-03-02 20:05:16 +0100303 strcpy(pcwd_private.fw_ver_str, "ERROR");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100304
305 if (set_command_mode()) {
306 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
307 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
308 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
309 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
Alan Cox261dcc72008-05-19 14:07:43 +0100310 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
311 one, ten, hund, minor);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100312 }
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100313 unset_command_mode();
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100314
315 return;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100316}
317
318static inline int pcwd_get_option_switches(void)
319{
Alan Cox261dcc72008-05-19 14:07:43 +0100320 int option_switches = 0;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100321
322 if (set_command_mode()) {
323 /* Get switch settings */
324 option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
325 }
326
327 unset_command_mode();
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000328 return option_switches;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100329}
330
331static void pcwd_show_card_info(void)
332{
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100333 int option_switches;
334
335 /* Get some extra info from the hardware (in command/debug/diag mode) */
336 if (pcwd_private.revision == PCWD_REVISION_A)
Joe Perches27c766a2012-02-15 15:06:19 -0800337 pr_info("ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
338 pcwd_private.io_addr);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100339 else if (pcwd_private.revision == PCWD_REVISION_C) {
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100340 pcwd_get_firmware();
Joe Perches27c766a2012-02-15 15:06:19 -0800341 pr_info("ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100342 pcwd_private.io_addr, pcwd_private.fw_ver_str);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100343 option_switches = pcwd_get_option_switches();
Joe Perches27c766a2012-02-15 15:06:19 -0800344 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100345 option_switches,
346 ((option_switches & 0x10) ? "ON" : "OFF"),
347 ((option_switches & 0x08) ? "ON" : "OFF"));
348
349 /* Reprogram internal heartbeat to 2 seconds */
350 if (set_command_mode()) {
351 send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
352 unset_command_mode();
353 }
354 }
355
356 if (pcwd_private.supports_temp)
Joe Perches27c766a2012-02-15 15:06:19 -0800357 pr_info("Temperature Option Detected\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100358
359 if (pcwd_private.boot_status & WDIOF_CARDRESET)
Joe Perches27c766a2012-02-15 15:06:19 -0800360 pr_info("Previous reboot was caused by the card\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100361
362 if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
Joe Perches27c766a2012-02-15 15:06:19 -0800363 pr_emerg("Card senses a CPU Overheat. Panicking!\n");
364 pr_emerg("CPU Overheat\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100365 }
366
367 if (pcwd_private.boot_status == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800368 pr_info("No previous trip detected - Cold boot or reset\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100369}
370
Kees Cooke99e88a2017-10-16 14:43:17 -0700371static void pcwd_timer_ping(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int wdrst_stat;
374
375 /* If we got a heartbeat pulse within the WDT_INTERVAL
376 * we agree to ping the WDT */
Alan Cox261dcc72008-05-19 14:07:43 +0100377 if (time_before(jiffies, pcwd_private.next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Ping the watchdog */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100379 spin_lock(&pcwd_private.io_lock);
380 if (pcwd_private.revision == PCWD_REVISION_A) {
Alan Cox261dcc72008-05-19 14:07:43 +0100381 /* Rev A cards are reset by setting the
382 WD_WDRST bit in register 1 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100383 wdrst_stat = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 wdrst_stat &= 0x0F;
385 wdrst_stat |= WD_WDRST;
386
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100387 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else {
389 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100390 outb_p(0x00, pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 /* Re-set the timer interval */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100394 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100396 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800398 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400}
401
402static int pcwd_start(void)
403{
404 int stat_reg;
405
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100406 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Start the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100409 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* Enable the port */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100412 if (pcwd_private.revision == PCWD_REVISION_C) {
413 spin_lock(&pcwd_private.io_lock);
414 outb_p(0x00, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100416 stat_reg = inb_p(pcwd_private.io_addr + 2);
417 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100418 if (stat_reg & WD_WDIS) {
Joe Perches27c766a2012-02-15 15:06:19 -0800419 pr_info("Could not start watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EIO;
421 }
422 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100423
424 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800425 pr_debug("Watchdog started\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
430static int pcwd_stop(void)
431{
432 int stat_reg;
433
434 /* Stop the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100435 del_timer(&pcwd_private.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* Disable the board */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100438 if (pcwd_private.revision == PCWD_REVISION_C) {
439 spin_lock(&pcwd_private.io_lock);
440 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100442 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100444 stat_reg = inb_p(pcwd_private.io_addr + 2);
445 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100446 if ((stat_reg & WD_WDIS) == 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800447 pr_info("Could not stop watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -EIO;
449 }
450 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100451
452 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800453 pr_debug("Watchdog stopped\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456}
457
458static int pcwd_keepalive(void)
459{
460 /* user land ping */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100461 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100462
463 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800464 pr_debug("Watchdog keepalive signal send\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467}
468
469static int pcwd_set_heartbeat(int t)
470{
Alan Cox261dcc72008-05-19 14:07:43 +0100471 if (t < 2 || t > 7200) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EINVAL;
473
474 heartbeat = t;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100475
476 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800477 pr_debug("New heartbeat: %d\n", heartbeat);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
482static int pcwd_get_status(int *status)
483{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100484 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Alan Cox261dcc72008-05-19 14:07:43 +0100486 *status = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100487 spin_lock(&pcwd_private.io_lock);
488 if (pcwd_private.revision == PCWD_REVISION_A)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Rev A cards return status information from
490 * the base register, which is used for the
491 * temperature in other cards. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100492 control_status = inb(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 else {
494 /* Rev C cards return card status in the base
495 * address + 1 register. And use different bits
496 * to indicate a card initiated reset, and an
497 * over-temperature condition. And the reboot
498 * status can be reset. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100499 control_status = inb(pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100501 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100503 if (pcwd_private.revision == PCWD_REVISION_A) {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100504 if (control_status & WD_WDRST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *status |= WDIOF_CARDRESET;
506
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100507 if (control_status & WD_T110) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *status |= WDIOF_OVERHEAT;
509 if (temp_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -0800510 pr_info("Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600511 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
514 } else {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100515 if (control_status & WD_REVC_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *status |= WDIOF_CARDRESET;
517
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100518 if (control_status & WD_REVC_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *status |= WDIOF_OVERHEAT;
520 if (temp_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -0800521 pr_info("Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600522 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 }
525 }
526
527 return 0;
528}
529
530static int pcwd_clear_status(void)
531{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100532 int control_status;
533
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100534 if (pcwd_private.revision == PCWD_REVISION_C) {
535 spin_lock(&pcwd_private.io_lock);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100536
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100537 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800538 pr_info("clearing watchdog trip status\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100539
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100540 control_status = inb_p(pcwd_private.io_addr + 1);
541
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100542 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800543 pr_debug("status was: 0x%02x\n", control_status);
544 pr_debug("sending: 0x%02x\n",
545 (control_status & WD_REVC_R2DS));
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100546 }
547
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100548 /* clear reset status & Keep Relay 2 disable state as it is */
Alan Cox261dcc72008-05-19 14:07:43 +0100549 outb_p((control_status & WD_REVC_R2DS),
550 pcwd_private.io_addr + 1);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100551
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100552 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 return 0;
555}
556
557static int pcwd_get_temperature(int *temperature)
558{
559 /* check that port 0 gives temperature info and no command results */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100560 if (pcwd_private.command_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -1;
562
563 *temperature = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100564 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -ENODEV;
566
567 /*
568 * Convert celsius to fahrenheit, since this was
569 * the decided 'standard' for this return value.
570 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100571 spin_lock(&pcwd_private.io_lock);
572 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
573 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100575 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800576 pr_debug("temperature is: %d F\n", *temperature);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
581
582/*
583 * /dev/watchdog handling
584 */
585
Alan Cox261dcc72008-05-19 14:07:43 +0100586static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 int rv;
589 int status;
590 int temperature;
591 int new_heartbeat;
592 int __user *argp = (int __user *)arg;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000593 static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 .options = WDIOF_OVERHEAT |
595 WDIOF_CARDRESET |
596 WDIOF_KEEPALIVEPING |
597 WDIOF_SETTIMEOUT |
598 WDIOF_MAGICCLOSE,
599 .firmware_version = 1,
600 .identity = "PCWD",
601 };
602
Alan Cox261dcc72008-05-19 14:07:43 +0100603 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 case WDIOC_GETSUPPORT:
Alan Cox261dcc72008-05-19 14:07:43 +0100605 if (copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EFAULT;
607 return 0;
608
609 case WDIOC_GETSTATUS:
610 pcwd_get_status(&status);
611 return put_user(status, argp);
612
613 case WDIOC_GETBOOTSTATUS:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100614 return put_user(pcwd_private.boot_status, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 case WDIOC_GETTEMP:
617 if (pcwd_get_temperature(&temperature))
618 return -EFAULT;
619
620 return put_user(temperature, argp);
621
622 case WDIOC_SETOPTIONS:
Alan Cox261dcc72008-05-19 14:07:43 +0100623 if (pcwd_private.revision == PCWD_REVISION_C) {
624 if (get_user(rv, argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EFAULT;
626
Alan Cox261dcc72008-05-19 14:07:43 +0100627 if (rv & WDIOS_DISABLECARD) {
628 status = pcwd_stop();
629 if (status < 0)
630 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Alan Cox261dcc72008-05-19 14:07:43 +0100632 if (rv & WDIOS_ENABLECARD) {
633 status = pcwd_start();
634 if (status < 0)
635 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (rv & WDIOS_TEMPPANIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 temp_panic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 return -EINVAL;
641
642 case WDIOC_KEEPALIVE:
643 pcwd_keepalive();
644 return 0;
645
646 case WDIOC_SETTIMEOUT:
647 if (get_user(new_heartbeat, argp))
648 return -EFAULT;
649
650 if (pcwd_set_heartbeat(new_heartbeat))
651 return -EINVAL;
652
653 pcwd_keepalive();
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500654 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 case WDIOC_GETTIMEOUT:
657 return put_user(heartbeat, argp);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000658
659 default:
660 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 return 0;
664}
665
666static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
667 loff_t *ppos)
668{
669 if (len) {
670 if (!nowayout) {
671 size_t i;
672
673 /* In case it was set long ago */
674 expect_close = 0;
675
676 for (i = 0; i != len; i++) {
677 char c;
678
679 if (get_user(c, buf + i))
680 return -EFAULT;
681 if (c == 'V')
682 expect_close = 42;
683 }
684 }
685 pcwd_keepalive();
686 }
687 return len;
688}
689
690static int pcwd_open(struct inode *inode, struct file *file)
691{
Alan Cox261dcc72008-05-19 14:07:43 +0100692 if (test_and_set_bit(0, &open_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (nowayout)
695 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Activate */
697 pcwd_start();
698 pcwd_keepalive();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300699 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static int pcwd_close(struct inode *inode, struct file *file)
703{
Alan Cox261dcc72008-05-19 14:07:43 +0100704 if (expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 pcwd_stop();
Alan Cox261dcc72008-05-19 14:07:43 +0100706 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800707 pr_crit("Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 pcwd_keepalive();
709 }
710 expect_close = 0;
Alan Cox261dcc72008-05-19 14:07:43 +0100711 clear_bit(0, &open_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
715/*
716 * /dev/temperature handling
717 */
718
719static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
720 loff_t *ppos)
721{
722 int temperature;
723
724 if (pcwd_get_temperature(&temperature))
725 return -EFAULT;
726
727 if (copy_to_user(buf, &temperature, 1))
728 return -EFAULT;
729
730 return 1;
731}
732
733static int pcwd_temp_open(struct inode *inode, struct file *file)
734{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100735 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return -ENODEV;
737
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300738 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741static int pcwd_temp_close(struct inode *inode, struct file *file)
742{
743 return 0;
744}
745
746/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * Kernel Interfaces
748 */
749
Arjan van de Ven62322d22006-07-03 00:24:21 -0700750static const struct file_operations pcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .owner = THIS_MODULE,
752 .llseek = no_llseek,
753 .write = pcwd_write,
Alan Cox261dcc72008-05-19 14:07:43 +0100754 .unlocked_ioctl = pcwd_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200755 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .open = pcwd_open,
757 .release = pcwd_close,
758};
759
760static struct miscdevice pcwd_miscdev = {
761 .minor = WATCHDOG_MINOR,
762 .name = "watchdog",
763 .fops = &pcwd_fops,
764};
765
Arjan van de Ven62322d22006-07-03 00:24:21 -0700766static const struct file_operations pcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 .owner = THIS_MODULE,
768 .llseek = no_llseek,
769 .read = pcwd_temp_read,
770 .open = pcwd_temp_open,
771 .release = pcwd_temp_close,
772};
773
774static struct miscdevice temp_miscdev = {
775 .minor = TEMP_MINOR,
776 .name = "temperature",
777 .fops = &pcwd_temp_fops,
778};
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*
781 * Init & exit routines
782 */
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784static inline int get_revision(void)
785{
786 int r = PCWD_REVISION_C;
787
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100788 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* REV A cards use only 2 io ports; test
790 * presumes a floating bus reads as 0xff. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100791 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
792 (inb(pcwd_private.io_addr + 3) == 0xFF))
Alan Cox261dcc72008-05-19 14:07:43 +0100793 r = PCWD_REVISION_A;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100794 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 return r;
797}
798
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000799/*
800 * The ISA cards have a heartbeat bit in one of the registers, which
801 * register is card dependent. The heartbeat bit is monitored, and if
802 * found, is considered proof that a Berkshire card has been found.
803 * The initial rate is once per second at board start up, then twice
804 * per second for normal operation.
805 */
Bill Pemberton2d991a12012-11-19 13:21:41 -0500806static int pcwd_isa_match(struct device *dev, unsigned int id)
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000807{
Alan Cox261dcc72008-05-19 14:07:43 +0100808 int base_addr = pcwd_ioports[id];
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000809 int port0, last_port0; /* Reg 0, in case it's REV A */
810 int port1, last_port1; /* Register 1 for REV C cards */
811 int i;
812 int retval;
813
814 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800815 pr_debug("pcwd_isa_match id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000816
Alan Cox261dcc72008-05-19 14:07:43 +0100817 if (!request_region(base_addr, 4, "PCWD")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800818 pr_info("Port 0x%04x unavailable\n", base_addr);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000819 return 0;
820 }
821
822 retval = 0;
823
824 port0 = inb_p(base_addr); /* For REV A boards */
825 port1 = inb_p(base_addr + 1); /* For REV C boards */
826 if (port0 != 0xff || port1 != 0xff) {
827 /* Not an 'ff' from a floating bus, so must be a card! */
828 for (i = 0; i < 4; ++i) {
829
830 msleep(500);
831
832 last_port0 = port0;
833 last_port1 = port1;
834
835 port0 = inb_p(base_addr);
836 port1 = inb_p(base_addr + 1);
837
838 /* Has either hearbeat bit changed? */
839 if ((port0 ^ last_port0) & WD_HRTBT ||
840 (port1 ^ last_port1) & WD_REVC_HRBT) {
841 retval = 1;
842 break;
843 }
844 }
845 }
Alan Cox261dcc72008-05-19 14:07:43 +0100846 release_region(base_addr, 4);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000847
848 return retval;
849}
850
Bill Pemberton2d991a12012-11-19 13:21:41 -0500851static int pcwd_isa_probe(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000855 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800856 pr_debug("pcwd_isa_probe id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 cards_found++;
859 if (cards_found == 1)
Joe Perches27c766a2012-02-15 15:06:19 -0800860 pr_info("v%s Ken Hollis (kenji@bitgate.com)\n",
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000861 WATCHDOG_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (cards_found > 1) {
Joe Perches27c766a2012-02-15 15:06:19 -0800864 pr_err("This driver only supports 1 device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return -ENODEV;
866 }
867
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000868 if (pcwd_ioports[id] == 0x0000) {
Joe Perches27c766a2012-02-15 15:06:19 -0800869 pr_err("No I/O-Address for card detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -ENODEV;
871 }
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000872 pcwd_private.io_addr = pcwd_ioports[id];
873
874 spin_lock_init(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Check card's revision */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100877 pcwd_private.revision = get_revision();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Alan Cox261dcc72008-05-19 14:07:43 +0100879 if (!request_region(pcwd_private.io_addr,
880 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800881 pr_err("I/O address 0x%04x already in use\n",
882 pcwd_private.io_addr);
Alan Cox261dcc72008-05-19 14:07:43 +0100883 ret = -EIO;
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000884 goto error_request_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
887 /* Initial variables */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100888 pcwd_private.supports_temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 temp_panic = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100890 pcwd_private.boot_status = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /* get the boot_status */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100893 pcwd_get_status(&pcwd_private.boot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* clear the "card caused reboot" flag */
896 pcwd_clear_status();
897
Kees Cooke99e88a2017-10-16 14:43:17 -0700898 timer_setup(&pcwd_private.timer, pcwd_timer_ping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Disable the board */
901 pcwd_stop();
902
903 /* Check whether or not the card supports the temperature device */
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100904 pcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100906 /* Show info about the card itself */
907 pcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100909 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
910 if (heartbeat == 0)
911 heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
912
Alan Cox261dcc72008-05-19 14:07:43 +0100913 /* Check that the heartbeat value is within it's range;
914 if not reset to the default */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100915 if (pcwd_set_heartbeat(heartbeat)) {
916 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
Joe Perches27c766a2012-02-15 15:06:19 -0800917 pr_info("heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
918 WATCHDOG_HEARTBEAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100921 if (pcwd_private.supports_temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ret = misc_register(&temp_miscdev);
923 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800924 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
925 TEMP_MINOR, ret);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000926 goto error_misc_register_temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 }
929
930 ret = misc_register(&pcwd_miscdev);
931 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800932 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
933 WATCHDOG_MINOR, ret);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000934 goto error_misc_register_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Joe Perches27c766a2012-02-15 15:06:19 -0800937 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 heartbeat, nowayout);
939
940 return 0;
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000941
942error_misc_register_watchdog:
943 if (pcwd_private.supports_temp)
944 misc_deregister(&temp_miscdev);
945error_misc_register_temp:
Alan Cox261dcc72008-05-19 14:07:43 +0100946 release_region(pcwd_private.io_addr,
947 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000948error_request_region:
949 pcwd_private.io_addr = 0x0000;
950 cards_found--;
951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Uwe Kleine-König30e88d02021-01-22 10:24:49 +0100954static void pcwd_isa_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000956 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800957 pr_debug("pcwd_isa_remove id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* Disable the board */
960 if (!nowayout)
961 pcwd_stop();
962
963 /* Deregister */
964 misc_deregister(&pcwd_miscdev);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100965 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 misc_deregister(&temp_miscdev);
Alan Cox261dcc72008-05-19 14:07:43 +0100967 release_region(pcwd_private.io_addr,
968 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100969 pcwd_private.io_addr = 0x0000;
Wim Van Sebroeckf1c3a052005-12-10 14:36:24 +0100970 cards_found--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000973static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
974{
975 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800976 pr_debug("pcwd_isa_shutdown id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000977
978 pcwd_stop();
979}
980
981static struct isa_driver pcwd_isa_driver = {
982 .match = pcwd_isa_match,
983 .probe = pcwd_isa_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500984 .remove = pcwd_isa_remove,
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000985 .shutdown = pcwd_isa_shutdown,
986 .driver = {
987 .owner = THIS_MODULE,
988 .name = WATCHDOG_NAME,
989 },
990};
991
William Breathitt Grayb7a8c422016-06-01 09:05:48 -0400992module_isa_driver(pcwd_isa_driver, PCWD_ISA_NR_CARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000994MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, "
995 "Wim Van Sebroeck <wim@iguana.be>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000997MODULE_VERSION(WATCHDOG_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998MODULE_LICENSE("GPL");