blob: 53f1b17429c6c6e6910e3c680cfe16c28362fda3 [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>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090018#include <linux/init.h>
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090019#include <linux/platform_device.h>
20#include <linux/clk.h>
21#include <linux/err.h>
22#include <linux/io.h>
23#include <asm/txx9tmr.h>
24
25#define TIMER_MARGIN 60 /* Default is 60 seconds */
26
27static int timeout = TIMER_MARGIN; /* in seconds */
28module_param(timeout, int, 0);
29MODULE_PARM_DESC(timeout,
30 "Watchdog timeout in seconds. "
31 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
32 "default=" __MODULE_STRING(TIMER_MARGIN) ")");
33
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010034static bool nowayout = WATCHDOG_NOWAYOUT;
35module_param(nowayout, bool, 0);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090036MODULE_PARM_DESC(nowayout,
37 "Watchdog cannot be stopped once started "
38 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
39
40#define WD_TIMER_CCD 7 /* 1/256 */
41#define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
42#define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
43
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090044static struct txx9_tmr_reg __iomem *txx9wdt_reg;
45static struct clk *txx9_imclk;
Adrian Bunkf8494e02008-08-08 18:18:46 +030046static DEFINE_SPINLOCK(txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090047
Axel Lind6245842012-03-16 11:53:53 +080048static int txx9wdt_ping(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090049{
Alan Cox8dc244f2008-05-19 14:09:12 +010050 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090051 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
Alan Cox8dc244f2008-05-19 14:09:12 +010052 spin_unlock(&txx9_lock);
Axel Lind6245842012-03-16 11:53:53 +080053 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090054}
55
Axel Lind6245842012-03-16 11:53:53 +080056static int txx9wdt_start(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090057{
Alan Cox8dc244f2008-05-19 14:09:12 +010058 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090059 __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra);
60 __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
61 __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
62 __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
63 &txx9wdt_reg->tcr);
64 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
Alan Cox8dc244f2008-05-19 14:09:12 +010065 spin_unlock(&txx9_lock);
Axel Lind6245842012-03-16 11:53:53 +080066 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090067}
68
Axel Lind6245842012-03-16 11:53:53 +080069static int txx9wdt_stop(struct watchdog_device *wdt_dev)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090070{
Alan Cox8dc244f2008-05-19 14:09:12 +010071 spin_lock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090072 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
73 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
74 &txx9wdt_reg->tcr);
Alan Cox8dc244f2008-05-19 14:09:12 +010075 spin_unlock(&txx9_lock);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090076 return 0;
77}
78
Axel Lind6245842012-03-16 11:53:53 +080079static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
80 unsigned int new_timeout)
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090081{
Axel Lind6245842012-03-16 11:53:53 +080082 timeout = new_timeout;
83 txx9wdt_stop(wdt_dev);
84 txx9wdt_start(wdt_dev);
85 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090086}
87
Axel Lind6245842012-03-16 11:53:53 +080088static const struct watchdog_info txx9wdt_info = {
89 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
90 .identity = "Hardware Watchdog for TXx9",
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +090091};
92
Axel Lind6245842012-03-16 11:53:53 +080093static const struct watchdog_ops txx9wdt_ops = {
94 .owner = THIS_MODULE,
95 .start = txx9wdt_start,
96 .stop = txx9wdt_stop,
97 .ping = txx9wdt_ping,
98 .set_timeout = txx9wdt_set_timeout,
99};
100
101static struct watchdog_device txx9wdt = {
102 .info = &txx9wdt_info,
103 .ops = &txx9wdt_ops,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900104};
105
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900106static int __init txx9wdt_probe(struct platform_device *dev)
107{
108 struct resource *res;
109 int ret;
110
111 txx9_imclk = clk_get(NULL, "imbus_clk");
112 if (IS_ERR(txx9_imclk)) {
113 ret = PTR_ERR(txx9_imclk);
114 txx9_imclk = NULL;
115 goto exit;
116 }
117 ret = clk_enable(txx9_imclk);
118 if (ret) {
119 clk_put(txx9_imclk);
120 txx9_imclk = NULL;
121 goto exit;
122 }
123
124 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Axel Lind6245842012-03-16 11:53:53 +0800125 txx9wdt_reg = devm_request_and_ioremap(&dev->dev, res);
126 if (!txx9wdt_reg) {
127 ret = -EBUSY;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900128 goto exit;
129 }
130
Axel Lind6245842012-03-16 11:53:53 +0800131 txx9wdt.min_timeout = 1;
132 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
133 watchdog_set_nowayout(&txx9wdt, nowayout);
134
135 ret = watchdog_register_device(&txx9wdt);
136 if (ret)
137 goto exit;
138
Joe Perches27c766a2012-02-15 15:06:19 -0800139 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
140 timeout, WD_MAX_TIMEOUT, nowayout);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900141
142 return 0;
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900143exit:
144 if (txx9_imclk) {
145 clk_disable(txx9_imclk);
146 clk_put(txx9_imclk);
147 }
148 return ret;
149}
150
151static int __exit txx9wdt_remove(struct platform_device *dev)
152{
Axel Lind6245842012-03-16 11:53:53 +0800153 watchdog_unregister_device(&txx9wdt);
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900154 clk_disable(txx9_imclk);
155 clk_put(txx9_imclk);
156 return 0;
157}
158
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000159static void txx9wdt_shutdown(struct platform_device *dev)
160{
Axel Lind6245842012-03-16 11:53:53 +0800161 txx9wdt_stop(&txx9wdt);
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000162}
163
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900164static struct platform_driver txx9wdt_driver = {
165 .remove = __exit_p(txx9wdt_remove),
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000166 .shutdown = txx9wdt_shutdown,
Atsushi Nemoto6f702fc2007-11-12 01:32:17 +0900167 .driver = {
168 .name = "txx9wdt",
169 .owner = THIS_MODULE,
170 },
171};
172
173static int __init watchdog_init(void)
174{
175 return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe);
176}
177
178static void __exit watchdog_exit(void)
179{
180 platform_driver_unregister(&txx9wdt_driver);
181}
182
183module_init(watchdog_init);
184module_exit(watchdog_exit);
185
186MODULE_DESCRIPTION("TXx9 Watchdog Driver");
187MODULE_LICENSE("GPL");
188MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700189MODULE_ALIAS("platform:txx9wdt");