^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Watchdog driver for SiByte SB1 SoCs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2007 OnStor, Inc. * Andrew Sharp <andy.sharp@lsi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This driver is intended to make the second of two hardware watchdogs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * on the Sibyte 12XX and 11XX SoCs available to the user. There are two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * such devices available on the SoC, but it seems that there isn't an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * enumeration class for watchdogs in Linux like there is for RTCs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * The second is used rather than the first because it uses IRQ 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * thereby avoiding all that IRQ 0 problematic nonsense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * I have not tried this driver on a 1480 processor; it might work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * just well enough to really screw things up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * It is a simple timer, and there is an interrupt that is raised the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * first time the timer expires. The second time it expires, the chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * is reset and there is no way to redirect that NMI. Which could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * be problematic in some cases where this chip is sitting on the HT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * bus and has just taken responsibility for providing a cache block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Since the reset can't be redirected to the external reset pin, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * possible that other HT connected processors might hang and not reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * For Linux, a soft reset would probably be even worse than a hard reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * There you have it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * The timer takes 23 bits of a 64 bit register (?) as a count value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * and decrements the count every microsecond, for a max value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * 0x7fffff usec or about 8.3ish seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * This watchdog borrows some user semantics from the softdog driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * in that if you close the fd, it leaves the watchdog running, unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * you previously wrote a 'V' to the fd, in which case it disables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * the watchdog when you close the fd like some other drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Based on various other watchdog drivers, which are probably all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * loosely based on something Alan Cox wrote years ago.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * modify it under the terms of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * version 1 or 2 as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <asm/sibyte/sb1250.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <asm/sibyte/sb1250_regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <asm/sibyte/sb1250_int.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <asm/sibyte/sb1250_scd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static DEFINE_SPINLOCK(sbwd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * set the initial count value of a timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * wdog is the iomem address of the cfg register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void sbwdog_set(char __iomem *wdog, unsigned long t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spin_lock(&sbwd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) __raw_writeb(0, wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) __raw_writeq(t & 0x7fffffUL, wdog - 0x10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) spin_unlock(&sbwd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * cause the timer to [re]load it's initial count and start counting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * all over again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * wdog is the iomem address of the cfg register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static void sbwdog_pet(char __iomem *wdog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spin_lock(&sbwd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) __raw_writeb(__raw_readb(wdog) | 1, wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) spin_unlock(&sbwd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static unsigned long sbwdog_gate; /* keeps it to one thread only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static int expect_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static const struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .identity = "SiByte Watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Allow only a single thread to walk the dog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static int sbwdog_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (test_and_set_bit(0, &sbwdog_gate))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Activate the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sbwdog_set(user_dog, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) __raw_writeb(1, user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Put the dog back in the kennel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int sbwdog_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (expect_close == 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) __raw_writeb(0, user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_crit("%s: Unexpected close, not stopping watchdog!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ident.identity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sbwdog_pet(user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) clear_bit(0, &sbwdog_gate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * 42 - the answer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static ssize_t sbwdog_write(struct file *file, const char __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * restart the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) for (i = 0; i != len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (get_user(c, data + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) expect_close = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) sbwdog_pet(user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static long sbwdog_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned long time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) sbwdog_pet(user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = get_user(time, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) time *= 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (time > 0x7fffffUL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) timeout = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) sbwdog_set(user_dog, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) sbwdog_pet(user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * get the remaining count from the ... count register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * which is 1*8 before the config register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = put_user((u32)__raw_readq(user_dog - 8) / 1000000, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * Notifier for system down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) void *erf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (code == SYS_DOWN || code == SYS_HALT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * sit and sit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) __raw_writeb(0, user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) __raw_writeb(0, kern_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static const struct file_operations sbwdog_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) .write = sbwdog_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) .unlocked_ioctl = sbwdog_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) .open = sbwdog_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) .release = sbwdog_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static struct miscdevice sbwdog_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) .minor = WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .fops = &sbwdog_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static struct notifier_block sbwdog_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .notifier_call = sbwdog_notify_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * interrupt handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * doesn't do a whole lot for user, but oh so cleverly written so kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * code can use it to re-up the watchdog, thereby saving the kernel from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * having to create and maintain a timer, just to tickle another timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * which is just so wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) irqreturn_t sbwdog_interrupt(int irq, void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) unsigned long wd_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) char *wd_cfg_reg = (char *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) u8 cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) cfg = __raw_readb(wd_cfg_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * if it's the second watchdog timer, it's for those users
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (wd_cfg_reg == user_dog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) pr_crit("%s in danger of initiating system reset "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) "in %ld.%01ld seconds\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ident.identity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) wd_init / 1000000, (wd_init / 100000) % 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) cfg |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) __raw_writeb(cfg, wd_cfg_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static int __init sbwdog_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * register a reboot notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ret = register_reboot_notifier(&sbwdog_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) pr_err("%s: cannot register reboot notifier (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ident.identity, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * get the resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ident.identity, (void *)user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) pr_err("%s: failed to request irq 1 - %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ident.identity, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = misc_register(&sbwdog_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) pr_info("%s: timeout is %ld.%ld secs\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ident.identity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) timeout / 1000000, (timeout / 100000) % 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) free_irq(1, (void *)user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unregister_reboot_notifier(&sbwdog_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static void __exit sbwdog_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) misc_deregister(&sbwdog_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) free_irq(1, (void *)user_dog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) unregister_reboot_notifier(&sbwdog_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) module_init(sbwdog_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) module_exit(sbwdog_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) MODULE_DESCRIPTION("SiByte Watchdog");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) module_param(timeout, ulong, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) MODULE_PARM_DESC(timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * example code that can be put in a platform code area to utilize the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * first watchdog timer for the kernels own purpose.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) void platform_wd_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) pr_crit("Watchdog IRQ zero(0) failed to be requested - %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */