blob: 331c95d962caa321524e35e4e4d3637a87caf8f5 [file] [log] [blame]
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +02001/*
2 * intel TCO Watchdog Driver (Used in i82801 and i6300ESB chipsets)
3 *
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +01004 * (c) Copyright 2006-2007 Wim Van Sebroeck <wim@iguana.be>.
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
12 * provide warranty for any of this software. This material is
13 * provided "AS-IS" and at no charge.
14 *
15 * The TCO watchdog is implemented in the following I/O controller hubs:
16 * (See the intel documentation on http://developer.intel.com.)
17 * 82801AA (ICH) : document number 290655-003, 290677-014,
18 * 82801AB (ICHO) : document number 290655-003, 290677-014,
19 * 82801BA (ICH2) : document number 290687-002, 298242-027,
20 * 82801BAM (ICH2-M) : document number 290687-002, 298242-027,
21 * 82801CA (ICH3-S) : document number 290733-003, 290739-013,
22 * 82801CAM (ICH3-M) : document number 290716-001, 290718-007,
23 * 82801DB (ICH4) : document number 290744-001, 290745-020,
24 * 82801DBM (ICH4-M) : document number 252337-001, 252663-005,
25 * 82801E (C-ICH) : document number 273599-001, 273645-002,
26 * 82801EB (ICH5) : document number 252516-001, 252517-003,
27 * 82801ER (ICH5R) : document number 252516-001, 252517-003,
28 * 82801FB (ICH6) : document number 301473-002, 301474-007,
29 * 82801FR (ICH6R) : document number 301473-002, 301474-007,
30 * 82801FBM (ICH6-M) : document number 301473-002, 301474-007,
31 * 82801FW (ICH6W) : document number 301473-001, 301474-007,
32 * 82801FRW (ICH6RW) : document number 301473-001, 301474-007,
33 * 82801GB (ICH7) : document number 307013-002, 307014-009,
34 * 82801GR (ICH7R) : document number 307013-002, 307014-009,
35 * 82801GDH (ICH7DH) : document number 307013-002, 307014-009,
36 * 82801GBM (ICH7-M) : document number 307013-002, 307014-009,
37 * 82801GHM (ICH7-M DH) : document number 307013-002, 307014-009,
Wim Van Sebroecka8edd742006-10-08 21:05:21 +020038 * 82801HB (ICH8) : document number 313056-002, 313057-004,
39 * 82801HR (ICH8R) : document number 313056-002, 313057-004,
40 * 82801HH (ICH8DH) : document number 313056-002, 313057-004,
41 * 82801HO (ICH8DO) : document number 313056-002, 313057-004,
Sergey Kononenko998e6782007-07-26 17:28:35 +030042 * 6300ESB (6300ESB) : document number 300641-003,
43 * 631xESB (631xESB) : document number 313082-001, 313075-005,
44 * 632xESB (632xESB) : document number 313082-001, 313075-005
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020045 */
46
47/*
48 * Includes, defines, variables, module parameters, ...
49 */
50
51/* Module and version information */
52#define DRV_NAME "iTCO_wdt"
Wim Van Sebroecke0333512006-11-12 18:05:09 +010053#define DRV_VERSION "1.01"
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +010054#define DRV_RELDATE "21-Jan-2007"
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020055#define PFX DRV_NAME ": "
56
57/* Includes */
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +020058#include <linux/module.h> /* For module specific items */
59#include <linux/moduleparam.h> /* For new moduleparam's */
60#include <linux/types.h> /* For standard types (like size_t) */
61#include <linux/errno.h> /* For the -ENODEV/... values */
62#include <linux/kernel.h> /* For printk/panic/... */
63#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
64#include <linux/watchdog.h> /* For the watchdog specific items */
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +020065#include <linux/init.h> /* For __init/__exit/... */
66#include <linux/fs.h> /* For file operations */
67#include <linux/platform_device.h> /* For platform_driver framework */
68#include <linux/pci.h> /* For pci functions */
69#include <linux/ioport.h> /* For io-port access */
70#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020071
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +020072#include <asm/uaccess.h> /* For copy_to_user/put_user/... */
73#include <asm/io.h> /* For inb/outb/... */
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020074
75/* TCO related info */
76enum iTCO_chipsets {
77 TCO_ICH = 0, /* ICH */
78 TCO_ICH0, /* ICH0 */
79 TCO_ICH2, /* ICH2 */
80 TCO_ICH2M, /* ICH2-M */
81 TCO_ICH3, /* ICH3-S */
82 TCO_ICH3M, /* ICH3-M */
83 TCO_ICH4, /* ICH4 */
84 TCO_ICH4M, /* ICH4-M */
85 TCO_CICH, /* C-ICH */
86 TCO_ICH5, /* ICH5 & ICH5R */
87 TCO_6300ESB, /* 6300ESB */
88 TCO_ICH6, /* ICH6 & ICH6R */
89 TCO_ICH6M, /* ICH6-M */
90 TCO_ICH6W, /* ICH6W & ICH6RW */
91 TCO_ICH7, /* ICH7 & ICH7R */
92 TCO_ICH7M, /* ICH7-M */
93 TCO_ICH7MDH, /* ICH7-M DH */
Wim Van Sebroecka8edd742006-10-08 21:05:21 +020094 TCO_ICH8, /* ICH8 & ICH8R */
95 TCO_ICH8DH, /* ICH8DH */
96 TCO_ICH8DO, /* ICH8DO */
Sergey Kononenko998e6782007-07-26 17:28:35 +030097 TCO_631XESB, /* 631xESB/632xESB */
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020098};
99
100static struct {
101 char *name;
102 unsigned int iTCO_version;
103} iTCO_chipset_info[] __devinitdata = {
104 {"ICH", 1},
105 {"ICH0", 1},
106 {"ICH2", 1},
107 {"ICH2-M", 1},
108 {"ICH3-S", 1},
109 {"ICH3-M", 1},
110 {"ICH4", 1},
111 {"ICH4-M", 1},
112 {"C-ICH", 1},
113 {"ICH5 or ICH5R", 1},
114 {"6300ESB", 1},
115 {"ICH6 or ICH6R", 2},
116 {"ICH6-M", 2},
117 {"ICH6W or ICH6RW", 2},
118 {"ICH7 or ICH7R", 2},
119 {"ICH7-M", 2},
120 {"ICH7-M DH", 2},
Arnaud Patard (Rtp)bcbf25b2006-10-04 14:18:29 +0200121 {"ICH8 or ICH8R", 2},
Wim Van Sebroecka8edd742006-10-08 21:05:21 +0200122 {"ICH8DH", 2},
123 {"ICH8DO", 2},
Sergey Kononenko998e6782007-07-26 17:28:35 +0300124 {"631xESB/632xESB", 2},
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200125 {NULL,0}
126};
127
128/*
129 * This data only exists for exporting the supported PCI ids
130 * via MODULE_DEVICE_TABLE. We do not actually register a
131 * pci_driver, because the I/O Controller Hub has also other
132 * functions that probably will be registered by other drivers.
133 */
134static struct pci_device_id iTCO_wdt_pci_tbl[] = {
135 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH },
136 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH0 },
137 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH2 },
138 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH2M },
139 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH3 },
140 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH3M },
141 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH4 },
142 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH4M },
143 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_CICH },
144 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH5 },
145 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_6300ESB },
146 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6 },
147 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6M },
148 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6W },
149 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7 },
150 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7M },
151 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_31, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7MDH },
Arnaud Patard (Rtp)bcbf25b2006-10-04 14:18:29 +0200152 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH8 },
Wim Van Sebroecka8edd742006-10-08 21:05:21 +0200153 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH8DH },
154 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH8DO },
Sergey Kononenko998e6782007-07-26 17:28:35 +0300155 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
Wim Van Sebroeck7e0a86f2007-07-26 20:43:50 +0000156 { PCI_VENDOR_ID_INTEL, 0x2671, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
157 { PCI_VENDOR_ID_INTEL, 0x2672, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
158 { PCI_VENDOR_ID_INTEL, 0x2673, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
159 { PCI_VENDOR_ID_INTEL, 0x2674, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
160 { PCI_VENDOR_ID_INTEL, 0x2675, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
161 { PCI_VENDOR_ID_INTEL, 0x2676, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
162 { PCI_VENDOR_ID_INTEL, 0x2677, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
163 { PCI_VENDOR_ID_INTEL, 0x2678, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
164 { PCI_VENDOR_ID_INTEL, 0x2679, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
165 { PCI_VENDOR_ID_INTEL, 0x267a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
166 { PCI_VENDOR_ID_INTEL, 0x267b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
167 { PCI_VENDOR_ID_INTEL, 0x267c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
168 { PCI_VENDOR_ID_INTEL, 0x267d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
169 { PCI_VENDOR_ID_INTEL, 0x267e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
170 { PCI_VENDOR_ID_INTEL, 0x267f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200171 { 0, }, /* End of list */
172};
173MODULE_DEVICE_TABLE (pci, iTCO_wdt_pci_tbl);
174
175/* Address definitions for the TCO */
176#define TCOBASE iTCO_wdt_private.ACPIBASE + 0x60 /* TCO base address */
177#define SMI_EN iTCO_wdt_private.ACPIBASE + 0x30 /* SMI Control and Enable Register */
178
179#define TCO_RLD TCOBASE + 0x00 /* TCO Timer Reload and Current Value */
180#define TCOv1_TMR TCOBASE + 0x01 /* TCOv1 Timer Initial Value */
181#define TCO_DAT_IN TCOBASE + 0x02 /* TCO Data In Register */
182#define TCO_DAT_OUT TCOBASE + 0x03 /* TCO Data Out Register */
183#define TCO1_STS TCOBASE + 0x04 /* TCO1 Status Register */
184#define TCO2_STS TCOBASE + 0x06 /* TCO2 Status Register */
185#define TCO1_CNT TCOBASE + 0x08 /* TCO1 Control Register */
186#define TCO2_CNT TCOBASE + 0x0a /* TCO2 Control Register */
187#define TCOv2_TMR TCOBASE + 0x12 /* TCOv2 Timer Initial Value */
188
189/* internal variables */
190static unsigned long is_active;
191static char expect_release;
192static struct { /* this is private data for the iTCO_wdt device */
193 unsigned int iTCO_version; /* TCO version/generation */
194 unsigned long ACPIBASE; /* The cards ACPIBASE address (TCOBASE = ACPIBASE+0x60) */
195 unsigned long __iomem *gcs; /* NO_REBOOT flag is Memory-Mapped GCS register bit 5 (TCO version 2) */
196 spinlock_t io_lock; /* the lock for io operations */
197 struct pci_dev *pdev; /* the PCI-device */
198} iTCO_wdt_private;
199
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200200static struct platform_device *iTCO_wdt_platform_device; /* the watchdog platform device */
201
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200202/* module parameters */
203#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
204static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
205module_param(heartbeat, int, 0);
206MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39 (TCO v1) or 613 (TCO v2), default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
207
208static int nowayout = WATCHDOG_NOWAYOUT;
209module_param(nowayout, int, 0);
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +0100210MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200211
Wim Van Sebroecke0333512006-11-12 18:05:09 +0100212/* iTCO Vendor Specific Support hooks */
213#ifdef CONFIG_ITCO_VENDOR_SUPPORT
214extern void iTCO_vendor_pre_start(unsigned long, unsigned int);
215extern void iTCO_vendor_pre_stop(unsigned long);
216extern void iTCO_vendor_pre_keepalive(unsigned long, unsigned int);
217extern void iTCO_vendor_pre_set_heartbeat(unsigned int);
218extern int iTCO_vendor_check_noreboot_on(void);
219#else
220#define iTCO_vendor_pre_start(acpibase, heartbeat) {}
221#define iTCO_vendor_pre_stop(acpibase) {}
222#define iTCO_vendor_pre_keepalive(acpibase,heartbeat) {}
223#define iTCO_vendor_pre_set_heartbeat(heartbeat) {}
224#define iTCO_vendor_check_noreboot_on() 1 /* 1=check noreboot; 0=don't check */
225#endif
226
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200227/*
228 * Some TCO specific functions
229 */
230
231static inline unsigned int seconds_to_ticks(int seconds)
232{
233 /* the internal timer is stored as ticks which decrement
234 * every 0.6 seconds */
235 return (seconds * 10) / 6;
236}
237
238static void iTCO_wdt_set_NO_REBOOT_bit(void)
239{
240 u32 val32;
241
242 /* Set the NO_REBOOT bit: this disables reboots */
243 if (iTCO_wdt_private.iTCO_version == 2) {
244 val32 = readl(iTCO_wdt_private.gcs);
245 val32 |= 0x00000020;
246 writel(val32, iTCO_wdt_private.gcs);
247 } else if (iTCO_wdt_private.iTCO_version == 1) {
248 pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
249 val32 |= 0x00000002;
250 pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
251 }
252}
253
254static int iTCO_wdt_unset_NO_REBOOT_bit(void)
255{
256 int ret = 0;
257 u32 val32;
258
259 /* Unset the NO_REBOOT bit: this enables reboots */
260 if (iTCO_wdt_private.iTCO_version == 2) {
261 val32 = readl(iTCO_wdt_private.gcs);
262 val32 &= 0xffffffdf;
263 writel(val32, iTCO_wdt_private.gcs);
264
265 val32 = readl(iTCO_wdt_private.gcs);
266 if (val32 & 0x00000020)
267 ret = -EIO;
268 } else if (iTCO_wdt_private.iTCO_version == 1) {
269 pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
270 val32 &= 0xfffffffd;
271 pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
272
273 pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
274 if (val32 & 0x00000002)
275 ret = -EIO;
276 }
277
278 return ret; /* returns: 0 = OK, -EIO = Error */
279}
280
281static int iTCO_wdt_start(void)
282{
283 unsigned int val;
284
285 spin_lock(&iTCO_wdt_private.io_lock);
286
Wim Van Sebroecke0333512006-11-12 18:05:09 +0100287 iTCO_vendor_pre_start(iTCO_wdt_private.ACPIBASE, heartbeat);
288
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200289 /* disable chipset's NO_REBOOT bit */
290 if (iTCO_wdt_unset_NO_REBOOT_bit()) {
291 printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
292 return -EIO;
293 }
294
295 /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
296 val = inw(TCO1_CNT);
297 val &= 0xf7ff;
298 outw(val, TCO1_CNT);
299 val = inw(TCO1_CNT);
300 spin_unlock(&iTCO_wdt_private.io_lock);
301
302 if (val & 0x0800)
303 return -1;
304 return 0;
305}
306
307static int iTCO_wdt_stop(void)
308{
309 unsigned int val;
310
311 spin_lock(&iTCO_wdt_private.io_lock);
312
Wim Van Sebroecke0333512006-11-12 18:05:09 +0100313 iTCO_vendor_pre_stop(iTCO_wdt_private.ACPIBASE);
314
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200315 /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
316 val = inw(TCO1_CNT);
317 val |= 0x0800;
318 outw(val, TCO1_CNT);
319 val = inw(TCO1_CNT);
320
321 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
322 iTCO_wdt_set_NO_REBOOT_bit();
323
324 spin_unlock(&iTCO_wdt_private.io_lock);
325
326 if ((val & 0x0800) == 0)
327 return -1;
328 return 0;
329}
330
331static int iTCO_wdt_keepalive(void)
332{
333 spin_lock(&iTCO_wdt_private.io_lock);
334
Wim Van Sebroecke0333512006-11-12 18:05:09 +0100335 iTCO_vendor_pre_keepalive(iTCO_wdt_private.ACPIBASE, heartbeat);
336
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200337 /* Reload the timer by writing to the TCO Timer Counter register */
338 if (iTCO_wdt_private.iTCO_version == 2) {
339 outw(0x01, TCO_RLD);
340 } else if (iTCO_wdt_private.iTCO_version == 1) {
341 outb(0x01, TCO_RLD);
342 }
343
344 spin_unlock(&iTCO_wdt_private.io_lock);
345 return 0;
346}
347
348static int iTCO_wdt_set_heartbeat(int t)
349{
350 unsigned int val16;
351 unsigned char val8;
352 unsigned int tmrval;
353
354 tmrval = seconds_to_ticks(t);
355 /* from the specs: */
356 /* "Values of 0h-3h are ignored and should not be attempted" */
357 if (tmrval < 0x04)
358 return -EINVAL;
359 if (((iTCO_wdt_private.iTCO_version == 2) && (tmrval > 0x3ff)) ||
360 ((iTCO_wdt_private.iTCO_version == 1) && (tmrval > 0x03f)))
361 return -EINVAL;
362
Wim Van Sebroecke0333512006-11-12 18:05:09 +0100363 iTCO_vendor_pre_set_heartbeat(tmrval);
364
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200365 /* Write new heartbeat to watchdog */
366 if (iTCO_wdt_private.iTCO_version == 2) {
367 spin_lock(&iTCO_wdt_private.io_lock);
368 val16 = inw(TCOv2_TMR);
369 val16 &= 0xfc00;
370 val16 |= tmrval;
371 outw(val16, TCOv2_TMR);
372 val16 = inw(TCOv2_TMR);
373 spin_unlock(&iTCO_wdt_private.io_lock);
374
375 if ((val16 & 0x3ff) != tmrval)
376 return -EINVAL;
377 } else if (iTCO_wdt_private.iTCO_version == 1) {
378 spin_lock(&iTCO_wdt_private.io_lock);
379 val8 = inb(TCOv1_TMR);
380 val8 &= 0xc0;
381 val8 |= (tmrval & 0xff);
382 outb(val8, TCOv1_TMR);
383 val8 = inb(TCOv1_TMR);
384 spin_unlock(&iTCO_wdt_private.io_lock);
385
386 if ((val8 & 0x3f) != tmrval)
387 return -EINVAL;
388 }
389
390 heartbeat = t;
391 return 0;
392}
393
394static int iTCO_wdt_get_timeleft (int *time_left)
395{
396 unsigned int val16;
397 unsigned char val8;
398
399 /* read the TCO Timer */
400 if (iTCO_wdt_private.iTCO_version == 2) {
401 spin_lock(&iTCO_wdt_private.io_lock);
402 val16 = inw(TCO_RLD);
403 val16 &= 0x3ff;
404 spin_unlock(&iTCO_wdt_private.io_lock);
405
406 *time_left = (val16 * 6) / 10;
407 } else if (iTCO_wdt_private.iTCO_version == 1) {
408 spin_lock(&iTCO_wdt_private.io_lock);
409 val8 = inb(TCO_RLD);
410 val8 &= 0x3f;
411 spin_unlock(&iTCO_wdt_private.io_lock);
412
413 *time_left = (val8 * 6) / 10;
Jeff Garzik80060362006-10-10 03:40:44 -0400414 } else
415 return -EINVAL;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200416 return 0;
417}
418
419/*
420 * /dev/watchdog handling
421 */
422
423static int iTCO_wdt_open (struct inode *inode, struct file *file)
424{
425 /* /dev/watchdog can only be opened once */
426 if (test_and_set_bit(0, &is_active))
427 return -EBUSY;
428
429 /*
430 * Reload and activate timer
431 */
432 iTCO_wdt_keepalive();
433 iTCO_wdt_start();
434 return nonseekable_open(inode, file);
435}
436
437static int iTCO_wdt_release (struct inode *inode, struct file *file)
438{
439 /*
440 * Shut off the timer.
441 */
442 if (expect_release == 42) {
443 iTCO_wdt_stop();
444 } else {
445 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
446 iTCO_wdt_keepalive();
447 }
448 clear_bit(0, &is_active);
449 expect_release = 0;
450 return 0;
451}
452
453static ssize_t iTCO_wdt_write (struct file *file, const char __user *data,
454 size_t len, loff_t * ppos)
455{
456 /* See if we got the magic character 'V' and reload the timer */
457 if (len) {
458 if (!nowayout) {
459 size_t i;
460
461 /* note: just in case someone wrote the magic character
462 * five months ago... */
463 expect_release = 0;
464
465 /* scan to see whether or not we got the magic character */
466 for (i = 0; i != len; i++) {
467 char c;
468 if (get_user(c, data+i))
469 return -EFAULT;
470 if (c == 'V')
471 expect_release = 42;
472 }
473 }
474
475 /* someone wrote to us, we should reload the timer */
476 iTCO_wdt_keepalive();
477 }
478 return len;
479}
480
481static int iTCO_wdt_ioctl (struct inode *inode, struct file *file,
482 unsigned int cmd, unsigned long arg)
483{
484 int new_options, retval = -EINVAL;
485 int new_heartbeat;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200486 void __user *argp = (void __user *)arg;
487 int __user *p = argp;
488 static struct watchdog_info ident = {
489 .options = WDIOF_SETTIMEOUT |
490 WDIOF_KEEPALIVEPING |
491 WDIOF_MAGICCLOSE,
492 .firmware_version = 0,
493 .identity = DRV_NAME,
494 };
495
496 switch (cmd) {
497 case WDIOC_GETSUPPORT:
498 return copy_to_user(argp, &ident,
499 sizeof (ident)) ? -EFAULT : 0;
500
501 case WDIOC_GETSTATUS:
502 case WDIOC_GETBOOTSTATUS:
503 return put_user(0, p);
504
505 case WDIOC_KEEPALIVE:
506 iTCO_wdt_keepalive();
507 return 0;
508
509 case WDIOC_SETOPTIONS:
510 {
511 if (get_user(new_options, p))
512 return -EFAULT;
513
514 if (new_options & WDIOS_DISABLECARD) {
515 iTCO_wdt_stop();
516 retval = 0;
517 }
518
519 if (new_options & WDIOS_ENABLECARD) {
520 iTCO_wdt_keepalive();
521 iTCO_wdt_start();
522 retval = 0;
523 }
524
525 return retval;
526 }
527
528 case WDIOC_SETTIMEOUT:
529 {
530 if (get_user(new_heartbeat, p))
531 return -EFAULT;
532
533 if (iTCO_wdt_set_heartbeat(new_heartbeat))
534 return -EINVAL;
535
536 iTCO_wdt_keepalive();
537 /* Fall */
538 }
539
540 case WDIOC_GETTIMEOUT:
541 return put_user(heartbeat, p);
542
543 case WDIOC_GETTIMELEFT:
544 {
Jeff Garzik80060362006-10-10 03:40:44 -0400545 int time_left;
546
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200547 if (iTCO_wdt_get_timeleft(&time_left))
548 return -EINVAL;
549
550 return put_user(time_left, p);
551 }
552
553 default:
Wim Van Sebroeckf3118962006-09-13 21:27:29 +0200554 return -ENOTTY;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200555 }
556}
557
558/*
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200559 * Kernel Interfaces
560 */
561
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800562static const struct file_operations iTCO_wdt_fops = {
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200563 .owner = THIS_MODULE,
564 .llseek = no_llseek,
565 .write = iTCO_wdt_write,
566 .ioctl = iTCO_wdt_ioctl,
567 .open = iTCO_wdt_open,
568 .release = iTCO_wdt_release,
569};
570
571static struct miscdevice iTCO_wdt_miscdev = {
572 .minor = WATCHDOG_MINOR,
573 .name = "watchdog",
574 .fops = &iTCO_wdt_fops,
575};
576
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200577/*
578 * Init & exit routines
579 */
580
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200581static int iTCO_wdt_init(struct pci_dev *pdev, const struct pci_device_id *ent, struct platform_device *dev)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200582{
583 int ret;
584 u32 base_address;
585 unsigned long RCBA;
586 unsigned long val32;
587
588 /*
589 * Find the ACPI/PM base I/O address which is the base
590 * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
591 * ACPIBASE is bits [15:7] from 0x40-0x43
592 */
593 pci_read_config_dword(pdev, 0x40, &base_address);
Wim Van Sebroeck0d4804b2007-05-11 18:59:24 +0000594 base_address &= 0x0000ff80;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200595 if (base_address == 0x00000000) {
596 /* Something's wrong here, ACPIBASE has to be set */
597 printk(KERN_ERR PFX "failed to get TCOBASE address\n");
Wim Van Sebroeck4802c652006-07-19 22:39:13 +0200598 pci_dev_put(pdev);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200599 return -ENODEV;
600 }
601 iTCO_wdt_private.iTCO_version = iTCO_chipset_info[ent->driver_data].iTCO_version;
602 iTCO_wdt_private.ACPIBASE = base_address;
603 iTCO_wdt_private.pdev = pdev;
604
605 /* Get the Memory-Mapped GCS register, we need it for the NO_REBOOT flag (TCO v2) */
606 /* To get access to it you have to read RCBA from PCI Config space 0xf0
607 and use it as base. GCS = RCBA + ICH6_GCS(0x3410). */
608 if (iTCO_wdt_private.iTCO_version == 2) {
609 pci_read_config_dword(pdev, 0xf0, &base_address);
610 RCBA = base_address & 0xffffc000;
611 iTCO_wdt_private.gcs = ioremap((RCBA + 0x3410),4);
612 }
613
614 /* Check chipset's NO_REBOOT bit */
Wim Van Sebroecke0333512006-11-12 18:05:09 +0100615 if (iTCO_wdt_unset_NO_REBOOT_bit() && iTCO_vendor_check_noreboot_on()) {
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200616 printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
617 ret = -ENODEV; /* Cannot reset NO_REBOOT bit */
618 goto out;
619 }
620
621 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
622 iTCO_wdt_set_NO_REBOOT_bit();
623
624 /* Set the TCO_EN bit in SMI_EN register */
625 if (!request_region(SMI_EN, 4, "iTCO_wdt")) {
626 printk(KERN_ERR PFX "I/O address 0x%04lx already in use\n",
627 SMI_EN );
628 ret = -EIO;
629 goto out;
630 }
631 val32 = inl(SMI_EN);
632 val32 &= 0xffffdfff; /* Turn off SMI clearing watchdog */
633 outl(val32, SMI_EN);
634 release_region(SMI_EN, 4);
635
636 /* The TCO I/O registers reside in a 32-byte range pointed to by the TCOBASE value */
637 if (!request_region (TCOBASE, 0x20, "iTCO_wdt")) {
638 printk (KERN_ERR PFX "I/O address 0x%04lx already in use\n",
639 TCOBASE);
640 ret = -EIO;
641 goto out;
642 }
643
644 printk(KERN_INFO PFX "Found a %s TCO device (Version=%d, TCOBASE=0x%04lx)\n",
645 iTCO_chipset_info[ent->driver_data].name,
646 iTCO_chipset_info[ent->driver_data].iTCO_version,
647 TCOBASE);
648
649 /* Clear out the (probably old) status */
650 outb(0, TCO1_STS);
651 outb(3, TCO2_STS);
652
653 /* Make sure the watchdog is not running */
654 iTCO_wdt_stop();
655
656 /* Check that the heartbeat value is within it's range ; if not reset to the default */
657 if (iTCO_wdt_set_heartbeat(heartbeat)) {
658 iTCO_wdt_set_heartbeat(WATCHDOG_HEARTBEAT);
659 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39 (TCO v1) or 613 (TCO v2), using %d\n",
660 heartbeat);
661 }
662
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200663 ret = misc_register(&iTCO_wdt_miscdev);
664 if (ret != 0) {
665 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
666 WATCHDOG_MINOR, ret);
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +0200667 goto unreg_region;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200668 }
669
670 printk (KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
671 heartbeat, nowayout);
672
673 return 0;
674
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200675unreg_region:
676 release_region (TCOBASE, 0x20);
677out:
678 if (iTCO_wdt_private.iTCO_version == 2)
679 iounmap(iTCO_wdt_private.gcs);
Wim Van Sebroeck4802c652006-07-19 22:39:13 +0200680 pci_dev_put(iTCO_wdt_private.pdev);
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +0200681 iTCO_wdt_private.ACPIBASE = 0;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200682 return ret;
683}
684
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200685static void iTCO_wdt_cleanup(void)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200686{
687 /* Stop the timer before we leave */
688 if (!nowayout)
689 iTCO_wdt_stop();
690
691 /* Deregister */
692 misc_deregister(&iTCO_wdt_miscdev);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200693 release_region(TCOBASE, 0x20);
694 if (iTCO_wdt_private.iTCO_version == 2)
695 iounmap(iTCO_wdt_private.gcs);
Wim Van Sebroeck4802c652006-07-19 22:39:13 +0200696 pci_dev_put(iTCO_wdt_private.pdev);
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +0200697 iTCO_wdt_private.ACPIBASE = 0;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200698}
699
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200700static int iTCO_wdt_probe(struct platform_device *dev)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200701{
702 int found = 0;
703 struct pci_dev *pdev = NULL;
704 const struct pci_device_id *ent;
705
706 spin_lock_init(&iTCO_wdt_private.io_lock);
707
708 for_each_pci_dev(pdev) {
709 ent = pci_match_id(iTCO_wdt_pci_tbl, pdev);
710 if (ent) {
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200711 if (!(iTCO_wdt_init(pdev, ent, dev))) {
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200712 found++;
713 break;
714 }
715 }
716 }
717
718 if (!found) {
719 printk(KERN_INFO PFX "No card detected\n");
720 return -ENODEV;
721 }
722
723 return 0;
724}
725
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200726static int iTCO_wdt_remove(struct platform_device *dev)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200727{
728 if (iTCO_wdt_private.ACPIBASE)
729 iTCO_wdt_cleanup();
730
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200731 return 0;
732}
733
734static void iTCO_wdt_shutdown(struct platform_device *dev)
735{
736 iTCO_wdt_stop();
737}
738
739#define iTCO_wdt_suspend NULL
740#define iTCO_wdt_resume NULL
741
742static struct platform_driver iTCO_wdt_driver = {
743 .probe = iTCO_wdt_probe,
744 .remove = iTCO_wdt_remove,
745 .shutdown = iTCO_wdt_shutdown,
746 .suspend = iTCO_wdt_suspend,
747 .resume = iTCO_wdt_resume,
748 .driver = {
749 .owner = THIS_MODULE,
750 .name = DRV_NAME,
751 },
752};
753
754static int __init iTCO_wdt_init_module(void)
755{
756 int err;
757
758 printk(KERN_INFO PFX "Intel TCO WatchDog Timer Driver v%s (%s)\n",
759 DRV_VERSION, DRV_RELDATE);
760
761 err = platform_driver_register(&iTCO_wdt_driver);
762 if (err)
763 return err;
764
765 iTCO_wdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
766 if (IS_ERR(iTCO_wdt_platform_device)) {
767 err = PTR_ERR(iTCO_wdt_platform_device);
768 goto unreg_platform_driver;
769 }
770
771 return 0;
772
773unreg_platform_driver:
774 platform_driver_unregister(&iTCO_wdt_driver);
775 return err;
776}
777
778static void __exit iTCO_wdt_cleanup_module(void)
779{
780 platform_device_unregister(iTCO_wdt_platform_device);
781 platform_driver_unregister(&iTCO_wdt_driver);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200782 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
783}
784
785module_init(iTCO_wdt_init_module);
786module_exit(iTCO_wdt_cleanup_module);
787
788MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
789MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200790MODULE_VERSION(DRV_VERSION);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200791MODULE_LICENSE("GPL");
792MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);