blob: 54d86fcb1837f763313c778b7687fee7b40d3104 [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Berkshire PCI-PC Watchdog Card Driver
4 *
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +01005 * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on source code of the following authors:
8 * Ken Hollis <kenji@bitgate.com>,
9 * Lindsay Harris <lindsay@bluegum.com>,
Alan Cox29fa0582008-10-27 15:17:56 +000010 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Matt Domsch <Matt_Domsch@dell.com>,
12 * Rob Radez <rob@osinvestor.com>
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
15 * provide warranty for any of this software. This material is
16 * provided "AS-IS" and at no charge.
17 */
18
19/*
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +020020 * A bells and whistles driver is available from:
Wim Van Sebroeckb3faed62005-10-22 16:27:19 +020021 * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
22 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000023 * More info available at
24 * http://www.berkprod.com/ or http://www.pcwatchdog.com/
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27/*
28 * Includes, defines, variables, module parameters, ...
29 */
30
Joe Perches27c766a2012-02-15 15:06:19 -080031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Wim Van Sebroeckc315b7e82005-09-12 00:21:19 +020033#include <linux/module.h> /* For module specific items */
34#include <linux/moduleparam.h> /* For new moduleparam's */
35#include <linux/types.h> /* For standard types (like size_t) */
36#include <linux/errno.h> /* For the -ENODEV/... values */
37#include <linux/kernel.h> /* For printk/panic/... */
38#include <linux/delay.h> /* For mdelay function */
Jean Delvare487722c2013-10-21 17:38:49 +020039#include <linux/miscdevice.h> /* For struct miscdevice */
Wim Van Sebroeckc315b7e82005-09-12 00:21:19 +020040#include <linux/watchdog.h> /* For the watchdog specific items */
41#include <linux/notifier.h> /* For notifier support */
42#include <linux/reboot.h> /* For reboot_notifier stuff */
43#include <linux/init.h> /* For __init/__exit/... */
44#include <linux/fs.h> /* For file operations */
45#include <linux/pci.h> /* For pci functions */
46#include <linux/ioport.h> /* For io-port access */
47#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000048#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
49#include <linux/io.h> /* For inb/outb/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* Module and version information */
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010052#define WATCHDOG_VERSION "1.03"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
54#define WATCHDOG_NAME "pcwd_pci"
Joe Perches27c766a2012-02-15 15:06:19 -080055#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* Stuff for the PCI ID's */
58#ifndef PCI_VENDOR_ID_QUICKLOGIC
59#define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
60#endif
61
62#ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
63#define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
64#endif
65
66/*
67 * These are the defines that describe the control status bits for the
68 * PCI-PC Watchdog card.
69 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020070/* Port 1 : Control Status #1 */
71#define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
72#define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
73#define WD_PCI_TTRP 0x04 /* Temperature Trip status */
74#define WD_PCI_RL2A 0x08 /* Relay 2 Active */
75#define WD_PCI_RL1A 0x10 /* Relay 1 Active */
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000076#define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
77 reset */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020078#define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
79/* Port 2 : Control Status #2 */
80#define WD_PCI_WDIS 0x10 /* Watchdog Disable */
81#define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
82#define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
83#define WD_PCI_PCMD 0x80 /* PC has sent command */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* according to documentation max. time to process a command for the pci
86 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
87#define PCI_COMMAND_TIMEOUT 150
88
89/* Watchdog's internal commands */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020090#define CMD_GET_STATUS 0x04
91#define CMD_GET_FIRMWARE_VERSION 0x08
92#define CMD_READ_WATCHDOG_TIMEOUT 0x18
93#define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
94#define CMD_GET_CLEAR_RESET_COUNT 0x84
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010096/* Watchdog's Dip Switch heartbeat values */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000097static const int heartbeat_tbl[] = {
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010098 5, /* OFF-OFF-OFF = 5 Sec */
99 10, /* OFF-OFF-ON = 10 Sec */
100 30, /* OFF-ON-OFF = 30 Sec */
101 60, /* OFF-ON-ON = 1 Min */
102 300, /* ON-OFF-OFF = 5 Min */
103 600, /* ON-OFF-ON = 10 Min */
104 1800, /* ON-ON-OFF = 30 Min */
105 3600, /* ON-ON-ON = 1 hour */
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* We can only use 1 card due to the /dev/watchdog restriction */
109static int cards_found;
110
111/* internal variables */
112static int temp_panic;
113static unsigned long is_active;
114static char expect_release;
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000115/* this is private data for each PCI-PC watchdog card */
116static struct {
117 /* Wether or not the card has a temperature device */
118 int supports_temp;
119 /* The card's boot status */
120 int boot_status;
121 /* The cards I/O address */
122 unsigned long io_addr;
123 /* the lock for io operations */
124 spinlock_t io_lock;
125 /* the PCI-device */
126 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127} pcipcwd_private;
128
129/* module parameters */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200130#define QUIET 0 /* Default */
131#define VERBOSE 1 /* Verbose */
132#define DEBUG 2 /* print fancy stuff too */
133static int debug = QUIET;
134module_param(debug, int, 0);
135MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
136
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000137#define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
138 delay-time from dip-switches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int heartbeat = WATCHDOG_HEARTBEAT;
140module_param(heartbeat, int, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000141MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
142 "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
143 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100145static bool nowayout = WATCHDOG_NOWAYOUT;
146module_param(nowayout, bool, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000147MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
148 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/*
151 * Internal functions
152 */
153
154static int send_command(int cmd, int *msb, int *lsb)
155{
156 int got_response, count;
157
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200158 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800159 pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
160 cmd, *msb, *lsb);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 spin_lock(&pcipcwd_private.io_lock);
163 /* If a command requires data it should be written first.
164 * Data for commands with 8 bits of data should be written to port 4.
165 * Commands with 16 bits of data, should be written as LSB to port 4
166 * and MSB to port 5.
167 * After the required data has been written then write the command to
168 * port 6. */
169 outb_p(*lsb, pcipcwd_private.io_addr + 4);
170 outb_p(*msb, pcipcwd_private.io_addr + 5);
171 outb_p(cmd, pcipcwd_private.io_addr + 6);
172
173 /* wait till the pci card processed the command, signaled by
174 * the WRSP bit in port 2 and give it a max. timeout of
175 * PCI_COMMAND_TIMEOUT to process */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200176 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000177 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
178 count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 mdelay(1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200180 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200183 if (debug >= DEBUG) {
184 if (got_response) {
Joe Perches27c766a2012-02-15 15:06:19 -0800185 pr_debug("time to process command was: %d ms\n",
186 count);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200187 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800188 pr_debug("card did not respond on command!\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200189 }
190 }
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (got_response) {
193 /* read back response */
194 *lsb = inb_p(pcipcwd_private.io_addr + 4);
195 *msb = inb_p(pcipcwd_private.io_addr + 5);
196
197 /* clear WRSP bit */
198 inb_p(pcipcwd_private.io_addr + 6);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200199
200 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800201 pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
202 cmd, *msb, *lsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_unlock(&pcipcwd_private.io_lock);
206
207 return got_response;
208}
209
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200210static inline void pcipcwd_check_temperature_support(void)
211{
212 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
213 pcipcwd_private.supports_temp = 1;
214}
215
216static int pcipcwd_get_option_switches(void)
217{
218 int option_switches;
219
220 option_switches = inb_p(pcipcwd_private.io_addr + 3);
221 return option_switches;
222}
223
224static void pcipcwd_show_card_info(void)
225{
226 int got_fw_rev, fw_rev_major, fw_rev_minor;
227 char fw_ver_str[20]; /* The cards firmware version */
228 int option_switches;
229
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000230 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
231 &fw_rev_minor);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000232 if (got_fw_rev)
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200233 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000234 else
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200235 sprintf(fw_ver_str, "<card no answer>");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200236
237 /* Get switch settings */
238 option_switches = pcipcwd_get_option_switches();
239
Joe Perches27c766a2012-02-15 15:06:19 -0800240 pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200241 (int) pcipcwd_private.io_addr, fw_ver_str,
242 (pcipcwd_private.supports_temp ? "with" : "without"));
243
Joe Perches27c766a2012-02-15 15:06:19 -0800244 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200245 option_switches,
246 ((option_switches & 0x10) ? "ON" : "OFF"),
247 ((option_switches & 0x08) ? "ON" : "OFF"));
248
249 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
Joe Perches27c766a2012-02-15 15:06:19 -0800250 pr_info("Previous reset was caused by the Watchdog card\n");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200251
252 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
Joe Perches27c766a2012-02-15 15:06:19 -0800253 pr_info("Card sensed a CPU Overheat\n");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200254
255 if (pcipcwd_private.boot_status == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800256 pr_info("No previous trip detected - Cold boot or reset\n");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static int pcipcwd_start(void)
260{
261 int stat_reg;
262
263 spin_lock(&pcipcwd_private.io_lock);
264 outb_p(0x00, pcipcwd_private.io_addr + 3);
265 udelay(1000);
266
267 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
268 spin_unlock(&pcipcwd_private.io_lock);
269
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200270 if (stat_reg & WD_PCI_WDIS) {
Joe Perches27c766a2012-02-15 15:06:19 -0800271 pr_err("Card timer not enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -1;
273 }
274
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200275 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800276 pr_debug("Watchdog started\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
280
281static int pcipcwd_stop(void)
282{
283 int stat_reg;
284
285 spin_lock(&pcipcwd_private.io_lock);
286 outb_p(0xA5, pcipcwd_private.io_addr + 3);
287 udelay(1000);
288
289 outb_p(0xA5, pcipcwd_private.io_addr + 3);
290 udelay(1000);
291
292 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
293 spin_unlock(&pcipcwd_private.io_lock);
294
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200295 if (!(stat_reg & WD_PCI_WDIS)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800296 pr_err("Card did not acknowledge disable attempt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -1;
298 }
299
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200300 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800301 pr_debug("Watchdog stopped\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304}
305
306static int pcipcwd_keepalive(void)
307{
308 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100309 spin_lock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200310 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100311 spin_unlock(&pcipcwd_private.io_lock);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200312
313 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800314 pr_debug("Watchdog keepalive signal send\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static int pcipcwd_set_heartbeat(int t)
320{
321 int t_msb = t / 256;
322 int t_lsb = t % 256;
323
324 if ((t < 0x0001) || (t > 0xFFFF))
325 return -EINVAL;
326
327 /* Write new heartbeat to watchdog */
328 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
329
330 heartbeat = t;
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200331 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800332 pr_debug("New heartbeat: %d\n", heartbeat);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
337static int pcipcwd_get_status(int *status)
338{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200339 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000341 *status = 0;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200342 control_status = inb_p(pcipcwd_private.io_addr + 1);
343 if (control_status & WD_PCI_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *status |= WDIOF_CARDRESET;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200345 if (control_status & WD_PCI_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *status |= WDIOF_OVERHEAT;
347 if (temp_panic)
Joe Perches27c766a2012-02-15 15:06:19 -0800348 panic(KBUILD_MODNAME ": Temperature overheat trip!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200351 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800352 pr_debug("Control Status #1: 0x%02x\n", control_status);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
357static int pcipcwd_clear_status(void)
358{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200359 int control_status;
360 int msb;
361 int reset_counter;
362
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200363 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800364 pr_info("clearing watchdog trip status & LED\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200365
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200366 control_status = inb_p(pcipcwd_private.io_addr + 1);
367
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200368 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800369 pr_debug("status was: 0x%02x\n", control_status);
370 pr_debug("sending: 0x%02x\n",
371 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200372 }
373
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200374 /* clear trip status & LED and keep mode of relay 2 */
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000375 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
376 pcipcwd_private.io_addr + 1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200377
378 /* clear reset counter */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000379 msb = 0;
380 reset_counter = 0xff;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200381 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
382
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200383 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800384 pr_debug("reset count was: 0x%02x\n", reset_counter);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
389
390static int pcipcwd_get_temperature(int *temperature)
391{
392 *temperature = 0;
393 if (!pcipcwd_private.supports_temp)
394 return -ENODEV;
395
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100396 spin_lock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200397 *temperature = inb_p(pcipcwd_private.io_addr);
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100398 spin_unlock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * Convert celsius to fahrenheit, since this was
402 * the decided 'standard' for this return value.
403 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200404 *temperature = (*temperature * 9 / 5) + 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200406 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800407 pr_debug("temperature is: %d F\n", *temperature);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200413static int pcipcwd_get_timeleft(int *time_left)
414{
415 int msb;
416 int lsb;
417
418 /* Read the time that's left before rebooting */
419 /* Note: if the board is not yet armed then we will read 0xFFFF */
420 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
421
422 *time_left = (msb << 8) + lsb;
423
424 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800425 pr_debug("Time left before next reboot: %d\n", *time_left);
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200426
427 return 0;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * /dev/watchdog handling
432 */
433
434static ssize_t pcipcwd_write(struct file *file, const char __user *data,
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200435 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 /* See if we got the magic character 'V' and reload the timer */
438 if (len) {
439 if (!nowayout) {
440 size_t i;
441
442 /* note: just in case someone wrote the magic character
443 * five months ago... */
444 expect_release = 0;
445
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000446 /* scan to see whether or not we got the
447 * magic character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 for (i = 0; i != len; i++) {
449 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000450 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -EFAULT;
452 if (c == 'V')
453 expect_release = 42;
454 }
455 }
456
457 /* someone wrote to us, we should reload the timer */
458 pcipcwd_keepalive();
459 }
460 return len;
461}
462
Alan Coxc9488522008-07-03 23:51:32 -0700463static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
464 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 void __user *argp = (void __user *)arg;
467 int __user *p = argp;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000468 static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .options = WDIOF_OVERHEAT |
470 WDIOF_CARDRESET |
471 WDIOF_KEEPALIVEPING |
472 WDIOF_SETTIMEOUT |
473 WDIOF_MAGICCLOSE,
474 .firmware_version = 1,
475 .identity = WATCHDOG_DRIVER_NAME,
476 };
477
478 switch (cmd) {
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000479 case WDIOC_GETSUPPORT:
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000480 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000482 case WDIOC_GETSTATUS:
483 {
484 int status;
485 pcipcwd_get_status(&status);
486 return put_user(status, p);
487 }
488
489 case WDIOC_GETBOOTSTATUS:
490 return put_user(pcipcwd_private.boot_status, p);
491
492 case WDIOC_GETTEMP:
493 {
494 int temperature;
495
496 if (pcipcwd_get_temperature(&temperature))
497 return -EFAULT;
498
499 return put_user(temperature, p);
500 }
501
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000502 case WDIOC_SETOPTIONS:
503 {
504 int new_options, retval = -EINVAL;
505
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000506 if (get_user(new_options, p))
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000507 return -EFAULT;
508
509 if (new_options & WDIOS_DISABLECARD) {
510 if (pcipcwd_stop())
511 return -EIO;
512 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000515 if (new_options & WDIOS_ENABLECARD) {
516 if (pcipcwd_start())
517 return -EIO;
518 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000521 if (new_options & WDIOS_TEMPPANIC) {
522 temp_panic = 1;
523 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000526 return retval;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000529 case WDIOC_KEEPALIVE:
530 pcipcwd_keepalive();
531 return 0;
532
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000533 case WDIOC_SETTIMEOUT:
534 {
535 int new_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000537 if (get_user(new_heartbeat, p))
538 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000540 if (pcipcwd_set_heartbeat(new_heartbeat))
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000543 pcipcwd_keepalive();
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000544 }
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500545 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000547 case WDIOC_GETTIMEOUT:
548 return put_user(heartbeat, p);
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200549
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000550 case WDIOC_GETTIMELEFT:
551 {
552 int time_left;
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200553
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000554 if (pcipcwd_get_timeleft(&time_left))
555 return -EFAULT;
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200556
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000557 return put_user(time_left, p);
558 }
559
560 default:
561 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563}
564
565static int pcipcwd_open(struct inode *inode, struct file *file)
566{
567 /* /dev/watchdog can only be opened once */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200568 if (test_and_set_bit(0, &is_active)) {
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200569 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800570 pr_err("Attempt to open already opened device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -EBUSY;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* Activate */
575 pcipcwd_start();
576 pcipcwd_keepalive();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300577 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580static int pcipcwd_release(struct inode *inode, struct file *file)
581{
582 /*
583 * Shut off the timer.
584 */
585 if (expect_release == 42) {
586 pcipcwd_stop();
587 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800588 pr_crit("Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 pcipcwd_keepalive();
590 }
591 expect_release = 0;
592 clear_bit(0, &is_active);
593 return 0;
594}
595
596/*
597 * /dev/temperature handling
598 */
599
600static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
601 size_t len, loff_t *ppos)
602{
603 int temperature;
604
605 if (pcipcwd_get_temperature(&temperature))
606 return -EFAULT;
607
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000608 if (copy_to_user(data, &temperature, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EFAULT;
610
611 return 1;
612}
613
614static int pcipcwd_temp_open(struct inode *inode, struct file *file)
615{
616 if (!pcipcwd_private.supports_temp)
617 return -ENODEV;
618
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300619 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static int pcipcwd_temp_release(struct inode *inode, struct file *file)
623{
624 return 0;
625}
626
627/*
628 * Notify system
629 */
630
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000631static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
632 void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000634 if (code == SYS_DOWN || code == SYS_HALT)
635 pcipcwd_stop(); /* Turn the WDT off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 return NOTIFY_DONE;
638}
639
640/*
641 * Kernel Interfaces
642 */
643
Arjan van de Ven62322d22006-07-03 00:24:21 -0700644static const struct file_operations pcipcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 .owner = THIS_MODULE,
646 .llseek = no_llseek,
647 .write = pcipcwd_write,
Alan Coxc9488522008-07-03 23:51:32 -0700648 .unlocked_ioctl = pcipcwd_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200649 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .open = pcipcwd_open,
651 .release = pcipcwd_release,
652};
653
654static struct miscdevice pcipcwd_miscdev = {
655 .minor = WATCHDOG_MINOR,
656 .name = "watchdog",
657 .fops = &pcipcwd_fops,
658};
659
Arjan van de Ven62322d22006-07-03 00:24:21 -0700660static const struct file_operations pcipcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 .owner = THIS_MODULE,
662 .llseek = no_llseek,
663 .read = pcipcwd_temp_read,
664 .open = pcipcwd_temp_open,
665 .release = pcipcwd_temp_release,
666};
667
668static struct miscdevice pcipcwd_temp_miscdev = {
669 .minor = TEMP_MINOR,
670 .name = "temperature",
671 .fops = &pcipcwd_temp_fops,
672};
673
674static struct notifier_block pcipcwd_notifier = {
675 .notifier_call = pcipcwd_notify_sys,
676};
677
678/*
679 * Init & exit routines
680 */
681
Bill Pemberton2d991a12012-11-19 13:21:41 -0500682static int pcipcwd_card_init(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 const struct pci_device_id *ent)
684{
685 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 cards_found++;
688 if (cards_found == 1)
Joe Perches27c766a2012-02-15 15:06:19 -0800689 pr_info("%s\n", DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (cards_found > 1) {
Joe Perches27c766a2012-02-15 15:06:19 -0800692 pr_err("This driver only supports 1 device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -ENODEV;
694 }
695
696 if (pci_enable_device(pdev)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800697 pr_err("Not possible to enable PCI Device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -ENODEV;
699 }
700
701 if (pci_resource_start(pdev, 0) == 0x0000) {
Joe Perches27c766a2012-02-15 15:06:19 -0800702 pr_err("No I/O-Address for card detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ret = -ENODEV;
704 goto err_out_disable_device;
705 }
706
Wim Van Sebroeck5ce9c372012-05-04 14:43:25 +0200707 spin_lock_init(&pcipcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 pcipcwd_private.pdev = pdev;
709 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
710
711 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800712 pr_err("I/O address 0x%04x already in use\n",
713 (int) pcipcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ret = -EIO;
715 goto err_out_disable_device;
716 }
717
718 /* get the boot_status */
719 pcipcwd_get_status(&pcipcwd_private.boot_status);
720
721 /* clear the "card caused reboot" flag */
722 pcipcwd_clear_status();
723
724 /* disable card */
725 pcipcwd_stop();
726
727 /* Check whether or not the card supports the temperature device */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200728 pcipcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200730 /* Show info about the card itself */
731 pcipcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100733 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
734 if (heartbeat == 0)
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000735 heartbeat =
736 heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100737
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000738 /* Check that the heartbeat value is within it's range ;
739 * if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (pcipcwd_set_heartbeat(heartbeat)) {
741 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
Joe Perches27c766a2012-02-15 15:06:19 -0800742 pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 WATCHDOG_HEARTBEAT);
744 }
745
746 ret = register_reboot_notifier(&pcipcwd_notifier);
747 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800748 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto err_out_release_region;
750 }
751
752 if (pcipcwd_private.supports_temp) {
753 ret = misc_register(&pcipcwd_temp_miscdev);
754 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800755 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
756 TEMP_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto err_out_unregister_reboot;
758 }
759 }
760
761 ret = misc_register(&pcipcwd_miscdev);
762 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800763 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
764 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto err_out_misc_deregister;
766 }
767
Joe Perches27c766a2012-02-15 15:06:19 -0800768 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 heartbeat, nowayout);
770
771 return 0;
772
773err_out_misc_deregister:
774 if (pcipcwd_private.supports_temp)
775 misc_deregister(&pcipcwd_temp_miscdev);
776err_out_unregister_reboot:
777 unregister_reboot_notifier(&pcipcwd_notifier);
778err_out_release_region:
779 pci_release_regions(pdev);
780err_out_disable_device:
781 pci_disable_device(pdev);
782 return ret;
783}
784
Bill Pemberton4b12b892012-11-19 13:26:24 -0500785static void pcipcwd_card_exit(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 /* Stop the timer before we leave */
788 if (!nowayout)
789 pcipcwd_stop();
790
791 /* Deregister */
792 misc_deregister(&pcipcwd_miscdev);
793 if (pcipcwd_private.supports_temp)
794 misc_deregister(&pcipcwd_temp_miscdev);
795 unregister_reboot_notifier(&pcipcwd_notifier);
796 pci_release_regions(pdev);
797 pci_disable_device(pdev);
798 cards_found--;
799}
800
Jingoo Hanbc17f9d2013-12-03 08:30:22 +0900801static const struct pci_device_id pcipcwd_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
803 PCI_ANY_ID, PCI_ANY_ID, },
804 { 0 }, /* End of list */
805};
806MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
807
808static struct pci_driver pcipcwd_driver = {
809 .name = WATCHDOG_NAME,
810 .id_table = pcipcwd_pci_tbl,
811 .probe = pcipcwd_card_init,
Bill Pemberton82268712012-11-19 13:21:12 -0500812 .remove = pcipcwd_card_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813};
814
Wim Van Sebroeck5ce9c372012-05-04 14:43:25 +0200815module_pci_driver(pcipcwd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
818MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
819MODULE_LICENSE("GPL");