source: src/linux/universal/linux-4.9/drivers/watchdog/cns3xxx_wdt.c @ 31574

Last change on this file since 31574 was 31574, checked in by brainslayer, 6 months ago

kernel 4.9 update

File size: 11.6 KB
Line 
1/*******************************************************************************
2 *
3 *  drivers/watchdog/cns3xxx_wdt.c
4 *
5 *  Watchdog timer driver for the CNS3XXX SOCs
6 *
7 *  Author: Scott Shu
8 *
9 *  Copyright (c) 2008 Cavium Networks
10 *
11 *  This file is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU General Public License, Version 2, as
13 *  published by the Free Software Foundation.
14 *
15 *  This file is distributed in the hope that it will be useful,
16 *  but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
18 *  NONINFRINGEMENT.  See the GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with this file; if not, write to the Free Software
22 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or
23 *  visit http://www.gnu.org/licenses/.
24 *
25 *  This file may also be available under a different license from Cavium.
26 *  Contact Cavium Networks for more information
27 *
28 ******************************************************************************/
29
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32#include <linux/types.h>
33#include <linux/miscdevice.h>
34#include <linux/watchdog.h>
35#include <linux/fs.h>
36#include <linux/reboot.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/platform_device.h>
40#include <linux/io.h>
41#include <linux/uaccess.h>
42#include <linux/slab.h>
43#include <linux/delay.h>
44
45#include <asm/hardware/arm_twd.h>
46
47struct cns3xxx_wdt {
48        unsigned long   timer_alive;
49        struct device   *dev;
50        void __iomem    *base;
51        int             irq;
52        unsigned int    perturb;
53        char            expect_close;
54};
55
56static struct platform_device *cns3xxx_wdt_dev;
57static unsigned int twd_timer_rate = 0;
58static spinlock_t wdt_lock;
59
60#define TIMER_MARGIN    60
61static int cns3xxx_margin = TIMER_MARGIN;
62module_param(cns3xxx_margin, int, 0);
63MODULE_PARM_DESC(cns3xxx_margin,
64        "CNS3XXX timer margin in seconds. (0 < cns3xxx_margin < 65536, default="
65                                __MODULE_STRING(TIMER_MARGIN) ")");
66
67static int nowayout = WATCHDOG_NOWAYOUT;
68module_param(nowayout, int, 0);
69MODULE_PARM_DESC(nowayout,
70        "Watchdog cannot be stopped once started (default="
71                                __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
72
73#define ONLY_TESTING    0
74static int cns3xxx_noboot = ONLY_TESTING;
75module_param(cns3xxx_noboot, int, 0);
76MODULE_PARM_DESC(cns3xxx_noboot, "CNS3XXX watchdog action, "
77        "set to 1 to ignore reboots, 0 to reboot (default="
78                                        __MODULE_STRING(ONLY_TESTING) ")");
79
80/*
81 *      This is the interrupt handler.  Note that we only use this
82 *      in testing mode, so don't actually do a reboot here.
83 */
84static irqreturn_t cns3xxx_wdt_fire(int irq, void *arg)
85{
86        struct cns3xxx_wdt *wdt = arg;
87
88        /* Check it really was our interrupt */
89        if (readl(wdt->base + TWD_WDOG_INTSTAT)) {
90                dev_printk(KERN_CRIT, wdt->dev,
91                                        "Triggered - Reboot ignored.\n");
92                /* Clear the interrupt on the watchdog */
93                writel(1, wdt->base + TWD_WDOG_INTSTAT);
94                return IRQ_HANDLED;
95        }
96        return IRQ_NONE;
97}
98
99/*
100 *      cns3xxx_wdt_keepalive - reload the timer
101 *
102 *      Note that the spec says a DIFFERENT value must be written to the reload
103 *      register each time.  The "perturb" variable deals with this by adding 1
104 *      to the count every other time the function is called.
105 */
106static void cns3xxx_wdt_keepalive(struct cns3xxx_wdt *wdt)
107{
108        unsigned int count;
109
110        /* Assume prescale is set to 256 */
111        count = (twd_timer_rate / 256) * cns3xxx_margin;
112
113        /* Reload the counter */
114        spin_lock(&wdt_lock);
115        writel(count + wdt->perturb, wdt->base + TWD_WDOG_LOAD);
116        wdt->perturb = wdt->perturb ? 0 : 1;
117        spin_unlock(&wdt_lock);
118}
119
120static void cns3xxx_wdt_stop(struct cns3xxx_wdt *wdt)
121{
122        spin_lock(&wdt_lock);
123        writel(0x12345678, wdt->base + TWD_WDOG_DISABLE);
124        writel(0x87654321, wdt->base + TWD_WDOG_DISABLE);
125        writel(0x0, wdt->base + TWD_WDOG_CONTROL);
126        spin_unlock(&wdt_lock);
127}
128
129static void cns3xxx_wdt_start(struct cns3xxx_wdt *wdt)
130{
131        dev_printk(KERN_INFO, wdt->dev, "enabling watchdog.\n");
132
133        /* This loads the count register but does NOT start the count yet */
134        cns3xxx_wdt_keepalive(wdt);
135
136        spin_lock(&wdt_lock);
137
138        if (cns3xxx_noboot) {
139                /* Enable watchdog - prescale=256, watchdog mode=0, enable=1 */
140                writel(0x0000FF01, wdt->base + TWD_WDOG_CONTROL);
141        } else {
142                /* Enable watchdog - prescale=256, watchdog mode=1, enable=1 */
143                writel(0x0000FF09, wdt->base + TWD_WDOG_CONTROL);
144        }
145
146        spin_unlock(&wdt_lock);
147}
148
149static int cns3xxx_wdt_set_heartbeat(int t)
150{
151        if (t < 0x0001 || t > 0xFFFF)
152                return -EINVAL;
153
154        cns3xxx_margin = t;
155        return 0;
156}
157
158/*
159 *      /dev/watchdog handling
160 */
161static int cns3xxx_wdt_open(struct inode *inode, struct file *file)
162{
163        struct cns3xxx_wdt *wdt = platform_get_drvdata(cns3xxx_wdt_dev);
164
165        if (test_and_set_bit(0, &wdt->timer_alive))
166                return -EBUSY;
167
168        if (nowayout)
169                __module_get(THIS_MODULE);
170
171        file->private_data = wdt;
172
173        /*
174         *      Activate timer
175         */
176        cns3xxx_wdt_start(wdt);
177
178        return nonseekable_open(inode, file);
179}
180
181static int cns3xxx_wdt_release(struct inode *inode, struct file *file)
182{
183        struct cns3xxx_wdt *wdt = file->private_data;
184
185        /*
186         *      Shut off the timer.
187         *      Lock it in if it's a module and we set nowayout
188         */
189        if (wdt->expect_close == 42)
190                cns3xxx_wdt_stop(wdt);
191        else {
192                dev_printk(KERN_CRIT, wdt->dev,
193                                "unexpected close, not stopping watchdog!\n");
194                cns3xxx_wdt_keepalive(wdt);
195        }
196        clear_bit(0, &wdt->timer_alive);
197        wdt->expect_close = 0;
198        return 0;
199}
200
201static ssize_t cns3xxx_wdt_write(struct file *file, const char *data,
202                                                size_t len, loff_t *ppos)
203{
204        struct cns3xxx_wdt *wdt = file->private_data;
205
206        /*
207         *      Refresh the timer.
208         */
209        if (len) {
210                if (!nowayout) {
211                        size_t i;
212
213                        /* In case it was set long ago */
214                        wdt->expect_close = 0;
215
216                        for (i = 0; i != len; i++) {
217                                char c;
218
219                                if (get_user(c, data + i))
220                                        return -EFAULT;
221                                if (c == 'V')
222                                        wdt->expect_close = 42;
223                        }
224                }
225                cns3xxx_wdt_keepalive(wdt);
226        }
227        return len;
228}
229
230static struct watchdog_info ident = {
231        .options                = WDIOF_SETTIMEOUT |
232                                  WDIOF_KEEPALIVEPING |
233                                  WDIOF_MAGICCLOSE,
234        .identity               = "CNS3XXX Watchdog",
235};
236
237static long cns3xxx_wdt_ioctl(struct file *file, unsigned int cmd,
238                                                        unsigned long arg)
239{
240        struct cns3xxx_wdt *wdt = file->private_data;
241        int ret;
242        union {
243                struct watchdog_info ident;
244                int i;
245        } uarg;
246
247        if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg))
248                return -ENOTTY;
249
250        if (_IOC_DIR(cmd) & _IOC_WRITE) {
251                ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd));
252                if (ret)
253                        return -EFAULT;
254        }
255
256        switch (cmd) {
257        case WDIOC_GETSUPPORT:
258                uarg.ident = ident;
259                ret = 0;
260                break;
261
262        case WDIOC_GETSTATUS:
263        case WDIOC_GETBOOTSTATUS:
264                uarg.i = 0;
265                ret = 0;
266                break;
267
268        case WDIOC_SETOPTIONS:
269                ret = -EINVAL;
270                if (uarg.i & WDIOS_DISABLECARD) {
271                        cns3xxx_wdt_stop(wdt);
272                        ret = 0;
273                }
274                if (uarg.i & WDIOS_ENABLECARD) {
275                        cns3xxx_wdt_start(wdt);
276                        ret = 0;
277                }
278                break;
279
280        case WDIOC_KEEPALIVE:
281                cns3xxx_wdt_keepalive(wdt);
282                ret = 0;
283                break;
284
285        case WDIOC_SETTIMEOUT:
286                ret = cns3xxx_wdt_set_heartbeat(uarg.i);
287                if (ret)
288                        break;
289
290                cns3xxx_wdt_keepalive(wdt);
291                /* Fall */
292        case WDIOC_GETTIMEOUT:
293                uarg.i = cns3xxx_margin;
294                ret = 0;
295                break;
296
297        default:
298                return -ENOTTY;
299        }
300
301        if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) {
302                ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd));
303                if (ret)
304                        ret = -EFAULT;
305        }
306        return ret;
307}
308
309/*
310 *      System shutdown handler.  Turn off the watchdog if we're
311 *      restarting or halting the system.
312 */
313static void cns3xxx_wdt_shutdown(struct platform_device *dev)
314{
315        struct cns3xxx_wdt *wdt = platform_get_drvdata(dev);
316
317        if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT)
318                cns3xxx_wdt_stop(wdt);
319}
320
321/*
322 *      Kernel Interfaces
323 */
324static const struct file_operations cns3xxx_wdt_fops = {
325        .owner          = THIS_MODULE,
326        .llseek         = no_llseek,
327        .write          = cns3xxx_wdt_write,
328        .unlocked_ioctl = cns3xxx_wdt_ioctl,
329        .open           = cns3xxx_wdt_open,
330        .release        = cns3xxx_wdt_release,
331};
332
333static struct miscdevice cns3xxx_wdt_miscdev = {
334        .minor          = WATCHDOG_MINOR,
335        .name           = "watchdog",
336        .fops           = &cns3xxx_wdt_fops,
337};
338
339static int cns3xxx_wdt_probe(struct platform_device *dev)
340{
341        struct cns3xxx_wdt *wdt;
342        struct resource *res;
343        int ret;
344        u64 waitjiffies;
345
346        printk("watchdog: cns3xxx_wdt_probe\n");
347
348        /* We only accept one device, and it must have an id of -1 */
349        if (dev->id != -1)
350                return -ENODEV;
351
352        res = platform_get_resource(dev, IORESOURCE_MEM, 0);
353        if (!res) {
354                ret = -ENODEV;
355                goto err_out;
356        }
357
358        wdt = kzalloc(sizeof(struct cns3xxx_wdt), GFP_KERNEL);
359        if (!wdt) {
360                ret = -ENOMEM;
361                goto err_out;
362        }
363
364        wdt->dev = &dev->dev;
365        wdt->irq = platform_get_irq(dev, 0);
366        printk("watchdog irq = %d\n", wdt->irq);
367        if (wdt->irq < 0) {
368                ret = -ENXIO;
369                goto err_free;
370        }
371        wdt->base = ioremap(res->start + 0x100, res->end - res->start + 1);
372        printk("watchdog start_regs  = 0x%8X\n", (unsigned int)wdt->base);
373        if (!wdt->base) {
374                ret = -ENOMEM;
375                goto err_free;
376        }
377
378        cns3xxx_wdt_miscdev.parent = &dev->dev;
379        ret = misc_register(&cns3xxx_wdt_miscdev);
380        if (ret) {
381                dev_printk(KERN_ERR, wdt->dev,
382                        "cannot register miscdev on minor=%d (err=%d)\n",
383                                                        WATCHDOG_MINOR, ret);
384                goto err_misc;
385        }
386
387        ret = request_irq(wdt->irq, cns3xxx_wdt_fire, 0,
388                        dev->name, wdt);
389        if (ret) {
390                dev_printk(KERN_ERR, wdt->dev,
391                        "cannot register IRQ%d for watchdog\n", wdt->irq);
392                goto err_irq;
393        }
394
395        /*
396         * If this is the first time round, we need to work out how fast
397         * the timer ticks
398         */
399        if (twd_timer_rate == 0) {
400                /* Wait for a tick to start */
401                waitjiffies = get_jiffies_64() + 1;
402
403                while (get_jiffies_64() < waitjiffies)
404                        udelay(10);
405
406                /* OK, now the tick has started, let's get the timer going */
407                waitjiffies += 5;
408
409                /* enable, no interrupt or reload */
410                __raw_writel(0x1, wdt->base + TWD_TIMER_CONTROL);
411
412                /* maximum value */
413                __raw_writel(0xFFFFFFFFU, wdt->base + TWD_TIMER_COUNTER);
414
415                while (get_jiffies_64() < waitjiffies)
416                        udelay(10);
417
418                twd_timer_rate = (0xFFFFFFFFU - __raw_readl(wdt->base + TWD_TIMER_COUNTER)) * (HZ / 5);
419
420                printk("Watchdog timer frequency : %u.%02uMHz.\n", twd_timer_rate / 1000000, (twd_timer_rate / 100000) % 100);
421        }
422
423        cns3xxx_wdt_stop(wdt);
424        platform_set_drvdata(dev, wdt);
425        cns3xxx_wdt_dev = dev;
426
427        return 0;
428
429err_irq:
430        misc_deregister(&cns3xxx_wdt_miscdev);
431err_misc:
432        platform_set_drvdata(dev, NULL);
433        iounmap(wdt->base);
434err_free:
435        kfree(wdt);
436err_out:
437        return ret;
438}
439
440static int cns3xxx_wdt_remove(struct platform_device *dev)
441{
442        struct cns3xxx_wdt *wdt = platform_get_drvdata(dev);
443
444        printk("watchdog: cns3xxx_wdt_remove\n");
445
446        platform_set_drvdata(dev, NULL);
447
448        misc_deregister(&cns3xxx_wdt_miscdev);
449
450        cns3xxx_wdt_dev = NULL;
451
452        free_irq(wdt->irq, wdt);
453        iounmap(wdt->base);
454        kfree(wdt);
455        return 0;
456}
457
458
459static struct platform_driver cns3xxx_wdt_driver = {
460        .probe          = cns3xxx_wdt_probe,
461        .remove         = cns3xxx_wdt_remove,
462        .shutdown       = cns3xxx_wdt_shutdown,
463        .driver         = {
464                .owner  = THIS_MODULE,
465                .name   = "cns3xxx-wdt",
466        },
467};
468
469static char banner[] __initdata = KERN_INFO
470                "CNS3XXX Watchdog Timer, noboot=%d margin=%d sec (nowayout= %d)\n";
471
472static int __init cns3xxx_wdt_init(void)
473{
474        /*
475         * Check that the margin value is within it's range;
476         * if not reset to the default
477         */
478        if (cns3xxx_wdt_set_heartbeat(cns3xxx_margin)) {
479                cns3xxx_wdt_set_heartbeat(TIMER_MARGIN);
480                printk(KERN_INFO "cns3xxx_margin value must be 0 < cns3xxx_margin < 65536, using %d\n",
481                        TIMER_MARGIN);
482        }
483
484        printk(banner, cns3xxx_noboot, cns3xxx_margin, nowayout);
485
486        spin_lock_init(&wdt_lock);
487
488        return platform_driver_register(&cns3xxx_wdt_driver);
489}
490
491static void __exit cns3xxx_wdt_exit(void)
492{
493        platform_driver_unregister(&cns3xxx_wdt_driver);
494}
495
496module_init(cns3xxx_wdt_init);
497module_exit(cns3xxx_wdt_exit);
498
499MODULE_AUTHOR("Scott Shu");
500MODULE_DESCRIPTION("CNS3XXX Watchdog Device Driver");
501MODULE_LICENSE("GPL");
502MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
503MODULE_ALIAS("platform:cns3xxx-wdt");
Note: See TracBrowser for help on using the repository browser.