| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Paul Mundt | b1fa888 | 2010-05-24 10:55:59 +0900 | [diff] [blame] | 2 |  * drivers/watchdog/shwdt.c | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 |  * | 
 | 4 |  * Watchdog driver for integrated watchdog in the SuperH processors. | 
 | 5 |  * | 
| Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 6 |  * Copyright (C) 2001 - 2012  Paul Mundt <lethal@linux-sh.org> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * | 
 | 8 |  * This program is free software; you can redistribute it and/or modify it | 
 | 9 |  * under the terms of the GNU General Public License as published by the | 
 | 10 |  * Free Software Foundation; either version 2 of the License, or (at your | 
 | 11 |  * option) any later version. | 
 | 12 |  * | 
 | 13 |  * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> | 
 | 14 |  *     Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT | 
 | 15 |  * | 
 | 16 |  * 19-Apr-2002 Rob Radez <rob@osinvestor.com> | 
 | 17 |  *     Added expect close support, made emulated timeout runtime changeable | 
 | 18 |  *     general cleanups, add some ioctls | 
 | 19 |  */ | 
| Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 20 |  | 
 | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 
 | 22 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> | 
 | 24 | #include <linux/moduleparam.h> | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 25 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/types.h> | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 28 | #include <linux/spinlock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/miscdevice.h> | 
 | 30 | #include <linux/watchdog.h> | 
| Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 31 | #include <linux/pm_runtime.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/fs.h> | 
| Paul Mundt | f118420 | 2006-09-27 17:53:59 +0900 | [diff] [blame] | 33 | #include <linux/mm.h> | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 35 | #include <linux/io.h> | 
| Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 36 | #include <linux/clk.h> | 
| Sachin Kamat | 6330c70 | 2013-03-04 10:36:41 +0530 | [diff] [blame] | 37 | #include <linux/err.h> | 
| Adrian Bunk | 58cf419 | 2008-08-08 18:39:11 +0300 | [diff] [blame] | 38 | #include <asm/watchdog.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 40 | #define DRV_NAME "sh-wdt" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
 | 42 | /* | 
 | 43 |  * Default clock division ratio is 5.25 msecs. For an additional table of | 
 | 44 |  * values, consult the asm-sh/watchdog.h. Overload this at module load | 
 | 45 |  * time. | 
 | 46 |  * | 
 | 47 |  * In order for this to work reliably we need to have HZ set to 1000 or | 
 | 48 |  * something quite higher than 100 (or we need a proper high-res timer | 
 | 49 |  * implementation that will deal with this properly), otherwise the 10ms | 
 | 50 |  * resolution of a jiffy is enough to trigger the overflow. For things like | 
 | 51 |  * the SH-4 and SH-5, this isn't necessarily that big of a problem, though | 
 | 52 |  * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely | 
 | 53 |  * necssary. | 
 | 54 |  * | 
 | 55 |  * As a result of this timing problem, the only modes that are particularly | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 56 |  * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  * overflow periods respectively. | 
 | 58 |  * | 
 | 59 |  * Also, since we can't really expect userspace to be responsive enough | 
| Joe Perches | ee0fc09 | 2008-02-03 17:32:52 +0200 | [diff] [blame] | 60 |  * before the overflow happens, we maintain two separate timers .. One in | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 |  * the kernel for clearing out WOVF every 2ms or so (again, this depends on | 
 | 62 |  * HZ == 1000), and another for monitoring userspace writes to the WDT device. | 
 | 63 |  * | 
 | 64 |  * As such, we currently use a configurable heartbeat interval which defaults | 
 | 65 |  * to 30s. In this case, the userspace daemon is only responsible for periodic | 
 | 66 |  * writes to the device before the next heartbeat is scheduled. If the daemon | 
 | 67 |  * misses its deadline, the kernel timer will allow the WDT to overflow. | 
 | 68 |  */ | 
 | 69 | static int clock_division_ratio = WTCSR_CKS_4096; | 
