| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 1 | /* | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 2 |  * Watchdog timer for PowerPC Book-E systems | 
 | 3 |  * | 
 | 4 |  * Author: Matthew McClintock | 
| Kumar Gala | 4c8d3d9 | 2005-11-13 16:06:30 -0800 | [diff] [blame] | 5 |  * Maintainer: Kumar Gala <galak@kernel.crashing.org> | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 6 |  * | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 7 |  * Copyright 2005, 2008 Freescale Semiconductor Inc. | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 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 |  | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 15 | #include <linux/module.h> | 
 | 16 | #include <linux/fs.h> | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 17 | #include <linux/smp.h> | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 18 | #include <linux/miscdevice.h> | 
 | 19 | #include <linux/notifier.h> | 
 | 20 | #include <linux/watchdog.h> | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 21 | #include <linux/uaccess.h> | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 22 |  | 
 | 23 | #include <asm/reg_booke.h> | 
| Kumar Gala | 39cdc4b | 2005-09-03 15:55:39 -0700 | [diff] [blame] | 24 | #include <asm/system.h> | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 25 |  | 
| Dave Jiang | 40ebbcb | 2007-04-12 13:34:55 -0700 | [diff] [blame] | 26 | /* If the kernel parameter wdt=1, the watchdog will be enabled at boot. | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 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 | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 35 | #define WDT_PERIOD_DEFAULT 63	/* Ex. wdt_period=28 bus=333Mhz,reset=~40sec */ | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 36 | #else | 
| Stefan Roese | f31909c | 2007-02-07 09:45:55 +0100 | [diff] [blame] | 37 | #define WDT_PERIOD_DEFAULT 3	/* Refer to the PPC40x and PPC4xx manuals */ | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 38 | #endif				/* for timing information */ | 
 | 39 |  | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 40 | u32 booke_wdt_enabled; | 
| Kumar Gala | 39cdc4b | 2005-09-03 15:55:39 -0700 | [diff] [blame] | 41 | u32 booke_wdt_period = WDT_PERIOD_DEFAULT; | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 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 |  | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 49 | static DEFINE_SPINLOCK(booke_wdt_lock); | 
 | 50 |  | 
 | 51 | static void __booke_wdt_ping(void *data) | 
| Stefan Roese | f31909c | 2007-02-07 09:45:55 +0100 | [diff] [blame] | 52 | { | 
 | 53 | 	mtspr(SPRN_TSR, TSR_ENW|TSR_WIS); | 
 | 54 | } | 
 | 55 |  | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 56 | static void booke_wdt_ping(void) | 
 | 57 | { | 
| Ingo Molnar | f6f88e9 | 2008-07-15 22:08:52 +0200 | [diff] [blame] | 58 | 	on_each_cpu(__booke_wdt_ping, NULL, 0); | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 59 | } | 
 | 60 |  | 
 | 61 | static void __booke_wdt_enable(void *data) | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 62 | { | 
 | 63 | 	u32 val; | 
 | 64 |  | 
| Stefan Roese | f31909c | 2007-02-07 09:45:55 +0100 | [diff] [blame] | 65 | 	/* clear status before enabling watchdog */ | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 66 | 	__booke_wdt_ping(NULL); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 67 | 	val = mfspr(SPRN_TCR); | 
| Kumar Gala | 39cdc4b | 2005-09-03 15:55:39 -0700 | [diff] [blame] | 68 | 	val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period)); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 69 |  | 
 | 70 | 	mtspr(SPRN_TCR, val); | 
 | 71 | } | 
 | 72 |  | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 73 | static ssize_t booke_wdt_write(struct file *file, const char __user *buf, | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 74 | 				size_t count, loff_t *ppos) | 
 | 75 | { | 
 | 76 | 	booke_wdt_ping(); | 
 | 77 | 	return count; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | static struct watchdog_info ident = { | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 81 | 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, | 
 | 82 | 	.identity = "PowerPC Book-E Watchdog", | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 83 | }; | 
 | 84 |  | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 85 | static long booke_wdt_ioctl(struct file *file, | 
 | 86 | 				unsigned int cmd, unsigned long arg) | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 87 | { | 
 | 88 | 	u32 tmp = 0; | 
| Al Viro | 538bacf | 2005-12-15 09:18:20 +0000 | [diff] [blame] | 89 | 	u32 __user *p = (u32 __user *)arg; | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 90 |  | 
 | 91 | 	switch (cmd) { | 
 | 92 | 	case WDIOC_GETSUPPORT: | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 93 | 		if (copy_to_user(arg, &ident, sizeof(struct watchdog_info))) | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 94 | 			return -EFAULT; | 
 | 95 | 	case WDIOC_GETSTATUS: | 
| Al Viro | 538bacf | 2005-12-15 09:18:20 +0000 | [diff] [blame] | 96 | 		return put_user(ident.options, p); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 97 | 	case WDIOC_GETBOOTSTATUS: | 
 | 98 | 		/* XXX: something is clearing TSR */ | 
 | 99 | 		tmp = mfspr(SPRN_TSR) & TSR_WRS(3); | 
 | 100 | 		/* returns 1 if last reset was caused by the WDT */ | 
 | 101 | 		return (tmp ? 1 : 0); | 
| Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 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; | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 111 | 	case WDIOC_KEEPALIVE: | 
 | 112 | 		booke_wdt_ping(); | 
 | 113 | 		return 0; | 
 | 114 | 	case WDIOC_SETTIMEOUT: | 
| Al Viro | 538bacf | 2005-12-15 09:18:20 +0000 | [diff] [blame] | 115 | 		if (get_user(booke_wdt_period, p)) | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 116 | 			return -EFAULT; | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 117 | 		mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP(0)) | | 
 | 118 | 						WDTP(booke_wdt_period)); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 119 | 		return 0; | 
 | 120 | 	case WDIOC_GETTIMEOUT: | 
| Al Viro | 538bacf | 2005-12-15 09:18:20 +0000 | [diff] [blame] | 121 | 		return put_user(booke_wdt_period, p); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 122 | 	default: | 
| Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 123 | 		return -ENOTTY; | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 124 | 	} | 
 | 125 |  | 
 | 126 | 	return 0; | 
 | 127 | } | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 128 |  | 
 | 129 | static int booke_wdt_open(struct inode *inode, struct file *file) | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 130 | { | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 131 | 	spin_lock(&booke_wdt_lock); | 
| Kumar Gala | 39cdc4b | 2005-09-03 15:55:39 -0700 | [diff] [blame] | 132 | 	if (booke_wdt_enabled == 0) { | 
 | 133 | 		booke_wdt_enabled = 1; | 
| Ingo Molnar | f6f88e9 | 2008-07-15 22:08:52 +0200 | [diff] [blame] | 134 | 		on_each_cpu(__booke_wdt_enable, NULL, 0); | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 135 | 		printk(KERN_INFO | 
 | 136 | 		      "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n", | 
 | 137 | 				booke_wdt_period); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 138 | 	} | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 139 | 	spin_unlock(&booke_wdt_lock); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 140 |  | 
| Wim Van Sebroeck | ec9505a | 2007-07-20 20:41:37 +0000 | [diff] [blame] | 141 | 	return nonseekable_open(inode, file); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 142 | } | 
 | 143 |  | 
| Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 144 | static const struct file_operations booke_wdt_fops = { | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 145 | 	.owner = THIS_MODULE, | 
 | 146 | 	.llseek = no_llseek, | 
 | 147 | 	.write = booke_wdt_write, | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 148 | 	.unlocked_ioctl = booke_wdt_ioctl, | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 149 | 	.open = booke_wdt_open, | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 150 | }; | 
 | 151 |  | 
 | 152 | static struct miscdevice booke_wdt_miscdev = { | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 153 | 	.minor = WATCHDOG_MINOR, | 
 | 154 | 	.name = "watchdog", | 
 | 155 | 	.fops = &booke_wdt_fops, | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 156 | }; | 
 | 157 |  | 
 | 158 | static void __exit booke_wdt_exit(void) | 
 | 159 | { | 
 | 160 | 	misc_deregister(&booke_wdt_miscdev); | 
 | 161 | } | 
 | 162 |  | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 163 | static int __init booke_wdt_init(void) | 
 | 164 | { | 
 | 165 | 	int ret = 0; | 
 | 166 |  | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 167 | 	printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n"); | 
| Al Viro | a78719c | 2005-12-16 22:35:28 +0000 | [diff] [blame] | 168 | 	ident.firmware_version = cur_cpu_spec->pvr_value; | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 169 |  | 
 | 170 | 	ret = misc_register(&booke_wdt_miscdev); | 
 | 171 | 	if (ret) { | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 172 | 		printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n", | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 173 | 				WATCHDOG_MINOR, ret); | 
 | 174 | 		return ret; | 
 | 175 | 	} | 
 | 176 |  | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 177 | 	spin_lock(&booke_wdt_lock); | 
| Kumar Gala | 39cdc4b | 2005-09-03 15:55:39 -0700 | [diff] [blame] | 178 | 	if (booke_wdt_enabled == 1) { | 
| Alan Cox | 00e9c20 | 2008-05-19 14:06:36 +0100 | [diff] [blame] | 179 | 		printk(KERN_INFO | 
 | 180 | 		      "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n", | 
 | 181 | 				booke_wdt_period); | 
| Ingo Molnar | f6f88e9 | 2008-07-15 22:08:52 +0200 | [diff] [blame] | 182 | 		on_each_cpu(__booke_wdt_enable, NULL, 0); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 183 | 	} | 
| Chen Gong | f172ddc | 2008-04-29 16:42:05 +0800 | [diff] [blame] | 184 | 	spin_unlock(&booke_wdt_lock); | 
| Kumar Gala | a2f40cc | 2005-09-03 15:55:33 -0700 | [diff] [blame] | 185 |  | 
 | 186 | 	return ret; | 
 | 187 | } | 
 | 188 | device_initcall(booke_wdt_init); |