Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* linux/drivers/char/watchdog/s3c2410_wdt.c |
| 2 | * |
| 3 | * Copyright (c) 2004 Simtec Electronics |
| 4 | * Ben Dooks <ben@simtec.co.uk> |
| 5 | * |
| 6 | * S3C2410 Watchdog Timer Support |
| 7 | * |
| 8 | * Based on, softdog.c by Alan Cox, |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 9 | * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/timer.h> |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 32 | #include <linux/miscdevice.h> /* for MODULE_ALIAS_MISCDEV */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/watchdog.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/init.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 35 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/interrupt.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 37 | #include <linux/clk.h> |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 38 | #include <linux/uaccess.h> |
| 39 | #include <linux/io.h> |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 40 | #include <linux/cpufreq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 42 | #include <linux/err.h> |
Wim Van Sebroeck | 3016a55 | 2012-05-03 05:24:17 +0000 | [diff] [blame] | 43 | #include <linux/of.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 45 | #include <mach/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Ben Dooks | b430708 | 2007-07-24 13:28:01 +0100 | [diff] [blame] | 47 | #undef S3C_VA_WATCHDOG |
| 48 | #define S3C_VA_WATCHDOG (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Ben Dooks | 180ee70 | 2008-10-30 10:14:32 +0000 | [diff] [blame] | 50 | #include <plat/regs-watchdog.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0) |
| 53 | #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15) |
| 54 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 55 | static bool nowayout = WATCHDOG_NOWAYOUT; |
Fabio Porcedda | c1fd5f6 | 2013-02-14 09:14:25 +0100 | [diff] [blame] | 56 | static int tmr_margin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT; |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 58 | static int soft_noboot; |
| 59 | static int debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | module_param(tmr_margin, int, 0); |
| 62 | module_param(tmr_atboot, int, 0); |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 63 | module_param(nowayout, bool, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | module_param(soft_noboot, int, 0); |
| 65 | module_param(debug, int, 0); |
| 66 | |
Randy Dunlap | 76550d3 | 2010-05-01 09:46:15 -0700 | [diff] [blame] | 67 | MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default=" |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 68 | __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")"); |
| 69 | MODULE_PARM_DESC(tmr_atboot, |
| 70 | "Watchdog is started at boot time if set to 1, default=" |
| 71 | __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT)); |
| 72 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" |
| 73 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 74 | MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, " |
Randy Dunlap | 76550d3 | 2010-05-01 09:46:15 -0700 | [diff] [blame] | 75 | "0 to reboot (default 0)"); |
| 76 | MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 78 | static struct device *wdt_dev; /* platform device attached to */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static struct resource *wdt_mem; |
| 80 | static struct resource *wdt_irq; |
| 81 | static struct clk *wdt_clock; |
| 82 | static void __iomem *wdt_base; |
| 83 | static unsigned int wdt_count; |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 84 | static DEFINE_SPINLOCK(wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* watchdog control routines */ |
| 87 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 88 | #define DBG(fmt, ...) \ |
| 89 | do { \ |
| 90 | if (debug) \ |
| 91 | pr_info(fmt, ##__VA_ARGS__); \ |
| 92 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | /* functions */ |
| 95 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 96 | static int s3c2410wdt_keepalive(struct watchdog_device *wdd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 98 | spin_lock(&wdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | writel(wdt_count, wdt_base + S3C2410_WTCNT); |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 100 | spin_unlock(&wdt_lock); |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 101 | |
| 102 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 105 | static void __s3c2410wdt_stop(void) |
| 106 | { |
| 107 | unsigned long wtcon; |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | wtcon = readl(wdt_base + S3C2410_WTCON); |
| 110 | wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN); |
| 111 | writel(wtcon, wdt_base + S3C2410_WTCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 114 | static int s3c2410wdt_stop(struct watchdog_device *wdd) |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 115 | { |
| 116 | spin_lock(&wdt_lock); |
| 117 | __s3c2410wdt_stop(); |
| 118 | spin_unlock(&wdt_lock); |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 119 | |
| 120 | return 0; |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 123 | static int s3c2410wdt_start(struct watchdog_device *wdd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | unsigned long wtcon; |
| 126 | |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 127 | spin_lock(&wdt_lock); |
| 128 | |
| 129 | __s3c2410wdt_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | wtcon = readl(wdt_base + S3C2410_WTCON); |
| 132 | wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128; |
| 133 | |
| 134 | if (soft_noboot) { |
| 135 | wtcon |= S3C2410_WTCON_INTEN; |
| 136 | wtcon &= ~S3C2410_WTCON_RSTEN; |
| 137 | } else { |
| 138 | wtcon &= ~S3C2410_WTCON_INTEN; |
| 139 | wtcon |= S3C2410_WTCON_RSTEN; |
| 140 | } |
| 141 | |
| 142 | DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n", |
Harvey Harrison | fa9363c | 2008-03-05 18:24:58 -0800 | [diff] [blame] | 143 | __func__, wdt_count, wtcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | writel(wdt_count, wdt_base + S3C2410_WTDAT); |
| 146 | writel(wdt_count, wdt_base + S3C2410_WTCNT); |
| 147 | writel(wtcon, wdt_base + S3C2410_WTCON); |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 148 | spin_unlock(&wdt_lock); |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 149 | |
| 150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 153 | static inline int s3c2410wdt_is_running(void) |
| 154 | { |
| 155 | return readl(wdt_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE; |
| 156 | } |
| 157 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 158 | static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 160 | unsigned long freq = clk_get_rate(wdt_clock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | unsigned int count; |
| 162 | unsigned int divisor = 1; |
| 163 | unsigned long wtcon; |
| 164 | |
| 165 | if (timeout < 1) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | freq /= 128; |
| 169 | count = timeout * freq; |
| 170 | |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 171 | DBG("%s: count=%d, timeout=%d, freq=%lu\n", |
Harvey Harrison | fa9363c | 2008-03-05 18:24:58 -0800 | [diff] [blame] | 172 | __func__, count, timeout, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* if the count is bigger than the watchdog register, |
| 175 | then work out what we need to do (and if) we can |
| 176 | actually make this value |
| 177 | */ |
| 178 | |
| 179 | if (count >= 0x10000) { |
| 180 | for (divisor = 1; divisor <= 0x100; divisor++) { |
| 181 | if ((count / divisor) < 0x10000) |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | if ((count / divisor) >= 0x10000) { |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 186 | dev_err(wdt_dev, "timeout %d too big\n", timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return -EINVAL; |
| 188 | } |
| 189 | } |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n", |
Harvey Harrison | fa9363c | 2008-03-05 18:24:58 -0800 | [diff] [blame] | 192 | __func__, timeout, divisor, count, count/divisor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | count /= divisor; |
| 195 | wdt_count = count; |
| 196 | |
| 197 | /* update the pre-scaler */ |
| 198 | wtcon = readl(wdt_base + S3C2410_WTCON); |
| 199 | wtcon &= ~S3C2410_WTCON_PRESCALE_MASK; |
| 200 | wtcon |= S3C2410_WTCON_PRESCALE(divisor-1); |
| 201 | |
| 202 | writel(count, wdt_base + S3C2410_WTDAT); |
| 203 | writel(wtcon, wdt_base + S3C2410_WTCON); |
| 204 | |
Hans de Goede | 5f2430f | 2012-05-11 12:00:27 +0200 | [diff] [blame] | 205 | wdd->timeout = (count * divisor) / freq; |
Wim Van Sebroeck | 0197c1c | 2012-02-29 20:20:58 +0100 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 210 | #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 212 | static const struct watchdog_info s3c2410_wdt_ident = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | .options = OPTIONS, |
| 214 | .firmware_version = 0, |
| 215 | .identity = "S3C2410 Watchdog", |
| 216 | }; |
| 217 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 218 | static struct watchdog_ops s3c2410wdt_ops = { |
| 219 | .owner = THIS_MODULE, |
| 220 | .start = s3c2410wdt_start, |
| 221 | .stop = s3c2410wdt_stop, |
| 222 | .ping = s3c2410wdt_keepalive, |
| 223 | .set_timeout = s3c2410wdt_set_heartbeat, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 226 | static struct watchdog_device s3c2410_wdd = { |
| 227 | .info = &s3c2410_wdt_ident, |
| 228 | .ops = &s3c2410wdt_ops, |
Fabio Porcedda | c1fd5f6 | 2013-02-14 09:14:25 +0100 | [diff] [blame] | 229 | .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | /* interrupt handler code */ |
| 233 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 234 | static irqreturn_t s3c2410wdt_irq(int irqno, void *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 236 | dev_info(wdt_dev, "watchdog timer expired (irq)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 238 | s3c2410wdt_keepalive(&s3c2410_wdd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return IRQ_HANDLED; |
| 240 | } |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 241 | |
| 242 | |
| 243 | #ifdef CONFIG_CPU_FREQ |
| 244 | |
| 245 | static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb, |
| 246 | unsigned long val, void *data) |
| 247 | { |
| 248 | int ret; |
| 249 | |
| 250 | if (!s3c2410wdt_is_running()) |
| 251 | goto done; |
| 252 | |
| 253 | if (val == CPUFREQ_PRECHANGE) { |
| 254 | /* To ensure that over the change we don't cause the |
| 255 | * watchdog to trigger, we perform an keep-alive if |
| 256 | * the watchdog is running. |
| 257 | */ |
| 258 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 259 | s3c2410wdt_keepalive(&s3c2410_wdd); |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 260 | } else if (val == CPUFREQ_POSTCHANGE) { |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 261 | s3c2410wdt_stop(&s3c2410_wdd); |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 262 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 263 | ret = s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout); |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 264 | |
| 265 | if (ret >= 0) |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 266 | s3c2410wdt_start(&s3c2410_wdd); |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 267 | else |
| 268 | goto err; |
| 269 | } |
| 270 | |
| 271 | done: |
| 272 | return 0; |
| 273 | |
| 274 | err: |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 275 | dev_err(wdt_dev, "cannot set new value for timeout %d\n", |
| 276 | s3c2410_wdd.timeout); |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static struct notifier_block s3c2410wdt_cpufreq_transition_nb = { |
| 281 | .notifier_call = s3c2410wdt_cpufreq_transition, |
| 282 | }; |
| 283 | |
| 284 | static inline int s3c2410wdt_cpufreq_register(void) |
| 285 | { |
| 286 | return cpufreq_register_notifier(&s3c2410wdt_cpufreq_transition_nb, |
| 287 | CPUFREQ_TRANSITION_NOTIFIER); |
| 288 | } |
| 289 | |
| 290 | static inline void s3c2410wdt_cpufreq_deregister(void) |
| 291 | { |
| 292 | cpufreq_unregister_notifier(&s3c2410wdt_cpufreq_transition_nb, |
| 293 | CPUFREQ_TRANSITION_NOTIFIER); |
| 294 | } |
| 295 | |
| 296 | #else |
| 297 | static inline int s3c2410wdt_cpufreq_register(void) |
| 298 | { |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static inline void s3c2410wdt_cpufreq_deregister(void) |
| 303 | { |
| 304 | } |
| 305 | #endif |
| 306 | |
Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 307 | static int s3c2410wdt_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 309 | struct device *dev; |
Ben Dooks | 46b814d | 2007-06-14 12:08:54 +0100 | [diff] [blame] | 310 | unsigned int wtcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | int started = 0; |
| 312 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Harvey Harrison | fa9363c | 2008-03-05 18:24:58 -0800 | [diff] [blame] | 314 | DBG("%s: probe=%p\n", __func__, pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 316 | dev = &pdev->dev; |
| 317 | wdt_dev = &pdev->dev; |
| 318 | |
Julia Lawall | f72401e | 2011-02-26 17:34:38 +0100 | [diff] [blame] | 319 | wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 320 | if (wdt_mem == NULL) { |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 321 | dev_err(dev, "no memory resource specified\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return -ENOENT; |
| 323 | } |
| 324 | |
MyungJoo Ham | 78d3e00 | 2012-01-13 14:14:23 +0900 | [diff] [blame] | 325 | wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 326 | if (wdt_irq == NULL) { |
| 327 | dev_err(dev, "no irq resource specified\n"); |
| 328 | ret = -ENOENT; |
| 329 | goto err; |
| 330 | } |
| 331 | |
| 332 | /* get the memory region for the watchdog timer */ |
Jingoo Han | 04ecc7d | 2013-01-10 11:06:33 +0900 | [diff] [blame] | 333 | wdt_base = devm_request_and_ioremap(dev, wdt_mem); |
Ben Dooks | b4253f8 | 2008-06-22 22:36:49 +0100 | [diff] [blame] | 334 | if (wdt_base == NULL) { |
Jingoo Han | 04ecc7d | 2013-01-10 11:06:33 +0900 | [diff] [blame] | 335 | dev_err(dev, "failed to devm_request_and_ioremap() region\n"); |
| 336 | ret = -ENOMEM; |
| 337 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | DBG("probe: mapped wdt_base=%p\n", wdt_base); |
| 341 | |
Jingoo Han | 04ecc7d | 2013-01-10 11:06:33 +0900 | [diff] [blame] | 342 | wdt_clock = devm_clk_get(dev, "watchdog"); |
Akinobu Mita | 9cd4461 | 2006-12-19 17:51:44 +0900 | [diff] [blame] | 343 | if (IS_ERR(wdt_clock)) { |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 344 | dev_err(dev, "failed to find watchdog clock source\n"); |
Akinobu Mita | 9cd4461 | 2006-12-19 17:51:44 +0900 | [diff] [blame] | 345 | ret = PTR_ERR(wdt_clock); |
Jingoo Han | 04ecc7d | 2013-01-10 11:06:33 +0900 | [diff] [blame] | 346 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Thomas Abraham | 50d854c8 | 2012-10-03 07:32:40 +0900 | [diff] [blame] | 349 | clk_prepare_enable(wdt_clock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
MyungJoo Ham | 78d3e00 | 2012-01-13 14:14:23 +0900 | [diff] [blame] | 351 | ret = s3c2410wdt_cpufreq_register(); |
| 352 | if (ret < 0) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 353 | pr_err("failed to register cpufreq\n"); |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 354 | goto err_clk; |
| 355 | } |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* see if we can actually set the requested timer margin, and if |
| 358 | * not, try the default value */ |
| 359 | |
Fabio Porcedda | c1fd5f6 | 2013-02-14 09:14:25 +0100 | [diff] [blame] | 360 | watchdog_init_timeout(&s3c2410_wdd, tmr_margin, &pdev->dev); |
| 361 | if (s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout)) { |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 362 | started = s3c2410wdt_set_heartbeat(&s3c2410_wdd, |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 363 | CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 365 | if (started == 0) |
| 366 | dev_info(dev, |
| 367 | "tmr_margin value out of range, default %d used\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME); |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 369 | else |
Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 370 | dev_info(dev, "default timer value is out of range, " |
| 371 | "cannot start\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jingoo Han | 04ecc7d | 2013-01-10 11:06:33 +0900 | [diff] [blame] | 374 | ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0, |
| 375 | pdev->name, pdev); |
MyungJoo Ham | 78d3e00 | 2012-01-13 14:14:23 +0900 | [diff] [blame] | 376 | if (ret != 0) { |
| 377 | dev_err(dev, "failed to install irq (%d)\n", ret); |
| 378 | goto err_cpufreq; |
| 379 | } |
| 380 | |
Wim Van Sebroeck | ff0b3cd | 2011-11-29 16:24:16 +0100 | [diff] [blame] | 381 | watchdog_set_nowayout(&s3c2410_wdd, nowayout); |
| 382 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 383 | ret = watchdog_register_device(&s3c2410_wdd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (ret) { |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 385 | dev_err(dev, "cannot register watchdog (%d)\n", ret); |
Jingoo Han | 04ecc7d | 2013-01-10 11:06:33 +0900 | [diff] [blame] | 386 | goto err_cpufreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (tmr_atboot && started == 0) { |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 390 | dev_info(dev, "starting watchdog timer\n"); |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 391 | s3c2410wdt_start(&s3c2410_wdd); |
Ben Dooks | 655516c | 2006-04-19 23:02:56 +0100 | [diff] [blame] | 392 | } else if (!tmr_atboot) { |
| 393 | /* if we're not enabling the watchdog, then ensure it is |
| 394 | * disabled if it has been left running from the bootloader |
| 395 | * or other source */ |
| 396 | |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 397 | s3c2410wdt_stop(&s3c2410_wdd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Ben Dooks | 46b814d | 2007-06-14 12:08:54 +0100 | [diff] [blame] | 400 | /* print out a statement of readiness */ |
| 401 | |
| 402 | wtcon = readl(wdt_base + S3C2410_WTCON); |
| 403 | |
Ben Dooks | e8ef92b | 2007-06-14 12:08:55 +0100 | [diff] [blame] | 404 | dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n", |
Ben Dooks | 46b814d | 2007-06-14 12:08:54 +0100 | [diff] [blame] | 405 | (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in", |
Dmitry Artamonow | 20403e8 | 2011-11-16 12:46:13 +0400 | [diff] [blame] | 406 | (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis", |
| 407 | (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis"); |
Alan Cox | 41dc8b7 | 2008-08-04 17:54:46 +0100 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return 0; |
Ben Dooks | 0b6dd8a | 2006-12-18 10:31:32 +0000 | [diff] [blame] | 410 | |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 411 | err_cpufreq: |
| 412 | s3c2410wdt_cpufreq_deregister(); |
| 413 | |
Ben Dooks | 0b6dd8a | 2006-12-18 10:31:32 +0000 | [diff] [blame] | 414 | err_clk: |
Thomas Abraham | 50d854c8 | 2012-10-03 07:32:40 +0900 | [diff] [blame] | 415 | clk_disable_unprepare(wdt_clock); |
MyungJoo Ham | 78d3e00 | 2012-01-13 14:14:23 +0900 | [diff] [blame] | 416 | wdt_clock = NULL; |
Ben Dooks | 0b6dd8a | 2006-12-18 10:31:32 +0000 | [diff] [blame] | 417 | |
MyungJoo Ham | 78d3e00 | 2012-01-13 14:14:23 +0900 | [diff] [blame] | 418 | err: |
| 419 | wdt_irq = NULL; |
| 420 | wdt_mem = NULL; |
Ben Dooks | 0b6dd8a | 2006-12-18 10:31:32 +0000 | [diff] [blame] | 421 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 424 | static int s3c2410wdt_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 426 | watchdog_unregister_device(&s3c2410_wdd); |
Wim Van Sebroeck | 9a37256 | 2010-05-21 08:11:42 +0000 | [diff] [blame] | 427 | |
Ben Dooks | e02f838 | 2009-10-30 00:30:25 +0000 | [diff] [blame] | 428 | s3c2410wdt_cpufreq_deregister(); |
| 429 | |
Thomas Abraham | 50d854c8 | 2012-10-03 07:32:40 +0900 | [diff] [blame] | 430 | clk_disable_unprepare(wdt_clock); |
Ben Dooks | 0b6dd8a | 2006-12-18 10:31:32 +0000 | [diff] [blame] | 431 | wdt_clock = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
MyungJoo Ham | 78d3e00 | 2012-01-13 14:14:23 +0900 | [diff] [blame] | 433 | wdt_irq = NULL; |
Wim Van Sebroeck | 9a37256 | 2010-05-21 08:11:42 +0000 | [diff] [blame] | 434 | wdt_mem = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 438 | static void s3c2410wdt_shutdown(struct platform_device *dev) |
Ben Dooks | 94f1e9f | 2005-08-17 09:04:52 +0200 | [diff] [blame] | 439 | { |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 440 | s3c2410wdt_stop(&s3c2410_wdd); |
Ben Dooks | 94f1e9f | 2005-08-17 09:04:52 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 443 | #ifdef CONFIG_PM |
| 444 | |
| 445 | static unsigned long wtcon_save; |
| 446 | static unsigned long wtdat_save; |
| 447 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 448 | static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state) |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 449 | { |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 450 | /* Save watchdog state, and turn it off. */ |
| 451 | wtcon_save = readl(wdt_base + S3C2410_WTCON); |
| 452 | wtdat_save = readl(wdt_base + S3C2410_WTDAT); |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 453 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 454 | /* Note that WTCNT doesn't need to be saved. */ |
Wolfram Sang | 25dc46e | 2011-09-26 15:40:14 +0200 | [diff] [blame] | 455 | s3c2410wdt_stop(&s3c2410_wdd); |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 460 | static int s3c2410wdt_resume(struct platform_device *dev) |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 461 | { |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 462 | /* Restore watchdog state. */ |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 463 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 464 | writel(wtdat_save, wdt_base + S3C2410_WTDAT); |
| 465 | writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */ |
| 466 | writel(wtcon_save, wdt_base + S3C2410_WTCON); |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 467 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 468 | pr_info("watchdog %sabled\n", |
| 469 | (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis"); |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | #else |
| 475 | #define s3c2410wdt_suspend NULL |
| 476 | #define s3c2410wdt_resume NULL |
| 477 | #endif /* CONFIG_PM */ |
| 478 | |
Thomas Abraham | 9487a9c | 2011-06-22 15:24:32 +0530 | [diff] [blame] | 479 | #ifdef CONFIG_OF |
| 480 | static const struct of_device_id s3c2410_wdt_match[] = { |
| 481 | { .compatible = "samsung,s3c2410-wdt" }, |
| 482 | {}, |
| 483 | }; |
| 484 | MODULE_DEVICE_TABLE(of, s3c2410_wdt_match); |
Thomas Abraham | 9487a9c | 2011-06-22 15:24:32 +0530 | [diff] [blame] | 485 | #endif |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 486 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 487 | static struct platform_driver s3c2410wdt_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | .probe = s3c2410wdt_probe, |
Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 489 | .remove = s3c2410wdt_remove, |
Ben Dooks | 94f1e9f | 2005-08-17 09:04:52 +0200 | [diff] [blame] | 490 | .shutdown = s3c2410wdt_shutdown, |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 491 | .suspend = s3c2410wdt_suspend, |
| 492 | .resume = s3c2410wdt_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 493 | .driver = { |
| 494 | .owner = THIS_MODULE, |
| 495 | .name = "s3c2410-wdt", |
Wim Van Sebroeck | 3016a55 | 2012-05-03 05:24:17 +0000 | [diff] [blame] | 496 | .of_match_table = of_match_ptr(s3c2410_wdt_match), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 497 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | }; |
| 499 | |
Sachin Kamat | 6b761b2 | 2012-07-12 17:17:40 +0530 | [diff] [blame] | 500 | module_platform_driver(s3c2410wdt_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Ben Dooks | af4bb82 | 2005-08-17 09:03:23 +0200 | [diff] [blame] | 502 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, " |
| 503 | "Dimitry Andric <dimitry.andric@tomtom.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver"); |
| 505 | MODULE_LICENSE("GPL"); |
| 506 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
Kay Sievers | f37d193 | 2008-04-10 21:29:23 -0700 | [diff] [blame] | 507 | MODULE_ALIAS("platform:s3c2410-wdt"); |