blob: 5f1b7bff8f1255f444971b3e527a08893d143797 [file] [log] [blame]
Kumar Galafabbfb92006-01-14 13:20:50 -08001/*
2 * mpc83xx_wdt.c - MPC83xx watchdog userspace interface
3 *
4 * Authors: Dave Updegraff <dave@cray.org>
5 * Kumar Gala <galak@kernel.crashing.org>
6 * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
7 * ..and from sc520_wdt
8 *
9 * Note: it appears that you can only actually ENABLE or DISABLE the thing
10 * once after POR. Once enabled, you cannot disable, and vice versa.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17
Kumar Galafabbfb92006-01-14 13:20:50 -080018#include <linux/fs.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/miscdevice.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070022#include <linux/of_platform.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080023#include <linux/module.h>
24#include <linux/watchdog.h>
Alan Coxf26ef3d2008-05-19 14:07:09 +010025#include <linux/io.h>
26#include <linux/uaccess.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070027#include <sysdev/fsl_soc.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080028
29struct mpc83xx_wdt {
30 __be32 res0;
31 __be32 swcrr; /* System watchdog control register */
32#define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
33#define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
34#define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
35#define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
36 __be32 swcnr; /* System watchdog count register */
37 u8 res1[2];
38 __be16 swsrr; /* System watchdog service register */
39 u8 res2[0xF0];
40};
41
42static struct mpc83xx_wdt __iomem *wd_base;
43
44static u16 timeout = 0xffff;
45module_param(timeout, ushort, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010046MODULE_PARM_DESC(timeout,
47 "Watchdog timeout in ticks. (0<timeout<65536, default=65535");
Kumar Galafabbfb92006-01-14 13:20:50 -080048
49static int reset = 1;
50module_param(reset, bool, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010051MODULE_PARM_DESC(reset,
52 "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
Kumar Galafabbfb92006-01-14 13:20:50 -080053
54/*
55 * We always prescale, but if someone really doesn't want to they can set this
56 * to 0
57 */
58static int prescale = 1;
59static unsigned int timeout_sec;
60
61static unsigned long wdt_is_open;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070062static DEFINE_SPINLOCK(wdt_spinlock);
Kumar Galafabbfb92006-01-14 13:20:50 -080063
64static void mpc83xx_wdt_keepalive(void)
65{
66 /* Ping the WDT */
67 spin_lock(&wdt_spinlock);
68 out_be16(&wd_base->swsrr, 0x556c);
69 out_be16(&wd_base->swsrr, 0xaa39);
70 spin_unlock(&wdt_spinlock);
71}
72
73static ssize_t mpc83xx_wdt_write(struct file *file, const char __user *buf,
74 size_t count, loff_t *ppos)
75{
76 if (count)
77 mpc83xx_wdt_keepalive();
78 return count;
79}
80
81static int mpc83xx_wdt_open(struct inode *inode, struct file *file)
82{
83 u32 tmp = SWCRR_SWEN;
84 if (test_and_set_bit(0, &wdt_is_open))
85 return -EBUSY;
86
87 /* Once we start the watchdog we can't stop it */
88 __module_get(THIS_MODULE);
89
90 /* Good, fire up the show */
91 if (prescale)
92 tmp |= SWCRR_SWPR;
93 if (reset)
94 tmp |= SWCRR_SWRI;
95
96 tmp |= timeout << 16;
97
98 out_be32(&wd_base->swcrr, tmp);
99
100 return nonseekable_open(inode, file);
101}
102
103static int mpc83xx_wdt_release(struct inode *inode, struct file *file)
104{
105 printk(KERN_CRIT "Unexpected close, not stopping watchdog!\n");
106 mpc83xx_wdt_keepalive();
107 clear_bit(0, &wdt_is_open);
108 return 0;
109}
110
Alan Coxf26ef3d2008-05-19 14:07:09 +0100111static long mpc83xx_wdt_ioctl(struct file *file, unsigned int cmd,
112 unsigned long arg)
Kumar Galafabbfb92006-01-14 13:20:50 -0800113{
114 void __user *argp = (void __user *)arg;
115 int __user *p = argp;
116 static struct watchdog_info ident = {
117 .options = WDIOF_KEEPALIVEPING,
118 .firmware_version = 1,
119 .identity = "MPC83xx",
120 };
121
122 switch (cmd) {
123 case WDIOC_GETSUPPORT:
124 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Wim Van Sebroeck5c4eb612007-07-21 13:42:18 +0000125 case WDIOC_GETSTATUS:
126 case WDIOC_GETBOOTSTATUS:
127 return put_user(0, p);
Kumar Galafabbfb92006-01-14 13:20:50 -0800128 case WDIOC_KEEPALIVE:
129 mpc83xx_wdt_keepalive();
130 return 0;
131 case WDIOC_GETTIMEOUT:
132 return put_user(timeout_sec, p);
133 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200134 return -ENOTTY;
Kumar Galafabbfb92006-01-14 13:20:50 -0800135 }
136}
137
Arjan van de Ven62322d22006-07-03 00:24:21 -0700138static const struct file_operations mpc83xx_wdt_fops = {
Kumar Galafabbfb92006-01-14 13:20:50 -0800139 .owner = THIS_MODULE,
140 .llseek = no_llseek,
141 .write = mpc83xx_wdt_write,
Alan Coxf26ef3d2008-05-19 14:07:09 +0100142 .unlocked_ioctl = mpc83xx_wdt_ioctl,
Kumar Galafabbfb92006-01-14 13:20:50 -0800143 .open = mpc83xx_wdt_open,
144 .release = mpc83xx_wdt_release,
145};
146
147static struct miscdevice mpc83xx_wdt_miscdev = {
148 .minor = WATCHDOG_MINOR,
149 .name = "watchdog",
150 .fops = &mpc83xx_wdt_fops,
151};
152
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700153static int __devinit mpc83xx_wdt_probe(struct of_device *ofdev,
154 const struct of_device_id *match)
Kumar Galafabbfb92006-01-14 13:20:50 -0800155{
Kumar Galafabbfb92006-01-14 13:20:50 -0800156 int ret;
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700157 u32 freq = fsl_get_sys_freq();
Kumar Galafabbfb92006-01-14 13:20:50 -0800158
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700159 if (!freq || freq == -1)
160 return -EINVAL;
Kumar Galafabbfb92006-01-14 13:20:50 -0800161
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700162 wd_base = of_iomap(ofdev->node, 0);
163 if (!wd_base)
164 return -ENOMEM;
Kumar Galafabbfb92006-01-14 13:20:50 -0800165
166 ret = misc_register(&mpc83xx_wdt_miscdev);
167 if (ret) {
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700168 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
169 WATCHDOG_MINOR, ret);
Kumar Galafabbfb92006-01-14 13:20:50 -0800170 goto err_unmap;
171 }
172
173 /* Calculate the timeout in seconds */
174 if (prescale)
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700175 timeout_sec = (timeout * 0x10000) / freq;
Kumar Galafabbfb92006-01-14 13:20:50 -0800176 else
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700177 timeout_sec = timeout / freq;
Kumar Galafabbfb92006-01-14 13:20:50 -0800178
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700179 pr_info("WDT driver for MPC83xx initialized. mode:%s timeout=%d "
180 "(%d seconds)\n", reset ? "reset" : "interrupt", timeout,
181 timeout_sec);
Kumar Galafabbfb92006-01-14 13:20:50 -0800182 return 0;
Kumar Galafabbfb92006-01-14 13:20:50 -0800183err_unmap:
184 iounmap(wd_base);
Kumar Galafabbfb92006-01-14 13:20:50 -0800185 return ret;
186}
187
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700188static int __devexit mpc83xx_wdt_remove(struct of_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800189{
190 misc_deregister(&mpc83xx_wdt_miscdev);
191 iounmap(wd_base);
192
193 return 0;
194}
195
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700196static const struct of_device_id mpc83xx_wdt_match[] = {
197 {
198 .compatible = "mpc83xx_wdt",
199 },
200 {},
201};
202MODULE_DEVICE_TABLE(of, mpc83xx_wdt_match);
203
204static struct of_platform_driver mpc83xx_wdt_driver = {
205 .match_table = mpc83xx_wdt_match,
Kumar Galafabbfb92006-01-14 13:20:50 -0800206 .probe = mpc83xx_wdt_probe,
207 .remove = __devexit_p(mpc83xx_wdt_remove),
208 .driver = {
209 .name = "mpc83xx_wdt",
Kay Sieversf37d1932008-04-10 21:29:23 -0700210 .owner = THIS_MODULE,
Kumar Galafabbfb92006-01-14 13:20:50 -0800211 },
212};
213
214static int __init mpc83xx_wdt_init(void)
215{
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700216 return of_register_platform_driver(&mpc83xx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800217}
218
219static void __exit mpc83xx_wdt_exit(void)
220{
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700221 of_unregister_platform_driver(&mpc83xx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800222}
223
224module_init(mpc83xx_wdt_init);
225module_exit(mpc83xx_wdt_exit);
226
227MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
228MODULE_DESCRIPTION("Driver for watchdog timer in MPC83xx uProcessor");
229MODULE_LICENSE("GPL");
230MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);