blob: d02804ccd280a575a504e256d25a12730e3b5b5c [file] [log] [blame]
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +09001/*
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 Perches27c766a2012-02-15 15:06:19 -080010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090013#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 Nemoto6f702fc2007-11-12 01:32:17 +090019#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
29static int timeout = TIMER_MARGIN; /* in seconds */
30module_param(timeout, int, 0);
31MODULE_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 Sebroeck86a1e182012-03-05 16:51:11 +010036static bool nowayout = WATCHDOG_NOWAYOUT;
37module_param(nowayout, bool, 0);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090038MODULE_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
46static unsigned long txx9wdt_alive;
47static int expect_close;
48static struct txx9_tmr_reg __iomem *txx9wdt_reg;
49static struct clk *txx9_imclk;
Adrian Bunkf8494e02008-08-08 18:18:46 +030050static DEFINE_SPINLOCK(txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090051
52static void txx9wdt_ping(void)
53{
Alan Cox8dc244f2008-05-19 14:09:12 +010054 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090055 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
Alan Cox8dc244f2008-05-19 14:09:12 +010056 spin_unlock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090057}
58
59static void txx9wdt_start(void)
60{
Alan Cox8dc244f2008-05-19 14:09:12 +010061 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090062 __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 Cox8dc244f2008-05-19 14:09:12 +010068 spin_unlock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090069}
70
71static void txx9wdt_stop(void)
72{
Alan Cox8dc244f2008-05-19 14:09:12 +010073 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090074 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
75 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
76 &txx9wdt_reg->tcr);
Alan Cox8dc244f2008-05-19 14:09:12 +010077 spin_unlock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090078}
79
80static 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
97static int txx9wdt_release(struct inode *inode, struct file *file)
98{
99 if (expect_close)
100 txx9wdt_stop();
101 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800102 pr_crit("Unexpected close, not stopping watchdog!\n");
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900103 txx9wdt_ping();
104 }
105 clear_bit(0, &txx9wdt_alive);
106 expect_close = 0;
107 return 0;
108}
109
110static 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 Cox8dc244f2008-05-19 14:09:12 +0100131static long txx9wdt_ioctl(struct file *file, unsigned int cmd,
132 unsigned long arg)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900133{
134 void __user *argp = (void __user *)arg;
135 int __user *p = argp;
136 int new_timeout;
Alan Cox8dc244f2008-05-19 14:09:12 +0100137 static const struct watchdog_info ident = {
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900138 .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 Nemoto6f702fc2007-11-12 01:32:17 +0900146 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 Sebroeck0c060902008-07-18 11:41:17 +0000165 default:
166 return -ENOTTY;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900167 }
168}
169
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900170static const struct file_operations txx9wdt_fops = {
Alan Cox8dc244f2008-05-19 14:09:12 +0100171 .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 Nemoto6f702fc2007-11-12 01:32:17 +0900177};
178
179static struct miscdevice txx9wdt_miscdev = {
Alan Cox8dc244f2008-05-19 14:09:12 +0100180 .minor = WATCHDOG_MINOR,
181 .name = "watchdog",
182 .fops = &txx9wdt_fops,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900183};
184
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900185static 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 Sweetenb782a562009-12-04 12:24:04 -0500206 if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900207 "txx9wdt"))
208 goto exit_busy;
H Hartley Sweetenb782a562009-12-04 12:24:04 -0500209 txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res));
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900210 if (!txx9wdt_reg)
211 goto exit_busy;
212
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900213 ret = misc_register(&txx9wdt_miscdev);
214 if (ret) {
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900215 goto exit;
216 }
217
Joe Perches27c766a2012-02-15 15:06:19 -0800218 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
219 timeout, WD_MAX_TIMEOUT, nowayout);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900220
221 return 0;
222exit_busy:
223 ret = -EBUSY;
224exit:
225 if (txx9_imclk) {
226 clk_disable(txx9_imclk);
227 clk_put(txx9_imclk);
228 }
229 return ret;
230}
231
232static int __exit txx9wdt_remove(struct platform_device *dev)
233{
234 misc_deregister(&txx9wdt_miscdev);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900235 clk_disable(txx9_imclk);
236 clk_put(txx9_imclk);
237 return 0;
238}
239
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000240static void txx9wdt_shutdown(struct platform_device *dev)
241{
242 txx9wdt_stop();
243}
244
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900245static struct platform_driver txx9wdt_driver = {
246 .remove = __exit_p(txx9wdt_remove),
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000247 .shutdown = txx9wdt_shutdown,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900248 .driver = {
249 .name = "txx9wdt",
250 .owner = THIS_MODULE,
251 },
252};
253
254static int __init watchdog_init(void)
255{
256 return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe);
257}
258
259static void __exit watchdog_exit(void)
260{
261 platform_driver_unregister(&txx9wdt_driver);
262}
263
264module_init(watchdog_init);
265module_exit(watchdog_exit);
266
267MODULE_DESCRIPTION("TXx9 Watchdog Driver");
268MODULE_LICENSE("GPL");
269MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700270MODULE_ALIAS("platform:txx9wdt");