blob: 8965e3f536c3572516053c522eb1d4208309988a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Watchdog driver for the SA11x0/PXA2xx
3 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00004 * (c) Copyright 2000 Oleg Drokin <green@crimea.edu>
5 * Based on SoftDog driver by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Neither Oleg Drokin nor iXcelerator.com admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
15 *
16 * (c) Copyright 2000 Oleg Drokin <green@crimea.edu>
17 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000018 * 27/11/2000 Initial release
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
Joe Perches27c766a2012-02-15 15:06:19 -080020
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/moduleparam.h>
Robert Jarzmik69240892016-09-19 21:12:14 +020025#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/fs.h>
29#include <linux/miscdevice.h>
30#include <linux/watchdog.h>
31#include <linux/init.h>
Rob Herring23019a72012-03-20 14:33:19 -050032#include <linux/io.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070033#include <linux/bitops.h>
Alan Coxf19e0312008-05-19 14:08:05 +010034#include <linux/uaccess.h>
Russell King87c52572008-11-29 17:35:51 +000035#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#ifdef CONFIG_ARCH_PXA
Eric Miao5bf3df32009-01-20 11:04:16 +080038#include <mach/regs-ost.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#endif
40
Russell Kingafd2fc02008-08-07 11:05:25 +010041#include <mach/reset.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010042#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Eric Miao28a62382008-12-24 11:32:45 +080044static unsigned long oscr_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static unsigned long sa1100wdt_users;
Raphael Assenata6f052e2009-06-29 13:56:52 -040046static unsigned int pre_margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int boot_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Allow only one person to hold it open
51 */
52static int sa1100dog_open(struct inode *inode, struct file *file)
53{
Alan Coxf19e0312008-05-19 14:08:05 +010054 if (test_and_set_bit(1, &sa1100wdt_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -EBUSY;
56
57 /* Activate SA1100 Watchdog timer */
Russell King31696632012-06-06 11:42:36 +010058 writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
59 writel_relaxed(OSSR_M3, OSSR);
60 writel_relaxed(OWER_WME, OWER);
61 writel_relaxed(readl_relaxed(OIER) | OIER_E3, OIER);
Wim Van Sebroeck6abe78b2007-07-24 21:38:37 +000062 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65/*
Ian Campbell9bbd0372005-08-03 20:34:52 +010066 * The watchdog cannot be disabled.
67 *
68 * Previous comments suggested that turning off the interrupt by
69 * clearing OIER[E3] would prevent the watchdog timing out but this
70 * does not appear to be true (at least on the PXA255).
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72static int sa1100dog_release(struct inode *inode, struct file *file)
73{
Joe Perches27c766a2012-02-15 15:06:19 -080074 pr_crit("Device closed - timer will not stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 clear_bit(1, &sa1100wdt_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77}
78
Alan Coxf19e0312008-05-19 14:08:05 +010079static ssize_t sa1100dog_write(struct file *file, const char __user *data,
80 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Ian Campbell9bbd0372005-08-03 20:34:52 +010082 if (len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* Refresh OSMR3 timer. */
Russell King31696632012-06-06 11:42:36 +010084 writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return len;
86}
87
Alan Coxf19e0312008-05-19 14:08:05 +010088static const struct watchdog_info ident = {
89 .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT
90 | WDIOF_KEEPALIVEPING,
Ian Campbell9bbd0372005-08-03 20:34:52 +010091 .identity = "SA1100/PXA255 Watchdog",
Raphael Assenata6f052e2009-06-29 13:56:52 -040092 .firmware_version = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
Alan Coxf19e0312008-05-19 14:08:05 +010095static long sa1100dog_ioctl(struct file *file, unsigned int cmd,
96 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Samuel Tardieu795b89d2006-09-09 17:34:31 +020098 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int time;
Ian Campbell3a69e572005-11-07 10:21:24 +0000100 void __user *argp = (void __user *)arg;
101 int __user *p = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 switch (cmd) {
104 case WDIOC_GETSUPPORT:
Ian Campbell3a69e572005-11-07 10:21:24 +0000105 ret = copy_to_user(argp, &ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 sizeof(ident)) ? -EFAULT : 0;
107 break;
108
109 case WDIOC_GETSTATUS:
Ian Campbell3a69e572005-11-07 10:21:24 +0000110 ret = put_user(0, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 break;
112
113 case WDIOC_GETBOOTSTATUS:
Ian Campbell3a69e572005-11-07 10:21:24 +0000114 ret = put_user(boot_status, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
116
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000117 case WDIOC_KEEPALIVE:
Russell King31696632012-06-06 11:42:36 +0100118 writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000119 ret = 0;
120 break;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 case WDIOC_SETTIMEOUT:
Ian Campbell3a69e572005-11-07 10:21:24 +0000123 ret = get_user(time, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (ret)
125 break;
126
Raphael Assenata6f052e2009-06-29 13:56:52 -0400127 if (time <= 0 || (oscr_freq * (long long)time >= 0xffffffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ret = -EINVAL;
129 break;
130 }
131
Eric Miao28a62382008-12-24 11:32:45 +0800132 pre_margin = oscr_freq * time;
Russell King31696632012-06-06 11:42:36 +0100133 writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*fall through*/
135
136 case WDIOC_GETTIMEOUT:
Eric Miao28a62382008-12-24 11:32:45 +0800137 ret = put_user(pre_margin / oscr_freq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 return ret;
141}
142
Alan Coxf19e0312008-05-19 14:08:05 +0100143static const struct file_operations sa1100dog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .owner = THIS_MODULE,
145 .llseek = no_llseek,
146 .write = sa1100dog_write,
Alan Coxf19e0312008-05-19 14:08:05 +0100147 .unlocked_ioctl = sa1100dog_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .open = sa1100dog_open,
149 .release = sa1100dog_release,
150};
151
Alan Coxf19e0312008-05-19 14:08:05 +0100152static struct miscdevice sa1100dog_miscdev = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .minor = WATCHDOG_MINOR,
Ian Campbell9bbd0372005-08-03 20:34:52 +0100154 .name = "watchdog",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .fops = &sa1100dog_fops,
156};
157
158static int margin __initdata = 60; /* (secs) Default is 1 minute */
Robert Jarzmik69240892016-09-19 21:12:14 +0200159static struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161static int __init sa1100dog_init(void)
162{
163 int ret;
164
Robert Jarzmik69240892016-09-19 21:12:14 +0200165 clk = clk_get(NULL, "OSTIMER0");
166 if (IS_ERR(clk)) {
167 pr_err("SA1100/PXA2xx Watchdog Timer: clock not found: %d\n",
168 (int) PTR_ERR(clk));
169 return PTR_ERR(clk);
170 }
171
172 ret = clk_prepare_enable(clk);
173 if (ret) {
174 pr_err("SA1100/PXA2xx Watchdog Timer: clock failed to prepare+enable: %d\n",
175 ret);
176 goto err;
177 }
178
179 oscr_freq = clk_get_rate(clk);
Eric Miao28a62382008-12-24 11:32:45 +0800180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Read the reset status, and save it for later. If
183 * we suspend, RCSR will be cleared, and the watchdog
184 * reset reason will be lost.
185 */
Eric Miao214c6a72008-07-29 14:39:34 +0800186 boot_status = (reset_status & RESET_STATUS_WATCHDOG) ?
187 WDIOF_CARDRESET : 0;
Eric Miao28a62382008-12-24 11:32:45 +0800188 pre_margin = oscr_freq * margin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 ret = misc_register(&sa1100dog_miscdev);
191 if (ret == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800192 pr_info("SA1100/PXA2xx Watchdog Timer: timer margin %d sec\n",
193 margin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return ret;
Robert Jarzmik69240892016-09-19 21:12:14 +0200195err:
196 clk_disable_unprepare(clk);
197 clk_put(clk);
198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static void __exit sa1100dog_exit(void)
202{
203 misc_deregister(&sa1100dog_miscdev);
Robert Jarzmik69240892016-09-19 21:12:14 +0200204 clk_disable_unprepare(clk);
205 clk_put(clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208module_init(sa1100dog_init);
209module_exit(sa1100dog_exit);
210
211MODULE_AUTHOR("Oleg Drokin <green@crimea.edu>");
212MODULE_DESCRIPTION("SA1100/PXA2xx Watchdog");
213
214module_param(margin, int, 0);
215MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217MODULE_LICENSE("GPL");