blob: d0ebebae607c847766b3e9cc7204dad7afad7cac [file] [log] [blame]
Kumar Galafabbfb92006-01-14 13:20:50 -08001/*
Anton Vorontsov0d7b1012008-07-03 23:51:36 -07002 * mpc8xxx_wdt.c - MPC8xx/MPC83xx/MPC86xx watchdog userspace interface
Kumar Galafabbfb92006-01-14 13:20:50 -08003 *
4 * Authors: Dave Updegraff <dave@cray.org>
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +00005 * Kumar Gala <galak@kernel.crashing.org>
6 * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
7 * ..and from sc520_wdt
Anton Vorontsov500c9192008-07-03 23:51:34 -07008 * Copyright (c) 2008 MontaVista Software, Inc.
9 * Anton Vorontsov <avorontsov@ru.mvista.com>
Kumar Galafabbfb92006-01-14 13:20:50 -080010 *
11 * Note: it appears that you can only actually ENABLE or DISABLE the thing
12 * once after POR. Once enabled, you cannot disable, and vice versa.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 */
19
Joe Perches27c766a2012-02-15 15:06:19 -080020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Kumar Galafabbfb92006-01-14 13:20:50 -080022#include <linux/fs.h>
23#include <linux/init.h>
24#include <linux/kernel.h>
Anton Vorontsov500c9192008-07-03 23:51:34 -070025#include <linux/timer.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080026#include <linux/miscdevice.h>
Rob Herring5af50732013-09-17 14:28:33 -050027#include <linux/of_address.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070028#include <linux/of_platform.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080029#include <linux/module.h>
30#include <linux/watchdog.h>
Alan Coxf26ef3d2008-05-19 14:07:09 +010031#include <linux/io.h>
32#include <linux/uaccess.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070033#include <sysdev/fsl_soc.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080034
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070035struct mpc8xxx_wdt {
Kumar Galafabbfb92006-01-14 13:20:50 -080036 __be32 res0;
37 __be32 swcrr; /* System watchdog control register */
38#define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
39#define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
40#define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
41#define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
42 __be32 swcnr; /* System watchdog count register */
43 u8 res1[2];
44 __be16 swsrr; /* System watchdog service register */
45 u8 res2[0xF0];
46};
47
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070048struct mpc8xxx_wdt_type {
Anton Vorontsov500c9192008-07-03 23:51:34 -070049 int prescaler;
50 bool hw_enabled;
51};
52
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070053static struct mpc8xxx_wdt __iomem *wd_base;
Anton Vorontsov593fc172008-08-20 16:38:36 -070054static int mpc8xxx_wdt_init_late(void);
Kumar Galafabbfb92006-01-14 13:20:50 -080055
56static u16 timeout = 0xffff;
57module_param(timeout, ushort, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010058MODULE_PARM_DESC(timeout,
Randy Dunlap76550d32010-05-01 09:46:15 -070059 "Watchdog timeout in ticks. (0<timeout<65536, default=65535)");
Kumar Galafabbfb92006-01-14 13:20:50 -080060
Rusty Russell90ab5ee2012-01-13 09:32:20 +103061static bool reset = 1;
Kumar Galafabbfb92006-01-14 13:20:50 -080062module_param(reset, bool, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010063MODULE_PARM_DESC(reset,
64 "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
Kumar Galafabbfb92006-01-14 13:20:50 -080065
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010066static bool nowayout = WATCHDOG_NOWAYOUT;
67module_param(nowayout, bool, 0);
Anton Vorontsov500c9192008-07-03 23:51:34 -070068MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
69 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
70
Kumar Galafabbfb92006-01-14 13:20:50 -080071/*
72 * We always prescale, but if someone really doesn't want to they can set this
73 * to 0
74 */
75static int prescale = 1;
76static unsigned int timeout_sec;
77
78static unsigned long wdt_is_open;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070079static DEFINE_SPINLOCK(wdt_spinlock);
Kumar Galafabbfb92006-01-14 13:20:50 -080080
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070081static void mpc8xxx_wdt_keepalive(void)
Kumar Galafabbfb92006-01-14 13:20:50 -080082{
83 /* Ping the WDT */
84 spin_lock(&wdt_spinlock);
85 out_be16(&wd_base->swsrr, 0x556c);
86 out_be16(&wd_base->swsrr, 0xaa39);
87 spin_unlock(&wdt_spinlock);
88}
89
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070090static void mpc8xxx_wdt_timer_ping(unsigned long arg);
91static DEFINE_TIMER(wdt_timer, mpc8xxx_wdt_timer_ping, 0, 0);
Anton Vorontsov500c9192008-07-03 23:51:34 -070092
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070093static void mpc8xxx_wdt_timer_ping(unsigned long arg)
Anton Vorontsov500c9192008-07-03 23:51:34 -070094{
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070095 mpc8xxx_wdt_keepalive();
Anton Vorontsov500c9192008-07-03 23:51:34 -070096 /* We're pinging it twice faster than needed, just to be sure. */
97 mod_timer(&wdt_timer, jiffies + HZ * timeout_sec / 2);
98}
99
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700100static void mpc8xxx_wdt_pr_warn(const char *msg)
Anton Vorontsov500c9192008-07-03 23:51:34 -0700101{
Joe Perches27c766a2012-02-15 15:06:19 -0800102 pr_crit("%s, expect the %s soon!\n", msg,
Anton Vorontsov500c9192008-07-03 23:51:34 -0700103 reset ? "reset" : "machine check exception");
104}
105
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700106static ssize_t mpc8xxx_wdt_write(struct file *file, const char __user *buf,
Kumar Galafabbfb92006-01-14 13:20:50 -0800107 size_t count, loff_t *ppos)
108{
109 if (count)
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700110 mpc8xxx_wdt_keepalive();
Kumar Galafabbfb92006-01-14 13:20:50 -0800111 return count;
112}
113
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700114static int mpc8xxx_wdt_open(struct inode *inode, struct file *file)
Kumar Galafabbfb92006-01-14 13:20:50 -0800115{
116 u32 tmp = SWCRR_SWEN;
117 if (test_and_set_bit(0, &wdt_is_open))
118 return -EBUSY;
119
120 /* Once we start the watchdog we can't stop it */
Anton Vorontsov500c9192008-07-03 23:51:34 -0700121 if (nowayout)
122 __module_get(THIS_MODULE);
Kumar Galafabbfb92006-01-14 13:20:50 -0800123
124 /* Good, fire up the show */
125 if (prescale)
126 tmp |= SWCRR_SWPR;
127 if (reset)
128 tmp |= SWCRR_SWRI;
129
130 tmp |= timeout << 16;
131
132 out_be32(&wd_base->swcrr, tmp);
133
Anton Vorontsov500c9192008-07-03 23:51:34 -0700134 del_timer_sync(&wdt_timer);
135
Kumar Galafabbfb92006-01-14 13:20:50 -0800136 return nonseekable_open(inode, file);
137}
138
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700139static int mpc8xxx_wdt_release(struct inode *inode, struct file *file)
Kumar Galafabbfb92006-01-14 13:20:50 -0800140{
Anton Vorontsov500c9192008-07-03 23:51:34 -0700141 if (!nowayout)
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700142 mpc8xxx_wdt_timer_ping(0);
Anton Vorontsov500c9192008-07-03 23:51:34 -0700143 else
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700144 mpc8xxx_wdt_pr_warn("watchdog closed");
Kumar Galafabbfb92006-01-14 13:20:50 -0800145 clear_bit(0, &wdt_is_open);
146 return 0;
147}
148
Anton Vorontsovcb55d282008-07-03 23:51:36 -0700149static long mpc8xxx_wdt_ioctl(struct file *file, unsigned int cmd,
Alan Coxf26ef3d2008-05-19 14:07:09 +0100150 unsigned long arg)
Kumar Galafabbfb92006-01-14 13:20:50 -0800151{
152 void __user *argp = (void __user *)arg;
153 int __user *p = argp;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000154 static const struct watchdog_info ident = {
Kumar Galafabbfb92006-01-14 13:20:50 -0800155 .options = WDIOF_KEEPALIVEPING,
156 .firmware_version = 1,
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700157 .identity = "MPC8xxx",
Kumar Galafabbfb92006-01-14 13:20:50 -0800158 };
159
160 switch (cmd) {
161 case WDIOC_GETSUPPORT:
162 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Wim Van Sebroeck5c4eb612007-07-21 13:42:18 +0000163 case WDIOC_GETSTATUS:
164 case WDIOC_GETBOOTSTATUS:
165 return put_user(0, p);
Kumar Galafabbfb92006-01-14 13:20:50 -0800166 case WDIOC_KEEPALIVE:
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700167 mpc8xxx_wdt_keepalive();
Kumar Galafabbfb92006-01-14 13:20:50 -0800168 return 0;
169 case WDIOC_GETTIMEOUT:
170 return put_user(timeout_sec, p);
171 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200172 return -ENOTTY;
Kumar Galafabbfb92006-01-14 13:20:50 -0800173 }
174}
175
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700176static const struct file_operations mpc8xxx_wdt_fops = {
Kumar Galafabbfb92006-01-14 13:20:50 -0800177 .owner = THIS_MODULE,
178 .llseek = no_llseek,
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700179 .write = mpc8xxx_wdt_write,
180 .unlocked_ioctl = mpc8xxx_wdt_ioctl,
181 .open = mpc8xxx_wdt_open,
182 .release = mpc8xxx_wdt_release,
Kumar Galafabbfb92006-01-14 13:20:50 -0800183};
184
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700185static struct miscdevice mpc8xxx_wdt_miscdev = {
Kumar Galafabbfb92006-01-14 13:20:50 -0800186 .minor = WATCHDOG_MINOR,
187 .name = "watchdog",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700188 .fops = &mpc8xxx_wdt_fops,
Kumar Galafabbfb92006-01-14 13:20:50 -0800189};
190
Grant Likelyb1608d62011-05-18 11:19:24 -0600191static const struct of_device_id mpc8xxx_wdt_match[];
Bill Pemberton2d991a12012-11-19 13:21:41 -0500192static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800193{
Kumar Galafabbfb92006-01-14 13:20:50 -0800194 int ret;
Grant Likelyb1608d62011-05-18 11:19:24 -0600195 const struct of_device_id *match;
Michael Guntschede2b6062010-06-02 02:25:52 -0600196 struct device_node *np = ofdev->dev.of_node;
Arnd Bergmann639397e2012-05-21 21:57:39 +0200197 const struct mpc8xxx_wdt_type *wdt_type;
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700198 u32 freq = fsl_get_sys_freq();
Anton Vorontsov500c9192008-07-03 23:51:34 -0700199 bool enabled;
Kumar Galafabbfb92006-01-14 13:20:50 -0800200
Grant Likelyb1608d62011-05-18 11:19:24 -0600201 match = of_match_device(mpc8xxx_wdt_match, &ofdev->dev);
202 if (!match)
Grant Likely1c48a5c2011-02-17 02:43:24 -0700203 return -EINVAL;
Grant Likelyb1608d62011-05-18 11:19:24 -0600204 wdt_type = match->data;
Grant Likely1c48a5c2011-02-17 02:43:24 -0700205
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700206 if (!freq || freq == -1)
207 return -EINVAL;
Kumar Galafabbfb92006-01-14 13:20:50 -0800208
Anton Vorontsov500c9192008-07-03 23:51:34 -0700209 wd_base = of_iomap(np, 0);
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700210 if (!wd_base)
211 return -ENOMEM;
Kumar Galafabbfb92006-01-14 13:20:50 -0800212
Anton Vorontsov500c9192008-07-03 23:51:34 -0700213 enabled = in_be32(&wd_base->swcrr) & SWCRR_SWEN;
214 if (!enabled && wdt_type->hw_enabled) {
Joe Perches27c766a2012-02-15 15:06:19 -0800215 pr_info("could not be enabled in software\n");
Anton Vorontsov500c9192008-07-03 23:51:34 -0700216 ret = -ENOSYS;
217 goto err_unmap;
218 }
219
Kumar Galafabbfb92006-01-14 13:20:50 -0800220 /* Calculate the timeout in seconds */
221 if (prescale)
Anton Vorontsov500c9192008-07-03 23:51:34 -0700222 timeout_sec = (timeout * wdt_type->prescaler) / freq;
Kumar Galafabbfb92006-01-14 13:20:50 -0800223 else
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700224 timeout_sec = timeout / freq;
Kumar Galafabbfb92006-01-14 13:20:50 -0800225
Anton Vorontsov593fc172008-08-20 16:38:36 -0700226#ifdef MODULE
227 ret = mpc8xxx_wdt_init_late();
228 if (ret)
229 goto err_unmap;
230#endif
231
Joe Perches27c766a2012-02-15 15:06:19 -0800232 pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d (%d seconds)\n",
233 reset ? "reset" : "interrupt", timeout, timeout_sec);
Anton Vorontsov500c9192008-07-03 23:51:34 -0700234
235 /*
236 * If the watchdog was previously enabled or we're running on
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700237 * MPC8xxx, we should ping the wdt from the kernel until the
Anton Vorontsov500c9192008-07-03 23:51:34 -0700238 * userspace handles it.
239 */
240 if (enabled)
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700241 mpc8xxx_wdt_timer_ping(0);
Kumar Galafabbfb92006-01-14 13:20:50 -0800242 return 0;
Kumar Galafabbfb92006-01-14 13:20:50 -0800243err_unmap:
244 iounmap(wd_base);
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700245 wd_base = NULL;
Kumar Galafabbfb92006-01-14 13:20:50 -0800246 return ret;
247}
248
Bill Pemberton4b12b892012-11-19 13:26:24 -0500249static int mpc8xxx_wdt_remove(struct platform_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800250{
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700251 mpc8xxx_wdt_pr_warn("watchdog removed");
Anton Vorontsov500c9192008-07-03 23:51:34 -0700252 del_timer_sync(&wdt_timer);
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700253 misc_deregister(&mpc8xxx_wdt_miscdev);
Kumar Galafabbfb92006-01-14 13:20:50 -0800254 iounmap(wd_base);
255
256 return 0;
257}
258
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700259static const struct of_device_id mpc8xxx_wdt_match[] = {
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700260 {
261 .compatible = "mpc83xx_wdt",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700262 .data = &(struct mpc8xxx_wdt_type) {
Anton Vorontsov500c9192008-07-03 23:51:34 -0700263 .prescaler = 0x10000,
264 },
265 },
266 {
267 .compatible = "fsl,mpc8610-wdt",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700268 .data = &(struct mpc8xxx_wdt_type) {
Anton Vorontsov500c9192008-07-03 23:51:34 -0700269 .prescaler = 0x10000,
270 .hw_enabled = true,
271 },
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700272 },
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700273 {
274 .compatible = "fsl,mpc823-wdt",
275 .data = &(struct mpc8xxx_wdt_type) {
276 .prescaler = 0x800,
277 },
278 },
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700279 {},
280};
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700281MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700282
Grant Likely1c48a5c2011-02-17 02:43:24 -0700283static struct platform_driver mpc8xxx_wdt_driver = {
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700284 .probe = mpc8xxx_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500285 .remove = mpc8xxx_wdt_remove,
Grant Likely40182942010-04-13 16:13:02 -0700286 .driver = {
287 .name = "mpc8xxx_wdt",
288 .owner = THIS_MODULE,
289 .of_match_table = mpc8xxx_wdt_match,
Kumar Galafabbfb92006-01-14 13:20:50 -0800290 },
291};
292
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700293/*
294 * We do wdt initialization in two steps: arch_initcall probes the wdt
295 * very early to start pinging the watchdog (misc devices are not yet
296 * available), and later module_init() just registers the misc device.
297 */
Anton Vorontsov593fc172008-08-20 16:38:36 -0700298static int mpc8xxx_wdt_init_late(void)
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700299{
300 int ret;
301
302 if (!wd_base)
303 return -ENODEV;
304
305 ret = misc_register(&mpc8xxx_wdt_miscdev);
306 if (ret) {
307 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
Joe Perches27c766a2012-02-15 15:06:19 -0800308 WATCHDOG_MINOR, ret);
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700309 return ret;
310 }
311 return 0;
312}
Anton Vorontsov593fc172008-08-20 16:38:36 -0700313#ifndef MODULE
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700314module_init(mpc8xxx_wdt_init_late);
Anton Vorontsov593fc172008-08-20 16:38:36 -0700315#endif
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700316
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700317static int __init mpc8xxx_wdt_init(void)
Kumar Galafabbfb92006-01-14 13:20:50 -0800318{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700319 return platform_driver_register(&mpc8xxx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800320}
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700321arch_initcall(mpc8xxx_wdt_init);
Kumar Galafabbfb92006-01-14 13:20:50 -0800322
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700323static void __exit mpc8xxx_wdt_exit(void)
Kumar Galafabbfb92006-01-14 13:20:50 -0800324{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700325 platform_driver_unregister(&mpc8xxx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800326}
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700327module_exit(mpc8xxx_wdt_exit);
Kumar Galafabbfb92006-01-14 13:20:50 -0800328
329MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700330MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
331 "uProcessors");
Kumar Galafabbfb92006-01-14 13:20:50 -0800332MODULE_LICENSE("GPL");
333MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);