watchdog: Use pr_<fmt> and pr_<level>
[firefly-linux-kernel-4.4.55.git] / drivers / watchdog / txx9wdt.c
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  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
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>
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
36 static int nowayout = WATCHDOG_NOWAYOUT;
37 module_param(nowayout, int, 0);
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;
50 static DEFINE_SPINLOCK(txx9_lock);
51
52 static void txx9wdt_ping(void)
53 {
54         spin_lock(&txx9_lock);
55         __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
56         spin_unlock(&txx9_lock);
57 }
58
59 static void txx9wdt_start(void)
60 {
61         spin_lock(&txx9_lock);
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);
68         spin_unlock(&txx9_lock);
69 }
70
71 static void txx9wdt_stop(void)
72 {
73         spin_lock(&txx9_lock);
74         __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
75         __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
76                      &txx9wdt_reg->tcr);
77         spin_unlock(&txx9_lock);
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 {
102                 pr_crit("Unexpected close, not stopping watchdog!\n");
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
131 static long txx9wdt_ioctl(struct file *file, unsigned int cmd,
132                                                         unsigned long arg)
133 {
134         void __user *argp = (void __user *)arg;
135         int __user *p = argp;
136         int new_timeout;
137         static const struct watchdog_info ident = {
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) {
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);
165         default:
166                 return -ENOTTY;
167         }
168 }
169
170 static const struct file_operations txx9wdt_fops = {
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,
177 };
178
179 static struct miscdevice txx9wdt_miscdev = {
180         .minor  =       WATCHDOG_MINOR,
181         .name   =       "watchdog",
182         .fops   =       &txx9wdt_fops,
183 };
184
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;
206         if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
207                                      "txx9wdt"))
208                 goto exit_busy;
209         txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res));
210         if (!txx9wdt_reg)
211                 goto exit_busy;
212
213         ret = misc_register(&txx9wdt_miscdev);
214         if (ret) {
215                 goto exit;
216         }
217
218         pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
219                 timeout, WD_MAX_TIMEOUT, nowayout);
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);
235         clk_disable(txx9_imclk);
236         clk_put(txx9_imclk);
237         return 0;
238 }
239
240 static void txx9wdt_shutdown(struct platform_device *dev)
241 {
242         txx9wdt_stop();
243 }
244
245 static struct platform_driver txx9wdt_driver = {
246         .remove = __exit_p(txx9wdt_remove),
247         .shutdown = txx9wdt_shutdown,
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);
270 MODULE_ALIAS("platform:txx9wdt");