1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/fs.h>
17#include <linux/smp.h>
18#include <linux/miscdevice.h>
19#include <linux/notifier.h>
20#include <linux/watchdog.h>
21#include <linux/uaccess.h>
22
23#include <asm/reg_booke.h>
24#include <asm/system.h>
25
26
27
28
29
30
31
32
33
34#ifdef CONFIG_FSL_BOOKE
35#define WDT_PERIOD_DEFAULT 63
36#else
37#define WDT_PERIOD_DEFAULT 3
38#endif
39
40u32 booke_wdt_enabled;
41u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
42
43#ifdef CONFIG_FSL_BOOKE
44#define WDTP(x) ((((63-x)&0x3)<<30)|(((63-x)&0x3c)<<15))
45#else
46#define WDTP(x) (TCR_WP(x))
47#endif
48
49static DEFINE_SPINLOCK(booke_wdt_lock);
50
51static void __booke_wdt_ping(void *data)
52{
53 mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
54}
55
56static void booke_wdt_ping(void)
57{
58 on_each_cpu(__booke_wdt_ping, NULL, 0);
59}
60
61static void __booke_wdt_enable(void *data)
62{
63 u32 val;
64
65
66 __booke_wdt_ping(NULL);
67 val = mfspr(SPRN_TCR);
68 val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
69
70 mtspr(SPRN_TCR, val);
71}
72
73static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
74 size_t count, loff_t *ppos)
75{
76 booke_wdt_ping();
77 return count;
78}
79
80static struct watchdog_info ident = {
81 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
82 .identity = "PowerPC Book-E Watchdog",
83};
84
85static long booke_wdt_ioctl(struct file *file,
86 unsigned int cmd, unsigned long arg)
87{
88 u32 tmp = 0;
89 u32 __user *p = (u32 __user *)arg;
90
91 switch (cmd) {
92 case WDIOC_GETSUPPORT:
93 if (copy_to_user(arg, &ident, sizeof(struct watchdog_info)))
94 return -EFAULT;
95 case WDIOC_GETSTATUS:
96 return put_user(ident.options, p);
97 case WDIOC_GETBOOTSTATUS:
98
99 tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
100
101 return (tmp ? 1 : 0);
102 case WDIOC_SETOPTIONS:
103 if (get_user(tmp, p))
104 return -EINVAL;
105 if (tmp == WDIOS_ENABLECARD) {
106 booke_wdt_ping();
107 break;
108 } else
109 return -EINVAL;
110 return 0;
111 case WDIOC_KEEPALIVE:
112 booke_wdt_ping();
113 return 0;
114 case WDIOC_SETTIMEOUT:
115 if (get_user(booke_wdt_period, p))
116 return -EFAULT;
117 mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP(0)) |
118 WDTP(booke_wdt_period));
119 return 0;
120 case WDIOC_GETTIMEOUT:
121 return put_user(booke_wdt_period, p);
122 default:
123 return -ENOTTY;
124 }
125
126 return 0;
127}
128
129static int booke_wdt_open(struct inode *inode, struct file *file)
130{
131 spin_lock(&booke_wdt_lock);
132 if (booke_wdt_enabled == 0) {
133 booke_wdt_enabled = 1;
134 on_each_cpu(__booke_wdt_enable, NULL, 0);
135 printk(KERN_INFO
136 "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
137 booke_wdt_period);
138 }
139 spin_unlock(&booke_wdt_lock);
140
141 return nonseekable_open(inode, file);
142}
143
144static const struct file_operations booke_wdt_fops = {
145 .owner = THIS_MODULE,
146 .llseek = no_llseek,
147 .write = booke_wdt_write,
148 .unlocked_ioctl = booke_wdt_ioctl,
149 .open = booke_wdt_open,
150};
151
152static struct miscdevice booke_wdt_miscdev = {
153 .minor = WATCHDOG_MINOR,
154 .name = "watchdog",
155 .fops = &booke_wdt_fops,
156};
157
158static void __exit booke_wdt_exit(void)
159{
160 misc_deregister(&booke_wdt_miscdev);
161}
162
163static int __init booke_wdt_init(void)
164{
165 int ret = 0;
166
167 printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
168 ident.firmware_version = cur_cpu_spec->pvr_value;
169
170 ret = misc_register(&booke_wdt_miscdev);
171 if (ret) {
172 printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
173 WATCHDOG_MINOR, ret);
174 return ret;
175 }
176
177 spin_lock(&booke_wdt_lock);
178 if (booke_wdt_enabled == 1) {
179 printk(KERN_INFO
180 "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
181 booke_wdt_period);
182 on_each_cpu(__booke_wdt_enable, NULL, 0);
183 }
184 spin_unlock(&booke_wdt_lock);
185
186 return ret;
187}
188device_initcall(booke_wdt_init);
189