blob: c1ba0db485016c04595fd4a872fd5b0363604291 [file] [log] [blame]
Kumar Galaa2f40cc2005-09-03 15:55:33 -07001/*
Kumar Galaa2f40cc2005-09-03 15:55:33 -07002 * Watchdog timer for PowerPC Book-E systems
3 *
4 * Author: Matthew McClintock
Kumar Gala4c8d3d92005-11-13 16:06:30 -08005 * Maintainer: Kumar Gala <galak@kernel.crashing.org>
Kumar Galaa2f40cc2005-09-03 15:55:33 -07006 *
Chen Gongf172ddc62008-04-29 16:42:05 +08007 * Copyright 2005, 2008 Freescale Semiconductor Inc.
Kumar Galaa2f40cc2005-09-03 15:55:33 -07008 *
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
Kumar Galaa2f40cc2005-09-03 15:55:33 -070015#include <linux/module.h>
16#include <linux/fs.h>
Chen Gongf172ddc62008-04-29 16:42:05 +080017#include <linux/smp.h>
Kumar Galaa2f40cc2005-09-03 15:55:33 -070018#include <linux/miscdevice.h>
19#include <linux/notifier.h>
20#include <linux/watchdog.h>
21
22#include <asm/reg_booke.h>
23#include <asm/uaccess.h>
Kumar Gala39cdc4b2005-09-03 15:55:39 -070024#include <asm/system.h>
Kumar Galaa2f40cc2005-09-03 15:55:33 -070025
Dave Jiang40ebbcb2007-04-12 13:34:55 -070026/* If the kernel parameter wdt=1, the watchdog will be enabled at boot.
Kumar Galaa2f40cc2005-09-03 15:55:33 -070027 * 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
35#define WDT_PERIOD_DEFAULT 63 /* Ex. wdt_period=28 bus=333Mhz , reset=~40sec */
36#else
Stefan Roesef31909c2007-02-07 09:45:55 +010037#define WDT_PERIOD_DEFAULT 3 /* Refer to the PPC40x and PPC4xx manuals */
Kumar Galaa2f40cc2005-09-03 15:55:33 -070038#endif /* for timing information */
39
Chen Gongf172ddc62008-04-29 16:42:05 +080040u32 booke_wdt_enabled;
Kumar Gala39cdc4b2005-09-03 15:55:39 -070041u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
Kumar Galaa2f40cc2005-09-03 15:55:33 -070042
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
Chen Gongf172ddc62008-04-29 16:42:05 +080049static DEFINE_SPINLOCK(booke_wdt_lock);
50
51static void __booke_wdt_ping(void *data)
Stefan Roesef31909c2007-02-07 09:45:55 +010052{
53 mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
54}
55
Chen Gongf172ddc62008-04-29 16:42:05 +080056static void booke_wdt_ping(void)
57{
58 on_each_cpu(__booke_wdt_ping, NULL, 0, 0);
59}
60
61static void __booke_wdt_enable(void *data)
Kumar Galaa2f40cc2005-09-03 15:55:33 -070062{
63 u32 val;
64
Stefan Roesef31909c2007-02-07 09:45:55 +010065 /* clear status before enabling watchdog */
Chen Gongf172ddc62008-04-29 16:42:05 +080066 __booke_wdt_ping(NULL);
Kumar Galaa2f40cc2005-09-03 15:55:33 -070067 val = mfspr(SPRN_TCR);
Kumar Gala39cdc4b2005-09-03 15:55:39 -070068 val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
Kumar Galaa2f40cc2005-09-03 15:55:33 -070069
70 mtspr(SPRN_TCR, val);
71}
72
Chen Gongf172ddc62008-04-29 16:42:05 +080073static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
Kumar Galaa2f40cc2005-09-03 15:55:33 -070074 size_t count, loff_t *ppos)
75{
76 booke_wdt_ping();
77 return count;
78}
79
80static struct watchdog_info ident = {
Chen Gongf172ddc62008-04-29 16:42:05 +080081 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
82 .identity = "PowerPC Book-E Watchdog",
Kumar Galaa2f40cc2005-09-03 15:55:33 -070083};
84
Chen Gongf172ddc62008-04-29 16:42:05 +080085static int booke_wdt_ioctl(struct inode *inode, struct file *file,
Kumar Galaa2f40cc2005-09-03 15:55:33 -070086 unsigned int cmd, unsigned long arg)
87{
88 u32 tmp = 0;
Al Viro538bacf2005-12-15 09:18:20 +000089 u32 __user *p = (u32 __user *)arg;
Kumar Galaa2f40cc2005-09-03 15:55:33 -070090
91 switch (cmd) {
92 case WDIOC_GETSUPPORT:
Chen Gongf172ddc62008-04-29 16:42:05 +080093 if (copy_to_user((struct watchdog_info __user *)arg, &ident,
Kumar Galaa2f40cc2005-09-03 15:55:33 -070094 sizeof(struct watchdog_info)))
95 return -EFAULT;
96 case WDIOC_GETSTATUS:
Al Viro538bacf2005-12-15 09:18:20 +000097 return put_user(ident.options, p);
Kumar Galaa2f40cc2005-09-03 15:55:33 -070098 case WDIOC_GETBOOTSTATUS:
99 /* XXX: something is clearing TSR */
100 tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
101 /* returns 1 if last reset was caused by the WDT */
102 return (tmp ? 1 : 0);
103 case WDIOC_KEEPALIVE:
104 booke_wdt_ping();
105 return 0;
106 case WDIOC_SETTIMEOUT:
Al Viro538bacf2005-12-15 09:18:20 +0000107 if (get_user(booke_wdt_period, p))
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700108 return -EFAULT;
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700109 mtspr(SPRN_TCR, (mfspr(SPRN_TCR)&~WDTP(0))|WDTP(booke_wdt_period));
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700110 return 0;
111 case WDIOC_GETTIMEOUT:
Al Viro538bacf2005-12-15 09:18:20 +0000112 return put_user(booke_wdt_period, p);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700113 case WDIOC_SETOPTIONS:
Al Viro538bacf2005-12-15 09:18:20 +0000114 if (get_user(tmp, p))
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700115 return -EINVAL;
116 if (tmp == WDIOS_ENABLECARD) {
117 booke_wdt_ping();
118 break;
119 } else
120 return -EINVAL;
121 return 0;
122 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200123 return -ENOTTY;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700124 }
125
126 return 0;
127}
Chen Gongf172ddc62008-04-29 16:42:05 +0800128
129static int booke_wdt_open(struct inode *inode, struct file *file)
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700130{
Chen Gongf172ddc62008-04-29 16:42:05 +0800131 spin_lock(&booke_wdt_lock);
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700132 if (booke_wdt_enabled == 0) {
133 booke_wdt_enabled = 1;
Chen Gongf172ddc62008-04-29 16:42:05 +0800134 on_each_cpu(__booke_wdt_enable, NULL, 0, 0);
135 printk(KERN_INFO "PowerPC Book-E Watchdog Timer Enabled "
136 "(wdt_period=%d)\n", booke_wdt_period);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700137 }
Chen Gongf172ddc62008-04-29 16:42:05 +0800138 spin_unlock(&booke_wdt_lock);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700139
Wim Van Sebroeckec9505a2007-07-20 20:41:37 +0000140 return nonseekable_open(inode, file);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700141}
142
Arjan van de Ven62322d22006-07-03 00:24:21 -0700143static const struct file_operations booke_wdt_fops = {
Chen Gongf172ddc62008-04-29 16:42:05 +0800144 .owner = THIS_MODULE,
145 .llseek = no_llseek,
146 .write = booke_wdt_write,
147 .ioctl = booke_wdt_ioctl,
148 .open = booke_wdt_open,
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700149};
150
151static struct miscdevice booke_wdt_miscdev = {
Chen Gongf172ddc62008-04-29 16:42:05 +0800152 .minor = WATCHDOG_MINOR,
153 .name = "watchdog",
154 .fops = &booke_wdt_fops,
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700155};
156
157static void __exit booke_wdt_exit(void)
158{
159 misc_deregister(&booke_wdt_miscdev);
160}
161
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700162static int __init booke_wdt_init(void)
163{
164 int ret = 0;
165
Chen Gongf172ddc62008-04-29 16:42:05 +0800166 printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
Al Viroa78719c2005-12-16 22:35:28 +0000167 ident.firmware_version = cur_cpu_spec->pvr_value;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700168
169 ret = misc_register(&booke_wdt_miscdev);
170 if (ret) {
Chen Gongf172ddc62008-04-29 16:42:05 +0800171 printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700172 WATCHDOG_MINOR, ret);
173 return ret;
174 }
175
Chen Gongf172ddc62008-04-29 16:42:05 +0800176 spin_lock(&booke_wdt_lock);
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700177 if (booke_wdt_enabled == 1) {
Chen Gongf172ddc62008-04-29 16:42:05 +0800178 printk(KERN_INFO "PowerPC Book-E Watchdog Timer Enabled "
179 "(wdt_period=%d)\n", booke_wdt_period);
180 on_each_cpu(__booke_wdt_enable, NULL, 0, 0);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700181 }
Chen Gongf172ddc62008-04-29 16:42:05 +0800182 spin_unlock(&booke_wdt_lock);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700183
184 return ret;
185}
186device_initcall(booke_wdt_init);