Commit | Line | Data |
---|---|---|
6f702fce AN |
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 | */ | |
27c766aa JP |
10 | |
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
6f702fce AN |
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> | |
6f702fce | 18 | #include <linux/init.h> |
6f702fce AN |
19 | #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 | ||
27 | static int timeout = TIMER_MARGIN; /* in seconds */ | |
28 | module_param(timeout, int, 0); | |
29 | MODULE_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 | ||
86a1e189 WVS |
34 | static bool nowayout = WATCHDOG_NOWAYOUT; |
35 | module_param(nowayout, bool, 0); | |
6f702fce AN |
36 | MODULE_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 | ||
6f702fce AN |
44 | static struct txx9_tmr_reg __iomem *txx9wdt_reg; |
45 | static struct clk *txx9_imclk; | |
f8494e06 | 46 | static DEFINE_SPINLOCK(txx9_lock); |
6f702fce | 47 | |
d6245842 | 48 | static int txx9wdt_ping(struct watchdog_device *wdt_dev) |
6f702fce | 49 | { |
8dc244f7 | 50 | spin_lock(&txx9_lock); |
6f702fce | 51 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); |
8dc244f7 | 52 | spin_unlock(&txx9_lock); |
d6245842 | 53 | return 0; |
6f702fce AN |
54 | } |
55 | ||
d6245842 | 56 | static int txx9wdt_start(struct watchdog_device *wdt_dev) |
6f702fce | 57 | { |
8dc244f7 | 58 | spin_lock(&txx9_lock); |
6f702fce AN |
59 | __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); | |
8dc244f7 | 65 | spin_unlock(&txx9_lock); |
d6245842 | 66 | return 0; |
6f702fce AN |
67 | } |
68 | ||
d6245842 | 69 | static int txx9wdt_stop(struct watchdog_device *wdt_dev) |
6f702fce | 70 | { |
8dc244f7 | 71 | spin_lock(&txx9_lock); |
6f702fce AN |
72 | __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr); |
73 | __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE, | |
74 | &txx9wdt_reg->tcr); | |
8dc244f7 | 75 | spin_unlock(&txx9_lock); |
6f702fce AN |
76 | return 0; |
77 | } | |
78 | ||
d6245842 AL |
79 | static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev, |
80 | unsigned int new_timeout) | |
6f702fce | 81 | { |
d6245842 AL |
82 | timeout = new_timeout; |
83 | txx9wdt_stop(wdt_dev); | |
84 | txx9wdt_start(wdt_dev); | |
85 | return 0; | |
6f702fce AN |
86 | } |
87 | ||
d6245842 AL |
88 | static const struct watchdog_info txx9wdt_info = { |
89 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, | |
90 | .identity = "Hardware Watchdog for TXx9", | |
91 | }; | |
6f702fce | 92 | |
d6245842 AL |
93 | static 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, | |
6f702fce AN |
99 | }; |
100 | ||
d6245842 AL |
101 | static struct watchdog_device txx9wdt = { |
102 | .info = &txx9wdt_info, | |
103 | .ops = &txx9wdt_ops, | |
6f702fce AN |
104 | }; |
105 | ||
6f702fce AN |
106 | static 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); | |
d6245842 AL |
125 | txx9wdt_reg = devm_request_and_ioremap(&dev->dev, res); |
126 | if (!txx9wdt_reg) { | |
127 | ret = -EBUSY; | |
6f702fce AN |
128 | goto exit; |
129 | } | |
130 | ||
d6245842 AL |
131 | 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 | ||
27c766aa JP |
139 | pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n", |
140 | timeout, WD_MAX_TIMEOUT, nowayout); | |
6f702fce AN |
141 | |
142 | return 0; | |
6f702fce AN |
143 | exit: |
144 | if (txx9_imclk) { | |
145 | clk_disable(txx9_imclk); | |
146 | clk_put(txx9_imclk); | |
147 | } | |
148 | return ret; | |
149 | } | |
150 | ||
151 | static int __exit txx9wdt_remove(struct platform_device *dev) | |
152 | { | |
d6245842 | 153 | watchdog_unregister_device(&txx9wdt); |
6f702fce AN |
154 | clk_disable(txx9_imclk); |
155 | clk_put(txx9_imclk); | |
156 | return 0; | |
157 | } | |
158 | ||
168b5251 WVS |
159 | static void txx9wdt_shutdown(struct platform_device *dev) |
160 | { | |
d6245842 | 161 | txx9wdt_stop(&txx9wdt); |
168b5251 WVS |
162 | } |
163 | ||
6f702fce AN |
164 | static struct platform_driver txx9wdt_driver = { |
165 | .remove = __exit_p(txx9wdt_remove), | |
168b5251 | 166 | .shutdown = txx9wdt_shutdown, |
6f702fce AN |
167 | .driver = { |
168 | .name = "txx9wdt", | |
169 | .owner = THIS_MODULE, | |
170 | }, | |
171 | }; | |
172 | ||
173 | static int __init watchdog_init(void) | |
174 | { | |
175 | return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe); | |
176 | } | |
177 | ||
178 | static void __exit watchdog_exit(void) | |
179 | { | |
180 | platform_driver_unregister(&txx9wdt_driver); | |
181 | } | |
182 | ||
183 | module_init(watchdog_init); | |
184 | module_exit(watchdog_exit); | |
185 | ||
186 | MODULE_DESCRIPTION("TXx9 Watchdog Driver"); | |
187 | MODULE_LICENSE("GPL"); | |
188 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | |
f37d193c | 189 | MODULE_ALIAS("platform:txx9wdt"); |