blob: f388a769dbd33dda522395702ac85efdefd26e90 [file] [log] [blame]
Philippe De Muyter88cce422010-11-03 15:07:28 +01001/*
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +01002 * drivers/watchdog/m54xx_wdt.c
Philippe De Muyter88cce422010-11-03 15:07:28 +01003 *
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +01004 * Watchdog driver for ColdFire MCF547x & MCF548x processors
Philippe De Muyter88cce422010-11-03 15:07:28 +01005 * 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
Joe Perches27c766a2012-02-15 15:06:19 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Philippe De Muyter88cce422010-11-03 15:07:28 +010021#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>
Thomas Gleixner072cb8b2020-05-08 21:26:58 +020032#include <linux/io.h>
Philippe De Muyter88cce422010-11-03 15:07:28 +010033
34#include <asm/coldfire.h>
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +010035#include <asm/m54xxsim.h>
36#include <asm/m54xxgpt.h>
Philippe De Muyter88cce422010-11-03 15:07:28 +010037
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010038static bool nowayout = WATCHDOG_NOWAYOUT;
Philippe De Muyter88cce422010-11-03 15:07:28 +010039static 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 */
Greg Ungerer944c3d82012-09-18 14:51:46 +100050 gms0 = __raw_readl(MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010051 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;
Greg Ungerer944c3d82012-09-18 14:51:46 +100056 __raw_writel(gms0, MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010057 __raw_writel(MCF_GPT_GCIR_PRE(heartbeat*(MCF_BUSCLK/0xffff)) |
Greg Ungerer944c3d82012-09-18 14:51:46 +100058 MCF_GPT_GCIR_CNT(0xffff), MCF_GPT_GCIR0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010059 gms0 |= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE;
Greg Ungerer944c3d82012-09-18 14:51:46 +100060 __raw_writel(gms0, MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010061}
62
63static void wdt_disable(void)
64{
65 unsigned int gms0;
66
67 /* disable watchdog */
Greg Ungerer944c3d82012-09-18 14:51:46 +100068 gms0 = __raw_readl(MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010069 gms0 &= ~(MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE);
Greg Ungerer944c3d82012-09-18 14:51:46 +100070 __raw_writel(gms0, MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010071}
72
73static void wdt_keepalive(void)
74{
75 unsigned int gms0;
76
Greg Ungerer944c3d82012-09-18 14:51:46 +100077 gms0 = __raw_readl(MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010078 gms0 |= MCF_GPT_GMS_OCPW(0xA5);
Greg Ungerer944c3d82012-09-18 14:51:46 +100079 __raw_writel(gms0, MCF_GPT_GMS0);
Philippe De Muyter88cce422010-11-03 15:07:28 +010080}
81
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +010082static int m54xx_wdt_open(struct inode *inode, struct file *file)
Philippe De Muyter88cce422010-11-03 15:07:28 +010083{
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();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +030089 return stream_open(inode, file);
Philippe De Muyter88cce422010-11-03 15:07:28 +010090}
91
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +010092static ssize_t m54xx_wdt_write(struct file *file, const char *data,
Philippe De Muyter88cce422010-11-03 15:07:28 +010093 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,
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100118 .identity = "Coldfire M54xx Watchdog",
Philippe De Muyter88cce422010-11-03 15:07:28 +0100119};
120
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100121static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
Philippe De Muyter88cce422010-11-03 15:07:28 +0100122 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();
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500158 fallthrough;
Philippe De Muyter88cce422010-11-03 15:07:28 +0100159
160 case WDIOC_GETTIMEOUT:
161 ret = put_user(heartbeat, (int *)arg);
162 break;
163 }
164 return ret;
165}
166
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100167static int m54xx_wdt_release(struct inode *inode, struct file *file)
Philippe De Muyter88cce422010-11-03 15:07:28 +0100168{
169 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
170 wdt_disable();
171 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800172 pr_crit("Device closed unexpectedly - timer will not stop\n");
Philippe De Muyter88cce422010-11-03 15:07:28 +0100173 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
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100182static const struct file_operations m54xx_wdt_fops = {
Philippe De Muyter88cce422010-11-03 15:07:28 +0100183 .owner = THIS_MODULE,
184 .llseek = no_llseek,
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100185 .write = m54xx_wdt_write,
186 .unlocked_ioctl = m54xx_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200187 .compat_ioctl = compat_ptr_ioctl,
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100188 .open = m54xx_wdt_open,
189 .release = m54xx_wdt_release,
Philippe De Muyter88cce422010-11-03 15:07:28 +0100190};
191
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100192static struct miscdevice m54xx_wdt_miscdev = {
Philippe De Muyter88cce422010-11-03 15:07:28 +0100193 .minor = WATCHDOG_MINOR,
194 .name = "watchdog",
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100195 .fops = &m54xx_wdt_fops,
Philippe De Muyter88cce422010-11-03 15:07:28 +0100196};
197
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100198static int __init m54xx_wdt_init(void)
Philippe De Muyter88cce422010-11-03 15:07:28 +0100199{
Greg Ungerer944c3d82012-09-18 14:51:46 +1000200 if (!request_mem_region(MCF_GPT_GCIR0, 4, "Coldfire M54xx Watchdog")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800201 pr_warn("I/O region busy\n");
Philippe De Muyter88cce422010-11-03 15:07:28 +0100202 return -EBUSY;
203 }
Joe Perches27c766a2012-02-15 15:06:19 -0800204 pr_info("driver is loaded\n");
Philippe De Muyter88cce422010-11-03 15:07:28 +0100205
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100206 return misc_register(&m54xx_wdt_miscdev);
Philippe De Muyter88cce422010-11-03 15:07:28 +0100207}
208
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100209static void __exit m54xx_wdt_exit(void)
Philippe De Muyter88cce422010-11-03 15:07:28 +0100210{
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100211 misc_deregister(&m54xx_wdt_miscdev);
Greg Ungerer944c3d82012-09-18 14:51:46 +1000212 release_mem_region(MCF_GPT_GCIR0, 4);
Philippe De Muyter88cce422010-11-03 15:07:28 +0100213}
214
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100215module_init(m54xx_wdt_init);
216module_exit(m54xx_wdt_exit);
Philippe De Muyter88cce422010-11-03 15:07:28 +0100217
218MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
Philippe De Muyter9b9c63ff2011-01-22 00:21:24 +0100219MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
Philippe De Muyter88cce422010-11-03 15:07:28 +0100220
221module_param(heartbeat, int, 0);
222MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 30s)");
223
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100224module_param(nowayout, bool, 0);
Philippe De Muyter88cce422010-11-03 15:07:28 +0100225MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
226
227MODULE_LICENSE("GPL");