2 * Intel_SCU 0.2: An Intel SCU IOH Based Watchdog Device
6 * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General
10 * Public License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 * The full GNU General Public License is included in this
21 * distribution in the file called COPYING.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/compiler.h>
28 #include <linux/kernel.h>
29 #include <linux/moduleparam.h>
30 #include <linux/types.h>
31 #include <linux/miscdevice.h>
32 #include <linux/watchdog.h>
34 #include <linux/notifier.h>
35 #include <linux/reboot.h>
36 #include <linux/init.h>
37 #include <linux/jiffies.h>
38 #include <linux/uaccess.h>
39 #include <linux/slab.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/signal.h>
45 #include <linux/sfi.h>
47 #include <linux/atomic.h>
48 #include <asm/intel_scu_ipc.h>
49 #include <asm/apb_timer.h>
50 #include <asm/intel-mid.h>
52 #include "intel_scu_watchdog.h"
54 /* Bounds number of times we will retry loading time count */
55 /* This retry is a work around for a silicon bug. */
58 #define IPC_SET_WATCHDOG_TIMER 0xF8
60 static int timer_margin = DEFAULT_SOFT_TO_HARD_MARGIN;
61 module_param(timer_margin, int, 0);
62 MODULE_PARM_DESC(timer_margin,
63 "Watchdog timer margin"
64 "Time between interrupt and resetting the system"
65 "The range is from 1 to 160"
66 "This is the time for all keep alives to arrive");
68 static int timer_set = DEFAULT_TIME;
69 module_param(timer_set, int, 0);
70 MODULE_PARM_DESC(timer_set,
71 "Default Watchdog timer setting"
73 "The range is from 1 to 170"
74 "This is the time for all keep alives to arrive");
76 /* After watchdog device is closed, check force_boot. If:
77 * force_boot == 0, then force boot on next watchdog interrupt after close,
78 * force_boot == 1, then force boot immediately when device is closed.
80 static int force_boot;
81 module_param(force_boot, int, 0);
82 MODULE_PARM_DESC(force_boot,
83 "A value of 1 means that the driver will reboot"
84 "the system immediately if the /dev/watchdog device is closed"
85 "A value of 0 means that when /dev/watchdog device is closed"
86 "the watchdog timer will be refreshed for one more interval"
87 "of length: timer_set. At the end of this interval, the"
88 "watchdog timer will reset the system."
91 /* there is only one device in the system now; this can be made into
92 * an array in the future if we have more than one device */
94 static struct intel_scu_watchdog_dev watchdog_device;
96 /* Forces restart, if force_reboot is set */
97 static void watchdog_fire(void)
100 pr_crit("Initiating system reboot\n");
102 pr_crit("Reboot didn't ?????\n");
106 pr_crit("Immediate Reboot Disabled\n");
107 pr_crit("System will reset when watchdog timer times out!\n");
111 static int check_timer_margin(int new_margin)
113 if ((new_margin < MIN_TIME_CYCLE) ||
114 (new_margin > MAX_TIME - timer_set)) {
115 pr_debug("value of new_margin %d is out of the range %d to %d\n",
116 new_margin, MIN_TIME_CYCLE, MAX_TIME - timer_set);
125 static int watchdog_set_ipc(int soft_threshold, int threshold)
128 u8 cbuf[16] = { '\0' };
131 ipc_wbuf = (u32 *)&cbuf;
132 ipc_wbuf[0] = soft_threshold;
133 ipc_wbuf[1] = threshold;
135 ipc_ret = intel_scu_ipc_command(
136 IPC_SET_WATCHDOG_TIMER,
144 pr_err("Error setting SCU watchdog timer: %x\n", ipc_ret);
150 * Intel_SCU operations
153 /* timer interrupt handler */
154 static irqreturn_t watchdog_timer_interrupt(int irq, void *dev_id)
157 int_status = ioread32(watchdog_device.timer_interrupt_status_addr);
159 pr_debug("irq, int_status: %x\n", int_status);
164 /* has the timer been started? If not, then this is spurious */
165 if (watchdog_device.timer_started == 0) {
166 pr_debug("spurious interrupt received\n");
170 /* temporarily disable the timer */
171 iowrite32(0x00000002, watchdog_device.timer_control_addr);
173 /* set the timer to the threshold */
174 iowrite32(watchdog_device.threshold,
175 watchdog_device.timer_load_count_addr);
177 /* allow the timer to run */
178 iowrite32(0x00000003, watchdog_device.timer_control_addr);
183 static int intel_scu_keepalive(void)
186 /* read eoi register - clears interrupt */
187 ioread32(watchdog_device.timer_clear_interrupt_addr);
189 /* temporarily disable the timer */
190 iowrite32(0x00000002, watchdog_device.timer_control_addr);
192 /* set the timer to the soft_threshold */
193 iowrite32(watchdog_device.soft_threshold,
194 watchdog_device.timer_load_count_addr);
196 /* allow the timer to run */
197 iowrite32(0x00000003, watchdog_device.timer_control_addr);
202 static int intel_scu_stop(void)
204 iowrite32(0, watchdog_device.timer_control_addr);
208 static int intel_scu_set_heartbeat(u32 t)
215 watchdog_device.timer_set = t;
216 watchdog_device.threshold =
217 timer_margin * watchdog_device.timer_tbl_ptr->freq_hz;
218 watchdog_device.soft_threshold =
219 (watchdog_device.timer_set - timer_margin)
220 * watchdog_device.timer_tbl_ptr->freq_hz;
222 pr_debug("set_heartbeat: timer freq is %d\n",
223 watchdog_device.timer_tbl_ptr->freq_hz);
224 pr_debug("set_heartbeat: timer_set is %x (hex)\n",
225 watchdog_device.timer_set);
226 pr_debug("set_heartbeat: timer_margin is %x (hex)\n", timer_margin);
227 pr_debug("set_heartbeat: threshold is %x (hex)\n",
228 watchdog_device.threshold);
229 pr_debug("set_heartbeat: soft_threshold is %x (hex)\n",
230 watchdog_device.soft_threshold);
232 /* Adjust thresholds by FREQ_ADJUSTMENT factor, to make the */
233 /* watchdog timing come out right. */
234 watchdog_device.threshold =
235 watchdog_device.threshold / FREQ_ADJUSTMENT;
236 watchdog_device.soft_threshold =
237 watchdog_device.soft_threshold / FREQ_ADJUSTMENT;
239 /* temporarily disable the timer */
240 iowrite32(0x00000002, watchdog_device.timer_control_addr);
242 /* send the threshold and soft_threshold via IPC to the processor */
243 ipc_ret = watchdog_set_ipc(watchdog_device.soft_threshold,
244 watchdog_device.threshold);
247 /* Make sure the watchdog timer is stopped */
252 /* Soft Threshold set loop. Early versions of silicon did */
253 /* not always set this count correctly. This loop checks */
254 /* the value and retries if it was not set correctly. */
257 soft_value = watchdog_device.soft_threshold & 0xFFFF0000;
260 /* Make sure timer is stopped */
263 if (MAX_RETRY < retry_count++) {
264 /* Unable to set timer value */
265 pr_err("Unable to set timer\n");
269 /* set the timer to the soft threshold */
270 iowrite32(watchdog_device.soft_threshold,
271 watchdog_device.timer_load_count_addr);
273 /* read count value before starting timer */
274 ioread32(watchdog_device.timer_load_count_addr);
276 /* Start the timer */
277 iowrite32(0x00000003, watchdog_device.timer_control_addr);
279 /* read the value the time loaded into its count reg */
280 hw_value = ioread32(watchdog_device.timer_load_count_addr);
281 hw_value = hw_value & 0xFFFF0000;
284 } while (soft_value != hw_value);
286 watchdog_device.timer_started = 1;
292 * /dev/watchdog handling
295 static int intel_scu_open(struct inode *inode, struct file *file)
298 /* Set flag to indicate that watchdog device is open */
299 if (test_and_set_bit(0, &watchdog_device.driver_open))
302 /* Check for reopen of driver. Reopens are not allowed */
303 if (watchdog_device.driver_closed)
306 return stream_open(inode, file);
309 static int intel_scu_release(struct inode *inode, struct file *file)
312 * This watchdog should not be closed, after the timer
313 * is started with the WDIPC_SETTIMEOUT ioctl
314 * If force_boot is set watchdog_fire() will cause an
315 * immediate reset. If force_boot is not set, the watchdog
316 * timer is refreshed for one more interval. At the end
317 * of that interval, the watchdog timer will reset the system.
320 if (!test_and_clear_bit(0, &watchdog_device.driver_open)) {
321 pr_debug("intel_scu_release, without open\n");
325 if (!watchdog_device.timer_started) {
326 /* Just close, since timer has not been started */
327 pr_debug("closed, without starting timer\n");
331 pr_crit("Unexpected close of /dev/watchdog!\n");
333 /* Since the timer was started, prevent future reopens */
334 watchdog_device.driver_closed = 1;
336 /* Refresh the timer for one more interval */
337 intel_scu_keepalive();
339 /* Reboot system (if force_boot is set) */
342 /* We should only reach this point if force_boot is not set */
346 static ssize_t intel_scu_write(struct file *file,
352 if (watchdog_device.timer_started)
353 /* Watchdog already started, keep it alive */
354 intel_scu_keepalive();
356 /* Start watchdog with timer value set by init */
357 intel_scu_set_heartbeat(watchdog_device.timer_set);
362 static long intel_scu_ioctl(struct file *file,
366 void __user *argp = (void __user *)arg;
367 u32 __user *p = argp;
371 static const struct watchdog_info ident = {
372 .options = WDIOF_SETTIMEOUT
373 | WDIOF_KEEPALIVEPING,
374 .firmware_version = 0, /* @todo Get from SCU via
375 ipc_get_scu_fw_version()? */
376 .identity = "Intel_SCU IOH Watchdog" /* len < 32 */
380 case WDIOC_GETSUPPORT:
381 return copy_to_user(argp,
383 sizeof(ident)) ? -EFAULT : 0;
384 case WDIOC_GETSTATUS:
385 case WDIOC_GETBOOTSTATUS:
386 return put_user(0, p);
387 case WDIOC_KEEPALIVE:
388 intel_scu_keepalive();
391 case WDIOC_SETTIMEOUT:
392 if (get_user(new_margin, p))
395 if (check_timer_margin(new_margin))
398 if (intel_scu_set_heartbeat(new_margin))
401 case WDIOC_GETTIMEOUT:
402 return put_user(watchdog_device.soft_threshold, p);
410 * Notifier for system down
412 static int intel_scu_notify_sys(struct notifier_block *this,
414 void *another_unused)
416 if (code == SYS_DOWN || code == SYS_HALT)
417 /* Turn off the watchdog timer. */
425 static const struct file_operations intel_scu_fops = {
426 .owner = THIS_MODULE,
428 .write = intel_scu_write,
429 .unlocked_ioctl = intel_scu_ioctl,
430 .open = intel_scu_open,
431 .release = intel_scu_release,
434 static int __init intel_scu_watchdog_init(void)
437 u32 __iomem *tmp_addr;
440 * We don't really need to check this as the SFI timer get will fail
441 * but if we do so we can exit with a clearer reason and no noise.
443 * If it isn't an intel MID device then it doesn't have this watchdog
445 if (!intel_mid_identify_cpu())
448 /* Check boot parameters to verify that their initial values */
450 /* Check value of timer_set boot parameter */
451 if ((timer_set < MIN_TIME_CYCLE) ||
452 (timer_set > MAX_TIME - MIN_TIME_CYCLE)) {
453 pr_err("value of timer_set %x (hex) is out of range from %x to %x (hex)\n",
454 timer_set, MIN_TIME_CYCLE, MAX_TIME - MIN_TIME_CYCLE);
458 /* Check value of timer_margin boot parameter */
459 if (check_timer_margin(timer_margin))
462 watchdog_device.timer_tbl_ptr = sfi_get_mtmr(sfi_mtimer_num-1);
464 if (watchdog_device.timer_tbl_ptr == NULL) {
465 pr_debug("timer is not available\n");
468 /* make sure the timer exists */
469 if (watchdog_device.timer_tbl_ptr->phys_addr == 0) {
470 pr_debug("timer %d does not have valid physical memory\n",
475 if (watchdog_device.timer_tbl_ptr->irq == 0) {
476 pr_debug("timer %d invalid irq\n", sfi_mtimer_num);
480 tmp_addr = ioremap_nocache(watchdog_device.timer_tbl_ptr->phys_addr,
483 if (tmp_addr == NULL) {
484 pr_debug("timer unable to ioremap\n");
488 watchdog_device.timer_load_count_addr = tmp_addr++;
489 watchdog_device.timer_current_value_addr = tmp_addr++;
490 watchdog_device.timer_control_addr = tmp_addr++;
491 watchdog_device.timer_clear_interrupt_addr = tmp_addr++;
492 watchdog_device.timer_interrupt_status_addr = tmp_addr++;
494 /* Set the default time values in device structure */
496 watchdog_device.timer_set = timer_set;
497 watchdog_device.threshold =
498 timer_margin * watchdog_device.timer_tbl_ptr->freq_hz;
499 watchdog_device.soft_threshold =
500 (watchdog_device.timer_set - timer_margin)
501 * watchdog_device.timer_tbl_ptr->freq_hz;
504 watchdog_device.intel_scu_notifier.notifier_call =
505 intel_scu_notify_sys;
507 ret = register_reboot_notifier(&watchdog_device.intel_scu_notifier);
509 pr_err("cannot register notifier %d)\n", ret);
510 goto register_reboot_error;
513 watchdog_device.miscdev.minor = WATCHDOG_MINOR;
514 watchdog_device.miscdev.name = "watchdog";
515 watchdog_device.miscdev.fops = &intel_scu_fops;
517 ret = misc_register(&watchdog_device.miscdev);
519 pr_err("cannot register miscdev %d err =%d\n",
520 WATCHDOG_MINOR, ret);
521 goto misc_register_error;
524 ret = request_irq((unsigned int)watchdog_device.timer_tbl_ptr->irq,
525 watchdog_timer_interrupt,
526 IRQF_SHARED, "watchdog",
527 &watchdog_device.timer_load_count_addr);
529 pr_err("error requesting irq %d\n", ret);
530 goto request_irq_error;
532 /* Make sure timer is disabled before returning */
539 misc_deregister(&watchdog_device.miscdev);
541 unregister_reboot_notifier(&watchdog_device.intel_scu_notifier);
542 register_reboot_error:
544 iounmap(watchdog_device.timer_load_count_addr);
547 late_initcall(intel_scu_watchdog_init);