blob: 639ae9a23fbc3361da6720f4b94c8e16af39c0e1 [file] [log] [blame]
Matteo Crocec283cf22007-09-20 18:06:41 +02001/*
2 * drivers/watchdog/ar7_wdt.c
3 *
4 * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
5 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
6 *
7 * Some code taken from:
8 * National Semiconductor SCx200 Watchdog support
9 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
Joe Perches27c766a2012-02-15 15:06:19 -080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Matteo Crocec283cf22007-09-20 18:06:41 +020028#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/errno.h>
31#include <linux/init.h>
32#include <linux/miscdevice.h>
Florian Fainelli64d40622009-07-21 12:11:32 +020033#include <linux/platform_device.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020034#include <linux/watchdog.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020035#include <linux/fs.h>
36#include <linux/ioport.h>
37#include <linux/io.h>
38#include <linux/uaccess.h>
Florian Fainelli780019d2010-01-27 09:10:06 +010039#include <linux/clk.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020040
41#include <asm/addrspace.h>
Florian Fainellic5e7f5a2009-06-03 13:05:48 +020042#include <asm/mach-ar7/ar7.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020043
Matteo Crocec283cf22007-09-20 18:06:41 +020044#define LONGNAME "TI AR7 Watchdog Timer"
45
46MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
47MODULE_DESCRIPTION(LONGNAME);
48MODULE_LICENSE("GPL");
49MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
50
51static int margin = 60;
52module_param(margin, int, 0);
53MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
54
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010055static bool nowayout = WATCHDOG_NOWAYOUT;
56module_param(nowayout, bool, 0);
Matteo Crocec283cf22007-09-20 18:06:41 +020057MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
58
59#define READ_REG(x) readl((void __iomem *)&(x))
60#define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
61
62struct ar7_wdt {
63 u32 kick_lock;
64 u32 kick;
65 u32 change_lock;
66 u32 change;
67 u32 disable_lock;
68 u32 disable;
69 u32 prescale_lock;
70 u32 prescale;
71};
72
Alan Cox670d59c2008-08-04 17:53:22 +010073static unsigned long wdt_is_open;
Matteo Crocec283cf22007-09-20 18:06:41 +020074static unsigned expect_close;
Axel Lin1334f322011-11-29 13:54:01 +080075static DEFINE_SPINLOCK(wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +020076
77/* XXX currently fixed, allows max margin ~68.72 secs */
78#define prescale_value 0xffff
79
Florian Fainelli64d40622009-07-21 12:11:32 +020080/* Resource of the WDT registers */
81static struct resource *ar7_regs_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020082/* Pointer to the remapped WDT IO space */
83static struct ar7_wdt *ar7_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020084
Florian Fainelli780019d2010-01-27 09:10:06 +010085static struct clk *vbus_clk;
86
Matteo Crocec283cf22007-09-20 18:06:41 +020087static void ar7_wdt_kick(u32 value)
88{
89 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
90 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
91 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
92 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
93 WRITE_REG(ar7_wdt->kick, value);
94 return;
95 }
96 }
Joe Perches27c766a2012-02-15 15:06:19 -080097 pr_err("failed to unlock WDT kick reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +020098}
99
100static void ar7_wdt_prescale(u32 value)
101{
102 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
103 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
104 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
105 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
106 WRITE_REG(ar7_wdt->prescale, value);
107 return;
108 }
109 }
Joe Perches27c766a2012-02-15 15:06:19 -0800110 pr_err("failed to unlock WDT prescale reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200111}
112
113static void ar7_wdt_change(u32 value)
114{
115 WRITE_REG(ar7_wdt->change_lock, 0x6666);
116 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
117 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
118 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
119 WRITE_REG(ar7_wdt->change, value);
120 return;
121 }
122 }
Joe Perches27c766a2012-02-15 15:06:19 -0800123 pr_err("failed to unlock WDT change reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200124}
125
126static void ar7_wdt_disable(u32 value)
127{
128 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
129 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
130 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
131 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
132 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
133 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
134 WRITE_REG(ar7_wdt->disable, value);
135 return;
136 }
137 }
138 }
Joe Perches27c766a2012-02-15 15:06:19 -0800139 pr_err("failed to unlock WDT disable reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200140}
141
142static void ar7_wdt_update_margin(int new_margin)
143{
144 u32 change;
Florian Fainelli780019d2010-01-27 09:10:06 +0100145 u32 vbus_rate;
Matteo Crocec283cf22007-09-20 18:06:41 +0200146
Florian Fainelli780019d2010-01-27 09:10:06 +0100147 vbus_rate = clk_get_rate(vbus_clk);
148 change = new_margin * (vbus_rate / prescale_value);
Alan Cox670d59c2008-08-04 17:53:22 +0100149 if (change < 1)
150 change = 1;
151 if (change > 0xffff)
152 change = 0xffff;
Matteo Crocec283cf22007-09-20 18:06:41 +0200153 ar7_wdt_change(change);
Florian Fainelli780019d2010-01-27 09:10:06 +0100154 margin = change * prescale_value / vbus_rate;
Joe Perches27c766a2012-02-15 15:06:19 -0800155 pr_info("timer margin %d seconds (prescale %d, change %d, freq %d)\n",
156 margin, prescale_value, change, vbus_rate);
Matteo Crocec283cf22007-09-20 18:06:41 +0200157}
158
159static void ar7_wdt_enable_wdt(void)
160{
Joe Perches27c766a2012-02-15 15:06:19 -0800161 pr_debug("enabling watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200162 ar7_wdt_disable(1);
163 ar7_wdt_kick(1);
164}
165
166static void ar7_wdt_disable_wdt(void)
167{
Joe Perches27c766a2012-02-15 15:06:19 -0800168 pr_debug("disabling watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200169 ar7_wdt_disable(0);
170}
171
172static int ar7_wdt_open(struct inode *inode, struct file *file)
173{
174 /* only allow one at a time */
Alan Cox670d59c2008-08-04 17:53:22 +0100175 if (test_and_set_bit(0, &wdt_is_open))
Matteo Crocec283cf22007-09-20 18:06:41 +0200176 return -EBUSY;
177 ar7_wdt_enable_wdt();
178 expect_close = 0;
179
180 return nonseekable_open(inode, file);
181}
182
183static int ar7_wdt_release(struct inode *inode, struct file *file)
184{
185 if (!expect_close)
Joe Perches27c766a2012-02-15 15:06:19 -0800186 pr_warn("watchdog device closed unexpectedly, will not disable the watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200187 else if (!nowayout)
188 ar7_wdt_disable_wdt();
Alan Cox670d59c2008-08-04 17:53:22 +0100189 clear_bit(0, &wdt_is_open);
Matteo Crocec283cf22007-09-20 18:06:41 +0200190 return 0;
191}
192
Matteo Crocec283cf22007-09-20 18:06:41 +0200193static ssize_t ar7_wdt_write(struct file *file, const char *data,
194 size_t len, loff_t *ppos)
195{
196 /* check for a magic close character */
197 if (len) {
198 size_t i;
199
Alan Cox670d59c2008-08-04 17:53:22 +0100200 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200201 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100202 spin_unlock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200203
204 expect_close = 0;
205 for (i = 0; i < len; ++i) {
206 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000207 if (get_user(c, data + i))
Matteo Crocec283cf22007-09-20 18:06:41 +0200208 return -EFAULT;
209 if (c == 'V')
210 expect_close = 1;
211 }
212
213 }
214 return len;
215}
216
Alan Cox670d59c2008-08-04 17:53:22 +0100217static long ar7_wdt_ioctl(struct file *file,
218 unsigned int cmd, unsigned long arg)
Matteo Crocec283cf22007-09-20 18:06:41 +0200219{
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000220 static const struct watchdog_info ident = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200221 .identity = LONGNAME,
222 .firmware_version = 1,
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000223 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
224 WDIOF_MAGICCLOSE),
Matteo Crocec283cf22007-09-20 18:06:41 +0200225 };
226 int new_margin;
227
228 switch (cmd) {
Matteo Crocec283cf22007-09-20 18:06:41 +0200229 case WDIOC_GETSUPPORT:
230 if (copy_to_user((struct watchdog_info *)arg, &ident,
231 sizeof(ident)))
232 return -EFAULT;
233 return 0;
234 case WDIOC_GETSTATUS:
235 case WDIOC_GETBOOTSTATUS:
236 if (put_user(0, (int *)arg))
237 return -EFAULT;
238 return 0;
239 case WDIOC_KEEPALIVE:
240 ar7_wdt_kick(1);
241 return 0;
242 case WDIOC_SETTIMEOUT:
243 if (get_user(new_margin, (int *)arg))
244 return -EFAULT;
245 if (new_margin < 1)
246 return -EINVAL;
247
Alan Cox670d59c2008-08-04 17:53:22 +0100248 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200249 ar7_wdt_update_margin(new_margin);
250 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100251 spin_unlock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200252
253 case WDIOC_GETTIMEOUT:
254 if (put_user(margin, (int *)arg))
255 return -EFAULT;
256 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000257 default:
258 return -ENOTTY;
Matteo Crocec283cf22007-09-20 18:06:41 +0200259 }
260}
261
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100262static const struct file_operations ar7_wdt_fops = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200263 .owner = THIS_MODULE,
264 .write = ar7_wdt_write,
Alan Cox670d59c2008-08-04 17:53:22 +0100265 .unlocked_ioctl = ar7_wdt_ioctl,
Matteo Crocec283cf22007-09-20 18:06:41 +0200266 .open = ar7_wdt_open,
267 .release = ar7_wdt_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200268 .llseek = no_llseek,
Matteo Crocec283cf22007-09-20 18:06:41 +0200269};
270
271static struct miscdevice ar7_wdt_miscdev = {
272 .minor = WATCHDOG_MINOR,
273 .name = "watchdog",
274 .fops = &ar7_wdt_fops,
275};
276
Florian Fainelli64d40622009-07-21 12:11:32 +0200277static int __devinit ar7_wdt_probe(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200278{
279 int rc;
280
Florian Fainelli64d40622009-07-21 12:11:32 +0200281 ar7_regs_wdt =
282 platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
283 if (!ar7_regs_wdt) {
Joe Perches27c766a2012-02-15 15:06:19 -0800284 pr_err("could not get registers resource\n");
Florian Fainelli64d40622009-07-21 12:11:32 +0200285 rc = -ENODEV;
286 goto out;
Matteo Crocec283cf22007-09-20 18:06:41 +0200287 }
288
Florian Fainelli64d40622009-07-21 12:11:32 +0200289 if (!request_mem_region(ar7_regs_wdt->start,
290 resource_size(ar7_regs_wdt), LONGNAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800291 pr_warn("watchdog I/O region busy\n");
Florian Fainelli64d40622009-07-21 12:11:32 +0200292 rc = -EBUSY;
293 goto out;
294 }
295
296 ar7_wdt = ioremap(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
297 if (!ar7_wdt) {
Joe Perches27c766a2012-02-15 15:06:19 -0800298 pr_err("could not ioremap registers\n");
Florian Fainelli64d40622009-07-21 12:11:32 +0200299 rc = -ENXIO;
Wim Van Sebroeckd7e97912009-08-31 13:49:14 +0000300 goto out_mem_region;
Florian Fainelli64d40622009-07-21 12:11:32 +0200301 }
Matteo Crocec283cf22007-09-20 18:06:41 +0200302
Florian Fainelli780019d2010-01-27 09:10:06 +0100303 vbus_clk = clk_get(NULL, "vbus");
304 if (IS_ERR(vbus_clk)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800305 pr_err("could not get vbus clock\n");
Florian Fainelli780019d2010-01-27 09:10:06 +0100306 rc = PTR_ERR(vbus_clk);
307 goto out_mem_region;
308 }
309
Matteo Crocec283cf22007-09-20 18:06:41 +0200310 ar7_wdt_disable_wdt();
311 ar7_wdt_prescale(prescale_value);
312 ar7_wdt_update_margin(margin);
313
Matteo Crocec283cf22007-09-20 18:06:41 +0200314 rc = misc_register(&ar7_wdt_miscdev);
315 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800316 pr_err("unable to register misc device\n");
Florian Fainelli64d40622009-07-21 12:11:32 +0200317 goto out_alloc;
Matteo Crocec283cf22007-09-20 18:06:41 +0200318 }
319 goto out;
320
Matteo Crocec283cf22007-09-20 18:06:41 +0200321out_alloc:
322 iounmap(ar7_wdt);
Wim Van Sebroeckd7e97912009-08-31 13:49:14 +0000323out_mem_region:
Florian Fainelli64d40622009-07-21 12:11:32 +0200324 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
Matteo Crocec283cf22007-09-20 18:06:41 +0200325out:
326 return rc;
327}
328
Florian Fainelli64d40622009-07-21 12:11:32 +0200329static int __devexit ar7_wdt_remove(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200330{
331 misc_deregister(&ar7_wdt_miscdev);
Matteo Crocec283cf22007-09-20 18:06:41 +0200332 iounmap(ar7_wdt);
Florian Fainelli64d40622009-07-21 12:11:32 +0200333 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
334
335 return 0;
336}
337
338static void ar7_wdt_shutdown(struct platform_device *pdev)
339{
340 if (!nowayout)
341 ar7_wdt_disable_wdt();
342}
343
344static struct platform_driver ar7_wdt_driver = {
345 .probe = ar7_wdt_probe,
346 .remove = __devexit_p(ar7_wdt_remove),
347 .shutdown = ar7_wdt_shutdown,
348 .driver = {
349 .owner = THIS_MODULE,
350 .name = "ar7_wdt",
351 },
352};
353
Axel Linb8ec6112011-11-29 13:56:27 +0800354module_platform_driver(ar7_wdt_driver);