blob: 639d84f0c0249f435f7a4bbbbeb2f154b13cc319 [file] [log] [blame]
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +02001/*
2 * intel TCO Watchdog Driver (Used in i82801 and i6300ESB chipsets)
3 *
4 * (c) Copyright 2006 Wim Van Sebroeck <wim@iguana.be>.
5 *
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,
38 * 6300ESB (6300ESB) : document number 300641-003
39 */
40
41/*
42 * Includes, defines, variables, module parameters, ...
43 */
44
45/* Module and version information */
46#define DRV_NAME "iTCO_wdt"
47#define DRV_VERSION "1.00"
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +020048#define DRV_RELDATE "30-Jul-2006"
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020049#define PFX DRV_NAME ": "
50
51/* Includes */
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +020052#include <linux/module.h> /* For module specific items */
53#include <linux/moduleparam.h> /* For new moduleparam's */
54#include <linux/types.h> /* For standard types (like size_t) */
55#include <linux/errno.h> /* For the -ENODEV/... values */
56#include <linux/kernel.h> /* For printk/panic/... */
57#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
58#include <linux/watchdog.h> /* For the watchdog specific items */
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +020059#include <linux/init.h> /* For __init/__exit/... */
60#include <linux/fs.h> /* For file operations */
61#include <linux/platform_device.h> /* For platform_driver framework */
62#include <linux/pci.h> /* For pci functions */
63#include <linux/ioport.h> /* For io-port access */
64#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020065
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +020066#include <asm/uaccess.h> /* For copy_to_user/put_user/... */
67#include <asm/io.h> /* For inb/outb/... */
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020068
69/* TCO related info */
70enum iTCO_chipsets {
71 TCO_ICH = 0, /* ICH */
72 TCO_ICH0, /* ICH0 */
73 TCO_ICH2, /* ICH2 */
74 TCO_ICH2M, /* ICH2-M */
75 TCO_ICH3, /* ICH3-S */
76 TCO_ICH3M, /* ICH3-M */
77 TCO_ICH4, /* ICH4 */
78 TCO_ICH4M, /* ICH4-M */
79 TCO_CICH, /* C-ICH */
80 TCO_ICH5, /* ICH5 & ICH5R */
81 TCO_6300ESB, /* 6300ESB */
82 TCO_ICH6, /* ICH6 & ICH6R */
83 TCO_ICH6M, /* ICH6-M */
84 TCO_ICH6W, /* ICH6W & ICH6RW */
85 TCO_ICH7, /* ICH7 & ICH7R */
86 TCO_ICH7M, /* ICH7-M */
87 TCO_ICH7MDH, /* ICH7-M DH */
Arnaud Patard (Rtp)bcbf25b2006-10-04 14:18:29 +020088 TCO_ICH8, /* ICH8 */
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +020089};
90
91static struct {
92 char *name;
93 unsigned int iTCO_version;
94} iTCO_chipset_info[] __devinitdata = {
95 {"ICH", 1},
96 {"ICH0", 1},
97 {"ICH2", 1},
98 {"ICH2-M", 1},
99 {"ICH3-S", 1},
100 {"ICH3-M", 1},
101 {"ICH4", 1},
102 {"ICH4-M", 1},
103 {"C-ICH", 1},
104 {"ICH5 or ICH5R", 1},
105 {"6300ESB", 1},
106 {"ICH6 or ICH6R", 2},
107 {"ICH6-M", 2},
108 {"ICH6W or ICH6RW", 2},
109 {"ICH7 or ICH7R", 2},
110 {"ICH7-M", 2},
111 {"ICH7-M DH", 2},
Arnaud Patard (Rtp)bcbf25b2006-10-04 14:18:29 +0200112 {"ICH8 or ICH8R", 2},
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200113 {NULL,0}
114};
115
116/*
117 * This data only exists for exporting the supported PCI ids
118 * via MODULE_DEVICE_TABLE. We do not actually register a
119 * pci_driver, because the I/O Controller Hub has also other
120 * functions that probably will be registered by other drivers.
121 */
122static struct pci_device_id iTCO_wdt_pci_tbl[] = {
123 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH },
124 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH0 },
125 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH2 },
126 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH2M },
127 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH3 },
128 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH3M },
129 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH4 },
130 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH4M },
131 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_CICH },
132 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH5 },
133 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_6300ESB },
134 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6 },
135 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6M },
136 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH6W },
137 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7 },
138 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH7M },
139 { 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 +0200140 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH8 },
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200141 { 0, }, /* End of list */
142};
143MODULE_DEVICE_TABLE (pci, iTCO_wdt_pci_tbl);
144
145/* Address definitions for the TCO */
146#define TCOBASE iTCO_wdt_private.ACPIBASE + 0x60 /* TCO base address */
147#define SMI_EN iTCO_wdt_private.ACPIBASE + 0x30 /* SMI Control and Enable Register */
148
149#define TCO_RLD TCOBASE + 0x00 /* TCO Timer Reload and Current Value */
150#define TCOv1_TMR TCOBASE + 0x01 /* TCOv1 Timer Initial Value */
151#define TCO_DAT_IN TCOBASE + 0x02 /* TCO Data In Register */
152#define TCO_DAT_OUT TCOBASE + 0x03 /* TCO Data Out Register */
153#define TCO1_STS TCOBASE + 0x04 /* TCO1 Status Register */
154#define TCO2_STS TCOBASE + 0x06 /* TCO2 Status Register */
155#define TCO1_CNT TCOBASE + 0x08 /* TCO1 Control Register */
156#define TCO2_CNT TCOBASE + 0x0a /* TCO2 Control Register */
157#define TCOv2_TMR TCOBASE + 0x12 /* TCOv2 Timer Initial Value */
158
159/* internal variables */
160static unsigned long is_active;
161static char expect_release;
162static struct { /* this is private data for the iTCO_wdt device */
163 unsigned int iTCO_version; /* TCO version/generation */
164 unsigned long ACPIBASE; /* The cards ACPIBASE address (TCOBASE = ACPIBASE+0x60) */
165 unsigned long __iomem *gcs; /* NO_REBOOT flag is Memory-Mapped GCS register bit 5 (TCO version 2) */
166 spinlock_t io_lock; /* the lock for io operations */
167 struct pci_dev *pdev; /* the PCI-device */
168} iTCO_wdt_private;
169
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200170static struct platform_device *iTCO_wdt_platform_device; /* the watchdog platform device */
171
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200172/* module parameters */
173#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
174static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
175module_param(heartbeat, int, 0);
176MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39 (TCO v1) or 613 (TCO v2), default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
177
178static int nowayout = WATCHDOG_NOWAYOUT;
179module_param(nowayout, int, 0);
180MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
181
182/*
183 * Some TCO specific functions
184 */
185
186static inline unsigned int seconds_to_ticks(int seconds)
187{
188 /* the internal timer is stored as ticks which decrement
189 * every 0.6 seconds */
190 return (seconds * 10) / 6;
191}
192
193static void iTCO_wdt_set_NO_REBOOT_bit(void)
194{
195 u32 val32;
196
197 /* Set the NO_REBOOT bit: this disables reboots */
198 if (iTCO_wdt_private.iTCO_version == 2) {
199 val32 = readl(iTCO_wdt_private.gcs);
200 val32 |= 0x00000020;
201 writel(val32, iTCO_wdt_private.gcs);
202 } else if (iTCO_wdt_private.iTCO_version == 1) {
203 pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
204 val32 |= 0x00000002;
205 pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
206 }
207}
208
209static int iTCO_wdt_unset_NO_REBOOT_bit(void)
210{
211 int ret = 0;
212 u32 val32;
213
214 /* Unset the NO_REBOOT bit: this enables reboots */
215 if (iTCO_wdt_private.iTCO_version == 2) {
216 val32 = readl(iTCO_wdt_private.gcs);
217 val32 &= 0xffffffdf;
218 writel(val32, iTCO_wdt_private.gcs);
219
220 val32 = readl(iTCO_wdt_private.gcs);
221 if (val32 & 0x00000020)
222 ret = -EIO;
223 } else if (iTCO_wdt_private.iTCO_version == 1) {
224 pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
225 val32 &= 0xfffffffd;
226 pci_write_config_dword(iTCO_wdt_private.pdev, 0xd4, val32);
227
228 pci_read_config_dword(iTCO_wdt_private.pdev, 0xd4, &val32);
229 if (val32 & 0x00000002)
230 ret = -EIO;
231 }
232
233 return ret; /* returns: 0 = OK, -EIO = Error */
234}
235
236static int iTCO_wdt_start(void)
237{
238 unsigned int val;
239
240 spin_lock(&iTCO_wdt_private.io_lock);
241
242 /* disable chipset's NO_REBOOT bit */
243 if (iTCO_wdt_unset_NO_REBOOT_bit()) {
244 printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
245 return -EIO;
246 }
247
248 /* Bit 11: TCO Timer Halt -> 0 = The TCO timer is enabled to count */
249 val = inw(TCO1_CNT);
250 val &= 0xf7ff;
251 outw(val, TCO1_CNT);
252 val = inw(TCO1_CNT);
253 spin_unlock(&iTCO_wdt_private.io_lock);
254
255 if (val & 0x0800)
256 return -1;
257 return 0;
258}
259
260static int iTCO_wdt_stop(void)
261{
262 unsigned int val;
263
264 spin_lock(&iTCO_wdt_private.io_lock);
265
266 /* Bit 11: TCO Timer Halt -> 1 = The TCO timer is disabled */
267 val = inw(TCO1_CNT);
268 val |= 0x0800;
269 outw(val, TCO1_CNT);
270 val = inw(TCO1_CNT);
271
272 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
273 iTCO_wdt_set_NO_REBOOT_bit();
274
275 spin_unlock(&iTCO_wdt_private.io_lock);
276
277 if ((val & 0x0800) == 0)
278 return -1;
279 return 0;
280}
281
282static int iTCO_wdt_keepalive(void)
283{
284 spin_lock(&iTCO_wdt_private.io_lock);
285
286 /* Reload the timer by writing to the TCO Timer Counter register */
287 if (iTCO_wdt_private.iTCO_version == 2) {
288 outw(0x01, TCO_RLD);
289 } else if (iTCO_wdt_private.iTCO_version == 1) {
290 outb(0x01, TCO_RLD);
291 }
292
293 spin_unlock(&iTCO_wdt_private.io_lock);
294 return 0;
295}
296
297static int iTCO_wdt_set_heartbeat(int t)
298{
299 unsigned int val16;
300 unsigned char val8;
301 unsigned int tmrval;
302
303 tmrval = seconds_to_ticks(t);
304 /* from the specs: */
305 /* "Values of 0h-3h are ignored and should not be attempted" */
306 if (tmrval < 0x04)
307 return -EINVAL;
308 if (((iTCO_wdt_private.iTCO_version == 2) && (tmrval > 0x3ff)) ||
309 ((iTCO_wdt_private.iTCO_version == 1) && (tmrval > 0x03f)))
310 return -EINVAL;
311
312 /* Write new heartbeat to watchdog */
313 if (iTCO_wdt_private.iTCO_version == 2) {
314 spin_lock(&iTCO_wdt_private.io_lock);
315 val16 = inw(TCOv2_TMR);
316 val16 &= 0xfc00;
317 val16 |= tmrval;
318 outw(val16, TCOv2_TMR);
319 val16 = inw(TCOv2_TMR);
320 spin_unlock(&iTCO_wdt_private.io_lock);
321
322 if ((val16 & 0x3ff) != tmrval)
323 return -EINVAL;
324 } else if (iTCO_wdt_private.iTCO_version == 1) {
325 spin_lock(&iTCO_wdt_private.io_lock);
326 val8 = inb(TCOv1_TMR);
327 val8 &= 0xc0;
328 val8 |= (tmrval & 0xff);
329 outb(val8, TCOv1_TMR);
330 val8 = inb(TCOv1_TMR);
331 spin_unlock(&iTCO_wdt_private.io_lock);
332
333 if ((val8 & 0x3f) != tmrval)
334 return -EINVAL;
335 }
336
337 heartbeat = t;
338 return 0;
339}
340
341static int iTCO_wdt_get_timeleft (int *time_left)
342{
343 unsigned int val16;
344 unsigned char val8;
345
346 /* read the TCO Timer */
347 if (iTCO_wdt_private.iTCO_version == 2) {
348 spin_lock(&iTCO_wdt_private.io_lock);
349 val16 = inw(TCO_RLD);
350 val16 &= 0x3ff;
351 spin_unlock(&iTCO_wdt_private.io_lock);
352
353 *time_left = (val16 * 6) / 10;
354 } else if (iTCO_wdt_private.iTCO_version == 1) {
355 spin_lock(&iTCO_wdt_private.io_lock);
356 val8 = inb(TCO_RLD);
357 val8 &= 0x3f;
358 spin_unlock(&iTCO_wdt_private.io_lock);
359
360 *time_left = (val8 * 6) / 10;
361 }
362 return 0;
363}
364
365/*
366 * /dev/watchdog handling
367 */
368
369static int iTCO_wdt_open (struct inode *inode, struct file *file)
370{
371 /* /dev/watchdog can only be opened once */
372 if (test_and_set_bit(0, &is_active))
373 return -EBUSY;
374
375 /*
376 * Reload and activate timer
377 */
378 iTCO_wdt_keepalive();
379 iTCO_wdt_start();
380 return nonseekable_open(inode, file);
381}
382
383static int iTCO_wdt_release (struct inode *inode, struct file *file)
384{
385 /*
386 * Shut off the timer.
387 */
388 if (expect_release == 42) {
389 iTCO_wdt_stop();
390 } else {
391 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
392 iTCO_wdt_keepalive();
393 }
394 clear_bit(0, &is_active);
395 expect_release = 0;
396 return 0;
397}
398
399static ssize_t iTCO_wdt_write (struct file *file, const char __user *data,
400 size_t len, loff_t * ppos)
401{
402 /* See if we got the magic character 'V' and reload the timer */
403 if (len) {
404 if (!nowayout) {
405 size_t i;
406
407 /* note: just in case someone wrote the magic character
408 * five months ago... */
409 expect_release = 0;
410
411 /* scan to see whether or not we got the magic character */
412 for (i = 0; i != len; i++) {
413 char c;
414 if (get_user(c, data+i))
415 return -EFAULT;
416 if (c == 'V')
417 expect_release = 42;
418 }
419 }
420
421 /* someone wrote to us, we should reload the timer */
422 iTCO_wdt_keepalive();
423 }
424 return len;
425}
426
427static int iTCO_wdt_ioctl (struct inode *inode, struct file *file,
428 unsigned int cmd, unsigned long arg)
429{
430 int new_options, retval = -EINVAL;
431 int new_heartbeat;
432 int time_left;
433 void __user *argp = (void __user *)arg;
434 int __user *p = argp;
435 static struct watchdog_info ident = {
436 .options = WDIOF_SETTIMEOUT |
437 WDIOF_KEEPALIVEPING |
438 WDIOF_MAGICCLOSE,
439 .firmware_version = 0,
440 .identity = DRV_NAME,
441 };
442
443 switch (cmd) {
444 case WDIOC_GETSUPPORT:
445 return copy_to_user(argp, &ident,
446 sizeof (ident)) ? -EFAULT : 0;
447
448 case WDIOC_GETSTATUS:
449 case WDIOC_GETBOOTSTATUS:
450 return put_user(0, p);
451
452 case WDIOC_KEEPALIVE:
453 iTCO_wdt_keepalive();
454 return 0;
455
456 case WDIOC_SETOPTIONS:
457 {
458 if (get_user(new_options, p))
459 return -EFAULT;
460
461 if (new_options & WDIOS_DISABLECARD) {
462 iTCO_wdt_stop();
463 retval = 0;
464 }
465
466 if (new_options & WDIOS_ENABLECARD) {
467 iTCO_wdt_keepalive();
468 iTCO_wdt_start();
469 retval = 0;
470 }
471
472 return retval;
473 }
474
475 case WDIOC_SETTIMEOUT:
476 {
477 if (get_user(new_heartbeat, p))
478 return -EFAULT;
479
480 if (iTCO_wdt_set_heartbeat(new_heartbeat))
481 return -EINVAL;
482
483 iTCO_wdt_keepalive();
484 /* Fall */
485 }
486
487 case WDIOC_GETTIMEOUT:
488 return put_user(heartbeat, p);
489
490 case WDIOC_GETTIMELEFT:
491 {
492 if (iTCO_wdt_get_timeleft(&time_left))
493 return -EINVAL;
494
495 return put_user(time_left, p);
496 }
497
498 default:
Wim Van Sebroeckf3118962006-09-13 21:27:29 +0200499 return -ENOTTY;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200500 }
501}
502
503/*
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200504 * Kernel Interfaces
505 */
506
507static struct file_operations iTCO_wdt_fops = {
508 .owner = THIS_MODULE,
509 .llseek = no_llseek,
510 .write = iTCO_wdt_write,
511 .ioctl = iTCO_wdt_ioctl,
512 .open = iTCO_wdt_open,
513 .release = iTCO_wdt_release,
514};
515
516static struct miscdevice iTCO_wdt_miscdev = {
517 .minor = WATCHDOG_MINOR,
518 .name = "watchdog",
519 .fops = &iTCO_wdt_fops,
520};
521
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200522/*
523 * Init & exit routines
524 */
525
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200526static 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 +0200527{
528 int ret;
529 u32 base_address;
530 unsigned long RCBA;
531 unsigned long val32;
532
533 /*
534 * Find the ACPI/PM base I/O address which is the base
535 * for the TCO registers (TCOBASE=ACPIBASE + 0x60)
536 * ACPIBASE is bits [15:7] from 0x40-0x43
537 */
538 pci_read_config_dword(pdev, 0x40, &base_address);
539 base_address &= 0x00007f80;
540 if (base_address == 0x00000000) {
541 /* Something's wrong here, ACPIBASE has to be set */
542 printk(KERN_ERR PFX "failed to get TCOBASE address\n");
Wim Van Sebroeck4802c652006-07-19 22:39:13 +0200543 pci_dev_put(pdev);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200544 return -ENODEV;
545 }
546 iTCO_wdt_private.iTCO_version = iTCO_chipset_info[ent->driver_data].iTCO_version;
547 iTCO_wdt_private.ACPIBASE = base_address;
548 iTCO_wdt_private.pdev = pdev;
549
550 /* Get the Memory-Mapped GCS register, we need it for the NO_REBOOT flag (TCO v2) */
551 /* To get access to it you have to read RCBA from PCI Config space 0xf0
552 and use it as base. GCS = RCBA + ICH6_GCS(0x3410). */
553 if (iTCO_wdt_private.iTCO_version == 2) {
554 pci_read_config_dword(pdev, 0xf0, &base_address);
555 RCBA = base_address & 0xffffc000;
556 iTCO_wdt_private.gcs = ioremap((RCBA + 0x3410),4);
557 }
558
559 /* Check chipset's NO_REBOOT bit */
560 if (iTCO_wdt_unset_NO_REBOOT_bit()) {
561 printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
562 ret = -ENODEV; /* Cannot reset NO_REBOOT bit */
563 goto out;
564 }
565
566 /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
567 iTCO_wdt_set_NO_REBOOT_bit();
568
569 /* Set the TCO_EN bit in SMI_EN register */
570 if (!request_region(SMI_EN, 4, "iTCO_wdt")) {
571 printk(KERN_ERR PFX "I/O address 0x%04lx already in use\n",
572 SMI_EN );
573 ret = -EIO;
574 goto out;
575 }
576 val32 = inl(SMI_EN);
577 val32 &= 0xffffdfff; /* Turn off SMI clearing watchdog */
578 outl(val32, SMI_EN);
579 release_region(SMI_EN, 4);
580
581 /* The TCO I/O registers reside in a 32-byte range pointed to by the TCOBASE value */
582 if (!request_region (TCOBASE, 0x20, "iTCO_wdt")) {
583 printk (KERN_ERR PFX "I/O address 0x%04lx already in use\n",
584 TCOBASE);
585 ret = -EIO;
586 goto out;
587 }
588
589 printk(KERN_INFO PFX "Found a %s TCO device (Version=%d, TCOBASE=0x%04lx)\n",
590 iTCO_chipset_info[ent->driver_data].name,
591 iTCO_chipset_info[ent->driver_data].iTCO_version,
592 TCOBASE);
593
594 /* Clear out the (probably old) status */
595 outb(0, TCO1_STS);
596 outb(3, TCO2_STS);
597
598 /* Make sure the watchdog is not running */
599 iTCO_wdt_stop();
600
601 /* Check that the heartbeat value is within it's range ; if not reset to the default */
602 if (iTCO_wdt_set_heartbeat(heartbeat)) {
603 iTCO_wdt_set_heartbeat(WATCHDOG_HEARTBEAT);
604 printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39 (TCO v1) or 613 (TCO v2), using %d\n",
605 heartbeat);
606 }
607
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200608 ret = misc_register(&iTCO_wdt_miscdev);
609 if (ret != 0) {
610 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
611 WATCHDOG_MINOR, ret);
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +0200612 goto unreg_region;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200613 }
614
615 printk (KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
616 heartbeat, nowayout);
617
618 return 0;
619
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200620unreg_region:
621 release_region (TCOBASE, 0x20);
622out:
623 if (iTCO_wdt_private.iTCO_version == 2)
624 iounmap(iTCO_wdt_private.gcs);
Wim Van Sebroeck4802c652006-07-19 22:39:13 +0200625 pci_dev_put(iTCO_wdt_private.pdev);
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +0200626 iTCO_wdt_private.ACPIBASE = 0;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200627 return ret;
628}
629
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200630static void iTCO_wdt_cleanup(void)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200631{
632 /* Stop the timer before we leave */
633 if (!nowayout)
634 iTCO_wdt_stop();
635
636 /* Deregister */
637 misc_deregister(&iTCO_wdt_miscdev);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200638 release_region(TCOBASE, 0x20);
639 if (iTCO_wdt_private.iTCO_version == 2)
640 iounmap(iTCO_wdt_private.gcs);
Wim Van Sebroeck4802c652006-07-19 22:39:13 +0200641 pci_dev_put(iTCO_wdt_private.pdev);
Wim Van Sebroeck1bef84b2006-08-05 20:59:01 +0200642 iTCO_wdt_private.ACPIBASE = 0;
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200643}
644
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200645static int iTCO_wdt_probe(struct platform_device *dev)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200646{
647 int found = 0;
648 struct pci_dev *pdev = NULL;
649 const struct pci_device_id *ent;
650
651 spin_lock_init(&iTCO_wdt_private.io_lock);
652
653 for_each_pci_dev(pdev) {
654 ent = pci_match_id(iTCO_wdt_pci_tbl, pdev);
655 if (ent) {
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200656 if (!(iTCO_wdt_init(pdev, ent, dev))) {
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200657 found++;
658 break;
659 }
660 }
661 }
662
663 if (!found) {
664 printk(KERN_INFO PFX "No card detected\n");
665 return -ENODEV;
666 }
667
668 return 0;
669}
670
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200671static int iTCO_wdt_remove(struct platform_device *dev)
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200672{
673 if (iTCO_wdt_private.ACPIBASE)
674 iTCO_wdt_cleanup();
675
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200676 return 0;
677}
678
679static void iTCO_wdt_shutdown(struct platform_device *dev)
680{
681 iTCO_wdt_stop();
682}
683
684#define iTCO_wdt_suspend NULL
685#define iTCO_wdt_resume NULL
686
687static struct platform_driver iTCO_wdt_driver = {
688 .probe = iTCO_wdt_probe,
689 .remove = iTCO_wdt_remove,
690 .shutdown = iTCO_wdt_shutdown,
691 .suspend = iTCO_wdt_suspend,
692 .resume = iTCO_wdt_resume,
693 .driver = {
694 .owner = THIS_MODULE,
695 .name = DRV_NAME,
696 },
697};
698
699static int __init iTCO_wdt_init_module(void)
700{
701 int err;
702
703 printk(KERN_INFO PFX "Intel TCO WatchDog Timer Driver v%s (%s)\n",
704 DRV_VERSION, DRV_RELDATE);
705
706 err = platform_driver_register(&iTCO_wdt_driver);
707 if (err)
708 return err;
709
710 iTCO_wdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
711 if (IS_ERR(iTCO_wdt_platform_device)) {
712 err = PTR_ERR(iTCO_wdt_platform_device);
713 goto unreg_platform_driver;
714 }
715
716 return 0;
717
718unreg_platform_driver:
719 platform_driver_unregister(&iTCO_wdt_driver);
720 return err;
721}
722
723static void __exit iTCO_wdt_cleanup_module(void)
724{
725 platform_device_unregister(iTCO_wdt_platform_device);
726 platform_driver_unregister(&iTCO_wdt_driver);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200727 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
728}
729
730module_init(iTCO_wdt_init_module);
731module_exit(iTCO_wdt_cleanup_module);
732
733MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
734MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
Wim Van Sebroeck3836cc02006-06-30 08:44:53 +0200735MODULE_VERSION(DRV_VERSION);
Wim Van Sebroeck9e0ea342006-05-21 14:37:44 +0200736MODULE_LICENSE("GPL");
737MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);