| David Engraf | bea1906 | 2011-07-20 15:03:39 +0200 | [diff] [blame] | 70 | #define next_ping_period(cks)	(jiffies + msecs_to_jiffies(cks - 4)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #define WATCHDOG_HEARTBEAT 30			/* 30 sec default heartbeat */ | 
 | 73 | static int heartbeat = WATCHDOG_HEARTBEAT;	/* in seconds */ | 
| Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 74 | static bool nowayout = WATCHDOG_NOWAYOUT; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 75 | static unsigned long next_heartbeat; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 77 | struct sh_wdt { | 
 | 78 | 	void __iomem		*base; | 
 | 79 | 	struct device		*dev; | 
| Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 80 | 	struct clk		*clk; | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 81 | 	spinlock_t		lock; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 82 |  | 
 | 83 | 	struct timer_list	timer; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 84 | }; | 
 | 85 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 86 | static int sh_wdt_start(struct watchdog_device *wdt_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 88 | 	struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 89 | 	unsigned long flags; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 90 | 	u8 csr; | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 91 |  | 
| Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 92 | 	pm_runtime_get_sync(wdt->dev); | 
| Paul Mundt | d42c974 | 2012-05-10 16:14:40 +0900 | [diff] [blame] | 93 | 	clk_enable(wdt->clk); | 
| Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 94 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 95 | 	spin_lock_irqsave(&wdt->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 |  | 
 | 97 | 	next_heartbeat = jiffies + (heartbeat * HZ); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 98 | 	mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 |  | 
 | 100 | 	csr = sh_wdt_read_csr(); | 
 | 101 | 	csr |= WTCSR_WT | clock_division_ratio; | 
 | 102 | 	sh_wdt_write_csr(csr); | 
 | 103 |  | 
 | 104 | 	sh_wdt_write_cnt(0); | 
 | 105 |  | 
 | 106 | 	/* | 
 | 107 | 	 * These processors have a bit of an inconsistent initialization | 
 | 108 | 	 * process.. starting with SH-3, RSTS was moved to WTCSR, and the | 
 | 109 | 	 * RSTCSR register was removed. | 
 | 110 | 	 * | 
 | 111 | 	 * On the SH-2 however, in addition with bits being in different | 
 | 112 | 	 * locations, we must deal with RSTCSR outright.. | 
 | 113 | 	 */ | 
 | 114 | 	csr = sh_wdt_read_csr(); | 
 | 115 | 	csr |= WTCSR_TME; | 
 | 116 | 	csr &= ~WTCSR_RSTS; | 
 | 117 | 	sh_wdt_write_csr(csr); | 
 | 118 |  | 
 | 119 | #ifdef CONFIG_CPU_SH2 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | 	csr = sh_wdt_read_rstcsr(); | 
 | 121 | 	csr &= ~RSTCSR_RSTS; | 
 | 122 | 	sh_wdt_write_rstcsr(csr); | 
 | 123 | #endif | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 124 | 	spin_unlock_irqrestore(&wdt->lock, flags); | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 125 |  | 
 | 126 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } | 
 | 128 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 129 | static int sh_wdt_stop(struct watchdog_device *wdt_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 131 | 	struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 132 | 	unsigned long flags; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 133 | 	u8 csr; | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 134 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 135 | 	spin_lock_irqsave(&wdt->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 137 | 	del_timer(&wdt->timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 |  | 
 | 139 | 	csr = sh_wdt_read_csr(); | 
 | 140 | 	csr &= ~WTCSR_TME; | 
 | 141 | 	sh_wdt_write_csr(csr); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 142 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 143 | 	spin_unlock_irqrestore(&wdt->lock, flags); | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 144 |  | 
| Paul Mundt | d42c974 | 2012-05-10 16:14:40 +0900 | [diff] [blame] | 145 | 	clk_disable(wdt->clk); | 
| Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 146 | 	pm_runtime_put_sync(wdt->dev); | 
 | 147 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 148 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } | 
 | 150 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 151 | static int sh_wdt_keepalive(struct watchdog_device *wdt_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 153 | 	struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 154 | 	unsigned long flags; | 
 | 155 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 156 | 	spin_lock_irqsave(&wdt->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | 	next_heartbeat = jiffies + (heartbeat * HZ); | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 158 | 	spin_unlock_irqrestore(&wdt->lock, flags); | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 159 |  | 
 | 160 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } | 
 | 162 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 163 | static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 165 | 	struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev); | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 166 | 	unsigned long flags; | 
 | 167 |  | 
 | 168 | 	if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | 		return -EINVAL; | 
 | 170 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 171 | 	spin_lock_irqsave(&wdt->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | 	heartbeat = t; | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 173 | 	wdt_dev->timeout = t; | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 174 | 	spin_unlock_irqrestore(&wdt->lock, flags); | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 175 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 	return 0; | 
 | 177 | } | 
 | 178 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | static void sh_wdt_ping(unsigned long data) | 
 | 180 | { | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 181 | 	struct sh_wdt *wdt = (struct sh_wdt *)data; | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 182 | 	unsigned long flags; | 
 | 183 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 184 | 	spin_lock_irqsave(&wdt->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | 	if (time_before(jiffies, next_heartbeat)) { | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 186 | 		u8 csr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 |  | 
 | 188 | 		csr = sh_wdt_read_csr(); | 
 | 189 | 		csr &= ~WTCSR_IOVF; | 
 | 190 | 		sh_wdt_write_csr(csr); | 
 | 191 |  | 
 | 192 | 		sh_wdt_write_cnt(0); | 
 | 193 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 194 | 		mod_timer(&wdt->timer, next_ping_period(clock_division_ratio)); | 
| Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 195 | 	} else | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 196 | 		dev_warn(wdt->dev, "Heartbeat lost! Will not ping " | 
 | 197 | 		         "the watchdog\n"); | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 198 | 	spin_unlock_irqrestore(&wdt->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } | 
 | 200 |  | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 201 | static const struct watchdog_info sh_wdt_info = { | 
| Paul Mundt | e4c2cfe | 2006-09-27 12:31:01 +0900 | [diff] [blame] | 202 | 	.options		= WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | | 
 | 203 | 				  WDIOF_MAGICCLOSE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | 	.firmware_version	= 1, | 
 | 205 | 	.identity		= "SH WDT", | 
 | 206 | }; | 
 | 207 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 208 | static const struct watchdog_ops sh_wdt_ops = { | 
 | 209 | 	.owner		= THIS_MODULE, | 
 | 210 | 	.start		= sh_wdt_start, | 
 | 211 | 	.stop		= sh_wdt_stop, | 
 | 212 | 	.ping		= sh_wdt_keepalive, | 
 | 213 | 	.set_timeout	= sh_wdt_set_heartbeat, | 
 | 214 | }; | 
 | 215 |  | 
 | 216 | static struct watchdog_device sh_wdt_dev = { | 
 | 217 | 	.info	= &sh_wdt_info, | 
 | 218 | 	.ops	= &sh_wdt_ops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | }; | 
 | 220 |  | 
| Bill Pemberton | 2d991a1 | 2012-11-19 13:21:41 -0500 | [diff] [blame] | 221 | static int sh_wdt_probe(struct platform_device *pdev) | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 222 | { | 
 | 223 | 	struct sh_wdt *wdt; | 
 | 224 | 	struct resource *res; | 
 | 225 | 	int rc; | 
 | 226 |  | 
 | 227 | 	/* | 
 | 228 | 	 * As this driver only covers the global watchdog case, reject | 
 | 229 | 	 * any attempts to register per-CPU watchdogs. | 
 | 230 | 	 */ | 
 | 231 | 	if (pdev->id != -1) | 
 | 232 | 		return -EINVAL; | 
 | 233 |  | 
 | 234 | 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
 | 235 | 	if (unlikely(!res)) | 
 | 236 | 		return -EINVAL; | 
 | 237 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 238 | 	wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL); | 
| Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 239 | 	if (unlikely(!wdt)) | 
 | 240 | 		return -ENOMEM; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 241 |  | 
 | 242 | 	wdt->dev = &pdev->dev; | 
 | 243 |  | 
| Jingoo Han | 2f7b9b4 | 2013-04-29 18:16:33 +0900 | [diff] [blame] | 244 | 	wdt->clk = devm_clk_get(&pdev->dev, NULL); | 
| Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 245 | 	if (IS_ERR(wdt->clk)) { | 
 | 246 | 		/* | 
 | 247 | 		 * Clock framework support is optional, continue on | 
 | 248 | 		 * anyways if we don't find a matching clock. | 
 | 249 | 		 */ | 
 | 250 | 		wdt->clk = NULL; | 
 | 251 | 	} | 
 | 252 |  | 
| Sachin Kamat | 6330c70 | 2013-03-04 10:36:41 +0530 | [diff] [blame] | 253 | 	wdt->base = devm_ioremap_resource(wdt->dev, res); | 
| Jingoo Han | 2f7b9b4 | 2013-04-29 18:16:33 +0900 | [diff] [blame] | 254 | 	if (IS_ERR(wdt->base)) | 
 | 255 | 		return PTR_ERR(wdt->base); | 
| Paul Mundt | 9ea6404 | 2012-05-10 15:46:48 +0900 | [diff] [blame] | 256 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 257 | 	watchdog_set_nowayout(&sh_wdt_dev, nowayout); | 
 | 258 | 	watchdog_set_drvdata(&sh_wdt_dev, wdt); | 
 | 259 |  | 
| Paul Mundt | f9fb360 | 2012-05-10 15:15:08 +0900 | [diff] [blame] | 260 | 	spin_lock_init(&wdt->lock); | 
 | 261 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 262 | 	rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 263 | 	if (unlikely(rc)) { | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 264 | 		/* Default timeout if invalid */ | 
 | 265 | 		sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT); | 
 | 266 |  | 
 | 267 | 		dev_warn(&pdev->dev, | 
 | 268 | 			 "heartbeat value must be 1<=x<=3600, using %d\n", | 
 | 269 | 			 sh_wdt_dev.timeout); | 
 | 270 | 	} | 
 | 271 |  | 
 | 272 | 	dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n", | 
 | 273 | 		 sh_wdt_dev.timeout, nowayout); | 
 | 274 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 275 | 	rc = watchdog_register_device(&sh_wdt_dev); | 
 | 276 | 	if (unlikely(rc)) { | 
 | 277 | 		dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc); | 
| Jingoo Han | 2f7b9b4 | 2013-04-29 18:16:33 +0900 | [diff] [blame] | 278 | 		return rc; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 279 | 	} | 
 | 280 |  | 
 | 281 | 	init_timer(&wdt->timer); | 
 | 282 | 	wdt->timer.function	= sh_wdt_ping; | 
 | 283 | 	wdt->timer.data		= (unsigned long)wdt; | 
 | 284 | 	wdt->timer.expires	= next_ping_period(clock_division_ratio); | 
 | 285 |  | 
 | 286 | 	platform_set_drvdata(pdev, wdt); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 287 |  | 
 | 288 | 	dev_info(&pdev->dev, "initialized.\n"); | 
 | 289 |  | 
| Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 290 | 	pm_runtime_enable(&pdev->dev); | 
 | 291 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 292 | 	return 0; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 293 | } | 
 | 294 |  | 
| Bill Pemberton | 4b12b89 | 2012-11-19 13:26:24 -0500 | [diff] [blame] | 295 | static int sh_wdt_remove(struct platform_device *pdev) | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 296 | { | 
 | 297 | 	struct sh_wdt *wdt = platform_get_drvdata(pdev); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 298 |  | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 299 | 	watchdog_unregister_device(&sh_wdt_dev); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 300 |  | 
| Paul Mundt | 8c013d96 | 2012-05-10 16:08:35 +0900 | [diff] [blame] | 301 | 	pm_runtime_disable(&pdev->dev); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 302 |  | 
 | 303 | 	return 0; | 
 | 304 | } | 
 | 305 |  | 
| Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 306 | static void sh_wdt_shutdown(struct platform_device *pdev) | 
 | 307 | { | 
| Paul Mundt | 1950f49 | 2012-05-10 15:07:53 +0900 | [diff] [blame] | 308 | 	sh_wdt_stop(&sh_wdt_dev); | 
| Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 309 | } | 
 | 310 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 311 | static struct platform_driver sh_wdt_driver = { | 
 | 312 | 	.driver		= { | 
 | 313 | 		.name	= DRV_NAME, | 
 | 314 | 		.owner	= THIS_MODULE, | 
 | 315 | 	}, | 
 | 316 |  | 
| Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 317 | 	.probe		= sh_wdt_probe, | 
| Bill Pemberton | 8226871 | 2012-11-19 13:21:12 -0500 | [diff] [blame] | 318 | 	.remove		= sh_wdt_remove, | 
| Paul Mundt | 4096812 | 2012-05-10 14:21:15 +0900 | [diff] [blame] | 319 | 	.shutdown	= sh_wdt_shutdown, | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 320 | }; | 
 | 321 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | static int __init sh_wdt_init(void) | 
 | 323 | { | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 324 | 	if (unlikely(clock_division_ratio < 0x5 || | 
 | 325 | 		     clock_division_ratio > 0x7)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | 		clock_division_ratio = WTCSR_CKS_4096; | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 327 |  | 
| Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 328 | 		pr_info("divisor must be 0x5<=x<=0x7, using %d\n", | 
 | 329 | 			clock_division_ratio); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 	} | 
 | 331 |  | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 332 | 	return platform_driver_register(&sh_wdt_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } | 
 | 334 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | static void __exit sh_wdt_exit(void) | 
 | 336 | { | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 337 | 	platform_driver_unregister(&sh_wdt_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 339 | module_init(sh_wdt_init); | 
 | 340 | module_exit(sh_wdt_exit); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 |  | 
 | 342 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); | 
 | 343 | MODULE_DESCRIPTION("SuperH watchdog driver"); | 
 | 344 | MODULE_LICENSE("GPL"); | 
| Paul Mundt | 8f5585e | 2010-05-25 18:31:12 +0900 | [diff] [blame] | 345 | MODULE_ALIAS("platform:" DRV_NAME); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | 
 | 347 |  | 
 | 348 | module_param(clock_division_ratio, int, 0); | 
| Wim Van Sebroeck | a77dba7 | 2009-04-14 20:20:07 +0000 | [diff] [blame] | 349 | MODULE_PARM_DESC(clock_division_ratio, | 
 | 350 | 	"Clock division ratio. Valid ranges are from 0x5 (1.31ms) " | 
| Randy Dunlap | 76550d3 | 2010-05-01 09:46:15 -0700 | [diff] [blame] | 351 | 	"to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 |  | 
 | 353 | module_param(heartbeat, int, 0); | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 354 | MODULE_PARM_DESC(heartbeat, | 
 | 355 | 	"Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default=" | 
 | 356 | 				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 |  | 
| Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 358 | module_param(nowayout, bool, 0); | 
| Alan Cox | 70b814e | 2008-05-19 14:08:55 +0100 | [diff] [blame] | 359 | MODULE_PARM_DESC(nowayout, | 
 | 360 | 	"Watchdog cannot be stopped once started (default=" | 
 | 361 | 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |