blob: e3cefc538b40476b341aeed5e4f61a874b64baa4 [file] [log] [blame]
Kumar Galaa2f40cc2005-09-03 15:55:33 -07001/*
2 * drivers/char/watchdog/booke_wdt.c
3 *
4 * Watchdog timer for PowerPC Book-E systems
5 *
6 * Author: Matthew McClintock
Kumar Gala4c8d3d92005-11-13 16:06:30 -08007 * Maintainer: Kumar Gala <galak@kernel.crashing.org>
Kumar Galaa2f40cc2005-09-03 15:55:33 -07008 *
9 * Copyright 2005 Freescale Semiconductor Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
Kumar Galaa2f40cc2005-09-03 15:55:33 -070017#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/miscdevice.h>
20#include <linux/notifier.h>
21#include <linux/watchdog.h>
22
23#include <asm/reg_booke.h>
24#include <asm/uaccess.h>
Kumar Gala39cdc4b2005-09-03 15:55:39 -070025#include <asm/system.h>
Kumar Galaa2f40cc2005-09-03 15:55:33 -070026
27/* If the kernel parameter wdt_enable=1, the watchdog will be enabled at boot.
28 * Also, the wdt_period sets the watchdog timer period timeout.
29 * For E500 cpus the wdt_period sets which bit changing from 0->1 will
30 * trigger a watchog timeout. This watchdog timeout will occur 3 times, the
31 * first time nothing will happen, the second time a watchdog exception will
32 * occur, and the final time the board will reset.
33 */
34
35#ifdef CONFIG_FSL_BOOKE
36#define WDT_PERIOD_DEFAULT 63 /* Ex. wdt_period=28 bus=333Mhz , reset=~40sec */
37#else
38#define WDT_PERIOD_DEFAULT 4 /* Refer to the PPC40x and PPC4xx manuals */
39#endif /* for timing information */
40
Kumar Gala39cdc4b2005-09-03 15:55:39 -070041u32 booke_wdt_enabled = 0;
42u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
Kumar Galaa2f40cc2005-09-03 15:55:33 -070043
44#ifdef CONFIG_FSL_BOOKE
45#define WDTP(x) ((((63-x)&0x3)<<30)|(((63-x)&0x3c)<<15))
46#else
47#define WDTP(x) (TCR_WP(x))
48#endif
49
50/*
51 * booke_wdt_enable:
52 */
53static __inline__ void booke_wdt_enable(void)
54{
55 u32 val;
56
57 val = mfspr(SPRN_TCR);
Kumar Gala39cdc4b2005-09-03 15:55:39 -070058 val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
Kumar Galaa2f40cc2005-09-03 15:55:33 -070059
60 mtspr(SPRN_TCR, val);
61}
62
63/*
64 * booke_wdt_ping:
65 */
66static __inline__ void booke_wdt_ping(void)
67{
68 mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
69}
70
71/*
72 * booke_wdt_write:
73 */
Al Viro538bacf2005-12-15 09:18:20 +000074static ssize_t booke_wdt_write (struct file *file, const char __user *buf,
Kumar Galaa2f40cc2005-09-03 15:55:33 -070075 size_t count, loff_t *ppos)
76{
77 booke_wdt_ping();
78 return count;
79}
80
81static struct watchdog_info ident = {
82 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
83 .firmware_version = 0,
84 .identity = "PowerPC Book-E Watchdog",
85};
86
87/*
88 * booke_wdt_ioctl:
89 */
90static int booke_wdt_ioctl (struct inode *inode, struct file *file,
91 unsigned int cmd, unsigned long arg)
92{
93 u32 tmp = 0;
Al Viro538bacf2005-12-15 09:18:20 +000094 u32 __user *p = (u32 __user *)arg;
Kumar Galaa2f40cc2005-09-03 15:55:33 -070095
96 switch (cmd) {
97 case WDIOC_GETSUPPORT:
Al Viro538bacf2005-12-15 09:18:20 +000098 if (copy_to_user ((struct watchdog_info __user *) arg, &ident,
Kumar Galaa2f40cc2005-09-03 15:55:33 -070099 sizeof(struct watchdog_info)))
100 return -EFAULT;
101 case WDIOC_GETSTATUS:
Al Viro538bacf2005-12-15 09:18:20 +0000102 return put_user(ident.options, p);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700103 case WDIOC_GETBOOTSTATUS:
104 /* XXX: something is clearing TSR */
105 tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
106 /* returns 1 if last reset was caused by the WDT */
107 return (tmp ? 1 : 0);
108 case WDIOC_KEEPALIVE:
109 booke_wdt_ping();
110 return 0;
111 case WDIOC_SETTIMEOUT:
Al Viro538bacf2005-12-15 09:18:20 +0000112 if (get_user(booke_wdt_period, p))
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700113 return -EFAULT;
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700114 mtspr(SPRN_TCR, (mfspr(SPRN_TCR)&~WDTP(0))|WDTP(booke_wdt_period));
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700115 return 0;
116 case WDIOC_GETTIMEOUT:
Al Viro538bacf2005-12-15 09:18:20 +0000117 return put_user(booke_wdt_period, p);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700118 case WDIOC_SETOPTIONS:
Al Viro538bacf2005-12-15 09:18:20 +0000119 if (get_user(tmp, p))
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700120 return -EINVAL;
121 if (tmp == WDIOS_ENABLECARD) {
122 booke_wdt_ping();
123 break;
124 } else
125 return -EINVAL;
126 return 0;
127 default:
128 return -ENOIOCTLCMD;
129 }
130
131 return 0;
132}
133/*
134 * booke_wdt_open:
135 */
136static int booke_wdt_open (struct inode *inode, struct file *file)
137{
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700138 if (booke_wdt_enabled == 0) {
139 booke_wdt_enabled = 1;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700140 booke_wdt_enable();
141 printk (KERN_INFO "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700142 booke_wdt_period);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700143 }
144
145 return 0;
146}
147
Arjan van de Ven62322d22006-07-03 00:24:21 -0700148static const struct file_operations booke_wdt_fops = {
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700149 .owner = THIS_MODULE,
150 .llseek = no_llseek,
151 .write = booke_wdt_write,
152 .ioctl = booke_wdt_ioctl,
153 .open = booke_wdt_open,
154};
155
156static struct miscdevice booke_wdt_miscdev = {
157 .minor = WATCHDOG_MINOR,
158 .name = "watchdog",
159 .fops = &booke_wdt_fops,
160};
161
162static void __exit booke_wdt_exit(void)
163{
164 misc_deregister(&booke_wdt_miscdev);
165}
166
167/*
168 * booke_wdt_init:
169 */
170static int __init booke_wdt_init(void)
171{
172 int ret = 0;
173
174 printk (KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
Al Viroa78719c2005-12-16 22:35:28 +0000175 ident.firmware_version = cur_cpu_spec->pvr_value;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700176
177 ret = misc_register(&booke_wdt_miscdev);
178 if (ret) {
179 printk (KERN_CRIT "Cannot register miscdev on minor=%d (err=%d)\n",
180 WATCHDOG_MINOR, ret);
181 return ret;
182 }
183
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700184 if (booke_wdt_enabled == 1) {
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700185 printk (KERN_INFO "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700186 booke_wdt_period);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700187 booke_wdt_enable();
188 }
189
190 return ret;
191}
192device_initcall(booke_wdt_init);