]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/watchdog/orion_wdt.c
Merge branch 'marvell/dt' into late2/dt
[mirror_ubuntu-artful-kernel.git] / drivers / watchdog / orion_wdt.c
1 /*
2 * drivers/watchdog/orion_wdt.c
3 *
4 * Watchdog driver for Orion/Kirkwood processors
5 *
6 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/fs.h>
20 #include <linux/miscdevice.h>
21 #include <linux/platform_device.h>
22 #include <linux/watchdog.h>
23 #include <linux/init.h>
24 #include <linux/uaccess.h>
25 #include <linux/io.h>
26 #include <linux/spinlock.h>
27 #include <linux/clk.h>
28 #include <linux/of.h>
29 #include <mach/bridge-regs.h>
30
31 /*
32 * Watchdog timer block registers.
33 */
34 #define TIMER_CTRL 0x0000
35 #define WDT_EN 0x0010
36 #define WDT_VAL 0x0024
37
38 #define WDT_MAX_CYCLE_COUNT 0xffffffff
39 #define WDT_IN_USE 0
40 #define WDT_OK_TO_CLOSE 1
41
42 static bool nowayout = WATCHDOG_NOWAYOUT;
43 static int heartbeat = -1; /* module parameter (seconds) */
44 static unsigned int wdt_max_duration; /* (seconds) */
45 static struct clk *clk;
46 static unsigned int wdt_tclk;
47 static void __iomem *wdt_reg;
48 static unsigned long wdt_status;
49 static DEFINE_SPINLOCK(wdt_lock);
50
51 static void orion_wdt_ping(void)
52 {
53 spin_lock(&wdt_lock);
54
55 /* Reload watchdog duration */
56 writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
57
58 spin_unlock(&wdt_lock);
59 }
60
61 static void orion_wdt_enable(void)
62 {
63 u32 reg;
64
65 spin_lock(&wdt_lock);
66
67 /* Set watchdog duration */
68 writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
69
70 /* Clear watchdog timer interrupt */
71 reg = readl(BRIDGE_CAUSE);
72 reg &= ~WDT_INT_REQ;
73 writel(reg, BRIDGE_CAUSE);
74
75 /* Enable watchdog timer */
76 reg = readl(wdt_reg + TIMER_CTRL);
77 reg |= WDT_EN;
78 writel(reg, wdt_reg + TIMER_CTRL);
79
80 /* Enable reset on watchdog */
81 reg = readl(RSTOUTn_MASK);
82 reg |= WDT_RESET_OUT_EN;
83 writel(reg, RSTOUTn_MASK);
84
85 spin_unlock(&wdt_lock);
86 }
87
88 static void orion_wdt_disable(void)
89 {
90 u32 reg;
91
92 spin_lock(&wdt_lock);
93
94 /* Disable reset on watchdog */
95 reg = readl(RSTOUTn_MASK);
96 reg &= ~WDT_RESET_OUT_EN;
97 writel(reg, RSTOUTn_MASK);
98
99 /* Disable watchdog timer */
100 reg = readl(wdt_reg + TIMER_CTRL);
101 reg &= ~WDT_EN;
102 writel(reg, wdt_reg + TIMER_CTRL);
103
104 spin_unlock(&wdt_lock);
105 }
106
107 static int orion_wdt_get_timeleft(int *time_left)
108 {
109 spin_lock(&wdt_lock);
110 *time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
111 spin_unlock(&wdt_lock);
112 return 0;
113 }
114
115 static int orion_wdt_open(struct inode *inode, struct file *file)
116 {
117 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
118 return -EBUSY;
119 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
120 orion_wdt_enable();
121 return nonseekable_open(inode, file);
122 }
123
124 static ssize_t orion_wdt_write(struct file *file, const char *data,
125 size_t len, loff_t *ppos)
126 {
127 if (len) {
128 if (!nowayout) {
129 size_t i;
130
131 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
132 for (i = 0; i != len; i++) {
133 char c;
134
135 if (get_user(c, data + i))
136 return -EFAULT;
137 if (c == 'V')
138 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
139 }
140 }
141 orion_wdt_ping();
142 }
143 return len;
144 }
145
146 static int orion_wdt_settimeout(int new_time)
147 {
148 if ((new_time <= 0) || (new_time > wdt_max_duration))
149 return -EINVAL;
150
151 /* Set new watchdog time to be used when
152 * orion_wdt_enable() or orion_wdt_ping() is called. */
153 heartbeat = new_time;
154 return 0;
155 }
156
157 static const struct watchdog_info ident = {
158 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
159 WDIOF_KEEPALIVEPING,
160 .identity = "Orion Watchdog",
161 };
162
163 static long orion_wdt_ioctl(struct file *file, unsigned int cmd,
164 unsigned long arg)
165 {
166 int ret = -ENOTTY;
167 int time;
168
169 switch (cmd) {
170 case WDIOC_GETSUPPORT:
171 ret = copy_to_user((struct watchdog_info *)arg, &ident,
172 sizeof(ident)) ? -EFAULT : 0;
173 break;
174
175 case WDIOC_GETSTATUS:
176 case WDIOC_GETBOOTSTATUS:
177 ret = put_user(0, (int *)arg);
178 break;
179
180 case WDIOC_KEEPALIVE:
181 orion_wdt_ping();
182 ret = 0;
183 break;
184
185 case WDIOC_SETTIMEOUT:
186 ret = get_user(time, (int *)arg);
187 if (ret)
188 break;
189
190 if (orion_wdt_settimeout(time)) {
191 ret = -EINVAL;
192 break;
193 }
194 orion_wdt_ping();
195 /* Fall through */
196
197 case WDIOC_GETTIMEOUT:
198 ret = put_user(heartbeat, (int *)arg);
199 break;
200
201 case WDIOC_GETTIMELEFT:
202 if (orion_wdt_get_timeleft(&time)) {
203 ret = -EINVAL;
204 break;
205 }
206 ret = put_user(time, (int *)arg);
207 break;
208 }
209 return ret;
210 }
211
212 static int orion_wdt_release(struct inode *inode, struct file *file)
213 {
214 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
215 orion_wdt_disable();
216 else
217 pr_crit("Device closed unexpectedly - timer will not stop\n");
218 clear_bit(WDT_IN_USE, &wdt_status);
219 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
220
221 return 0;
222 }
223
224
225 static const struct file_operations orion_wdt_fops = {
226 .owner = THIS_MODULE,
227 .llseek = no_llseek,
228 .write = orion_wdt_write,
229 .unlocked_ioctl = orion_wdt_ioctl,
230 .open = orion_wdt_open,
231 .release = orion_wdt_release,
232 };
233
234 static struct miscdevice orion_wdt_miscdev = {
235 .minor = WATCHDOG_MINOR,
236 .name = "watchdog",
237 .fops = &orion_wdt_fops,
238 };
239
240 static int __devinit orion_wdt_probe(struct platform_device *pdev)
241 {
242 struct resource *res;
243 int ret;
244
245 clk = clk_get(&pdev->dev, NULL);
246 if (IS_ERR(clk)) {
247 printk(KERN_ERR "Orion Watchdog missing clock\n");
248 return -ENODEV;
249 }
250 clk_prepare_enable(clk);
251 wdt_tclk = clk_get_rate(clk);
252
253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
254
255 wdt_reg = ioremap(res->start, resource_size(res));
256
257 if (orion_wdt_miscdev.parent)
258 return -EBUSY;
259 orion_wdt_miscdev.parent = &pdev->dev;
260
261 wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
262 if (orion_wdt_settimeout(heartbeat))
263 heartbeat = wdt_max_duration;
264
265 ret = misc_register(&orion_wdt_miscdev);
266 if (ret)
267 return ret;
268
269 pr_info("Initial timeout %d sec%s\n",
270 heartbeat, nowayout ? ", nowayout" : "");
271 return 0;
272 }
273
274 static int __devexit orion_wdt_remove(struct platform_device *pdev)
275 {
276 int ret;
277
278 if (test_bit(WDT_IN_USE, &wdt_status)) {
279 orion_wdt_disable();
280 clear_bit(WDT_IN_USE, &wdt_status);
281 }
282
283 ret = misc_deregister(&orion_wdt_miscdev);
284 if (!ret)
285 orion_wdt_miscdev.parent = NULL;
286
287 clk_disable_unprepare(clk);
288 clk_put(clk);
289
290 return ret;
291 }
292
293 static void orion_wdt_shutdown(struct platform_device *pdev)
294 {
295 if (test_bit(WDT_IN_USE, &wdt_status))
296 orion_wdt_disable();
297 }
298
299 static const struct of_device_id orion_wdt_of_match_table[] __devinitdata = {
300 { .compatible = "marvell,orion-wdt", },
301 {},
302 };
303 MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);
304
305 static struct platform_driver orion_wdt_driver = {
306 .probe = orion_wdt_probe,
307 .remove = __devexit_p(orion_wdt_remove),
308 .shutdown = orion_wdt_shutdown,
309 .driver = {
310 .owner = THIS_MODULE,
311 .name = "orion_wdt",
312 .of_match_table = of_match_ptr(orion_wdt_of_match_table),
313 },
314 };
315
316 module_platform_driver(orion_wdt_driver);
317
318 MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
319 MODULE_DESCRIPTION("Orion Processor Watchdog");
320
321 module_param(heartbeat, int, 0);
322 MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
323
324 module_param(nowayout, bool, 0);
325 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
326 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
327
328 MODULE_LICENSE("GPL");
329 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);