| 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 | * |
| 16 | * Copyright (C) 2008 John Crispin <blogic@openwrt.org> |
| 17 | * Based on EP93xx wdt driver |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/watchdog.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | |
| 28 | #include <ifxmips.h> |
| 29 | #include <ifxmips_cgu.h> |
| 30 | |
| 31 | #define IFXMIPS_WDT_PW1 0x00BE0000 |
| 32 | #define IFXMIPS_WDT_PW2 0x00DC0000 |
| 33 | |
| 34 | #ifndef CONFIG_WATCHDOG_NOWAYOUT |
| 35 | static int wdt_ok_to_close; |
| 36 | #endif |
| 37 | |
| 38 | static int wdt_timeout = 30; |
| 39 | |
| 40 | int ifxmips_wdt_enable(unsigned int timeout) |
| 41 | { |
| 42 | u32 fpi; |
| 43 | fpi = cgu_get_io_region_clock(); |
| 44 | ifxmips_w32(IFXMIPS_WDT_PW1, IFXMIPS_BIU_WDT_CR); |
| 45 | ifxmips_w32(IFXMIPS_WDT_PW2 | |
| 46 | (0x3 << 26) | /* PWL */ |
| 47 | (0x3 << 24) | /* CLKDIV */ |
| 48 | (0x1 << 31) | /* enable */ |
| 49 | ((timeout * (fpi / 0x40000)) + 0x1000), /* reload */ |
| 50 | IFXMIPS_BIU_WDT_CR); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | void ifxmips_wdt_disable(void) |
| 55 | { |
| 56 | #ifndef CONFIG_WATCHDOG_NOWAYOUT |
| 57 | wdt_ok_to_close = 0; |
| 58 | #endif |
| 59 | ifxmips_w32(IFXMIPS_WDT_PW1, IFXMIPS_BIU_WDT_CR); |
| 60 | ifxmips_w32(IFXMIPS_WDT_PW2, IFXMIPS_BIU_WDT_CR); |
| 61 | } |
| 62 | |
| 63 | static ssize_t ifxmips_wdt_write(struct file *file, const char __user *data, |
| 64 | size_t len, loff_t *ppos) |
| 65 | { |
| 66 | size_t i; |
| 67 | |
| 68 | if (!len) |
| 69 | return 0; |
| 70 | |
| 71 | #ifndef CONFIG_WATCHDOG_NOWAYOUT |
| 72 | for (i = 0; i != len; i++) { |
| 73 | char c; |
| 74 | if (get_user(c, data + i)) |
| 75 | return -EFAULT; |
| 76 | if (c == 'V') |
| 77 | wdt_ok_to_close = 1; |
| 78 | } |
| 79 | #endif |
| 80 | ifxmips_wdt_enable(wdt_timeout); |
| 81 | return len; |
| 82 | } |
| 83 | |
| 84 | static struct watchdog_info ident = { |
| 85 | .options = WDIOF_MAGICCLOSE, |
| 86 | .identity = "ifxmips Watchdog", |
| 87 | }; |
| 88 | |
| 89 | static int ifxmips_wdt_ioctl(struct inode *inode, struct file *file, |
| 90 | unsigned int cmd, unsigned long arg) |
| 91 | { |
| 92 | int ret = -ENOTTY; |
| 93 | |
| 94 | switch (cmd) { |
| 95 | case WDIOC_GETSUPPORT: |
| 96 | ret = copy_to_user((struct watchdog_info __user *)arg, &ident, |
| 97 | sizeof(ident)) ? -EFAULT : 0; |
| 98 | break; |
| 99 | |
| 100 | case WDIOC_GETTIMEOUT: |
| 101 | ret = put_user(wdt_timeout, (int __user *)arg); |
| 102 | break; |
| 103 | |
| 104 | case WDIOC_SETTIMEOUT: |
| 105 | ret = get_user(wdt_timeout, (int __user *)arg); |
| 106 | break; |
| 107 | |
| 108 | case WDIOC_KEEPALIVE: |
| 109 | ifxmips_wdt_enable(wdt_timeout); |
| 110 | ret = 0; |
| 111 | break; |
| 112 | } |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static int ifxmips_wdt_open(struct inode *inode, struct file *file) |
| 117 | { |
| 118 | ifxmips_wdt_enable(wdt_timeout); |
| 119 | return nonseekable_open(inode, file); |
| 120 | } |
| 121 | |
| 122 | static int ifxmips_wdt_release(struct inode *inode, struct file *file) |
| 123 | { |
| 124 | #ifndef CONFIG_WATCHDOG_NOWAYOUT |
| 125 | if (wdt_ok_to_close) |
| 126 | ifxmips_wdt_disable(); |
| 127 | else |
| 128 | #endif |
| 129 | printk(KERN_ERR "ifxmips_wdt: watchdog closed without warning," |
| 130 | " rebooting system\n"); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static const struct file_operations ifxmips_wdt_fops = { |
| 135 | .owner = THIS_MODULE, |
| 136 | .write = ifxmips_wdt_write, |
| 137 | .ioctl = ifxmips_wdt_ioctl, |
| 138 | .open = ifxmips_wdt_open, |
| 139 | .release = ifxmips_wdt_release, |
| 140 | }; |
| 141 | |
| 142 | static struct miscdevice ifxmips_wdt_miscdev = { |
| 143 | .minor = WATCHDOG_MINOR, |
| 144 | .name = "watchdog", |
| 145 | .fops = &ifxmips_wdt_fops, |
| 146 | }; |
| 147 | |
| 148 | static int ifxmips_wdt_probe(struct platform_device *dev) |
| 149 | { |
| 150 | int err; |
| 151 | err = misc_register(&ifxmips_wdt_miscdev); |
| 152 | if (err) |
| 153 | printk(KERN_INFO "ifxmips_wdt: error creating device\n"); |
| 154 | else |
| 155 | printk(KERN_INFO "ifxmips_wdt: loaded\n"); |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | static int ifxmips_wdt_remove(struct platform_device *dev) |
| 160 | { |
| 161 | ifxmips_wdt_disable(); |
| 162 | misc_deregister(&ifxmips_wdt_miscdev); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static struct platform_driver ifxmips_wdt_driver = { |
| 168 | .probe = ifxmips_wdt_probe, |
| 169 | .remove = ifxmips_wdt_remove, |
| 170 | .driver = { |
| 171 | .name = "ifxmips_wdt", |
| 172 | .owner = THIS_MODULE, |
| 173 | }, |
| 174 | }; |
| 175 | |
| 176 | static int __init init_ifxmips_wdt(void) |
| 177 | { |
| 178 | int ret = platform_driver_register(&ifxmips_wdt_driver); |
| 179 | if (ret) |
| 180 | printk(KERN_INFO "ifxmips_wdt: error registering platfom driver!"); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static void __exit exit_ifxmips_wdt(void) |
| 185 | { |
| 186 | platform_driver_unregister(&ifxmips_wdt_driver); |
| 187 | } |
| 188 | |
| 189 | module_init(init_ifxmips_wdt); |
| 190 | module_exit(exit_ifxmips_wdt); |
| 191 | |
| 192 | MODULE_AUTHOR("John Crispin <blogic@openwrt.org>"); |
| 193 | MODULE_DESCRIPTION("ifxmips Watchdog"); |
| 194 | MODULE_LICENSE("GPL"); |
| 195 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 196 | |