blob: 743e171d97a37d54106e6e65a041e47a474ff42d [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
Matteo Crocec283cf22007-09-20 18:06:41 +020066/* Pointer to the remapped WDT IO space */
67static struct ar7_wdt *ar7_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020068
Florian Fainelli780019d2010-01-27 09:10:06 +010069static struct clk *vbus_clk;
70
Matteo Crocec283cf22007-09-20 18:06:41 +020071static void ar7_wdt_kick(u32 value)
72{
73 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
74 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
75 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
76 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
77 WRITE_REG(ar7_wdt->kick, value);
78 return;
79 }
80 }
Joe Perches27c766a2012-02-15 15:06:19 -080081 pr_err("failed to unlock WDT kick reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +020082}
83
84static void ar7_wdt_prescale(u32 value)
85{
86 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
87 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
88 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
89 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
90 WRITE_REG(ar7_wdt->prescale, value);
91 return;
92 }
93 }
Joe Perches27c766a2012-02-15 15:06:19 -080094 pr_err("failed to unlock WDT prescale reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +020095}
96
97static void ar7_wdt_change(u32 value)
98{
99 WRITE_REG(ar7_wdt->change_lock, 0x6666);
100 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
101 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
102 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
103 WRITE_REG(ar7_wdt->change, value);
104 return;
105 }
106 }
Joe Perches27c766a2012-02-15 15:06:19 -0800107 pr_err("failed to unlock WDT change reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200108}
109
110static void ar7_wdt_disable(u32 value)
111{
112 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
113 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
114 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
115 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
116 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
117 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
118 WRITE_REG(ar7_wdt->disable, value);
119 return;
120 }
121 }
122 }
Joe Perches27c766a2012-02-15 15:06:19 -0800123 pr_err("failed to unlock WDT disable reg\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200124}
125
126static void ar7_wdt_update_margin(int new_margin)
127{
128 u32 change;
Florian Fainelli780019d2010-01-27 09:10:06 +0100129 u32 vbus_rate;
Matteo Crocec283cf22007-09-20 18:06:41 +0200130
Florian Fainelli780019d2010-01-27 09:10:06 +0100131 vbus_rate = clk_get_rate(vbus_clk);
132 change = new_margin * (vbus_rate / prescale_value);
Alan Cox670d59c2008-08-04 17:53:22 +0100133 if (change < 1)
134 change = 1;
135 if (change > 0xffff)
136 change = 0xffff;
Matteo Crocec283cf22007-09-20 18:06:41 +0200137 ar7_wdt_change(change);
Florian Fainelli780019d2010-01-27 09:10:06 +0100138 margin = change * prescale_value / vbus_rate;
Joe Perches27c766a2012-02-15 15:06:19 -0800139 pr_info("timer margin %d seconds (prescale %d, change %d, freq %d)\n",
140 margin, prescale_value, change, vbus_rate);
Matteo Crocec283cf22007-09-20 18:06:41 +0200141}
142
143static void ar7_wdt_enable_wdt(void)
144{
Joe Perches27c766a2012-02-15 15:06:19 -0800145 pr_debug("enabling watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200146 ar7_wdt_disable(1);
147 ar7_wdt_kick(1);
148}
149
150static void ar7_wdt_disable_wdt(void)
151{
Joe Perches27c766a2012-02-15 15:06:19 -0800152 pr_debug("disabling watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200153 ar7_wdt_disable(0);
154}
155
156static int ar7_wdt_open(struct inode *inode, struct file *file)
157{
158 /* only allow one at a time */
Alan Cox670d59c2008-08-04 17:53:22 +0100159 if (test_and_set_bit(0, &wdt_is_open))
Matteo Crocec283cf22007-09-20 18:06:41 +0200160 return -EBUSY;
161 ar7_wdt_enable_wdt();
162 expect_close = 0;
163
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300164 return stream_open(inode, file);
Matteo Crocec283cf22007-09-20 18:06:41 +0200165}
166
167static int ar7_wdt_release(struct inode *inode, struct file *file)
168{
169 if (!expect_close)
Joe Perches27c766a2012-02-15 15:06:19 -0800170 pr_warn("watchdog device closed unexpectedly, will not disable the watchdog timer\n");
Matteo Crocec283cf22007-09-20 18:06:41 +0200171 else if (!nowayout)
172 ar7_wdt_disable_wdt();
Alan Cox670d59c2008-08-04 17:53:22 +0100173 clear_bit(0, &wdt_is_open);
Matteo Crocec283cf22007-09-20 18:06:41 +0200174 return 0;
175}
176
Matteo Crocec283cf22007-09-20 18:06:41 +0200177static ssize_t ar7_wdt_write(struct file *file, const char *data,
178 size_t len, loff_t *ppos)
179{
180 /* check for a magic close character */
181 if (len) {
182 size_t i;
183
Alan Cox670d59c2008-08-04 17:53:22 +0100184 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200185 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100186 spin_unlock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200187
188 expect_close = 0;
189 for (i = 0; i < len; ++i) {
190 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000191 if (get_user(c, data + i))
Matteo Crocec283cf22007-09-20 18:06:41 +0200192 return -EFAULT;
193 if (c == 'V')
194 expect_close = 1;
195 }
196
197 }
198 return len;
199}
200
Alan Cox670d59c2008-08-04 17:53:22 +0100201static long ar7_wdt_ioctl(struct file *file,
202 unsigned int cmd, unsigned long arg)
Matteo Crocec283cf22007-09-20 18:06:41 +0200203{
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000204 static const struct watchdog_info ident = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200205 .identity = LONGNAME,
206 .firmware_version = 1,
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000207 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
208 WDIOF_MAGICCLOSE),
Matteo Crocec283cf22007-09-20 18:06:41 +0200209 };
210 int new_margin;
211
212 switch (cmd) {
Matteo Crocec283cf22007-09-20 18:06:41 +0200213 case WDIOC_GETSUPPORT:
214 if (copy_to_user((struct watchdog_info *)arg, &ident,
215 sizeof(ident)))
216 return -EFAULT;
217 return 0;
218 case WDIOC_GETSTATUS:
219 case WDIOC_GETBOOTSTATUS:
220 if (put_user(0, (int *)arg))
221 return -EFAULT;
222 return 0;
223 case WDIOC_KEEPALIVE:
224 ar7_wdt_kick(1);
225 return 0;
226 case WDIOC_SETTIMEOUT:
227 if (get_user(new_margin, (int *)arg))
228 return -EFAULT;
229 if (new_margin < 1)
230 return -EINVAL;
231
Alan Cox670d59c2008-08-04 17:53:22 +0100232 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200233 ar7_wdt_update_margin(new_margin);
234 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100235 spin_unlock(&wdt_lock);
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500236 fallthrough;
Matteo Crocec283cf22007-09-20 18:06:41 +0200237 case WDIOC_GETTIMEOUT:
238 if (put_user(margin, (int *)arg))
239 return -EFAULT;
240 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000241 default:
242 return -ENOTTY;
Matteo Crocec283cf22007-09-20 18:06:41 +0200243 }
244}
245
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100246static const struct file_operations ar7_wdt_fops = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200247 .owner = THIS_MODULE,
248 .write = ar7_wdt_write,
Alan Cox670d59c2008-08-04 17:53:22 +0100249 .unlocked_ioctl = ar7_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200250 .compat_ioctl = compat_ptr_ioctl,
Matteo Crocec283cf22007-09-20 18:06:41 +0200251 .open = ar7_wdt_open,
252 .release = ar7_wdt_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200253 .llseek = no_llseek,
Matteo Crocec283cf22007-09-20 18:06:41 +0200254};
255
256static struct miscdevice ar7_wdt_miscdev = {
257 .minor = WATCHDOG_MINOR,
258 .name = "watchdog",
259 .fops = &ar7_wdt_fops,
260};
261
Bill Pemberton2d991a12012-11-19 13:21:41 -0500262static int ar7_wdt_probe(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200263{
264 int rc;
265
Cai Huoqing54ccba22021-09-07 15:42:22 +0800266 ar7_wdt = devm_platform_ioremap_resource_byname(pdev, "regs");
Thierry Reding4c271bb2013-01-21 11:09:25 +0100267 if (IS_ERR(ar7_wdt))
268 return PTR_ERR(ar7_wdt);
Matteo Crocec283cf22007-09-20 18:06:41 +0200269
Florian Fainelli780019d2010-01-27 09:10:06 +0100270 vbus_clk = clk_get(NULL, "vbus");
271 if (IS_ERR(vbus_clk)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800272 pr_err("could not get vbus clock\n");
Julia Lawallae21cc22012-04-15 12:27:33 +0200273 return PTR_ERR(vbus_clk);
Florian Fainelli780019d2010-01-27 09:10:06 +0100274 }
275
Matteo Crocec283cf22007-09-20 18:06:41 +0200276 ar7_wdt_disable_wdt();
277 ar7_wdt_prescale(prescale_value);
278 ar7_wdt_update_margin(margin);
279
Matteo Crocec283cf22007-09-20 18:06:41 +0200280 rc = misc_register(&ar7_wdt_miscdev);
281 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800282 pr_err("unable to register misc device\n");
Julia Lawallae21cc22012-04-15 12:27:33 +0200283 goto out;
Matteo Crocec283cf22007-09-20 18:06:41 +0200284 }
Julia Lawallae21cc22012-04-15 12:27:33 +0200285 return 0;
Matteo Crocec283cf22007-09-20 18:06:41 +0200286
Matteo Crocec283cf22007-09-20 18:06:41 +0200287out:
Julia Lawallae21cc22012-04-15 12:27:33 +0200288 clk_put(vbus_clk);
289 vbus_clk = NULL;
Matteo Crocec283cf22007-09-20 18:06:41 +0200290 return rc;
291}
292
Bill Pemberton4b12b892012-11-19 13:26:24 -0500293static int ar7_wdt_remove(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200294{
295 misc_deregister(&ar7_wdt_miscdev);
Julia Lawallae21cc22012-04-15 12:27:33 +0200296 clk_put(vbus_clk);
297 vbus_clk = NULL;
Florian Fainelli64d40622009-07-21 12:11:32 +0200298 return 0;
299}
300
301static void ar7_wdt_shutdown(struct platform_device *pdev)
302{
303 if (!nowayout)
304 ar7_wdt_disable_wdt();
305}
306
307static struct platform_driver ar7_wdt_driver = {
308 .probe = ar7_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500309 .remove = ar7_wdt_remove,
Florian Fainelli64d40622009-07-21 12:11:32 +0200310 .shutdown = ar7_wdt_shutdown,
311 .driver = {
Florian Fainelli64d40622009-07-21 12:11:32 +0200312 .name = "ar7_wdt",
313 },
314};
315
Axel Linb8ec6112011-11-29 13:56:27 +0800316module_platform_driver(ar7_wdt_driver);