2 * drivers/watchdog/m54xx_wdt.c
4 * Watchdog driver for ColdFire MCF547x & MCF548x processors
5 * Copyright 2010 (c) Philippe De Muyter <phdm@macqel.be>
7 * Adapted from the IXP4xx watchdog driver, which carries these notices:
9 * Author: Deepak Saxena <dsaxena@plexity.net>
11 * Copyright 2004 (c) MontaVista, Software, Inc.
12 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14 * This file is licensed under the terms of the GNU General Public
15 * License version 2. This program is licensed "as is" without any
16 * warranty of any kind, whether express or implied.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
26 #include <linux/miscdevice.h>
27 #include <linux/watchdog.h>
28 #include <linux/init.h>
29 #include <linux/bitops.h>
30 #include <linux/ioport.h>
31 #include <linux/uaccess.h>
33 #include <asm/coldfire.h>
34 #include <asm/m54xxsim.h>
35 #include <asm/m54xxgpt.h>
37 static bool nowayout = WATCHDOG_NOWAYOUT;
38 static unsigned int heartbeat = 30; /* (secs) Default is 0.5 minute */
39 static unsigned long wdt_status;
42 #define WDT_OK_TO_CLOSE 1
44 static void wdt_enable(void)
48 /* preserve GPIO usage, if any */
49 gms0 = __raw_readl(MCF_GPT_GMS0);
50 if (gms0 & MCF_GPT_GMS_TMS_GPIO)
51 gms0 &= (MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_GPIO_MASK
54 gms0 = MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_OD;
55 __raw_writel(gms0, MCF_GPT_GMS0);
56 __raw_writel(MCF_GPT_GCIR_PRE(heartbeat*(MCF_BUSCLK/0xffff)) |
57 MCF_GPT_GCIR_CNT(0xffff), MCF_GPT_GCIR0);
58 gms0 |= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE;
59 __raw_writel(gms0, MCF_GPT_GMS0);
62 static void wdt_disable(void)
66 /* disable watchdog */
67 gms0 = __raw_readl(MCF_GPT_GMS0);
68 gms0 &= ~(MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE);
69 __raw_writel(gms0, MCF_GPT_GMS0);
72 static void wdt_keepalive(void)
76 gms0 = __raw_readl(MCF_GPT_GMS0);
77 gms0 |= MCF_GPT_GMS_OCPW(0xA5);
78 __raw_writel(gms0, MCF_GPT_GMS0);
81 static int m54xx_wdt_open(struct inode *inode, struct file *file)
83 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
86 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
88 return nonseekable_open(inode, file);
91 static ssize_t m54xx_wdt_write(struct file *file, const char *data,
92 size_t len, loff_t *ppos)
98 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
100 for (i = 0; i != len; i++) {
103 if (get_user(c, data + i))
106 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
114 static const struct watchdog_info ident = {
115 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
117 .identity = "Coldfire M54xx Watchdog",
120 static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
127 case WDIOC_GETSUPPORT:
128 ret = copy_to_user((struct watchdog_info *)arg, &ident,
129 sizeof(ident)) ? -EFAULT : 0;
132 case WDIOC_GETSTATUS:
133 ret = put_user(0, (int *)arg);
136 case WDIOC_GETBOOTSTATUS:
137 ret = put_user(0, (int *)arg);
140 case WDIOC_KEEPALIVE:
145 case WDIOC_SETTIMEOUT:
146 ret = get_user(time, (int *)arg);
150 if (time <= 0 || time > 30) {
159 case WDIOC_GETTIMEOUT:
160 ret = put_user(heartbeat, (int *)arg);
166 static int m54xx_wdt_release(struct inode *inode, struct file *file)
168 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
171 pr_crit("Device closed unexpectedly - timer will not stop\n");
174 clear_bit(WDT_IN_USE, &wdt_status);
175 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
181 static const struct file_operations m54xx_wdt_fops = {
182 .owner = THIS_MODULE,
184 .write = m54xx_wdt_write,
185 .unlocked_ioctl = m54xx_wdt_ioctl,
186 .open = m54xx_wdt_open,
187 .release = m54xx_wdt_release,
190 static struct miscdevice m54xx_wdt_miscdev = {
191 .minor = WATCHDOG_MINOR,
193 .fops = &m54xx_wdt_fops,
196 static int __init m54xx_wdt_init(void)
198 if (!request_mem_region(MCF_GPT_GCIR0, 4, "Coldfire M54xx Watchdog")) {
199 pr_warn("I/O region busy\n");
202 pr_info("driver is loaded\n");
204 return misc_register(&m54xx_wdt_miscdev);
207 static void __exit m54xx_wdt_exit(void)
209 misc_deregister(&m54xx_wdt_miscdev);
210 release_mem_region(MCF_GPT_GCIR0, 4);
213 module_init(m54xx_wdt_init);
214 module_exit(m54xx_wdt_exit);
216 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
217 MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
219 module_param(heartbeat, int, 0);
220 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 30s)");
222 module_param(nowayout, bool, 0);
223 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
225 MODULE_LICENSE("GPL");