blob: c087027ffd5d5da7fbf059017f24d98356806a92 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Matteo Crocec283cf22007-09-20 18:06:41 +02002/*
3 * drivers/watchdog/ar7_wdt.c
4 *
5 * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
6 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
7 *
8 * Some code taken from:
9 * National Semiconductor SCx200 Watchdog support
10 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
11 *
Matteo Crocec283cf22007-09-20 18:06:41 +020012 */
13
Joe Perches27c766a2012-02-15 15:06:19 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Matteo Crocec283cf22007-09-20 18:06:41 +020016#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/errno.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020019#include <linux/miscdevice.h>
Florian Fainelli64d40622009-07-21 12:11:32 +020020#include <linux/platform_device.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020021#include <linux/watchdog.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020022#include <linux/fs.h>
23#include <linux/ioport.h>
24#include <linux/io.h>
25#include <linux/uaccess.h>
Florian Fainelli780019d2010-01-27 09:10:06 +010026#include <linux/clk.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020027
28#include <asm/addrspace.h>
Florian Fainellic5e7f5a2009-06-03 13:05:48 +020029#include <asm/mach-ar7/ar7.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020030
Matteo Crocec283cf22007-09-20 18:06:41 +020031#define LONGNAME "TI AR7 Watchdog Timer"
32
33MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
34MODULE_DESCRIPTION(LONGNAME);
35MODULE_LICENSE("GPL");
Matteo Crocec283cf22007-09-20 18:06:41 +020036
37static int margin = 60;
38module_param(margin, int, 0);
39MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
40
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010041static bool nowayout = WATCHDOG_NOWAYOUT;
42module_param(nowayout, bool, 0);
Matteo Crocec283cf22007-09-20 18:06:41 +020043MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
44
45#define READ_REG(x) readl((void __iomem *)&(x))
46#define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
47
48struct ar7_wdt {
49 u32 kick_lock;
50 u32 kick;
51 u32 change_lock;
52 u32 change;
53 u32 disable_lock;
54 u32 disable;
55 u32 prescale_lock;
56 u32 prescale;
57};
58
Alan Cox670d59c2008-08-04 17:53:22 +010059static unsigned long wdt_is_open;
Matteo Crocec283cf22007-09-20 18:06:41 +020060static unsigned expect_close;
Axel Lin1334f322011-11-29 13:54:01 +080061static DEFINE_SPINLOCK(wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +020062
63/* XXX currently fixed, allows max margin ~68.72 secs */
64#define prescale_value 0xffff
65
Florian Fainelli64d40622009-07-21 12:11:32 +020066/* Resource of the WDT registers */
67static struct resource *ar7_regs_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020068/* Pointer to the remapped WDT IO space */
69static struct ar7_wdt *ar7_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020070
Florian Fainelli780019d2010-01-27 09:10:06 +010071static struct clk *vbus_clk;
72
Matteo Crocec283cf22007-09-20 18:06:41 +020073static void ar7_wdt_kick(u32 value)
74{
75 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
76 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
77 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
78 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
79 WRITE_REG(ar7_wdt->kick, value);
80 return;
81 }
82 }
Joe Perches27c766a2012-02-15 15:06:19 -080083 pr_err("failed to unlock WDT kick reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +020084}
85
86static void ar7_wdt_prescale(u32 value)
87{
88 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
89 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
90 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
91 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
92 WRITE_REG(ar7_wdt->prescale, value);
93 return;
94 }
95 }
Joe Perches27c766a2012-02-15 15:06:19 -080096 pr_err("failed to unlock WDT prescale reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +020097}
98
99static void ar7_wdt_change(u32 value)
100{
101 WRITE_REG(ar7_wdt->change_lock, 0x6666);
102 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
103 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
104 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
105 WRITE_REG(ar7_wdt->change, value);
106 return;
107 }
108 }
Joe Perches27c766a2012-02-15 15:06:19 -0800109 pr_err("failed to unlock WDT change reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200110}
111
112static void ar7_wdt_disable(u32 value)
113{
114 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
115 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
116 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
117 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
118 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
119 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
120 WRITE_REG(ar7_wdt->disable, value);
121 return;
122 }
123 }
124 }
Joe Perches27c766a2012-02-15 15:06:19 -0800125 pr_err("failed to unlock WDT disable reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200126}
127
128static void ar7_wdt_update_margin(int new_margin)
129{
130 u32 change;
Florian Fainelli780019d2010-01-27 09:10:06 +0100131 u32 vbus_rate;
Matteo Crocec283cf22007-09-20 18:06:41 +0200132
Florian Fainelli780019d2010-01-27 09:10:06 +0100133 vbus_rate = clk_get_rate(vbus_clk);
134 change = new_margin * (vbus_rate / prescale_value);
Alan Cox670d59c2008-08-04 17:53:22 +0100135 if (change < 1)
136 change = 1;
137 if (change > 0xffff)
138 change = 0xffff;
Matteo Crocec283cf22007-09-20 18:06:41 +0200139 ar7_wdt_change(change);
Florian Fainelli780019d2010-01-27 09:10:06 +0100140 margin = change * prescale_value / vbus_rate;
Joe Perches27c766a2012-02-15 15:06:19 -0800141 pr_info("timer margin %d seconds (prescale %d, change %d, freq %d)\n",
142 margin, prescale_value, change, vbus_rate);
Matteo Crocec283cf22007-09-20 18:06:41 +0200143}
144
145static void ar7_wdt_enable_wdt(void)
146{
Joe Perches27c766a2012-02-15 15:06:19 -0800147 pr_debug("enabling watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200148 ar7_wdt_disable(1);
149 ar7_wdt_kick(1);
150}
151
152static void ar7_wdt_disable_wdt(void)
153{
Joe Perches27c766a2012-02-15 15:06:19 -0800154 pr_debug("disabling watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200155 ar7_wdt_disable(0);
156}
157
158static int ar7_wdt_open(struct inode *inode, struct file *file)
159{
160 /* only allow one at a time */
Alan Cox670d59c2008-08-04 17:53:22 +0100161 if (test_and_set_bit(0, &wdt_is_open))
Matteo Crocec283cf22007-09-20 18:06:41 +0200162 return -EBUSY;
163 ar7_wdt_enable_wdt();
164 expect_close = 0;
165
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300166 return stream_open(inode, file);
Matteo Crocec283cf22007-09-20 18:06:41 +0200167}
168
169static int ar7_wdt_release(struct inode *inode, struct file *file)
170{
171 if (!expect_close)
Joe Perches27c766a2012-02-15 15:06:19 -0800172 pr_warn("watchdog device closed unexpectedly, will not disable the watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200173 else if (!nowayout)
174 ar7_wdt_disable_wdt();
Alan Cox670d59c2008-08-04 17:53:22 +0100175 clear_bit(0, &wdt_is_open);
Matteo Crocec283cf22007-09-20 18:06:41 +0200176 return 0;
177}
178
Matteo Crocec283cf22007-09-20 18:06:41 +0200179static ssize_t ar7_wdt_write(struct file *file, const char *data,
180 size_t len, loff_t *ppos)
181{
182 /* check for a magic close character */
183 if (len) {
184 size_t i;
185
Alan Cox670d59c2008-08-04 17:53:22 +0100186 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200187 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100188 spin_unlock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200189
190 expect_close = 0;
191 for (i = 0; i < len; ++i) {
192 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000193 if (get_user(c, data + i))
Matteo Crocec283cf22007-09-20 18:06:41 +0200194 return -EFAULT;
195 if (c == 'V')
196 expect_close = 1;
197 }
198
199 }
200 return len;
201}
202
Alan Cox670d59c2008-08-04 17:53:22 +0100203static long ar7_wdt_ioctl(struct file *file,
204 unsigned int cmd, unsigned long arg)
Matteo Crocec283cf22007-09-20 18:06:41 +0200205{
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000206 static const struct watchdog_info ident = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200207 .identity = LONGNAME,
208 .firmware_version = 1,
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000209 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
210 WDIOF_MAGICCLOSE),
Matteo Crocec283cf22007-09-20 18:06:41 +0200211 };
212 int new_margin;
213
214 switch (cmd) {
Matteo Crocec283cf22007-09-20 18:06:41 +0200215 case WDIOC_GETSUPPORT:
216 if (copy_to_user((struct watchdog_info *)arg, &ident,
217 sizeof(ident)))
218 return -EFAULT;
219 return 0;
220 case WDIOC_GETSTATUS:
221 case WDIOC_GETBOOTSTATUS:
222 if (put_user(0, (int *)arg))
223 return -EFAULT;
224 return 0;
225 case WDIOC_KEEPALIVE:
226 ar7_wdt_kick(1);
227 return 0;
228 case WDIOC_SETTIMEOUT:
229 if (get_user(new_margin, (int *)arg))
230 return -EFAULT;
231 if (new_margin < 1)
232 return -EINVAL;
233
Alan Cox670d59c2008-08-04 17:53:22 +0100234 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200235 ar7_wdt_update_margin(new_margin);
236 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100237 spin_unlock(&wdt_lock);
Gustavo A. R. Silvad259f942019-07-29 10:08:05 -0500238 /* Fall through */
Matteo Crocec283cf22007-09-20 18:06:41 +0200239
240 case WDIOC_GETTIMEOUT:
241 if (put_user(margin, (int *)arg))
242 return -EFAULT;
243 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000244 default:
245 return -ENOTTY;
Matteo Crocec283cf22007-09-20 18:06:41 +0200246 }
247}
248
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100249static const struct file_operations ar7_wdt_fops = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200250 .owner = THIS_MODULE,
251 .write = ar7_wdt_write,
Alan Cox670d59c2008-08-04 17:53:22 +0100252 .unlocked_ioctl = ar7_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200253 .compat_ioctl = compat_ptr_ioctl,
Matteo Crocec283cf22007-09-20 18:06:41 +0200254 .open = ar7_wdt_open,
255 .release = ar7_wdt_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200256 .llseek = no_llseek,
Matteo Crocec283cf22007-09-20 18:06:41 +0200257};
258
259static struct miscdevice ar7_wdt_miscdev = {
260 .minor = WATCHDOG_MINOR,
261 .name = "watchdog",
262 .fops = &ar7_wdt_fops,
263};
264
Bill Pemberton2d991a12012-11-19 13:21:41 -0500265static int ar7_wdt_probe(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200266{
267 int rc;
268
Florian Fainelli64d40622009-07-21 12:11:32 +0200269 ar7_regs_wdt =
270 platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Thierry Reding4c271bb2013-01-21 11:09:25 +0100271 ar7_wdt = devm_ioremap_resource(&pdev->dev, ar7_regs_wdt);
272 if (IS_ERR(ar7_wdt))
273 return PTR_ERR(ar7_wdt);
Matteo Crocec283cf22007-09-20 18:06:41 +0200274
Florian Fainelli780019d2010-01-27 09:10:06 +0100275 vbus_clk = clk_get(NULL, "vbus");
276 if (IS_ERR(vbus_clk)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800277 pr_err("could not get vbus clock\n");
Julia Lawallae21cc22012-04-15 12:27:33 +0200278 return PTR_ERR(vbus_clk);
Florian Fainelli780019d2010-01-27 09:10:06 +0100279 }
280
Matteo Crocec283cf22007-09-20 18:06:41 +0200281 ar7_wdt_disable_wdt();
282 ar7_wdt_prescale(prescale_value);
283 ar7_wdt_update_margin(margin);
284
Matteo Crocec283cf22007-09-20 18:06:41 +0200285 rc = misc_register(&ar7_wdt_miscdev);
286 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800287 pr_err("unable to register misc device\n");
Julia Lawallae21cc22012-04-15 12:27:33 +0200288 goto out;
Matteo Crocec283cf22007-09-20 18:06:41 +0200289 }
Julia Lawallae21cc22012-04-15 12:27:33 +0200290 return 0;
Matteo Crocec283cf22007-09-20 18:06:41 +0200291
Matteo Crocec283cf22007-09-20 18:06:41 +0200292out:
Julia Lawallae21cc22012-04-15 12:27:33 +0200293 clk_put(vbus_clk);
294 vbus_clk = NULL;
Matteo Crocec283cf22007-09-20 18:06:41 +0200295 return rc;
296}
297
Bill Pemberton4b12b892012-11-19 13:26:24 -0500298static int ar7_wdt_remove(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200299{
300 misc_deregister(&ar7_wdt_miscdev);
Julia Lawallae21cc22012-04-15 12:27:33 +0200301 clk_put(vbus_clk);
302 vbus_clk = NULL;
Florian Fainelli64d40622009-07-21 12:11:32 +0200303 return 0;
304}
305
306static void ar7_wdt_shutdown(struct platform_device *pdev)
307{
308 if (!nowayout)
309 ar7_wdt_disable_wdt();
310}
311
312static struct platform_driver ar7_wdt_driver = {
313 .probe = ar7_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500314 .remove = ar7_wdt_remove,
Florian Fainelli64d40622009-07-21 12:11:32 +0200315 .shutdown = ar7_wdt_shutdown,
316 .driver = {
Florian Fainelli64d40622009-07-21 12:11:32 +0200317 .name = "ar7_wdt",
318 },
319};
320
Axel Linb8ec6112011-11-29 13:56:27 +0800321module_platform_driver(ar7_wdt_driver);