1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/watchdog/m54xx_wdt.c
5 * Watchdog driver for ColdFire MCF547x & MCF548x processors
6 * Copyright 2010 (c) Philippe De Muyter <phdm@macqel.be>
8 * Adapted from the IXP4xx watchdog driver, which carries these notices:
10 * Author: Deepak Saxena <dsaxena@plexity.net>
12 * Copyright 2004 (c) MontaVista, Software, Inc.
13 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
24 #include <linux/miscdevice.h>
25 #include <linux/watchdog.h>
26 #include <linux/init.h>
27 #include <linux/bitops.h>
28 #include <linux/ioport.h>
29 #include <linux/uaccess.h>
32 #include <asm/coldfire.h>
33 #include <asm/m54xxsim.h>
34 #include <asm/m54xxgpt.h>
36 static bool nowayout
= WATCHDOG_NOWAYOUT
;
37 static unsigned int heartbeat
= 30; /* (secs) Default is 0.5 minute */
38 static unsigned long wdt_status
;
41 #define WDT_OK_TO_CLOSE 1
43 static void wdt_enable(void)
47 /* preserve GPIO usage, if any */
48 gms0
= __raw_readl(MCF_GPT_GMS0
);
49 if (gms0
& MCF_GPT_GMS_TMS_GPIO
)
50 gms0
&= (MCF_GPT_GMS_TMS_GPIO
| MCF_GPT_GMS_GPIO_MASK
53 gms0
= MCF_GPT_GMS_TMS_GPIO
| MCF_GPT_GMS_OD
;
54 __raw_writel(gms0
, MCF_GPT_GMS0
);
55 __raw_writel(MCF_GPT_GCIR_PRE(heartbeat
*(MCF_BUSCLK
/0xffff)) |
56 MCF_GPT_GCIR_CNT(0xffff), MCF_GPT_GCIR0
);
57 gms0
|= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN
| MCF_GPT_GMS_CE
;
58 __raw_writel(gms0
, MCF_GPT_GMS0
);
61 static void wdt_disable(void)
65 /* disable watchdog */
66 gms0
= __raw_readl(MCF_GPT_GMS0
);
67 gms0
&= ~(MCF_GPT_GMS_WDEN
| MCF_GPT_GMS_CE
);
68 __raw_writel(gms0
, MCF_GPT_GMS0
);
71 static void wdt_keepalive(void)
75 gms0
= __raw_readl(MCF_GPT_GMS0
);
76 gms0
|= MCF_GPT_GMS_OCPW(0xA5);
77 __raw_writel(gms0
, MCF_GPT_GMS0
);
80 static int m54xx_wdt_open(struct inode
*inode
, struct file
*file
)
82 if (test_and_set_bit(WDT_IN_USE
, &wdt_status
))
85 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
87 return stream_open(inode
, file
);
90 static ssize_t
m54xx_wdt_write(struct file
*file
, const char *data
,
91 size_t len
, loff_t
*ppos
)
97 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
99 for (i
= 0; i
!= len
; i
++) {
102 if (get_user(c
, data
+ i
))
105 set_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
113 static const struct watchdog_info ident
= {
114 .options
= WDIOF_MAGICCLOSE
| WDIOF_SETTIMEOUT
|
116 .identity
= "Coldfire M54xx Watchdog",
119 static long m54xx_wdt_ioctl(struct file
*file
, unsigned int cmd
,
126 case WDIOC_GETSUPPORT
:
127 ret
= copy_to_user((struct watchdog_info
*)arg
, &ident
,
128 sizeof(ident
)) ? -EFAULT
: 0;
131 case WDIOC_GETSTATUS
:
132 ret
= put_user(0, (int *)arg
);
135 case WDIOC_GETBOOTSTATUS
:
136 ret
= put_user(0, (int *)arg
);
139 case WDIOC_KEEPALIVE
:
144 case WDIOC_SETTIMEOUT
:
145 ret
= get_user(time
, (int *)arg
);
149 if (time
<= 0 || time
> 30) {
158 case WDIOC_GETTIMEOUT
:
159 ret
= put_user(heartbeat
, (int *)arg
);
165 static int m54xx_wdt_release(struct inode
*inode
, struct file
*file
)
167 if (test_bit(WDT_OK_TO_CLOSE
, &wdt_status
))
170 pr_crit("Device closed unexpectedly - timer will not stop\n");
173 clear_bit(WDT_IN_USE
, &wdt_status
);
174 clear_bit(WDT_OK_TO_CLOSE
, &wdt_status
);
180 static const struct file_operations m54xx_wdt_fops
= {
181 .owner
= THIS_MODULE
,
182 .write
= m54xx_wdt_write
,
183 .unlocked_ioctl
= m54xx_wdt_ioctl
,
184 .compat_ioctl
= compat_ptr_ioctl
,
185 .open
= m54xx_wdt_open
,
186 .release
= m54xx_wdt_release
,
189 static struct miscdevice m54xx_wdt_miscdev
= {
190 .minor
= WATCHDOG_MINOR
,
192 .fops
= &m54xx_wdt_fops
,
195 static int __init
m54xx_wdt_init(void)
197 if (!request_mem_region(MCF_GPT_GCIR0
, 4, "Coldfire M54xx Watchdog")) {
198 pr_warn("I/O region busy\n");
201 pr_info("driver is loaded\n");
203 return misc_register(&m54xx_wdt_miscdev
);
206 static void __exit
m54xx_wdt_exit(void)
208 misc_deregister(&m54xx_wdt_miscdev
);
209 release_mem_region(MCF_GPT_GCIR0
, 4);
212 module_init(m54xx_wdt_init
);
213 module_exit(m54xx_wdt_exit
);
215 MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
216 MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
218 module_param(heartbeat
, int, 0);
219 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds (default 30s)");
221 module_param(nowayout
, bool, 0);
222 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started");
224 MODULE_LICENSE("GPL");