Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Atheros AR71XX/AR724X/AR913X built-in hardware watchdog timer. |
| 3 | * |
| 4 | * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org> |
| 5 | * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> |
| 6 | * |
| 7 | * This driver was based on: drivers/watchdog/ixp4xx_wdt.c |
| 8 | * Author: Deepak Saxena <dsaxena@plexity.net> |
| 9 | * Copyright 2004 (c) MontaVista, Software, Inc. |
| 10 | * |
| 11 | * which again was based on sa1100 driver, |
| 12 | * Copyright (C) 2000 Oleg Drokin <green@crimea.edu> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify it |
| 15 | * under the terms of the GNU General Public License version 2 as published |
| 16 | * by the Free Software Foundation. |
| 17 | * |
| 18 | */ |
| 19 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 22 | #include <linux/bitops.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/init.h> |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 26 | #include <linux/io.h> |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/moduleparam.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/watchdog.h> |
| 34 | #include <linux/clk.h> |
| 35 | #include <linux/err.h> |
| 36 | |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 37 | #define DRIVER_NAME "ath79-wdt" |
| 38 | |
| 39 | #define WDT_TIMEOUT 15 /* seconds */ |
| 40 | |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 41 | #define WDOG_REG_CTRL 0x00 |
| 42 | #define WDOG_REG_TIMER 0x04 |
| 43 | |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 44 | #define WDOG_CTRL_LAST_RESET BIT(31) |
| 45 | #define WDOG_CTRL_ACTION_MASK 3 |
| 46 | #define WDOG_CTRL_ACTION_NONE 0 /* no action */ |
| 47 | #define WDOG_CTRL_ACTION_GPI 1 /* general purpose interrupt */ |
| 48 | #define WDOG_CTRL_ACTION_NMI 2 /* NMI */ |
| 49 | #define WDOG_CTRL_ACTION_FCR 3 /* full chip reset */ |
| 50 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 51 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 52 | module_param(nowayout, bool, 0); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 53 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started " |
| 54 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 55 | |
| 56 | static int timeout = WDT_TIMEOUT; |
| 57 | module_param(timeout, int, 0); |
| 58 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds " |
| 59 | "(default=" __MODULE_STRING(WDT_TIMEOUT) "s)"); |
| 60 | |
| 61 | static unsigned long wdt_flags; |
| 62 | |
| 63 | #define WDT_FLAGS_BUSY 0 |
| 64 | #define WDT_FLAGS_EXPECT_CLOSE 1 |
| 65 | |
| 66 | static struct clk *wdt_clk; |
| 67 | static unsigned long wdt_freq; |
| 68 | static int boot_status; |
| 69 | static int max_timeout; |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 70 | static void __iomem *wdt_base; |
| 71 | |
| 72 | static inline void ath79_wdt_wr(unsigned reg, u32 val) |
| 73 | { |
| 74 | iowrite32(val, wdt_base + reg); |
| 75 | } |
| 76 | |
| 77 | static inline u32 ath79_wdt_rr(unsigned reg) |
| 78 | { |
| 79 | return ioread32(wdt_base + reg); |
| 80 | } |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 81 | |
| 82 | static inline void ath79_wdt_keepalive(void) |
| 83 | { |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 84 | ath79_wdt_wr(WDOG_REG_TIMER, wdt_freq * timeout); |
Gabor Juhos | 86955e2 | 2011-12-23 19:25:42 +0100 | [diff] [blame] | 85 | /* flush write */ |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 86 | ath79_wdt_rr(WDOG_REG_TIMER); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static inline void ath79_wdt_enable(void) |
| 90 | { |
| 91 | ath79_wdt_keepalive(); |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 92 | ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_FCR); |
Gabor Juhos | 86955e2 | 2011-12-23 19:25:42 +0100 | [diff] [blame] | 93 | /* flush write */ |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 94 | ath79_wdt_rr(WDOG_REG_CTRL); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static inline void ath79_wdt_disable(void) |
| 98 | { |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 99 | ath79_wdt_wr(WDOG_REG_CTRL, WDOG_CTRL_ACTION_NONE); |
Gabor Juhos | 86955e2 | 2011-12-23 19:25:42 +0100 | [diff] [blame] | 100 | /* flush write */ |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 101 | ath79_wdt_rr(WDOG_REG_CTRL); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int ath79_wdt_set_timeout(int val) |
| 105 | { |
| 106 | if (val < 1 || val > max_timeout) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | timeout = val; |
| 110 | ath79_wdt_keepalive(); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int ath79_wdt_open(struct inode *inode, struct file *file) |
| 116 | { |
| 117 | if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags)) |
| 118 | return -EBUSY; |
| 119 | |
| 120 | clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags); |
| 121 | ath79_wdt_enable(); |
| 122 | |
| 123 | return nonseekable_open(inode, file); |
| 124 | } |
| 125 | |
| 126 | static int ath79_wdt_release(struct inode *inode, struct file *file) |
| 127 | { |
| 128 | if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags)) |
| 129 | ath79_wdt_disable(); |
| 130 | else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 131 | pr_crit("device closed unexpectedly, watchdog timer will not stop!\n"); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 132 | ath79_wdt_keepalive(); |
| 133 | } |
| 134 | |
| 135 | clear_bit(WDT_FLAGS_BUSY, &wdt_flags); |
| 136 | clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static ssize_t ath79_wdt_write(struct file *file, const char *data, |
| 142 | size_t len, loff_t *ppos) |
| 143 | { |
| 144 | if (len) { |
| 145 | if (!nowayout) { |
| 146 | size_t i; |
| 147 | |
| 148 | clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags); |
| 149 | |
| 150 | for (i = 0; i != len; i++) { |
| 151 | char c; |
| 152 | |
| 153 | if (get_user(c, data + i)) |
| 154 | return -EFAULT; |
| 155 | |
| 156 | if (c == 'V') |
| 157 | set_bit(WDT_FLAGS_EXPECT_CLOSE, |
| 158 | &wdt_flags); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | ath79_wdt_keepalive(); |
| 163 | } |
| 164 | |
| 165 | return len; |
| 166 | } |
| 167 | |
| 168 | static const struct watchdog_info ath79_wdt_info = { |
| 169 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | |
| 170 | WDIOF_MAGICCLOSE | WDIOF_CARDRESET, |
| 171 | .firmware_version = 0, |
| 172 | .identity = "ATH79 watchdog", |
| 173 | }; |
| 174 | |
| 175 | static long ath79_wdt_ioctl(struct file *file, unsigned int cmd, |
| 176 | unsigned long arg) |
| 177 | { |
| 178 | void __user *argp = (void __user *)arg; |
| 179 | int __user *p = argp; |
| 180 | int err; |
| 181 | int t; |
| 182 | |
| 183 | switch (cmd) { |
| 184 | case WDIOC_GETSUPPORT: |
| 185 | err = copy_to_user(argp, &ath79_wdt_info, |
| 186 | sizeof(ath79_wdt_info)) ? -EFAULT : 0; |
| 187 | break; |
| 188 | |
| 189 | case WDIOC_GETSTATUS: |
| 190 | err = put_user(0, p); |
| 191 | break; |
| 192 | |
| 193 | case WDIOC_GETBOOTSTATUS: |
| 194 | err = put_user(boot_status, p); |
| 195 | break; |
| 196 | |
| 197 | case WDIOC_KEEPALIVE: |
| 198 | ath79_wdt_keepalive(); |
| 199 | err = 0; |
| 200 | break; |
| 201 | |
| 202 | case WDIOC_SETTIMEOUT: |
| 203 | err = get_user(t, p); |
| 204 | if (err) |
| 205 | break; |
| 206 | |
| 207 | err = ath79_wdt_set_timeout(t); |
| 208 | if (err) |
| 209 | break; |
| 210 | |
| 211 | /* fallthrough */ |
| 212 | case WDIOC_GETTIMEOUT: |
| 213 | err = put_user(timeout, p); |
| 214 | break; |
| 215 | |
| 216 | default: |
| 217 | err = -ENOTTY; |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | static const struct file_operations ath79_wdt_fops = { |
| 225 | .owner = THIS_MODULE, |
| 226 | .llseek = no_llseek, |
| 227 | .write = ath79_wdt_write, |
| 228 | .unlocked_ioctl = ath79_wdt_ioctl, |
| 229 | .open = ath79_wdt_open, |
| 230 | .release = ath79_wdt_release, |
| 231 | }; |
| 232 | |
| 233 | static struct miscdevice ath79_wdt_miscdev = { |
| 234 | .minor = WATCHDOG_MINOR, |
| 235 | .name = "watchdog", |
| 236 | .fops = &ath79_wdt_fops, |
| 237 | }; |
| 238 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 239 | static int ath79_wdt_probe(struct platform_device *pdev) |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 240 | { |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 241 | struct resource *res; |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 242 | u32 ctrl; |
| 243 | int err; |
| 244 | |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 245 | if (wdt_base) |
| 246 | return -EBUSY; |
| 247 | |
| 248 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 249 | if (!res) { |
| 250 | dev_err(&pdev->dev, "no memory resource found\n"); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | |
| 254 | wdt_base = devm_request_and_ioremap(&pdev->dev, res); |
| 255 | if (!wdt_base) { |
| 256 | dev_err(&pdev->dev, "unable to remap memory region\n"); |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | |
Gabor Juhos | 5071a88 | 2012-12-27 15:38:24 +0100 | [diff] [blame] | 260 | wdt_clk = devm_clk_get(&pdev->dev, "wdt"); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 261 | if (IS_ERR(wdt_clk)) |
| 262 | return PTR_ERR(wdt_clk); |
| 263 | |
| 264 | err = clk_enable(wdt_clk); |
| 265 | if (err) |
Gabor Juhos | 5071a88 | 2012-12-27 15:38:24 +0100 | [diff] [blame] | 266 | return err; |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 267 | |
| 268 | wdt_freq = clk_get_rate(wdt_clk); |
| 269 | if (!wdt_freq) { |
| 270 | err = -EINVAL; |
| 271 | goto err_clk_disable; |
| 272 | } |
| 273 | |
| 274 | max_timeout = (0xfffffffful / wdt_freq); |
| 275 | if (timeout < 1 || timeout > max_timeout) { |
| 276 | timeout = max_timeout; |
| 277 | dev_info(&pdev->dev, |
| 278 | "timeout value must be 0 < timeout < %d, using %d\n", |
| 279 | max_timeout, timeout); |
| 280 | } |
| 281 | |
Gabor Juhos | 09f5100 | 2012-12-27 15:38:26 +0100 | [diff] [blame^] | 282 | ctrl = ath79_wdt_rr(WDOG_REG_CTRL); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 283 | boot_status = (ctrl & WDOG_CTRL_LAST_RESET) ? WDIOF_CARDRESET : 0; |
| 284 | |
| 285 | err = misc_register(&ath79_wdt_miscdev); |
| 286 | if (err) { |
| 287 | dev_err(&pdev->dev, |
| 288 | "unable to register misc device, err=%d\n", err); |
| 289 | goto err_clk_disable; |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | |
| 294 | err_clk_disable: |
| 295 | clk_disable(wdt_clk); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 296 | return err; |
| 297 | } |
| 298 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 299 | static int ath79_wdt_remove(struct platform_device *pdev) |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 300 | { |
| 301 | misc_deregister(&ath79_wdt_miscdev); |
| 302 | clk_disable(wdt_clk); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void ath97_wdt_shutdown(struct platform_device *pdev) |
| 307 | { |
| 308 | ath79_wdt_disable(); |
| 309 | } |
| 310 | |
| 311 | static struct platform_driver ath79_wdt_driver = { |
Gabor Juhos | 8157bec | 2012-08-14 15:35:19 +0200 | [diff] [blame] | 312 | .probe = ath79_wdt_probe, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 313 | .remove = ath79_wdt_remove, |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 314 | .shutdown = ath97_wdt_shutdown, |
| 315 | .driver = { |
| 316 | .name = DRIVER_NAME, |
| 317 | .owner = THIS_MODULE, |
| 318 | }, |
| 319 | }; |
| 320 | |
Gabor Juhos | 8157bec | 2012-08-14 15:35:19 +0200 | [diff] [blame] | 321 | module_platform_driver(ath79_wdt_driver); |
Gabor Juhos | f8394f61 | 2011-01-04 21:28:19 +0100 | [diff] [blame] | 322 | |
| 323 | MODULE_DESCRIPTION("Atheros AR71XX/AR724X/AR913X hardware watchdog driver"); |
| 324 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org"); |
| 325 | MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org"); |
| 326 | MODULE_LICENSE("GPL v2"); |
| 327 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 328 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |