blob: 225398fd504932df76ef895986742e8190e67d6e [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 Gongf172ddc2008-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 Gongf172ddc2008-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>
Alan Cox00e9c202008-05-19 14:06:36 +010021#include <linux/uaccess.h>
Kumar Galaa2f40cc2005-09-03 15:55:33 -070022
23#include <asm/reg_booke.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
Alan Cox00e9c202008-05-19 14:06:36 +010035#define WDT_PERIOD_DEFAULT 63 /* Ex. wdt_period=28 bus=333Mhz,reset=~40sec */
Kumar Galaa2f40cc2005-09-03 15:55:33 -070036#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 Gongf172ddc2008-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))
Matthias Fuchs0a0e9e02008-11-05 21:53:56 +010045#define WDTP_MASK (WDTP(0))
Kumar Galaa2f40cc2005-09-03 15:55:33 -070046#else
47#define WDTP(x) (TCR_WP(x))
Matthias Fuchs0a0e9e02008-11-05 21:53:56 +010048#define WDTP_MASK (TCR_WP_MASK)
Kumar Galaa2f40cc2005-09-03 15:55:33 -070049#endif
50
Chen Gongf172ddc2008-04-29 16:42:05 +080051static DEFINE_SPINLOCK(booke_wdt_lock);
52
53static void __booke_wdt_ping(void *data)
Stefan Roesef31909c2007-02-07 09:45:55 +010054{
55 mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
56}
57
Chen Gongf172ddc2008-04-29 16:42:05 +080058static void booke_wdt_ping(void)
59{
Ingo Molnarf6f88e92008-07-15 22:08:52 +020060 on_each_cpu(__booke_wdt_ping, NULL, 0);
Chen Gongf172ddc2008-04-29 16:42:05 +080061}
62
63static void __booke_wdt_enable(void *data)
Kumar Galaa2f40cc2005-09-03 15:55:33 -070064{
65 u32 val;
66
Stefan Roesef31909c2007-02-07 09:45:55 +010067 /* clear status before enabling watchdog */
Chen Gongf172ddc2008-04-29 16:42:05 +080068 __booke_wdt_ping(NULL);
Kumar Galaa2f40cc2005-09-03 15:55:33 -070069 val = mfspr(SPRN_TCR);
Matthias Fuchs0a0e9e02008-11-05 21:53:56 +010070 val &= ~WDTP_MASK;
Kumar Gala39cdc4b2005-09-03 15:55:39 -070071 val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
Kumar Galaa2f40cc2005-09-03 15:55:33 -070072
73 mtspr(SPRN_TCR, val);
74}
75
Chen Gongf172ddc2008-04-29 16:42:05 +080076static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
Kumar Galaa2f40cc2005-09-03 15:55:33 -070077 size_t count, loff_t *ppos)
78{
79 booke_wdt_ping();
80 return count;
81}
82
83static struct watchdog_info ident = {
Chen Gongf172ddc2008-04-29 16:42:05 +080084 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
85 .identity = "PowerPC Book-E Watchdog",
Kumar Galaa2f40cc2005-09-03 15:55:33 -070086};
87
Alan Cox00e9c202008-05-19 14:06:36 +010088static long booke_wdt_ioctl(struct file *file,
89 unsigned int cmd, unsigned long arg)
Kumar Galaa2f40cc2005-09-03 15:55:33 -070090{
91 u32 tmp = 0;
Al Viro538bacf2005-12-15 09:18:20 +000092 u32 __user *p = (u32 __user *)arg;
Kumar Galaa2f40cc2005-09-03 15:55:33 -070093
94 switch (cmd) {
95 case WDIOC_GETSUPPORT:
Alan Cox00e9c202008-05-19 14:06:36 +010096 if (copy_to_user(arg, &ident, sizeof(struct watchdog_info)))
Kumar Galaa2f40cc2005-09-03 15:55:33 -070097 return -EFAULT;
98 case WDIOC_GETSTATUS:
Al Viro538bacf2005-12-15 09:18:20 +000099 return put_user(ident.options, p);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700100 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);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000105 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;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700114 case WDIOC_KEEPALIVE:
115 booke_wdt_ping();
116 return 0;
117 case WDIOC_SETTIMEOUT:
Al Viro538bacf2005-12-15 09:18:20 +0000118 if (get_user(booke_wdt_period, p))
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700119 return -EFAULT;
Matthias Fuchs0a0e9e02008-11-05 21:53:56 +0100120 mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP_MASK) |
Alan Cox00e9c202008-05-19 14:06:36 +0100121 WDTP(booke_wdt_period));
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700122 return 0;
123 case WDIOC_GETTIMEOUT:
Al Viro538bacf2005-12-15 09:18:20 +0000124 return put_user(booke_wdt_period, p);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700125 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200126 return -ENOTTY;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700127 }
128
129 return 0;
130}
Chen Gongf172ddc2008-04-29 16:42:05 +0800131
132static int booke_wdt_open(struct inode *inode, struct file *file)
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700133{
Chen Gongf172ddc2008-04-29 16:42:05 +0800134 spin_lock(&booke_wdt_lock);
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700135 if (booke_wdt_enabled == 0) {
136 booke_wdt_enabled = 1;
Ingo Molnarf6f88e92008-07-15 22:08:52 +0200137 on_each_cpu(__booke_wdt_enable, NULL, 0);
Alan Cox00e9c202008-05-19 14:06:36 +0100138 printk(KERN_INFO
139 "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
140 booke_wdt_period);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700141 }
Chen Gongf172ddc2008-04-29 16:42:05 +0800142 spin_unlock(&booke_wdt_lock);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700143
Wim Van Sebroeckec9505a2007-07-20 20:41:37 +0000144 return nonseekable_open(inode, file);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700145}
146
Arjan van de Ven62322d22006-07-03 00:24:21 -0700147static const struct file_operations booke_wdt_fops = {
Chen Gongf172ddc2008-04-29 16:42:05 +0800148 .owner = THIS_MODULE,
149 .llseek = no_llseek,
150 .write = booke_wdt_write,
Alan Cox00e9c202008-05-19 14:06:36 +0100151 .unlocked_ioctl = booke_wdt_ioctl,
Chen Gongf172ddc2008-04-29 16:42:05 +0800152 .open = booke_wdt_open,
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700153};
154
155static struct miscdevice booke_wdt_miscdev = {
Chen Gongf172ddc2008-04-29 16:42:05 +0800156 .minor = WATCHDOG_MINOR,
157 .name = "watchdog",
158 .fops = &booke_wdt_fops,
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700159};
160
161static void __exit booke_wdt_exit(void)
162{
163 misc_deregister(&booke_wdt_miscdev);
164}
165
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700166static int __init booke_wdt_init(void)
167{
168 int ret = 0;
169
Chen Gongf172ddc2008-04-29 16:42:05 +0800170 printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
Al Viroa78719c2005-12-16 22:35:28 +0000171 ident.firmware_version = cur_cpu_spec->pvr_value;
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700172
173 ret = misc_register(&booke_wdt_miscdev);
174 if (ret) {
Chen Gongf172ddc2008-04-29 16:42:05 +0800175 printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700176 WATCHDOG_MINOR, ret);
177 return ret;
178 }
179
Chen Gongf172ddc2008-04-29 16:42:05 +0800180 spin_lock(&booke_wdt_lock);
Kumar Gala39cdc4b2005-09-03 15:55:39 -0700181 if (booke_wdt_enabled == 1) {
Alan Cox00e9c202008-05-19 14:06:36 +0100182 printk(KERN_INFO
183 "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
184 booke_wdt_period);
Ingo Molnarf6f88e92008-07-15 22:08:52 +0200185 on_each_cpu(__booke_wdt_enable, NULL, 0);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700186 }
Chen Gongf172ddc2008-04-29 16:42:05 +0800187 spin_unlock(&booke_wdt_lock);
Kumar Galaa2f40cc2005-09-03 15:55:33 -0700188
189 return ret;
190}
191device_initcall(booke_wdt_init);