Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.git] / drivers / watchdog / booke_wdt.c
CommitLineData
a2f40ccd 1/*
a2f40ccd
KG
2 * Watchdog timer for PowerPC Book-E systems
3 *
4 * Author: Matthew McClintock
4c8d3d99 5 * Maintainer: Kumar Gala <galak@kernel.crashing.org>
a2f40ccd 6 *
f172ddc6 7 * Copyright 2005, 2008 Freescale Semiconductor Inc.
a2f40ccd
KG
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14
a2f40ccd
KG
15#include <linux/module.h>
16#include <linux/fs.h>
f172ddc6 17#include <linux/smp.h>
a2f40ccd
KG
18#include <linux/miscdevice.h>
19#include <linux/notifier.h>
20#include <linux/watchdog.h>
00e9c205 21#include <linux/uaccess.h>
a2f40ccd
KG
22
23#include <asm/reg_booke.h>
39cdc4bf 24#include <asm/system.h>
a2f40ccd 25
40ebbcbf 26/* If the kernel parameter wdt=1, the watchdog will be enabled at boot.
a2f40ccd
KG
27 * Also, the wdt_period sets the watchdog timer period timeout.
28 * For E500 cpus the wdt_period sets which bit changing from 0->1 will
29 * trigger a watchog timeout. This watchdog timeout will occur 3 times, the
30 * first time nothing will happen, the second time a watchdog exception will
31 * occur, and the final time the board will reset.
32 */
33
34#ifdef CONFIG_FSL_BOOKE
00e9c205 35#define WDT_PERIOD_DEFAULT 63 /* Ex. wdt_period=28 bus=333Mhz,reset=~40sec */
a2f40ccd 36#else
f31909c0 37#define WDT_PERIOD_DEFAULT 3 /* Refer to the PPC40x and PPC4xx manuals */
a2f40ccd
KG
38#endif /* for timing information */
39
f172ddc6 40u32 booke_wdt_enabled;
39cdc4bf 41u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
a2f40ccd
KG
42
43#ifdef CONFIG_FSL_BOOKE
44#define WDTP(x) ((((63-x)&0x3)<<30)|(((63-x)&0x3c)<<15))
0a0e9e0c 45#define WDTP_MASK (WDTP(0))
a2f40ccd
KG
46#else
47#define WDTP(x) (TCR_WP(x))
0a0e9e0c 48#define WDTP_MASK (TCR_WP_MASK)
a2f40ccd
KG
49#endif
50
f172ddc6
CG
51static DEFINE_SPINLOCK(booke_wdt_lock);
52
53static void __booke_wdt_ping(void *data)
f31909c0
SR
54{
55 mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
56}
57
f172ddc6
CG
58static void booke_wdt_ping(void)
59{
f6f88e9b 60 on_each_cpu(__booke_wdt_ping, NULL, 0);
f172ddc6
CG
61}
62
63static void __booke_wdt_enable(void *data)
a2f40ccd
KG
64{
65 u32 val;
66
f31909c0 67 /* clear status before enabling watchdog */
f172ddc6 68 __booke_wdt_ping(NULL);
a2f40ccd 69 val = mfspr(SPRN_TCR);
0a0e9e0c 70 val &= ~WDTP_MASK;
39cdc4bf 71 val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
a2f40ccd
KG
72
73 mtspr(SPRN_TCR, val);
74}
75
f172ddc6 76static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
a2f40ccd
KG
77 size_t count, loff_t *ppos)
78{
79 booke_wdt_ping();
80 return count;
81}
82
83static struct watchdog_info ident = {
f172ddc6
CG
84 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
85 .identity = "PowerPC Book-E Watchdog",
a2f40ccd
KG
86};
87
00e9c205
AC
88static long booke_wdt_ioctl(struct file *file,
89 unsigned int cmd, unsigned long arg)
a2f40ccd
KG
90{
91 u32 tmp = 0;
538bacf8 92 u32 __user *p = (u32 __user *)arg;
a2f40ccd
KG
93
94 switch (cmd) {
95 case WDIOC_GETSUPPORT:
00e9c205 96 if (copy_to_user(arg, &ident, sizeof(struct watchdog_info)))
a2f40ccd
KG
97 return -EFAULT;
98 case WDIOC_GETSTATUS:
538bacf8 99 return put_user(ident.options, p);
a2f40ccd
KG
100 case WDIOC_GETBOOTSTATUS:
101 /* XXX: something is clearing TSR */
102 tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
103 /* returns 1 if last reset was caused by the WDT */
104 return (tmp ? 1 : 0);
0c06090c
WVS
105 case WDIOC_SETOPTIONS:
106 if (get_user(tmp, p))
107 return -EINVAL;
108 if (tmp == WDIOS_ENABLECARD) {
109 booke_wdt_ping();
110 break;
111 } else
112 return -EINVAL;
113 return 0;
a2f40ccd
KG
114 case WDIOC_KEEPALIVE:
115 booke_wdt_ping();
116 return 0;
117 case WDIOC_SETTIMEOUT:
538bacf8 118 if (get_user(booke_wdt_period, p))
a2f40ccd 119 return -EFAULT;
0a0e9e0c 120 mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP_MASK) |
00e9c205 121 WDTP(booke_wdt_period));
a2f40ccd
KG
122 return 0;
123 case WDIOC_GETTIMEOUT:
538bacf8 124 return put_user(booke_wdt_period, p);
a2f40ccd 125 default:
795b89d2 126 return -ENOTTY;
a2f40ccd
KG
127 }
128
129 return 0;
130}
f172ddc6
CG
131
132static int booke_wdt_open(struct inode *inode, struct file *file)
a2f40ccd 133{
f172ddc6 134 spin_lock(&booke_wdt_lock);
39cdc4bf
KG
135 if (booke_wdt_enabled == 0) {
136 booke_wdt_enabled = 1;
f6f88e9b 137 on_each_cpu(__booke_wdt_enable, NULL, 0);
00e9c205
AC
138 printk(KERN_INFO
139 "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
140 booke_wdt_period);
a2f40ccd 141 }
f172ddc6 142 spin_unlock(&booke_wdt_lock);
a2f40ccd 143
ec9505a7 144 return nonseekable_open(inode, file);
a2f40ccd
KG
145}
146
62322d25 147static const struct file_operations booke_wdt_fops = {
f172ddc6
CG
148 .owner = THIS_MODULE,
149 .llseek = no_llseek,
150 .write = booke_wdt_write,
00e9c205 151 .unlocked_ioctl = booke_wdt_ioctl,
f172ddc6 152 .open = booke_wdt_open,
a2f40ccd
KG
153};
154
155static struct miscdevice booke_wdt_miscdev = {
f172ddc6
CG
156 .minor = WATCHDOG_MINOR,
157 .name = "watchdog",
158 .fops = &booke_wdt_fops,
a2f40ccd
KG
159};
160
161static void __exit booke_wdt_exit(void)
162{
163 misc_deregister(&booke_wdt_miscdev);
164}
165
a2f40ccd
KG
166static int __init booke_wdt_init(void)
167{
168 int ret = 0;
169
f172ddc6 170 printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
a78719c3 171 ident.firmware_version = cur_cpu_spec->pvr_value;
a2f40ccd
KG
172
173 ret = misc_register(&booke_wdt_miscdev);
174 if (ret) {
f172ddc6 175 printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
a2f40ccd
KG
176 WATCHDOG_MINOR, ret);
177 return ret;
178 }
179
f172ddc6 180 spin_lock(&booke_wdt_lock);
39cdc4bf 181 if (booke_wdt_enabled == 1) {
00e9c205
AC
182 printk(KERN_INFO
183 "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
184 booke_wdt_period);
f6f88e9b 185 on_each_cpu(__booke_wdt_enable, NULL, 0);
a2f40ccd 186 }
f172ddc6 187 spin_unlock(&booke_wdt_lock);
a2f40ccd
KG
188
189 return ret;
190}
191device_initcall(booke_wdt_init);