Blame view

drivers/watchdog/ie6xx_wdt.c 7.42 KB
75d67a549   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
101ce87b3   Alexander Stein   watchdog: Add wat...
2
3
4
5
6
  /*
   *      Intel Atom E6xx Watchdog driver
   *
   *      Copyright (C) 2011 Alexander Stein
   *                <alexander.stein@systec-electronic.com>
101ce87b3   Alexander Stein   watchdog: Add wat...
7
8
9
10
11
   */
  
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/platform_device.h>
491d9e2a5   Randy Dunlap   watchdog: ie6xx_w...
12
  #include <linux/io.h>
101ce87b3   Alexander Stein   watchdog: Add wat...
13
14
15
  #include <linux/kernel.h>
  #include <linux/types.h>
  #include <linux/watchdog.h>
101ce87b3   Alexander Stein   watchdog: Add wat...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include <linux/seq_file.h>
  #include <linux/debugfs.h>
  #include <linux/uaccess.h>
  #include <linux/spinlock.h>
  
  #define DRIVER_NAME "ie6xx_wdt"
  
  #define PV1	0x00
  #define PV2	0x04
  
  #define RR0	0x0c
  #define RR1	0x0d
  #define WDT_RELOAD	0x01
  #define WDT_TOUT	0x02
  
  #define WDTCR	0x10
  #define WDT_PRE_SEL	0x04
  #define WDT_RESET_SEL	0x08
  #define WDT_RESET_EN	0x10
  #define WDT_TOUT_EN	0x20
  
  #define DCR	0x14
  
  #define WDTLR	0x18
  #define WDT_LOCK	0x01
  #define WDT_ENABLE	0x02
  #define WDT_TOUT_CNF	0x03
  
  #define MIN_TIME	1
  #define MAX_TIME	(10 * 60) /* 10 minutes */
  #define DEFAULT_TIME	60
  
  static unsigned int timeout = DEFAULT_TIME;
  module_param(timeout, uint, 0);
  MODULE_PARM_DESC(timeout,
  		"Default Watchdog timer setting ("
  		__MODULE_STRING(DEFAULT_TIME) "s)."
  		"The range is from 1 to 600");
  
  static bool nowayout = WATCHDOG_NOWAYOUT;
  module_param(nowayout, bool, 0);
  MODULE_PARM_DESC(nowayout,
  	"Watchdog cannot be stopped once started (default="
  		__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  
  static u8 resetmode = 0x10;
  module_param(resetmode, byte, 0);
  MODULE_PARM_DESC(resetmode,
  	"Resetmode bits: 0x08 warm reset (cold reset otherwise), "
  	"0x10 reset enable, 0x20 disable toggle GPIO[4] (default=0x10)");
  
  static struct {
  	unsigned short sch_wdtba;
053bc5764   Sebastian Andrzej Siewior   watchdog: ie6xx_w...
69
  	spinlock_t unlock_sequence;
101ce87b3   Alexander Stein   watchdog: Add wat...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  #ifdef CONFIG_DEBUG_FS
  	struct dentry *debugfs;
  #endif
  } ie6xx_wdt_data;
  
  /*
   * This is needed to write to preload and reload registers
   * struct ie6xx_wdt_data.unlock_sequence must be used
   * to prevent sequence interrupts
   */
  static void ie6xx_wdt_unlock_registers(void)
  {
  	outb(0x80, ie6xx_wdt_data.sch_wdtba + RR0);
  	outb(0x86, ie6xx_wdt_data.sch_wdtba + RR0);
  }
  
  static int ie6xx_wdt_ping(struct watchdog_device *wdd)
  {
  	spin_lock(&ie6xx_wdt_data.unlock_sequence);
  	ie6xx_wdt_unlock_registers();
  	outb(WDT_RELOAD, ie6xx_wdt_data.sch_wdtba + RR1);
  	spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  	return 0;
  }
  
  static int ie6xx_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  {
  	u32 preload;
  	u64 clock;
  	u8 wdtcr;
  
  	/* Watchdog clock is PCI Clock (33MHz) */
  	clock = 33000000;
  	/* and the preload value is loaded into [34:15] of the down counter */
  	preload = (t * clock) >> 15;
  	/*
  	 * Manual states preload must be one less.
  	 * Does not wrap as t is at least 1
  	 */
  	preload -= 1;
  
  	spin_lock(&ie6xx_wdt_data.unlock_sequence);
  
  	/* Set ResetMode & Enable prescaler for range 10ms to 10 min */
  	wdtcr = resetmode & 0x38;
  	outb(wdtcr, ie6xx_wdt_data.sch_wdtba + WDTCR);
  
  	ie6xx_wdt_unlock_registers();
  	outl(0, ie6xx_wdt_data.sch_wdtba + PV1);
  
  	ie6xx_wdt_unlock_registers();
  	outl(preload, ie6xx_wdt_data.sch_wdtba + PV2);
  
  	ie6xx_wdt_unlock_registers();
  	outb(WDT_RELOAD | WDT_TOUT, ie6xx_wdt_data.sch_wdtba + RR1);
  
  	spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  
  	wdd->timeout = t;
  	return 0;
  }
  
  static int ie6xx_wdt_start(struct watchdog_device *wdd)
  {
  	ie6xx_wdt_set_timeout(wdd, wdd->timeout);
  
  	/* Enable the watchdog timer */
  	spin_lock(&ie6xx_wdt_data.unlock_sequence);
  	outb(WDT_ENABLE, ie6xx_wdt_data.sch_wdtba + WDTLR);
  	spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  
  	return 0;
  }
  
  static int ie6xx_wdt_stop(struct watchdog_device *wdd)
  {
  	if (inb(ie6xx_wdt_data.sch_wdtba + WDTLR) & WDT_LOCK)
  		return -1;
  
  	/* Disable the watchdog timer */
  	spin_lock(&ie6xx_wdt_data.unlock_sequence);
  	outb(0, ie6xx_wdt_data.sch_wdtba + WDTLR);
  	spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  
  	return 0;
  }
  
  static const struct watchdog_info ie6xx_wdt_info = {
  	.identity =	"Intel Atom E6xx Watchdog",
  	.options =	WDIOF_SETTIMEOUT |
  			WDIOF_MAGICCLOSE |
  			WDIOF_KEEPALIVEPING,
  };
  
  static const struct watchdog_ops ie6xx_wdt_ops = {
  	.owner =	THIS_MODULE,
  	.start =	ie6xx_wdt_start,
  	.stop =		ie6xx_wdt_stop,
  	.ping =		ie6xx_wdt_ping,
  	.set_timeout =	ie6xx_wdt_set_timeout,
  };
  
  static struct watchdog_device ie6xx_wdt_dev = {
  	.info =		&ie6xx_wdt_info,
  	.ops =		&ie6xx_wdt_ops,
  	.min_timeout =	MIN_TIME,
  	.max_timeout =	MAX_TIME,
  };
  
  #ifdef CONFIG_DEBUG_FS
248e655b4   Yangtao Li   watchdog: ie6xx_w...
180
  static int ie6xx_wdt_show(struct seq_file *s, void *unused)
101ce87b3   Alexander Stein   watchdog: Add wat...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  {
  	seq_printf(s, "PV1   = 0x%08x
  ",
  		inl(ie6xx_wdt_data.sch_wdtba + PV1));
  	seq_printf(s, "PV2   = 0x%08x
  ",
  		inl(ie6xx_wdt_data.sch_wdtba + PV2));
  	seq_printf(s, "RR    = 0x%08x
  ",
  		inw(ie6xx_wdt_data.sch_wdtba + RR0));
  	seq_printf(s, "WDTCR = 0x%08x
  ",
  		inw(ie6xx_wdt_data.sch_wdtba + WDTCR));
  	seq_printf(s, "DCR   = 0x%08x
  ",
  		inl(ie6xx_wdt_data.sch_wdtba + DCR));
  	seq_printf(s, "WDTLR = 0x%08x
  ",
  		inw(ie6xx_wdt_data.sch_wdtba + WDTLR));
  
  	seq_printf(s, "
  ");
  	return 0;
  }
248e655b4   Yangtao Li   watchdog: ie6xx_w...
205
  DEFINE_SHOW_ATTRIBUTE(ie6xx_wdt);
101ce87b3   Alexander Stein   watchdog: Add wat...
206

2d991a164   Bill Pemberton   watchdog: remove ...
207
  static void ie6xx_wdt_debugfs_init(void)
101ce87b3   Alexander Stein   watchdog: Add wat...
208
209
210
  {
  	/* /sys/kernel/debug/ie6xx_wdt */
  	ie6xx_wdt_data.debugfs = debugfs_create_file("ie6xx_wdt",
248e655b4   Yangtao Li   watchdog: ie6xx_w...
211
  		S_IFREG | S_IRUGO, NULL, NULL, &ie6xx_wdt_fops);
101ce87b3   Alexander Stein   watchdog: Add wat...
212
  }
0402450f4   Gerard Snitselaar   watchdog: ie6xx_w...
213
  static void ie6xx_wdt_debugfs_exit(void)
101ce87b3   Alexander Stein   watchdog: Add wat...
214
215
216
217
218
  {
  	debugfs_remove(ie6xx_wdt_data.debugfs);
  }
  
  #else
2d991a164   Bill Pemberton   watchdog: remove ...
219
  static void ie6xx_wdt_debugfs_init(void)
101ce87b3   Alexander Stein   watchdog: Add wat...
220
221
  {
  }
0402450f4   Gerard Snitselaar   watchdog: ie6xx_w...
222
  static void ie6xx_wdt_debugfs_exit(void)
101ce87b3   Alexander Stein   watchdog: Add wat...
223
224
225
  {
  }
  #endif
2d991a164   Bill Pemberton   watchdog: remove ...
226
  static int ie6xx_wdt_probe(struct platform_device *pdev)
101ce87b3   Alexander Stein   watchdog: Add wat...
227
228
229
230
231
232
233
234
235
236
  {
  	struct resource *res;
  	u8 wdtlr;
  	int ret;
  
  	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  	if (!res)
  		return -ENODEV;
  
  	if (!request_region(res->start, resource_size(res), pdev->name)) {
744f6f4d3   Randy Dunlap   watchdog: ie6xx_w...
237
238
239
  		dev_err(&pdev->dev, "Watchdog region 0x%llx already in use!
  ",
  			(u64)res->start);
101ce87b3   Alexander Stein   watchdog: Add wat...
240
241
242
243
244
245
246
247
248
  		return -EBUSY;
  	}
  
  	ie6xx_wdt_data.sch_wdtba = res->start;
  	dev_dbg(&pdev->dev, "WDT = 0x%X
  ", ie6xx_wdt_data.sch_wdtba);
  
  	ie6xx_wdt_dev.timeout = timeout;
  	watchdog_set_nowayout(&ie6xx_wdt_dev, nowayout);
6551881c8   Pratyush Anand   Watchdog: Fix par...
249
  	ie6xx_wdt_dev.parent = &pdev->dev;
101ce87b3   Alexander Stein   watchdog: Add wat...
250
251
252
253
254
255
256
257
258
259
260
261
  
  	spin_lock_init(&ie6xx_wdt_data.unlock_sequence);
  
  	wdtlr = inb(ie6xx_wdt_data.sch_wdtba + WDTLR);
  	if (wdtlr & WDT_LOCK)
  		dev_warn(&pdev->dev,
  			"Watchdog Timer is Locked (Reg=0x%x)
  ", wdtlr);
  
  	ie6xx_wdt_debugfs_init();
  
  	ret = watchdog_register_device(&ie6xx_wdt_dev);
8f952c015   Wolfram Sang   watchdog: ie6xx_w...
262
  	if (ret)
101ce87b3   Alexander Stein   watchdog: Add wat...
263
  		goto misc_register_error;
101ce87b3   Alexander Stein   watchdog: Add wat...
264
265
266
267
268
269
270
271
272
  
  	return 0;
  
  misc_register_error:
  	ie6xx_wdt_debugfs_exit();
  	release_region(res->start, resource_size(res));
  	ie6xx_wdt_data.sch_wdtba = 0;
  	return ret;
  }
4b12b896c   Bill Pemberton   watchdog: remove ...
273
  static int ie6xx_wdt_remove(struct platform_device *pdev)
101ce87b3   Alexander Stein   watchdog: Add wat...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  {
  	struct resource *res;
  
  	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  	ie6xx_wdt_stop(NULL);
  	watchdog_unregister_device(&ie6xx_wdt_dev);
  	ie6xx_wdt_debugfs_exit();
  	release_region(res->start, resource_size(res));
  	ie6xx_wdt_data.sch_wdtba = 0;
  
  	return 0;
  }
  
  static struct platform_driver ie6xx_wdt_driver = {
  	.probe		= ie6xx_wdt_probe,
82268714b   Bill Pemberton   watchdog: remove ...
289
  	.remove		= ie6xx_wdt_remove,
101ce87b3   Alexander Stein   watchdog: Add wat...
290
291
  	.driver		= {
  		.name	= DRIVER_NAME,
101ce87b3   Alexander Stein   watchdog: Add wat...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  	},
  };
  
  static int __init ie6xx_wdt_init(void)
  {
  	/* Check boot parameters to verify that their initial values */
  	/* are in range. */
  	if ((timeout < MIN_TIME) ||
  	    (timeout > MAX_TIME)) {
  		pr_err("Watchdog timer: value of timeout %d (dec) "
  		  "is out of range from %d to %d (dec)
  ",
  		  timeout, MIN_TIME, MAX_TIME);
  		return -EINVAL;
  	}
  
  	return platform_driver_register(&ie6xx_wdt_driver);
  }
  
  static void __exit ie6xx_wdt_exit(void)
  {
  	platform_driver_unregister(&ie6xx_wdt_driver);
  }
  
  late_initcall(ie6xx_wdt_init);
  module_exit(ie6xx_wdt_exit);
  
  MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
  MODULE_DESCRIPTION("Intel Atom E6xx Watchdog Device Driver");
  MODULE_LICENSE("GPL");
101ce87b3   Alexander Stein   watchdog: Add wat...
322
  MODULE_ALIAS("platform:" DRIVER_NAME);