Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 1 | /* |
| 2 | * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs |
| 3 | * |
| 4 | * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/miscdevice.h> |
| 17 | #include <linux/watchdog.h> |
| 18 | #include <linux/fs.h> |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <asm/txx9tmr.h> |
| 26 | |
| 27 | #define TIMER_MARGIN 60 /* Default is 60 seconds */ |
| 28 | |
| 29 | static int timeout = TIMER_MARGIN; /* in seconds */ |
| 30 | module_param(timeout, int, 0); |
| 31 | MODULE_PARM_DESC(timeout, |
| 32 | "Watchdog timeout in seconds. " |
| 33 | "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), " |
| 34 | "default=" __MODULE_STRING(TIMER_MARGIN) ")"); |
| 35 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame^] | 36 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 37 | module_param(nowayout, bool, 0); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 38 | MODULE_PARM_DESC(nowayout, |
| 39 | "Watchdog cannot be stopped once started " |
| 40 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 41 | |
| 42 | #define WD_TIMER_CCD 7 /* 1/256 */ |
| 43 | #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD)) |
| 44 | #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK) |
| 45 | |
| 46 | static unsigned long txx9wdt_alive; |
| 47 | static int expect_close; |
| 48 | static struct txx9_tmr_reg __iomem *txx9wdt_reg; |
| 49 | static struct clk *txx9_imclk; |
Adrian Bunk | f8494e0 | 2008-08-08 18:18:46 +0300 | [diff] [blame] | 50 | static DEFINE_SPINLOCK(txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 51 | |
| 52 | static void txx9wdt_ping(void) |
| 53 | { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 54 | spin_lock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 55 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 56 | spin_unlock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void txx9wdt_start(void) |
| 60 | { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 61 | spin_lock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 62 | __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra); |
| 63 | __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr); |
| 64 | __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */ |
| 65 | __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG, |
| 66 | &txx9wdt_reg->tcr); |
| 67 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 68 | spin_unlock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void txx9wdt_stop(void) |
| 72 | { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 73 | spin_lock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 74 | __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr); |
| 75 | __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE, |
| 76 | &txx9wdt_reg->tcr); |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 77 | spin_unlock(&txx9_lock); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int txx9wdt_open(struct inode *inode, struct file *file) |
| 81 | { |
| 82 | if (test_and_set_bit(0, &txx9wdt_alive)) |
| 83 | return -EBUSY; |
| 84 | |
| 85 | if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) { |
| 86 | clear_bit(0, &txx9wdt_alive); |
| 87 | return -EBUSY; |
| 88 | } |
| 89 | |
| 90 | if (nowayout) |
| 91 | __module_get(THIS_MODULE); |
| 92 | |
| 93 | txx9wdt_start(); |
| 94 | return nonseekable_open(inode, file); |
| 95 | } |
| 96 | |
| 97 | static int txx9wdt_release(struct inode *inode, struct file *file) |
| 98 | { |
| 99 | if (expect_close) |
| 100 | txx9wdt_stop(); |
| 101 | else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 102 | pr_crit("Unexpected close, not stopping watchdog!\n"); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 103 | txx9wdt_ping(); |
| 104 | } |
| 105 | clear_bit(0, &txx9wdt_alive); |
| 106 | expect_close = 0; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static ssize_t txx9wdt_write(struct file *file, const char __user *data, |
| 111 | size_t len, loff_t *ppos) |
| 112 | { |
| 113 | if (len) { |
| 114 | if (!nowayout) { |
| 115 | size_t i; |
| 116 | |
| 117 | expect_close = 0; |
| 118 | for (i = 0; i != len; i++) { |
| 119 | char c; |
| 120 | if (get_user(c, data + i)) |
| 121 | return -EFAULT; |
| 122 | if (c == 'V') |
| 123 | expect_close = 1; |
| 124 | } |
| 125 | } |
| 126 | txx9wdt_ping(); |
| 127 | } |
| 128 | return len; |
| 129 | } |
| 130 | |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 131 | static long txx9wdt_ioctl(struct file *file, unsigned int cmd, |
| 132 | unsigned long arg) |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 133 | { |
| 134 | void __user *argp = (void __user *)arg; |
| 135 | int __user *p = argp; |
| 136 | int new_timeout; |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 137 | static const struct watchdog_info ident = { |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 138 | .options = WDIOF_SETTIMEOUT | |
| 139 | WDIOF_KEEPALIVEPING | |
| 140 | WDIOF_MAGICCLOSE, |
| 141 | .firmware_version = 0, |
| 142 | .identity = "Hardware Watchdog for TXx9", |
| 143 | }; |
| 144 | |
| 145 | switch (cmd) { |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 146 | case WDIOC_GETSUPPORT: |
| 147 | return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; |
| 148 | case WDIOC_GETSTATUS: |
| 149 | case WDIOC_GETBOOTSTATUS: |
| 150 | return put_user(0, p); |
| 151 | case WDIOC_KEEPALIVE: |
| 152 | txx9wdt_ping(); |
| 153 | return 0; |
| 154 | case WDIOC_SETTIMEOUT: |
| 155 | if (get_user(new_timeout, p)) |
| 156 | return -EFAULT; |
| 157 | if (new_timeout < 1 || new_timeout > WD_MAX_TIMEOUT) |
| 158 | return -EINVAL; |
| 159 | timeout = new_timeout; |
| 160 | txx9wdt_stop(); |
| 161 | txx9wdt_start(); |
| 162 | /* Fall */ |
| 163 | case WDIOC_GETTIMEOUT: |
| 164 | return put_user(timeout, p); |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 165 | default: |
| 166 | return -ENOTTY; |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 170 | static const struct file_operations txx9wdt_fops = { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 171 | .owner = THIS_MODULE, |
| 172 | .llseek = no_llseek, |
| 173 | .write = txx9wdt_write, |
| 174 | .unlocked_ioctl = txx9wdt_ioctl, |
| 175 | .open = txx9wdt_open, |
| 176 | .release = txx9wdt_release, |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static struct miscdevice txx9wdt_miscdev = { |
Alan Cox | 8dc244f | 2008-05-19 14:09:12 +0100 | [diff] [blame] | 180 | .minor = WATCHDOG_MINOR, |
| 181 | .name = "watchdog", |
| 182 | .fops = &txx9wdt_fops, |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 183 | }; |
| 184 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 185 | static int __init txx9wdt_probe(struct platform_device *dev) |
| 186 | { |
| 187 | struct resource *res; |
| 188 | int ret; |
| 189 | |
| 190 | txx9_imclk = clk_get(NULL, "imbus_clk"); |
| 191 | if (IS_ERR(txx9_imclk)) { |
| 192 | ret = PTR_ERR(txx9_imclk); |
| 193 | txx9_imclk = NULL; |
| 194 | goto exit; |
| 195 | } |
| 196 | ret = clk_enable(txx9_imclk); |
| 197 | if (ret) { |
| 198 | clk_put(txx9_imclk); |
| 199 | txx9_imclk = NULL; |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 204 | if (!res) |
| 205 | goto exit_busy; |
H Hartley Sweeten | b782a56 | 2009-12-04 12:24:04 -0500 | [diff] [blame] | 206 | if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res), |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 207 | "txx9wdt")) |
| 208 | goto exit_busy; |
H Hartley Sweeten | b782a56 | 2009-12-04 12:24:04 -0500 | [diff] [blame] | 209 | txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res)); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 210 | if (!txx9wdt_reg) |
| 211 | goto exit_busy; |
| 212 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 213 | ret = misc_register(&txx9wdt_miscdev); |
| 214 | if (ret) { |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 215 | goto exit; |
| 216 | } |
| 217 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 218 | pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n", |
| 219 | timeout, WD_MAX_TIMEOUT, nowayout); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | exit_busy: |
| 223 | ret = -EBUSY; |
| 224 | exit: |
| 225 | if (txx9_imclk) { |
| 226 | clk_disable(txx9_imclk); |
| 227 | clk_put(txx9_imclk); |
| 228 | } |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | static int __exit txx9wdt_remove(struct platform_device *dev) |
| 233 | { |
| 234 | misc_deregister(&txx9wdt_miscdev); |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 235 | clk_disable(txx9_imclk); |
| 236 | clk_put(txx9_imclk); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Wim Van Sebroeck | 168b525 | 2009-12-26 19:13:00 +0000 | [diff] [blame] | 240 | static void txx9wdt_shutdown(struct platform_device *dev) |
| 241 | { |
| 242 | txx9wdt_stop(); |
| 243 | } |
| 244 | |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 245 | static struct platform_driver txx9wdt_driver = { |
| 246 | .remove = __exit_p(txx9wdt_remove), |
Wim Van Sebroeck | 168b525 | 2009-12-26 19:13:00 +0000 | [diff] [blame] | 247 | .shutdown = txx9wdt_shutdown, |
Atsushi Nemoto | 6f702fc | 2007-11-12 01:32:17 +0900 | [diff] [blame] | 248 | .driver = { |
| 249 | .name = "txx9wdt", |
| 250 | .owner = THIS_MODULE, |
| 251 | }, |
| 252 | }; |
| 253 | |
| 254 | static int __init watchdog_init(void) |
| 255 | { |
| 256 | return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe); |
| 257 | } |
| 258 | |
| 259 | static void __exit watchdog_exit(void) |
| 260 | { |
| 261 | platform_driver_unregister(&txx9wdt_driver); |
| 262 | } |
| 263 | |
| 264 | module_init(watchdog_init); |
| 265 | module_exit(watchdog_exit); |
| 266 | |
| 267 | MODULE_DESCRIPTION("TXx9 Watchdog Driver"); |
| 268 | MODULE_LICENSE("GPL"); |
| 269 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
Kay Sievers | f37d193 | 2008-04-10 21:29:23 -0700 | [diff] [blame] | 270 | MODULE_ALIAS("platform:txx9wdt"); |