1 /*
2 * ALi M7101 PMU Computer Watchdog Timer driver for Linux 2.4.x
3 *
4 * Based on w83877f_wdt.c by Scott Jennings <management@oro.net>
5 * and the Cobalt kernel WDT timer driver by Tim Hockin
6 * <thockin@cobaltnet.com>
7 *
8 * (c)2002 Steve Hill <steve@navaho.co.uk>
9 *
10 * Theory of operation:
11 * A Watchdog Timer (WDT) is a hardware circuit that can
12 * reset the computer system in case of a software fault.
13 * You probably knew that already.
14 *
15 * Usually a userspace daemon will notify the kernel WDT driver
16 * via the /proc/watchdog special device file that userspace is
17 * still alive, at regular intervals. When such a notification
18 * occurs, the driver will usually tell the hardware watchdog
19 * that everything is in order, and that the watchdog should wait
20 * for yet another little while to reset the system.
21 * If userspace fails (RAM error, kernel bug, whatever), the
22 * notifications cease to occur, and the hardware watchdog will
23 * reset the system (causing a reboot) after the timeout occurs.
24 *
25 * This WDT driver is different from most other Linux WDT
26 * drivers in that the driver will ping the watchdog by itself,
27 * because this particular WDT has a very short timeout (1.6
28 * seconds) and it would be insane to count on any userspace
29 * daemon always getting scheduled within that time frame.
30 */
31
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/timer.h>
38 #include <linux/sched.h>
39 #include <linux/miscdevice.h>
40 #include <linux/watchdog.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
44 #include <linux/smp_lock.h>
45 #include <asm/io.h>
46 #include <asm/uaccess.h>
47 #include <asm/system.h>
48 #include <linux/notifier.h>
49 #include <linux/reboot.h>
50 #include <linux/init.h>
51 #include <linux/pci.h>
52
53 #define OUR_NAME "alim7101_wdt"
54
55 #define WDT_ENABLE 0x9C
56 #define WDT_DISABLE 0x8C
57
58 #define ALI_7101_WDT 0x92
59 #define ALI_WDT_ARM 0x01
60
61 /*
62 * We're going to use a 1 second timeout.
63 * If we reset the watchdog every ~250ms we should be safe. */
64
65 #define WDT_INTERVAL (HZ/4+1)
66
67 /*
68 * We must not require too good response from the userspace daemon.
69 * Here we require the userspace daemon to send us a heartbeat
70 * char to /dev/watchdog every 30 seconds.
71 */
72
73 #define WDT_HEARTBEAT (HZ * 30)
74
75 static void wdt_timer_ping(unsigned long);
76 static struct timer_list timer;
77 static unsigned long next_heartbeat;
78 static unsigned long wdt_is_open;
79 static int wdt_expect_close;
80 static struct pci_dev *alim7101_pmu;
81
82 #ifdef CONFIG_WATCHDOG_NOWAYOUT
83 static int nowayout = 1;
84 #else
85 static int nowayout = 0;
86 #endif
87
88 MODULE_PARM(nowayout,"i");
89 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
90
91 /*
92 * Whack the dog
93 */
94
wdt_timer_ping(unsigned long data)95 static void wdt_timer_ping(unsigned long data)
96 {
97 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
98 * we agree to ping the WDT
99 */
100 char tmp;
101
102 if(time_before(jiffies, next_heartbeat))
103 {
104 /* Ping the WDT (this is actually a disarm/arm sequence) */
105 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
106 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
107 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
108 } else {
109 printk(OUR_NAME ": Heartbeat lost! Will not ping the watchdog\n");
110 }
111 /* Re-set the timer interval */
112 timer.expires = jiffies + WDT_INTERVAL;
113 add_timer(&timer);
114 }
115
116 /*
117 * Utility routines
118 */
119
wdt_change(int writeval)120 static void wdt_change(int writeval)
121 {
122 char tmp;
123
124 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
125 if (writeval == WDT_ENABLE)
126 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
127 else
128 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
129 }
130
wdt_startup(void)131 static void wdt_startup(void)
132 {
133 next_heartbeat = jiffies + WDT_HEARTBEAT;
134
135 /* We must enable before we kick off the timer in case the timer
136 occurs as we ping it */
137
138 wdt_change(WDT_ENABLE);
139
140 /* Start the timer */
141 timer.expires = jiffies + WDT_INTERVAL;
142 add_timer(&timer);
143
144
145 printk(OUR_NAME ": Watchdog timer is now enabled.\n");
146 }
147
wdt_turnoff(void)148 static void wdt_turnoff(void)
149 {
150 /* Stop the timer */
151 del_timer_sync(&timer);
152 wdt_change(WDT_DISABLE);
153 printk(OUR_NAME ": Watchdog timer is now disabled...\n");
154 }
155
156 /*
157 * /dev/watchdog handling
158 */
159
fop_write(struct file * file,const char * buf,size_t count,loff_t * ppos)160 static ssize_t fop_write(struct file * file, const char * buf, size_t count, loff_t * ppos)
161 {
162 /* We can't seek */
163 if(ppos != &file->f_pos)
164 return -ESPIPE;
165
166 /* See if we got the magic character */
167 if(count)
168 {
169 if (!nowayout) {
170 size_t ofs;
171
172 /* note: just in case someone wrote the magic character
173 * five months ago... */
174 wdt_expect_close = 0;
175
176 /* now scan */
177 for(ofs = 0; ofs != count; ofs++)
178 {
179 char c;
180 if(get_user(c, buf+ofs))
181 return -EFAULT;
182 if(c == 'V')
183 wdt_expect_close = 1;
184 }
185 }
186
187 /* someone wrote to us, we should restart timer */
188 next_heartbeat = jiffies + WDT_HEARTBEAT;
189 return 1;
190 };
191 return 0;
192 }
193
fop_read(struct file * file,char * buf,size_t count,loff_t * ppos)194 static ssize_t fop_read(struct file * file, char * buf, size_t count, loff_t * ppos)
195 {
196 /* No can do */
197 return -EINVAL;
198 }
199
fop_open(struct inode * inode,struct file * file)200 static int fop_open(struct inode * inode, struct file * file)
201 {
202 /* Just in case we're already talking to someone... */
203 if(test_and_set_bit(0, &wdt_is_open))
204 return -EBUSY;
205 /* Good, fire up the show */
206 wdt_startup();
207 return 0;
208 }
209
fop_close(struct inode * inode,struct file * file)210 static int fop_close(struct inode * inode, struct file * file)
211 {
212 if ((wdt_expect_close) || (! nowayout))
213 wdt_turnoff();
214 else {
215 printk(OUR_NAME ": device file closed unexpectedly. Will not stop the WDT!\n");
216 }
217 clear_bit(0, &wdt_is_open);
218 return 0;
219 }
220
fop_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)221 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
222 {
223 static struct watchdog_info ident=
224 {
225 WDIOF_MAGICCLOSE,
226 1,
227 "ALiM7101"
228 };
229
230 switch(cmd)
231 {
232 case WDIOC_GETSUPPORT:
233 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
234 case WDIOC_KEEPALIVE:
235 next_heartbeat = jiffies + WDT_HEARTBEAT;
236 return 0;
237 default:
238 return -ENOTTY;
239 }
240 }
241
242 static struct file_operations wdt_fops = {
243 owner: THIS_MODULE,
244 llseek: no_llseek,
245 read: fop_read,
246 write: fop_write,
247 open: fop_open,
248 release: fop_close,
249 ioctl: fop_ioctl
250 };
251
252 static struct miscdevice wdt_miscdev = {
253 WATCHDOG_MINOR,
254 "watchdog",
255 &wdt_fops
256 };
257
258 /*
259 * Notifier for system down
260 */
261
wdt_notify_sys(struct notifier_block * this,unsigned long code,void * unused)262 static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
263 {
264 if (code==SYS_DOWN || code==SYS_HALT) wdt_turnoff();
265 if (code==SYS_RESTART) {
266 /*
267 * Cobalt devices have no way of rebooting themselves other than
268 * getting the watchdog to pull reset, so we restart the watchdog on
269 * reboot with no heartbeat
270 */
271 wdt_change(WDT_ENABLE);
272 printk(OUR_NAME ": Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
273 };
274 return NOTIFY_DONE;
275 }
276
277 /*
278 * The WDT needs to learn about soft shutdowns in order to
279 * turn the timebomb registers off.
280 */
281
282 static struct notifier_block wdt_notifier=
283 {
284 wdt_notify_sys,
285 0,
286 0
287 };
288
alim7101_wdt_unload(void)289 static void __exit alim7101_wdt_unload(void)
290 {
291 wdt_turnoff();
292 /* Deregister */
293 misc_deregister(&wdt_miscdev);
294 unregister_reboot_notifier(&wdt_notifier);
295 }
296
alim7101_wdt_init(void)297 static int __init alim7101_wdt_init(void)
298 {
299 int rc = -EBUSY;
300 struct pci_dev *ali1543_south;
301 char tmp;
302
303 printk(KERN_INFO OUR_NAME ": Steve Hill <steve@navaho.co.uk>.\n");
304 alim7101_pmu = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,NULL);
305 if (!alim7101_pmu) {
306 printk(KERN_INFO OUR_NAME ": ALi M7101 PMU not present - WDT not set\n");
307 return -EBUSY;
308 };
309
310 /* Set the WDT in the PMU to 1 second */
311 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
312
313 ali1543_south = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
314 if (!ali1543_south) {
315 printk(KERN_INFO OUR_NAME ": ALi 1543 South-Bridge not present - WDT not set\n");
316 return -EBUSY;
317 };
318 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
319 if ((tmp & 0x1e) != 0x12) {
320 printk(KERN_INFO OUR_NAME ": ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
321 return -EBUSY;
322 };
323
324 init_timer(&timer);
325 timer.function = wdt_timer_ping;
326 timer.data = 1;
327
328 rc = misc_register(&wdt_miscdev);
329 if (rc)
330 return rc;
331
332 rc = register_reboot_notifier(&wdt_notifier);
333 if (rc) {
334 misc_deregister(&wdt_miscdev);
335 return rc;
336 };
337
338 printk(KERN_INFO OUR_NAME ": WDT driver for ALi M7101 initialised.\n");
339 return 0;
340 }
341
342 module_init(alim7101_wdt_init);
343 module_exit(alim7101_wdt_unload);
344
345 EXPORT_NO_SYMBOLS;
346 MODULE_AUTHOR("Steve Hill");
347 MODULE_LICENSE("GPL");
348