]> Pileus Git - ~andy/linux/blob - drivers/watchdog/w83627hf_wdt.c
watchdog: w83627hf: Enable watchdog device only if not already enabled
[~andy/linux] / drivers / watchdog / w83627hf_wdt.c
1 /*
2  *      w83627hf/thf WDT driver
3  *
4  *      (c) Copyright 2013 Guenter Roeck
5  *              converted to watchdog infrastructure
6  *
7  *      (c) Copyright 2007 Vlad Drukker <vlad@storewiz.com>
8  *              added support for W83627THF.
9  *
10  *      (c) Copyright 2003,2007 Pádraig Brady <P@draigBrady.com>
11  *
12  *      Based on advantechwdt.c which is based on wdt.c.
13  *      Original copyright messages:
14  *
15  *      (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
16  *
17  *      (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
18  *                                              All Rights Reserved.
19  *
20  *      This program is free software; you can redistribute it and/or
21  *      modify it under the terms of the GNU General Public License
22  *      as published by the Free Software Foundation; either version
23  *      2 of the License, or (at your option) any later version.
24  *
25  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
26  *      warranty for any of this software. This material is provided
27  *      "AS-IS" and at no charge.
28  *
29  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
30  */
31
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/types.h>
37 #include <linux/watchdog.h>
38 #include <linux/ioport.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/init.h>
42 #include <linux/io.h>
43
44 #define WATCHDOG_NAME "w83627hf/thf/hg/dhg WDT"
45 #define WATCHDOG_TIMEOUT 60             /* 60 sec default timeout */
46
47 /* You must set this - there is no sane way to probe for this board. */
48 static int wdt_io = 0x2E;
49 module_param(wdt_io, int, 0);
50 MODULE_PARM_DESC(wdt_io, "w83627hf/thf WDT io port (default 0x2E)");
51
52 static int timeout;                     /* in seconds */
53 module_param(timeout, int, 0);
54 MODULE_PARM_DESC(timeout,
55                 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
56                                 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
57
58 static bool nowayout = WATCHDOG_NOWAYOUT;
59 module_param(nowayout, bool, 0);
60 MODULE_PARM_DESC(nowayout,
61                 "Watchdog cannot be stopped once started (default="
62                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
63
64 /*
65  *      Kernel methods.
66  */
67
68 #define WDT_EFER (wdt_io+0)   /* Extended Function Enable Registers */
69 #define WDT_EFIR (wdt_io+0)   /* Extended Function Index Register
70                                                         (same as EFER) */
71 #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
72
73 static void w83627hf_select_wd_register(void)
74 {
75         outb_p(0x87, WDT_EFER); /* Enter extended function mode */
76         outb_p(0x87, WDT_EFER); /* Again according to manual */
77         outb_p(0x07, WDT_EFER); /* point to logical device number reg */
78         outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
79 }
80
81 static void w83627hf_unselect_wd_register(void)
82 {
83         outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
84 }
85
86 /* tyan motherboards seem to set F5 to 0x4C ?
87  * So explicitly init to appropriate value. */
88
89 static void w83627hf_init(struct watchdog_device *wdog)
90 {
91         unsigned char t;
92
93         w83627hf_select_wd_register();
94
95         outb(0x20, WDT_EFER);   /* check chip version   */
96         t = inb(WDT_EFDR);
97         if (t == 0x82) {        /* W83627THF            */
98                 outb_p(0x2b, WDT_EFER); /* select GPIO3 */
99                 t = ((inb_p(WDT_EFDR) & 0xf7) | 0x04); /* select WDT0 */
100                 outb_p(0x2b, WDT_EFER);
101                 outb_p(t, WDT_EFDR);    /* set GPIO3 to WDT0 */
102         } else if (t == 0x88 || t == 0xa0) {    /* W83627EHF / W83627DHG */
103                 outb_p(0x2d, WDT_EFER); /* select GPIO5 */
104                 t = inb_p(WDT_EFDR) & ~0x01; /* PIN77 -> WDT0# */
105                 outb_p(0x2d, WDT_EFER);
106                 outb_p(t, WDT_EFDR); /* set GPIO5 to WDT0 */
107         }
108
109         outb_p(0x30, WDT_EFER); /* select CR30 */
110         t = inb(WDT_EFDR);
111         if (!(t & 0x01))
112                 outb_p(t | 0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
113
114         outb_p(0xF6, WDT_EFER); /* Select CRF6 */
115         t = inb_p(WDT_EFDR);      /* read CRF6 */
116         if (t != 0) {
117                 pr_info("Watchdog already running. Resetting timeout to %d sec\n",
118                         wdog->timeout);
119                 outb_p(wdog->timeout, WDT_EFDR);        /* Write back to CRF6 */
120         }
121
122         outb_p(0xF5, WDT_EFER); /* Select CRF5 */
123         t = inb_p(WDT_EFDR);      /* read CRF5 */
124         t &= ~0x0C;               /* set second mode & disable keyboard
125                                     turning off watchdog */
126         t |= 0x02;                /* enable the WDTO# output low pulse
127                                     to the KBRST# pin (PIN60) */
128         outb_p(t, WDT_EFDR);    /* Write back to CRF5 */
129
130         outb_p(0xF7, WDT_EFER); /* Select CRF7 */
131         t = inb_p(WDT_EFDR);      /* read CRF7 */
132         t &= ~0xC0;               /* disable keyboard & mouse turning off
133                                     watchdog */
134         outb_p(t, WDT_EFDR);    /* Write back to CRF7 */
135
136         w83627hf_unselect_wd_register();
137 }
138
139 static int wdt_set_time(unsigned int timeout)
140 {
141         w83627hf_select_wd_register();
142
143         outb_p(0xF6, WDT_EFER);    /* Select CRF6 */
144         outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
145
146         w83627hf_unselect_wd_register();
147
148         return 0;
149 }
150
151 static int wdt_start(struct watchdog_device *wdog)
152 {
153         return wdt_set_time(wdog->timeout);
154 }
155
156 static int wdt_stop(struct watchdog_device *wdog)
157 {
158         return wdt_set_time(0);
159 }
160
161 static int wdt_set_timeout(struct watchdog_device *wdog, unsigned int timeout)
162 {
163         wdog->timeout = timeout;
164
165         return 0;
166 }
167
168 static unsigned int wdt_get_time(struct watchdog_device *wdog)
169 {
170         unsigned int timeleft;
171
172         w83627hf_select_wd_register();
173
174         outb_p(0xF6, WDT_EFER);    /* Select CRF6 */
175         timeleft = inb_p(WDT_EFDR); /* Read Timeout counter to CRF6 */
176
177         w83627hf_unselect_wd_register();
178
179         return timeleft;
180 }
181
182 /*
183  *      Notifier for system down
184  */
185 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
186         void *unused)
187 {
188         if (code == SYS_DOWN || code == SYS_HALT)
189                 wdt_set_time(0);        /* Turn the WDT off */
190
191         return NOTIFY_DONE;
192 }
193
194 /*
195  *      Kernel Interfaces
196  */
197
198 static struct watchdog_info wdt_info = {
199         .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
200         .identity = "W83627HF Watchdog",
201 };
202
203 static struct watchdog_ops wdt_ops = {
204         .owner = THIS_MODULE,
205         .start = wdt_start,
206         .stop = wdt_stop,
207         .set_timeout = wdt_set_timeout,
208         .get_timeleft = wdt_get_time,
209 };
210
211 static struct watchdog_device wdt_dev = {
212         .info = &wdt_info,
213         .ops = &wdt_ops,
214         .timeout = WATCHDOG_TIMEOUT,
215         .min_timeout = 1,
216         .max_timeout = 255,
217 };
218
219 /*
220  *      The WDT needs to learn about soft shutdowns in order to
221  *      turn the timebomb registers off.
222  */
223
224 static struct notifier_block wdt_notifier = {
225         .notifier_call = wdt_notify_sys,
226 };
227
228 static int __init wdt_init(void)
229 {
230         int ret;
231
232         pr_info("WDT driver for the Winbond(TM) W83627HF/THF/HG/DHG Super I/O chip initialising\n");
233
234         if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
235                 pr_err("I/O address 0x%04x already in use\n", wdt_io);
236                 return -EIO;
237         }
238
239         watchdog_init_timeout(&wdt_dev, timeout, NULL);
240         watchdog_set_nowayout(&wdt_dev, nowayout);
241
242         w83627hf_init(&wdt_dev);
243
244         ret = register_reboot_notifier(&wdt_notifier);
245         if (ret != 0) {
246                 pr_err("cannot register reboot notifier (err=%d)\n", ret);
247                 goto unreg_regions;
248         }
249
250         ret = watchdog_register_device(&wdt_dev);
251         if (ret)
252                 goto unreg_reboot;
253
254         pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
255                 wdt_dev.timeout, nowayout);
256
257         return ret;
258
259 unreg_reboot:
260         unregister_reboot_notifier(&wdt_notifier);
261 unreg_regions:
262         release_region(wdt_io, 1);
263         return ret;
264 }
265
266 static void __exit wdt_exit(void)
267 {
268         watchdog_unregister_device(&wdt_dev);
269         unregister_reboot_notifier(&wdt_notifier);
270         release_region(wdt_io, 1);
271 }
272
273 module_init(wdt_init);
274 module_exit(wdt_exit);
275
276 MODULE_LICENSE("GPL");
277 MODULE_AUTHOR("Pádraig  Brady <P@draigBrady.com>");
278 MODULE_DESCRIPTION("w83627hf/thf WDT driver");