blob: 1531e0256c34be306482e1a278dc7936764ded6b [file] [log] [blame]
Sylver Bruneau22ac9232008-06-26 10:47:45 +02001/*
Nicolas Pitre3b937a72009-06-01 13:56:02 -04002 * drivers/watchdog/orion_wdt.c
Sylver Bruneau22ac9232008-06-26 10:47:45 +02003 *
Nicolas Pitre3b937a72009-06-01 13:56:02 -04004 * Watchdog driver for Orion/Kirkwood processors
Sylver Bruneau22ac9232008-06-26 10:47:45 +02005 *
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
Joe Perches27c766a2012-02-15 15:06:19 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Sylver Bruneau22ac9232008-06-26 10:47:45 +020015#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/fs.h>
20#include <linux/miscdevice.h>
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080021#include <linux/platform_device.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020022#include <linux/watchdog.h>
23#include <linux/init.h>
24#include <linux/uaccess.h>
25#include <linux/io.h>
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000026#include <linux/spinlock.h>
Andrew Lunn4f04be62012-03-04 16:57:31 +010027#include <linux/clk.h>
Andrew Lunn1e7bad02012-06-10 15:20:06 +020028#include <linux/of.h>
Nicolas Pitrefdd8b072009-04-22 20:08:17 +010029#include <mach/bridge-regs.h>
Sylver Bruneau22ac9232008-06-26 10:47:45 +020030
31/*
32 * Watchdog timer block registers.
33 */
Jason Coopera855a7c2012-03-15 00:33:26 +000034#define TIMER_CTRL 0x0000
Sylver Bruneau22ac9232008-06-26 10:47:45 +020035#define WDT_EN 0x0010
Jason Coopera855a7c2012-03-15 00:33:26 +000036#define WDT_VAL 0x0024
Sylver Bruneau22ac9232008-06-26 10:47:45 +020037
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080038#define WDT_MAX_CYCLE_COUNT 0xffffffff
Sylver Bruneau22ac9232008-06-26 10:47:45 +020039#define WDT_IN_USE 0
40#define WDT_OK_TO_CLOSE 1
41
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010042static bool nowayout = WATCHDOG_NOWAYOUT;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080043static int heartbeat = -1; /* module parameter (seconds) */
44static unsigned int wdt_max_duration; /* (seconds) */
Andrew Lunn4f04be62012-03-04 16:57:31 +010045static struct clk *clk;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -080046static unsigned int wdt_tclk;
Jason Coopera855a7c2012-03-15 00:33:26 +000047static void __iomem *wdt_reg;
Sylver Bruneau22ac9232008-06-26 10:47:45 +020048static unsigned long wdt_status;
Axel Lin1334f322011-11-29 13:54:01 +080049static DEFINE_SPINLOCK(wdt_lock);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020050
Nicolas Pitre3b937a72009-06-01 13:56:02 -040051static void orion_wdt_ping(void)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010052{
53 spin_lock(&wdt_lock);
54
55 /* Reload watchdog duration */
Jason Coopera855a7c2012-03-15 00:33:26 +000056 writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +010057
58 spin_unlock(&wdt_lock);
59}
60
Nicolas Pitre3b937a72009-06-01 13:56:02 -040061static void orion_wdt_enable(void)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020062{
63 u32 reg;
64
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000065 spin_lock(&wdt_lock);
66
Sylver Bruneau22ac9232008-06-26 10:47:45 +020067 /* Set watchdog duration */
Jason Coopera855a7c2012-03-15 00:33:26 +000068 writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020069
70 /* Clear watchdog timer interrupt */
71 reg = readl(BRIDGE_CAUSE);
72 reg &= ~WDT_INT_REQ;
73 writel(reg, BRIDGE_CAUSE);
74
75 /* Enable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +000076 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020077 reg |= WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +000078 writel(reg, wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020079
80 /* Enable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020081 reg = readl(RSTOUTn_MASK);
82 reg |= WDT_RESET_OUT_EN;
83 writel(reg, RSTOUTn_MASK);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000084
85 spin_unlock(&wdt_lock);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020086}
87
Nicolas Pitre3b937a72009-06-01 13:56:02 -040088static void orion_wdt_disable(void)
Sylver Bruneau22ac9232008-06-26 10:47:45 +020089{
90 u32 reg;
91
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +000092 spin_lock(&wdt_lock);
93
Sylver Bruneau22ac9232008-06-26 10:47:45 +020094 /* Disable reset on watchdog */
Thomas Reitmayr6462c612009-06-01 13:38:33 +020095 reg = readl(RSTOUTn_MASK);
96 reg &= ~WDT_RESET_OUT_EN;
97 writel(reg, RSTOUTn_MASK);
Sylver Bruneau22ac9232008-06-26 10:47:45 +020098
99 /* Disable watchdog timer */
Jason Coopera855a7c2012-03-15 00:33:26 +0000100 reg = readl(wdt_reg + TIMER_CTRL);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200101 reg &= ~WDT_EN;
Jason Coopera855a7c2012-03-15 00:33:26 +0000102 writel(reg, wdt_reg + TIMER_CTRL);
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000103
104 spin_unlock(&wdt_lock);
105}
106
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400107static int orion_wdt_get_timeleft(int *time_left)
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000108{
109 spin_lock(&wdt_lock);
Jason Coopera855a7c2012-03-15 00:33:26 +0000110 *time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000111 spin_unlock(&wdt_lock);
112 return 0;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200113}
114
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400115static int orion_wdt_open(struct inode *inode, struct file *file)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200116{
117 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
118 return -EBUSY;
119 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400120 orion_wdt_enable();
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200121 return nonseekable_open(inode, file);
122}
123
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400124static ssize_t orion_wdt_write(struct file *file, const char *data,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200125 size_t len, loff_t *ppos)
126{
127 if (len) {
128 if (!nowayout) {
129 size_t i;
130
131 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
132 for (i = 0; i != len; i++) {
133 char c;
134
135 if (get_user(c, data + i))
136 return -EFAULT;
137 if (c == 'V')
138 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
139 }
140 }
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400141 orion_wdt_ping();
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200142 }
143 return len;
144}
145
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400146static int orion_wdt_settimeout(int new_time)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100147{
148 if ((new_time <= 0) || (new_time > wdt_max_duration))
149 return -EINVAL;
150
151 /* Set new watchdog time to be used when
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400152 * orion_wdt_enable() or orion_wdt_ping() is called. */
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100153 heartbeat = new_time;
154 return 0;
155}
156
157static const struct watchdog_info ident = {
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200158 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
159 WDIOF_KEEPALIVEPING,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400160 .identity = "Orion Watchdog",
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200161};
162
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400163static long orion_wdt_ioctl(struct file *file, unsigned int cmd,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200164 unsigned long arg)
165{
166 int ret = -ENOTTY;
167 int time;
168
169 switch (cmd) {
170 case WDIOC_GETSUPPORT:
171 ret = copy_to_user((struct watchdog_info *)arg, &ident,
172 sizeof(ident)) ? -EFAULT : 0;
173 break;
174
175 case WDIOC_GETSTATUS:
176 case WDIOC_GETBOOTSTATUS:
177 ret = put_user(0, (int *)arg);
178 break;
179
180 case WDIOC_KEEPALIVE:
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400181 orion_wdt_ping();
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200182 ret = 0;
183 break;
184
185 case WDIOC_SETTIMEOUT:
186 ret = get_user(time, (int *)arg);
187 if (ret)
188 break;
189
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400190 if (orion_wdt_settimeout(time)) {
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200191 ret = -EINVAL;
192 break;
193 }
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400194 orion_wdt_ping();
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200195 /* Fall through */
196
197 case WDIOC_GETTIMEOUT:
198 ret = put_user(heartbeat, (int *)arg);
199 break;
200
201 case WDIOC_GETTIMELEFT:
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400202 if (orion_wdt_get_timeleft(&time)) {
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200203 ret = -EINVAL;
204 break;
205 }
206 ret = put_user(time, (int *)arg);
207 break;
208 }
209 return ret;
210}
211
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400212static int orion_wdt_release(struct inode *inode, struct file *file)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200213{
214 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400215 orion_wdt_disable();
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200216 else
Joe Perches27c766a2012-02-15 15:06:19 -0800217 pr_crit("Device closed unexpectedly - timer will not stop\n");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200218 clear_bit(WDT_IN_USE, &wdt_status);
219 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
220
221 return 0;
222}
223
224
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400225static const struct file_operations orion_wdt_fops = {
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200226 .owner = THIS_MODULE,
227 .llseek = no_llseek,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400228 .write = orion_wdt_write,
229 .unlocked_ioctl = orion_wdt_ioctl,
230 .open = orion_wdt_open,
231 .release = orion_wdt_release,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200232};
233
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400234static struct miscdevice orion_wdt_miscdev = {
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200235 .minor = WATCHDOG_MINOR,
236 .name = "watchdog",
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400237 .fops = &orion_wdt_fops,
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200238};
239
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400240static int __devinit orion_wdt_probe(struct platform_device *pdev)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800241{
Jason Coopera855a7c2012-03-15 00:33:26 +0000242 struct resource *res;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800243 int ret;
244
Andrew Lunn4f04be62012-03-04 16:57:31 +0100245 clk = clk_get(&pdev->dev, NULL);
246 if (IS_ERR(clk)) {
247 printk(KERN_ERR "Orion Watchdog missing clock\n");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800248 return -ENODEV;
249 }
Andrew Lunn4f04be62012-03-04 16:57:31 +0100250 clk_prepare_enable(clk);
251 wdt_tclk = clk_get_rate(clk);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800252
Jason Coopera855a7c2012-03-15 00:33:26 +0000253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
254
255 wdt_reg = ioremap(res->start, resource_size(res));
256
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400257 if (orion_wdt_miscdev.parent)
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800258 return -EBUSY;
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400259 orion_wdt_miscdev.parent = &pdev->dev;
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800260
261 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400262 if (orion_wdt_settimeout(heartbeat))
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800263 heartbeat = wdt_max_duration;
264
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400265 ret = misc_register(&orion_wdt_miscdev);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800266 if (ret)
267 return ret;
268
Joe Perches27c766a2012-02-15 15:06:19 -0800269 pr_info("Initial timeout %d sec%s\n",
270 heartbeat, nowayout ? ", nowayout" : "");
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800271 return 0;
272}
273
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400274static int __devexit orion_wdt_remove(struct platform_device *pdev)
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200275{
276 int ret;
277
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800278 if (test_bit(WDT_IN_USE, &wdt_status)) {
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400279 orion_wdt_disable();
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800280 clear_bit(WDT_IN_USE, &wdt_status);
281 }
Wim Van Sebroeck6d0f0df2008-10-02 09:32:45 +0000282
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400283 ret = misc_deregister(&orion_wdt_miscdev);
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800284 if (!ret)
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400285 orion_wdt_miscdev.parent = NULL;
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200286
Andrew Lunn4f04be62012-03-04 16:57:31 +0100287 clk_disable_unprepare(clk);
288 clk_put(clk);
289
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200290 return ret;
291}
292
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400293static void orion_wdt_shutdown(struct platform_device *pdev)
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100294{
295 if (test_bit(WDT_IN_USE, &wdt_status))
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400296 orion_wdt_disable();
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100297}
298
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200299static const struct of_device_id orion_wdt_of_match_table[] __devinitdata = {
300 { .compatible = "marvell,orion-wdt", },
301 {},
302};
303MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
304
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400305static struct platform_driver orion_wdt_driver = {
306 .probe = orion_wdt_probe,
307 .remove = __devexit_p(orion_wdt_remove),
308 .shutdown = orion_wdt_shutdown,
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800309 .driver = {
310 .owner = THIS_MODULE,
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400311 .name = "orion_wdt",
Andrew Lunn1e7bad02012-06-10 15:20:06 +0200312 .of_match_table = of_match_ptr(orion_wdt_of_match_table),
Thomas Reitmayr9e058d42009-02-24 14:59:22 -0800313 },
314};
315
Axel Linb8ec6112011-11-29 13:56:27 +0800316module_platform_driver(orion_wdt_driver);
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200317
318MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
Nicolas Pitre3b937a72009-06-01 13:56:02 -0400319MODULE_DESCRIPTION("Orion Processor Watchdog");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200320
321module_param(heartbeat, int, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100322MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200323
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100324module_param(nowayout, bool, 0);
Thomas Reitmayrdf6707b2009-02-20 19:44:59 +0100325MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
326 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sylver Bruneau22ac9232008-06-26 10:47:45 +0200327
328MODULE_LICENSE("GPL");
329MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);