blob: c891399bed6ae9022915cbe3c12e566aada25f38 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Berkshire PCI-PC Watchdog Card Driver
3 *
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +01004 * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Based on source code of the following authors:
7 * Ken Hollis <kenji@bitgate.com>,
8 * Lindsay Harris <lindsay@bluegum.com>,
Alan Cox29fa0582008-10-27 15:17:56 +00009 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Matt Domsch <Matt_Domsch@dell.com>,
11 * Rob Radez <rob@osinvestor.com>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
19 * provide warranty for any of this software. This material is
20 * provided "AS-IS" and at no charge.
21 */
22
23/*
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +020024 * A bells and whistles driver is available from:
Wim Van Sebroeckb3faed62005-10-22 16:27:19 +020025 * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
26 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000027 * More info available at
28 * http://www.berkprod.com/ or http://www.pcwatchdog.com/
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31/*
32 * Includes, defines, variables, module parameters, ...
33 */
34
Joe Perches27c766a2012-02-15 15:06:19 -080035#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
Wim Van Sebroeckc315b7e2005-09-12 00:21:19 +020037#include <linux/module.h> /* For module specific items */
38#include <linux/moduleparam.h> /* For new moduleparam's */
39#include <linux/types.h> /* For standard types (like size_t) */
40#include <linux/errno.h> /* For the -ENODEV/... values */
41#include <linux/kernel.h> /* For printk/panic/... */
42#include <linux/delay.h> /* For mdelay function */
43#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
44#include <linux/watchdog.h> /* For the watchdog specific items */
45#include <linux/notifier.h> /* For notifier support */
46#include <linux/reboot.h> /* For reboot_notifier stuff */
47#include <linux/init.h> /* For __init/__exit/... */
48#include <linux/fs.h> /* For file operations */
49#include <linux/pci.h> /* For pci functions */
50#include <linux/ioport.h> /* For io-port access */
51#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000052#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
53#include <linux/io.h> /* For inb/outb/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* Module and version information */
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010056#define WATCHDOG_VERSION "1.03"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
58#define WATCHDOG_NAME "pcwd_pci"
Joe Perches27c766a2012-02-15 15:06:19 -080059#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* Stuff for the PCI ID's */
62#ifndef PCI_VENDOR_ID_QUICKLOGIC
63#define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
64#endif
65
66#ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
67#define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
68#endif
69
70/*
71 * These are the defines that describe the control status bits for the
72 * PCI-PC Watchdog card.
73 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020074/* Port 1 : Control Status #1 */
75#define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
76#define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
77#define WD_PCI_TTRP 0x04 /* Temperature Trip status */
78#define WD_PCI_RL2A 0x08 /* Relay 2 Active */
79#define WD_PCI_RL1A 0x10 /* Relay 1 Active */
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000080#define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
81 reset */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020082#define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
83/* Port 2 : Control Status #2 */
84#define WD_PCI_WDIS 0x10 /* Watchdog Disable */
85#define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
86#define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
87#define WD_PCI_PCMD 0x80 /* PC has sent command */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* according to documentation max. time to process a command for the pci
90 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
91#define PCI_COMMAND_TIMEOUT 150
92
93/* Watchdog's internal commands */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020094#define CMD_GET_STATUS 0x04
95#define CMD_GET_FIRMWARE_VERSION 0x08
96#define CMD_READ_WATCHDOG_TIMEOUT 0x18
97#define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
98#define CMD_GET_CLEAR_RESET_COUNT 0x84
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100100/* Watchdog's Dip Switch heartbeat values */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000101static const int heartbeat_tbl[] = {
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100102 5, /* OFF-OFF-OFF = 5 Sec */
103 10, /* OFF-OFF-ON = 10 Sec */
104 30, /* OFF-ON-OFF = 30 Sec */
105 60, /* OFF-ON-ON = 1 Min */
106 300, /* ON-OFF-OFF = 5 Min */
107 600, /* ON-OFF-ON = 10 Min */
108 1800, /* ON-ON-OFF = 30 Min */
109 3600, /* ON-ON-ON = 1 hour */
110};
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* We can only use 1 card due to the /dev/watchdog restriction */
113static int cards_found;
114
115/* internal variables */
116static int temp_panic;
117static unsigned long is_active;
118static char expect_release;
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000119/* this is private data for each PCI-PC watchdog card */
120static struct {
121 /* Wether or not the card has a temperature device */
122 int supports_temp;
123 /* The card's boot status */
124 int boot_status;
125 /* The cards I/O address */
126 unsigned long io_addr;
127 /* the lock for io operations */
128 spinlock_t io_lock;
129 /* the PCI-device */
130 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131} pcipcwd_private;
132
133/* module parameters */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200134#define QUIET 0 /* Default */
135#define VERBOSE 1 /* Verbose */
136#define DEBUG 2 /* print fancy stuff too */
137static int debug = QUIET;
138module_param(debug, int, 0);
139MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
140
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000141#define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
142 delay-time from dip-switches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int heartbeat = WATCHDOG_HEARTBEAT;
144module_param(heartbeat, int, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000145MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
146 "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
147 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100149static bool nowayout = WATCHDOG_NOWAYOUT;
150module_param(nowayout, bool, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000151MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
152 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*
155 * Internal functions
156 */
157
158static int send_command(int cmd, int *msb, int *lsb)
159{
160 int got_response, count;
161
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200162 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800163 pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
164 cmd, *msb, *lsb);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 spin_lock(&pcipcwd_private.io_lock);
167 /* If a command requires data it should be written first.
168 * Data for commands with 8 bits of data should be written to port 4.
169 * Commands with 16 bits of data, should be written as LSB to port 4
170 * and MSB to port 5.
171 * After the required data has been written then write the command to
172 * port 6. */
173 outb_p(*lsb, pcipcwd_private.io_addr + 4);
174 outb_p(*msb, pcipcwd_private.io_addr + 5);
175 outb_p(cmd, pcipcwd_private.io_addr + 6);
176
177 /* wait till the pci card processed the command, signaled by
178 * the WRSP bit in port 2 and give it a max. timeout of
179 * PCI_COMMAND_TIMEOUT to process */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200180 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000181 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
182 count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 mdelay(1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200184 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200187 if (debug >= DEBUG) {
188 if (got_response) {
Joe Perches27c766a2012-02-15 15:06:19 -0800189 pr_debug("time to process command was: %d ms\n",
190 count);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200191 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800192 pr_debug("card did not respond on command!\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200193 }
194 }
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (got_response) {
197 /* read back response */
198 *lsb = inb_p(pcipcwd_private.io_addr + 4);
199 *msb = inb_p(pcipcwd_private.io_addr + 5);
200
201 /* clear WRSP bit */
202 inb_p(pcipcwd_private.io_addr + 6);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200203
204 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800205 pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
206 cmd, *msb, *lsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 spin_unlock(&pcipcwd_private.io_lock);
210
211 return got_response;
212}
213
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200214static inline void pcipcwd_check_temperature_support(void)
215{
216 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
217 pcipcwd_private.supports_temp = 1;
218}
219
220static int pcipcwd_get_option_switches(void)
221{
222 int option_switches;
223
224 option_switches = inb_p(pcipcwd_private.io_addr + 3);
225 return option_switches;
226}
227
228static void pcipcwd_show_card_info(void)
229{
230 int got_fw_rev, fw_rev_major, fw_rev_minor;
231 char fw_ver_str[20]; /* The cards firmware version */
232 int option_switches;
233
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000234 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
235 &fw_rev_minor);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000236 if (got_fw_rev)
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200237 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000238 else
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200239 sprintf(fw_ver_str, "<card no answer>");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200240
241 /* Get switch settings */
242 option_switches = pcipcwd_get_option_switches();
243
Joe Perches27c766a2012-02-15 15:06:19 -0800244 pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200245 (int) pcipcwd_private.io_addr, fw_ver_str,
246 (pcipcwd_private.supports_temp ? "with" : "without"));
247
Joe Perches27c766a2012-02-15 15:06:19 -0800248 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200249 option_switches,
250 ((option_switches & 0x10) ? "ON" : "OFF"),
251 ((option_switches & 0x08) ? "ON" : "OFF"));
252
253 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
Joe Perches27c766a2012-02-15 15:06:19 -0800254 pr_info("Previous reset was caused by the Watchdog card\n");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200255
256 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
Joe Perches27c766a2012-02-15 15:06:19 -0800257 pr_info("Card sensed a CPU Overheat\n");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200258
259 if (pcipcwd_private.boot_status == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800260 pr_info("No previous trip detected - Cold boot or reset\n");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int pcipcwd_start(void)
264{
265 int stat_reg;
266
267 spin_lock(&pcipcwd_private.io_lock);
268 outb_p(0x00, pcipcwd_private.io_addr + 3);
269 udelay(1000);
270
271 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
272 spin_unlock(&pcipcwd_private.io_lock);
273
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200274 if (stat_reg & WD_PCI_WDIS) {
Joe Perches27c766a2012-02-15 15:06:19 -0800275 pr_err("Card timer not enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -1;
277 }
278
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200279 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800280 pr_debug("Watchdog started\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
285static int pcipcwd_stop(void)
286{
287 int stat_reg;
288
289 spin_lock(&pcipcwd_private.io_lock);
290 outb_p(0xA5, pcipcwd_private.io_addr + 3);
291 udelay(1000);
292
293 outb_p(0xA5, pcipcwd_private.io_addr + 3);
294 udelay(1000);
295
296 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
297 spin_unlock(&pcipcwd_private.io_lock);
298
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200299 if (!(stat_reg & WD_PCI_WDIS)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800300 pr_err("Card did not acknowledge disable attempt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -1;
302 }
303
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200304 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800305 pr_debug("Watchdog stopped\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308}
309
310static int pcipcwd_keepalive(void)
311{
312 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100313 spin_lock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200314 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100315 spin_unlock(&pcipcwd_private.io_lock);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200316
317 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800318 pr_debug("Watchdog keepalive signal send\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321}
322
323static int pcipcwd_set_heartbeat(int t)
324{
325 int t_msb = t / 256;
326 int t_lsb = t % 256;
327
328 if ((t < 0x0001) || (t > 0xFFFF))
329 return -EINVAL;
330
331 /* Write new heartbeat to watchdog */
332 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
333
334 heartbeat = t;
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200335 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800336 pr_debug("New heartbeat: %d\n", heartbeat);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339}
340
341static int pcipcwd_get_status(int *status)
342{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200343 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000345 *status = 0;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200346 control_status = inb_p(pcipcwd_private.io_addr + 1);
347 if (control_status & WD_PCI_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *status |= WDIOF_CARDRESET;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200349 if (control_status & WD_PCI_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *status |= WDIOF_OVERHEAT;
351 if (temp_panic)
Joe Perches27c766a2012-02-15 15:06:19 -0800352 panic(KBUILD_MODNAME ": Temperature overheat trip!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200355 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800356 pr_debug("Control Status #1: 0x%02x\n", control_status);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359}
360
361static int pcipcwd_clear_status(void)
362{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200363 int control_status;
364 int msb;
365 int reset_counter;
366
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200367 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800368 pr_info("clearing watchdog trip status & LED\n");
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200369
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200370 control_status = inb_p(pcipcwd_private.io_addr + 1);
371
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200372 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800373 pr_debug("status was: 0x%02x\n", control_status);
374 pr_debug("sending: 0x%02x\n",
375 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200376 }
377
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200378 /* clear trip status & LED and keep mode of relay 2 */
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000379 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
380 pcipcwd_private.io_addr + 1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200381
382 /* clear reset counter */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000383 msb = 0;
384 reset_counter = 0xff;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200385 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
386
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200387 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800388 pr_debug("reset count was: 0x%02x\n", reset_counter);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200389 }
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
394static int pcipcwd_get_temperature(int *temperature)
395{
396 *temperature = 0;
397 if (!pcipcwd_private.supports_temp)
398 return -ENODEV;
399
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100400 spin_lock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200401 *temperature = inb_p(pcipcwd_private.io_addr);
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100402 spin_unlock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
405 * Convert celsius to fahrenheit, since this was
406 * the decided 'standard' for this return value.
407 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200408 *temperature = (*temperature * 9 / 5) + 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200410 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800411 pr_debug("temperature is: %d F\n", *temperature);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
415}
416
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200417static int pcipcwd_get_timeleft(int *time_left)
418{
419 int msb;
420 int lsb;
421
422 /* Read the time that's left before rebooting */
423 /* Note: if the board is not yet armed then we will read 0xFFFF */
424 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
425
426 *time_left = (msb << 8) + lsb;
427
428 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800429 pr_debug("Time left before next reboot: %d\n", *time_left);
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200430
431 return 0;
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * /dev/watchdog handling
436 */
437
438static ssize_t pcipcwd_write(struct file *file, const char __user *data,
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200439 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 /* See if we got the magic character 'V' and reload the timer */
442 if (len) {
443 if (!nowayout) {
444 size_t i;
445
446 /* note: just in case someone wrote the magic character
447 * five months ago... */
448 expect_release = 0;
449
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000450 /* scan to see whether or not we got the
451 * magic character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for (i = 0; i != len; i++) {
453 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000454 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -EFAULT;
456 if (c == 'V')
457 expect_release = 42;
458 }
459 }
460
461 /* someone wrote to us, we should reload the timer */
462 pcipcwd_keepalive();
463 }
464 return len;
465}
466
Alan Coxc9488522008-07-03 23:51:32 -0700467static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
468 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 void __user *argp = (void __user *)arg;
471 int __user *p = argp;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000472 static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 .options = WDIOF_OVERHEAT |
474 WDIOF_CARDRESET |
475 WDIOF_KEEPALIVEPING |
476 WDIOF_SETTIMEOUT |
477 WDIOF_MAGICCLOSE,
478 .firmware_version = 1,
479 .identity = WATCHDOG_DRIVER_NAME,
480 };
481
482 switch (cmd) {
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000483 case WDIOC_GETSUPPORT:
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000484 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000486 case WDIOC_GETSTATUS:
487 {
488 int status;
489 pcipcwd_get_status(&status);
490 return put_user(status, p);
491 }
492
493 case WDIOC_GETBOOTSTATUS:
494 return put_user(pcipcwd_private.boot_status, p);
495
496 case WDIOC_GETTEMP:
497 {
498 int temperature;
499
500 if (pcipcwd_get_temperature(&temperature))
501 return -EFAULT;
502
503 return put_user(temperature, p);
504 }
505
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000506 case WDIOC_SETOPTIONS:
507 {
508 int new_options, retval = -EINVAL;
509
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000510 if (get_user(new_options, p))
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000511 return -EFAULT;
512
513 if (new_options & WDIOS_DISABLECARD) {
514 if (pcipcwd_stop())
515 return -EIO;
516 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000519 if (new_options & WDIOS_ENABLECARD) {
520 if (pcipcwd_start())
521 return -EIO;
522 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000525 if (new_options & WDIOS_TEMPPANIC) {
526 temp_panic = 1;
527 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000530 return retval;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000533 case WDIOC_KEEPALIVE:
534 pcipcwd_keepalive();
535 return 0;
536
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000537 case WDIOC_SETTIMEOUT:
538 {
539 int new_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000541 if (get_user(new_heartbeat, p))
542 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000544 if (pcipcwd_set_heartbeat(new_heartbeat))
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000547 pcipcwd_keepalive();
548 /* Fall */
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000551 case WDIOC_GETTIMEOUT:
552 return put_user(heartbeat, p);
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200553
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000554 case WDIOC_GETTIMELEFT:
555 {
556 int time_left;
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200557
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000558 if (pcipcwd_get_timeleft(&time_left))
559 return -EFAULT;
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200560
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000561 return put_user(time_left, p);
562 }
563
564 default:
565 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567}
568
569static int pcipcwd_open(struct inode *inode, struct file *file)
570{
571 /* /dev/watchdog can only be opened once */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200572 if (test_and_set_bit(0, &is_active)) {
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200573 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800574 pr_err("Attempt to open already opened device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -EBUSY;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* Activate */
579 pcipcwd_start();
580 pcipcwd_keepalive();
581 return nonseekable_open(inode, file);
582}
583
584static int pcipcwd_release(struct inode *inode, struct file *file)
585{
586 /*
587 * Shut off the timer.
588 */
589 if (expect_release == 42) {
590 pcipcwd_stop();
591 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800592 pr_crit("Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pcipcwd_keepalive();
594 }
595 expect_release = 0;
596 clear_bit(0, &is_active);
597 return 0;
598}
599
600/*
601 * /dev/temperature handling
602 */
603
604static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
605 size_t len, loff_t *ppos)
606{
607 int temperature;
608
609 if (pcipcwd_get_temperature(&temperature))
610 return -EFAULT;
611
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000612 if (copy_to_user(data, &temperature, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EFAULT;
614
615 return 1;
616}
617
618static int pcipcwd_temp_open(struct inode *inode, struct file *file)
619{
620 if (!pcipcwd_private.supports_temp)
621 return -ENODEV;
622
623 return nonseekable_open(inode, file);
624}
625
626static int pcipcwd_temp_release(struct inode *inode, struct file *file)
627{
628 return 0;
629}
630
631/*
632 * Notify system
633 */
634
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000635static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
636 void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000638 if (code == SYS_DOWN || code == SYS_HALT)
639 pcipcwd_stop(); /* Turn the WDT off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return NOTIFY_DONE;
642}
643
644/*
645 * Kernel Interfaces
646 */
647
Arjan van de Ven62322d22006-07-03 00:24:21 -0700648static const struct file_operations pcipcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .owner = THIS_MODULE,
650 .llseek = no_llseek,
651 .write = pcipcwd_write,
Alan Coxc9488522008-07-03 23:51:32 -0700652 .unlocked_ioctl = pcipcwd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 .open = pcipcwd_open,
654 .release = pcipcwd_release,
655};
656
657static struct miscdevice pcipcwd_miscdev = {
658 .minor = WATCHDOG_MINOR,
659 .name = "watchdog",
660 .fops = &pcipcwd_fops,
661};
662
Arjan van de Ven62322d22006-07-03 00:24:21 -0700663static const struct file_operations pcipcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .owner = THIS_MODULE,
665 .llseek = no_llseek,
666 .read = pcipcwd_temp_read,
667 .open = pcipcwd_temp_open,
668 .release = pcipcwd_temp_release,
669};
670
671static struct miscdevice pcipcwd_temp_miscdev = {
672 .minor = TEMP_MINOR,
673 .name = "temperature",
674 .fops = &pcipcwd_temp_fops,
675};
676
677static struct notifier_block pcipcwd_notifier = {
678 .notifier_call = pcipcwd_notify_sys,
679};
680
681/*
682 * Init & exit routines
683 */
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
686 const struct pci_device_id *ent)
687{
688 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 cards_found++;
691 if (cards_found == 1)
Joe Perches27c766a2012-02-15 15:06:19 -0800692 pr_info("%s\n", DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (cards_found > 1) {
Joe Perches27c766a2012-02-15 15:06:19 -0800695 pr_err("This driver only supports 1 device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -ENODEV;
697 }
698
699 if (pci_enable_device(pdev)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800700 pr_err("Not possible to enable PCI Device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -ENODEV;
702 }
703
704 if (pci_resource_start(pdev, 0) == 0x0000) {
Joe Perches27c766a2012-02-15 15:06:19 -0800705 pr_err("No I/O-Address for card detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ret = -ENODEV;
707 goto err_out_disable_device;
708 }
709
710 pcipcwd_private.pdev = pdev;
711 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
712
713 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800714 pr_err("I/O address 0x%04x already in use\n",
715 (int) pcipcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 ret = -EIO;
717 goto err_out_disable_device;
718 }
719
720 /* get the boot_status */
721 pcipcwd_get_status(&pcipcwd_private.boot_status);
722
723 /* clear the "card caused reboot" flag */
724 pcipcwd_clear_status();
725
726 /* disable card */
727 pcipcwd_stop();
728
729 /* Check whether or not the card supports the temperature device */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200730 pcipcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200732 /* Show info about the card itself */
733 pcipcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100735 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
736 if (heartbeat == 0)
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000737 heartbeat =
738 heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100739
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000740 /* Check that the heartbeat value is within it's range ;
741 * if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (pcipcwd_set_heartbeat(heartbeat)) {
743 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
Joe Perches27c766a2012-02-15 15:06:19 -0800744 pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 WATCHDOG_HEARTBEAT);
746 }
747
748 ret = register_reboot_notifier(&pcipcwd_notifier);
749 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800750 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto err_out_release_region;
752 }
753
754 if (pcipcwd_private.supports_temp) {
755 ret = misc_register(&pcipcwd_temp_miscdev);
756 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800757 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
758 TEMP_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto err_out_unregister_reboot;
760 }
761 }
762
763 ret = misc_register(&pcipcwd_miscdev);
764 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800765 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
766 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto err_out_misc_deregister;
768 }
769
Joe Perches27c766a2012-02-15 15:06:19 -0800770 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 heartbeat, nowayout);
772
773 return 0;
774
775err_out_misc_deregister:
776 if (pcipcwd_private.supports_temp)
777 misc_deregister(&pcipcwd_temp_miscdev);
778err_out_unregister_reboot:
779 unregister_reboot_notifier(&pcipcwd_notifier);
780err_out_release_region:
781 pci_release_regions(pdev);
782err_out_disable_device:
783 pci_disable_device(pdev);
784 return ret;
785}
786
787static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
788{
789 /* Stop the timer before we leave */
790 if (!nowayout)
791 pcipcwd_stop();
792
793 /* Deregister */
794 misc_deregister(&pcipcwd_miscdev);
795 if (pcipcwd_private.supports_temp)
796 misc_deregister(&pcipcwd_temp_miscdev);
797 unregister_reboot_notifier(&pcipcwd_notifier);
798 pci_release_regions(pdev);
799 pci_disable_device(pdev);
800 cards_found--;
801}
802
Wim Van Sebroeck4562f532011-02-21 12:16:44 +0000803static DEFINE_PCI_DEVICE_TABLE(pcipcwd_pci_tbl) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
805 PCI_ANY_ID, PCI_ANY_ID, },
806 { 0 }, /* End of list */
807};
808MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
809
810static struct pci_driver pcipcwd_driver = {
811 .name = WATCHDOG_NAME,
812 .id_table = pcipcwd_pci_tbl,
813 .probe = pcipcwd_card_init,
814 .remove = __devexit_p(pcipcwd_card_exit),
815};
816
817static int __init pcipcwd_init_module(void)
818{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200819 spin_lock_init(&pcipcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return pci_register_driver(&pcipcwd_driver);
822}
823
824static void __exit pcipcwd_cleanup_module(void)
825{
826 pci_unregister_driver(&pcipcwd_driver);
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100827
Joe Perches27c766a2012-02-15 15:06:19 -0800828 pr_info("Watchdog Module Unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831module_init(pcipcwd_init_module);
832module_exit(pcipcwd_cleanup_module);
833
834MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
835MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
836MODULE_LICENSE("GPL");
837MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
838MODULE_ALIAS_MISCDEV(TEMP_MINOR);