Loading...
1/*
2 * drivers/watchdog/m54xx_wdt.c
3 *
4 * Watchdog driver for ColdFire MCF547x & MCF548x processors
5 * Copyright 2010 (c) Philippe De Muyter <phdm@macqel.be>
6 *
7 * Adapted from the IXP4xx watchdog driver, which carries these notices:
8 *
9 * Author: Deepak Saxena <dsaxena@plexity.net>
10 *
11 * Copyright 2004 (c) MontaVista, Software, Inc.
12 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
13 *
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.
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/types.h>
24#include <linux/kernel.h>
25#include <linux/fs.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>
32#include <linux/io.h>
33
34#include <asm/coldfire.h>
35#include <asm/m54xxsim.h>
36#include <asm/m54xxgpt.h>
37
38static bool nowayout = WATCHDOG_NOWAYOUT;
39static unsigned int heartbeat = 30; /* (secs) Default is 0.5 minute */
40static unsigned long wdt_status;
41
42#define WDT_IN_USE 0
43#define WDT_OK_TO_CLOSE 1
44
45static void wdt_enable(void)
46{
47 unsigned int gms0;
48
49 /* preserve GPIO usage, if any */
50 gms0 = __raw_readl(MCF_GPT_GMS0);
51 if (gms0 & MCF_GPT_GMS_TMS_GPIO)
52 gms0 &= (MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_GPIO_MASK
53 | MCF_GPT_GMS_OD);
54 else
55 gms0 = MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_OD;
56 __raw_writel(gms0, MCF_GPT_GMS0);
57 __raw_writel(MCF_GPT_GCIR_PRE(heartbeat*(MCF_BUSCLK/0xffff)) |
58 MCF_GPT_GCIR_CNT(0xffff), MCF_GPT_GCIR0);
59 gms0 |= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE;
60 __raw_writel(gms0, MCF_GPT_GMS0);
61}
62
63static void wdt_disable(void)
64{
65 unsigned int gms0;
66
67 /* disable watchdog */
68 gms0 = __raw_readl(MCF_GPT_GMS0);
69 gms0 &= ~(MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE);
70 __raw_writel(gms0, MCF_GPT_GMS0);
71}
72
73static void wdt_keepalive(void)
74{
75 unsigned int gms0;
76
77 gms0 = __raw_readl(MCF_GPT_GMS0);
78 gms0 |= MCF_GPT_GMS_OCPW(0xA5);
79 __raw_writel(gms0, MCF_GPT_GMS0);
80}
81
82static int m54xx_wdt_open(struct inode *inode, struct file *file)
83{
84 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
85 return -EBUSY;
86
87 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
88 wdt_enable();
89 return stream_open(inode, file);
90}
91
92static ssize_t m54xx_wdt_write(struct file *file, const char *data,
93 size_t len, loff_t *ppos)
94{
95 if (len) {
96 if (!nowayout) {
97 size_t i;
98
99 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
100
101 for (i = 0; i != len; i++) {
102 char c;
103
104 if (get_user(c, data + i))
105 return -EFAULT;
106 if (c == 'V')
107 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
108 }
109 }
110 wdt_keepalive();
111 }
112 return len;
113}
114
115static const struct watchdog_info ident = {
116 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
117 WDIOF_KEEPALIVEPING,
118 .identity = "Coldfire M54xx Watchdog",
119};
120
121static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
122 unsigned long arg)
123{
124 int ret = -ENOTTY;
125 int time;
126
127 switch (cmd) {
128 case WDIOC_GETSUPPORT:
129 ret = copy_to_user((struct watchdog_info *)arg, &ident,
130 sizeof(ident)) ? -EFAULT : 0;
131 break;
132
133 case WDIOC_GETSTATUS:
134 ret = put_user(0, (int *)arg);
135 break;
136
137 case WDIOC_GETBOOTSTATUS:
138 ret = put_user(0, (int *)arg);
139 break;
140
141 case WDIOC_KEEPALIVE:
142 wdt_keepalive();
143 ret = 0;
144 break;
145
146 case WDIOC_SETTIMEOUT:
147 ret = get_user(time, (int *)arg);
148 if (ret)
149 break;
150
151 if (time <= 0 || time > 30) {
152 ret = -EINVAL;
153 break;
154 }
155
156 heartbeat = time;
157 wdt_enable();
158 fallthrough;
159
160 case WDIOC_GETTIMEOUT:
161 ret = put_user(heartbeat, (int *)arg);
162 break;
163 }
164 return ret;
165}
166
167static int m54xx_wdt_release(struct inode *inode, struct file *file)
168{
169 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
170 wdt_disable();
171 else {
172 pr_crit("Device closed unexpectedly - timer will not stop\n");
173 wdt_keepalive();
174 }
175 clear_bit(WDT_IN_USE, &wdt_status);
176 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
177
178 return 0;
179}
180
181
182static const struct file_operations m54xx_wdt_fops = {
183 .owner = THIS_MODULE,
184 .llseek = no_llseek,
185 .write = m54xx_wdt_write,
186 .unlocked_ioctl = m54xx_wdt_ioctl,
187 .compat_ioctl = compat_ptr_ioctl,
188 .open = m54xx_wdt_open,
189 .release = m54xx_wdt_release,
190};
191
192static struct miscdevice m54xx_wdt_miscdev = {
193 .minor = WATCHDOG_MINOR,
194 .name = "watchdog",
195 .fops = &m54xx_wdt_fops,
196};
197
198static int __init m54xx_wdt_init(void)
199{
200 if (!request_mem_region(MCF_GPT_GCIR0, 4, "Coldfire M54xx Watchdog")) {
201 pr_warn("I/O region busy\n");
202 return -EBUSY;
203 }
204 pr_info("driver is loaded\n");
205
206 return misc_register(&m54xx_wdt_miscdev);
207}
208
209static void __exit m54xx_wdt_exit(void)
210{
211 misc_deregister(&m54xx_wdt_miscdev);
212 release_mem_region(MCF_GPT_GCIR0, 4);
213}
214
215module_init(m54xx_wdt_init);
216module_exit(m54xx_wdt_exit);
217
218MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
219MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
220
221module_param(heartbeat, int, 0);
222MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 30s)");
223
224module_param(nowayout, bool, 0);
225MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
226
227MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * drivers/watchdog/m54xx_wdt.c
4 *
5 * Watchdog driver for ColdFire MCF547x & MCF548x processors
6 * Copyright 2010 (c) Philippe De Muyter <phdm@macqel.be>
7 *
8 * Adapted from the IXP4xx watchdog driver, which carries these notices:
9 *
10 * Author: Deepak Saxena <dsaxena@plexity.net>
11 *
12 * Copyright 2004 (c) MontaVista, Software, Inc.
13 * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
14 *
15 */
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/fs.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>
30#include <linux/io.h>
31
32#include <asm/coldfire.h>
33#include <asm/m54xxsim.h>
34#include <asm/m54xxgpt.h>
35
36static bool nowayout = WATCHDOG_NOWAYOUT;
37static unsigned int heartbeat = 30; /* (secs) Default is 0.5 minute */
38static unsigned long wdt_status;
39
40#define WDT_IN_USE 0
41#define WDT_OK_TO_CLOSE 1
42
43static void wdt_enable(void)
44{
45 unsigned int gms0;
46
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
51 | MCF_GPT_GMS_OD);
52 else
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);
59}
60
61static void wdt_disable(void)
62{
63 unsigned int gms0;
64
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);
69}
70
71static void wdt_keepalive(void)
72{
73 unsigned int gms0;
74
75 gms0 = __raw_readl(MCF_GPT_GMS0);
76 gms0 |= MCF_GPT_GMS_OCPW(0xA5);
77 __raw_writel(gms0, MCF_GPT_GMS0);
78}
79
80static int m54xx_wdt_open(struct inode *inode, struct file *file)
81{
82 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
83 return -EBUSY;
84
85 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
86 wdt_enable();
87 return stream_open(inode, file);
88}
89
90static ssize_t m54xx_wdt_write(struct file *file, const char *data,
91 size_t len, loff_t *ppos)
92{
93 if (len) {
94 if (!nowayout) {
95 size_t i;
96
97 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
98
99 for (i = 0; i != len; i++) {
100 char c;
101
102 if (get_user(c, data + i))
103 return -EFAULT;
104 if (c == 'V')
105 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
106 }
107 }
108 wdt_keepalive();
109 }
110 return len;
111}
112
113static const struct watchdog_info ident = {
114 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
115 WDIOF_KEEPALIVEPING,
116 .identity = "Coldfire M54xx Watchdog",
117};
118
119static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
120 unsigned long arg)
121{
122 int ret = -ENOTTY;
123 int time;
124
125 switch (cmd) {
126 case WDIOC_GETSUPPORT:
127 ret = copy_to_user((struct watchdog_info *)arg, &ident,
128 sizeof(ident)) ? -EFAULT : 0;
129 break;
130
131 case WDIOC_GETSTATUS:
132 ret = put_user(0, (int *)arg);
133 break;
134
135 case WDIOC_GETBOOTSTATUS:
136 ret = put_user(0, (int *)arg);
137 break;
138
139 case WDIOC_KEEPALIVE:
140 wdt_keepalive();
141 ret = 0;
142 break;
143
144 case WDIOC_SETTIMEOUT:
145 ret = get_user(time, (int *)arg);
146 if (ret)
147 break;
148
149 if (time <= 0 || time > 30) {
150 ret = -EINVAL;
151 break;
152 }
153
154 heartbeat = time;
155 wdt_enable();
156 fallthrough;
157
158 case WDIOC_GETTIMEOUT:
159 ret = put_user(heartbeat, (int *)arg);
160 break;
161 }
162 return ret;
163}
164
165static int m54xx_wdt_release(struct inode *inode, struct file *file)
166{
167 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
168 wdt_disable();
169 else {
170 pr_crit("Device closed unexpectedly - timer will not stop\n");
171 wdt_keepalive();
172 }
173 clear_bit(WDT_IN_USE, &wdt_status);
174 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
175
176 return 0;
177}
178
179
180static 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,
187};
188
189static struct miscdevice m54xx_wdt_miscdev = {
190 .minor = WATCHDOG_MINOR,
191 .name = "watchdog",
192 .fops = &m54xx_wdt_fops,
193};
194
195static int __init m54xx_wdt_init(void)
196{
197 if (!request_mem_region(MCF_GPT_GCIR0, 4, "Coldfire M54xx Watchdog")) {
198 pr_warn("I/O region busy\n");
199 return -EBUSY;
200 }
201 pr_info("driver is loaded\n");
202
203 return misc_register(&m54xx_wdt_miscdev);
204}
205
206static void __exit m54xx_wdt_exit(void)
207{
208 misc_deregister(&m54xx_wdt_miscdev);
209 release_mem_region(MCF_GPT_GCIR0, 4);
210}
211
212module_init(m54xx_wdt_init);
213module_exit(m54xx_wdt_exit);
214
215MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
216MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
217
218module_param(heartbeat, int, 0);
219MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 30s)");
220
221module_param(nowayout, bool, 0);
222MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
223
224MODULE_LICENSE("GPL");