]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/watchdog/iTCO_wdt.c
[WATCHDOG] 631xESB/632xESB support for iTCO_wdt - add all LPC bridges
[linux-2.6-omap-h63xx.git] / drivers / char / watchdog / iTCO_wdt.c
1 /*
2  *      intel TCO Watchdog Driver (Used in i82801 and i6300ESB chipsets)
3  *
4  *      (c) Copyright 2006-2007 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  *      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,
42  *      6300ESB  (6300ESB)   : document number 300641-003,
43  *      631xESB  (631xESB)   : document number 313082-001, 313075-005,
44  *      632xESB  (632xESB)   : document number 313082-001, 313075-005
45  */
46
47 /*
48  *      Includes, defines, variables, module parameters, ...
49  */
50
51 /* Module and version information */
52 #define DRV_NAME        "iTCO_wdt"
53 #define DRV_VERSION     "1.01"
54 #define DRV_RELDATE     "21-Jan-2007"
55 #define PFX             DRV_NAME ": "
56
57 /* Includes */
58 #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 */
65 #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/... */
71
72 #include <asm/uaccess.h>                /* For copy_to_user/put_user/... */
73 #include <asm/io.h>                     /* For inb/outb/... */
74
75 /* TCO related info */
76 enum 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 */
94         TCO_ICH8,       /* ICH8 & ICH8R */
95         TCO_ICH8DH,     /* ICH8DH */
96         TCO_ICH8DO,     /* ICH8DO */
97         TCO_631XESB,    /* 631xESB/632xESB */
98 };
99
100 static 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},
121         {"ICH8 or ICH8R", 2},
122         {"ICH8DH", 2},
123         {"ICH8DO", 2},
124         {"631xESB/632xESB", 2},
125         {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  */
134 static 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 },
152         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8_0,      PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_ICH8    },
153         { 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  },
155         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_0,      PCI_ANY_ID, PCI_ANY_ID, 0, 0, TCO_631XESB },
156         { 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 },
171         { 0, },                 /* End of list */
172 };
173 MODULE_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 */
190 static unsigned long is_active;
191 static char expect_release;
192 static 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
200 static struct platform_device *iTCO_wdt_platform_device;        /* the watchdog platform device */
201
202 /* module parameters */
203 #define WATCHDOG_HEARTBEAT 30   /* 30 sec default heartbeat */
204 static int heartbeat = WATCHDOG_HEARTBEAT;  /* in seconds */
205 module_param(heartbeat, int, 0);
206 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39 (TCO v1) or 613 (TCO v2), default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
207
208 static int nowayout = WATCHDOG_NOWAYOUT;
209 module_param(nowayout, int, 0);
210 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
211
212 /* iTCO Vendor Specific Support hooks */
213 #ifdef CONFIG_ITCO_VENDOR_SUPPORT
214 extern void iTCO_vendor_pre_start(unsigned long, unsigned int);
215 extern void iTCO_vendor_pre_stop(unsigned long);
216 extern void iTCO_vendor_pre_keepalive(unsigned long, unsigned int);
217 extern void iTCO_vendor_pre_set_heartbeat(unsigned int);
218 extern 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
227 /*
228  * Some TCO specific functions
229  */
230
231 static 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
238 static 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
254 static 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
281 static int iTCO_wdt_start(void)
282 {
283         unsigned int val;
284
285         spin_lock(&iTCO_wdt_private.io_lock);
286
287         iTCO_vendor_pre_start(iTCO_wdt_private.ACPIBASE, heartbeat);
288
289         /* 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
307 static int iTCO_wdt_stop(void)
308 {
309         unsigned int val;
310
311         spin_lock(&iTCO_wdt_private.io_lock);
312
313         iTCO_vendor_pre_stop(iTCO_wdt_private.ACPIBASE);
314
315         /* 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
331 static int iTCO_wdt_keepalive(void)
332 {
333         spin_lock(&iTCO_wdt_private.io_lock);
334
335         iTCO_vendor_pre_keepalive(iTCO_wdt_private.ACPIBASE, heartbeat);
336
337         /* 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
348 static 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
363         iTCO_vendor_pre_set_heartbeat(tmrval);
364
365         /* 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
394 static 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;
414         } else
415                 return -EINVAL;
416         return 0;
417 }
418
419 /*
420  *      /dev/watchdog handling
421  */
422
423 static 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
437 static 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
453 static 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
481 static 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;
486         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                 {
545                         int time_left;
546
547                         if (iTCO_wdt_get_timeleft(&time_left))
548                                 return -EINVAL;
549
550                         return put_user(time_left, p);
551                 }
552
553                 default:
554                         return -ENOTTY;
555         }
556 }
557
558 /*
559  *      Kernel Interfaces
560  */
561
562 static const struct file_operations iTCO_wdt_fops = {
563         .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
571 static struct miscdevice iTCO_wdt_miscdev = {
572         .minor =        WATCHDOG_MINOR,
573         .name =         "watchdog",
574         .fops =         &iTCO_wdt_fops,
575 };
576
577 /*
578  *      Init & exit routines
579  */
580
581 static int iTCO_wdt_init(struct pci_dev *pdev, const struct pci_device_id *ent, struct platform_device *dev)
582 {
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);
594         base_address &= 0x0000ff80;
595         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");
598                 pci_dev_put(pdev);
599                 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 */
615         if (iTCO_wdt_unset_NO_REBOOT_bit() && iTCO_vendor_check_noreboot_on()) {
616                 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
663         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);
667                 goto unreg_region;
668         }
669
670         printk (KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
671                 heartbeat, nowayout);
672
673         return 0;
674
675 unreg_region:
676         release_region (TCOBASE, 0x20);
677 out:
678         if (iTCO_wdt_private.iTCO_version == 2)
679                 iounmap(iTCO_wdt_private.gcs);
680         pci_dev_put(iTCO_wdt_private.pdev);
681         iTCO_wdt_private.ACPIBASE = 0;
682         return ret;
683 }
684
685 static void iTCO_wdt_cleanup(void)
686 {
687         /* Stop the timer before we leave */
688         if (!nowayout)
689                 iTCO_wdt_stop();
690
691         /* Deregister */
692         misc_deregister(&iTCO_wdt_miscdev);
693         release_region(TCOBASE, 0x20);
694         if (iTCO_wdt_private.iTCO_version == 2)
695                 iounmap(iTCO_wdt_private.gcs);
696         pci_dev_put(iTCO_wdt_private.pdev);
697         iTCO_wdt_private.ACPIBASE = 0;
698 }
699
700 static int iTCO_wdt_probe(struct platform_device *dev)
701 {
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) {
711                         if (!(iTCO_wdt_init(pdev, ent, dev))) {
712                                 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
726 static int iTCO_wdt_remove(struct platform_device *dev)
727 {
728         if (iTCO_wdt_private.ACPIBASE)
729                 iTCO_wdt_cleanup();
730
731         return 0;
732 }
733
734 static 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
742 static 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
754 static 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
773 unreg_platform_driver:
774         platform_driver_unregister(&iTCO_wdt_driver);
775         return err;
776 }
777
778 static void __exit iTCO_wdt_cleanup_module(void)
779 {
780         platform_device_unregister(iTCO_wdt_platform_device);
781         platform_driver_unregister(&iTCO_wdt_driver);
782         printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
783 }
784
785 module_init(iTCO_wdt_init_module);
786 module_exit(iTCO_wdt_cleanup_module);
787
788 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
789 MODULE_DESCRIPTION("Intel TCO WatchDog Timer Driver");
790 MODULE_VERSION(DRV_VERSION);
791 MODULE_LICENSE("GPL");
792 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);