summaryrefslogtreecommitdiffstats
path: root/target/linux/danube/files/drivers/char/watchdog/danube_wdt.c
blob: 036a4ece49258a4d86b8dc83f6d0488466dfaca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
69
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *   Copyright (C) 2006 infineon
 *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/tty.h>
#include <linux/selection.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/kdev_t.h>
#include <linux/ioctl.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm-mips/danube/danube_wdt.h>
#include <asm-mips/danube/danube.h>

extern unsigned int danube_get_fpi_hz (void);

static int danube_wdt_inuse = 0;
static int danube_wdt_major = 0;

int
danube_wdt_enable (unsigned int timeout)
{
	unsigned int wdt_cr = 0;
	unsigned int wdt_reload = 0;
	unsigned int wdt_clkdiv, clkdiv, wdt_pwl, pwl, ffpi;

	/* clock divider & prewarning limit */
	switch (clkdiv = DANUBE_BIU_WDT_CR_CLKDIV_GET(readl(DANUBE_BIU_WDT_CR)))
	{
	case 0:
		wdt_clkdiv = 1;
		break;
	case 1:
		wdt_clkdiv = 64;
		break;
	case 2:
		wdt_clkdiv = 4096;
		break;
	case 3:
		wdt_clkdiv = 262144;
		break;
	}

	switch (pwl = DANUBE_BIU_WDT_CR_PWL_GET (readl(DANUBE_BIU_WDT_CR)))
	{
	case 0:
		wdt_pwl = 0x8000;
		break;
	case 1:
		wdt_pwl = 0x4000;
		break;
	case 2:
		wdt_pwl = 0x2000;
		break;
	case 3:
		wdt_pwl = 0x1000;
		break;
	}

	//TODO
	printk("WARNING FUNCTION CALL MISSING!!!");
	//ffpi = cgu_get_io_region_clock();
	printk("cpu clock = %d\n", ffpi);

	/* caculate reload value */
	wdt_reload = (timeout * (ffpi / wdt_clkdiv)) + wdt_pwl;

	printk("wdt_pwl=0x%x, wdt_clkdiv=%d, ffpi=%d, wdt_reload = 0x%x\n",
		wdt_pwl, wdt_clkdiv, ffpi, wdt_reload);

	if (wdt_reload > 0xFFFF) {
		printk ("timeout too large %d\n", timeout);
		return -EINVAL;
	}

	/* Write first part of password access */
	*DANUBE_BIU_WDT_CR = DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW1);

	wdt_cr = *DANUBE_BIU_WDT_CR;
	wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff) &
		   !DANUBE_BIU_WDT_CR_PWL_SET (0x3) &
		   !DANUBE_BIU_WDT_CR_CLKDIV_SET (0x3) &
		   !DANUBE_BIU_WDT_CR_RELOAD_SET (0xffff));

	wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
		   DANUBE_BIU_WDT_CR_PWL_SET (pwl) |
		   DANUBE_BIU_WDT_CR_CLKDIV_SET (clkdiv) |
		   DANUBE_BIU_WDT_CR_RELOAD_SET (wdt_reload) |
		   DANUBE_BIU_WDT_CR_GEN);

	/* Set reload value in second password access */
	*DANUBE_BIU_WDT_CR = wdt_cr;
	printk ("enabled\n");
	return 0;
}

void
danube_wdt_disable (void)
{
	/* Write first part of password access */
	writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);

	/* Disable the watchdog in second password access (GEN=0) */
	writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW2), DANUBE_BIU_WDT_CR);
}

void
danube_wdt_low_power (int en)
{
	unsigned int wdt_cr = 0;

	writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);

	wdt_cr = readl(DANUBE_BIU_WDT_CR);

	if (en)
	{
		wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET(0xff));
		wdt_cr |=
			(DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
			 DANUBE_BIU_WDT_CR_LPEN);
	}
	else {
		wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff) &
			   !DANUBE_BIU_WDT_CR_LPEN);
		wdt_cr |= DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2);
	}

	/* Set reload value in second password access */
	writel(wdt_cr, DANUBE_BIU_WDT_CR);
}

void
danube_wdt_debug_suspend (int en)
{
	unsigned int wdt_cr = 0;

	writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);

	wdt_cr = readl(DANUBE_BIU_WDT_CR);
	if (en)
	{
		wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff));
		wdt_cr |=
			(DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
			 DANUBE_BIU_WDT_CR_DSEN);
	}
	else {
		wdt_cr &= (!DANUBE_BIU_WDT_CR_PW_SET (0xff) &
			   !DANUBE_BIU_WDT_CR_DSEN);
		wdt_cr |= DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2);
	}

	/* Set reload value in second password access */
	writel(wdt_cr, DANUBE_BIU_WDT_CR);
}

void
danube_wdt_prewarning_limit (int pwl)
{
	unsigned int wdt_cr = 0;

	wdt_cr = readl(DANUBE_BIU_WDT_CR);
	writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);

	wdt_cr &= 0xff00ffff;	//(!DANUBE_BIU_WDT_CR_PW_SET(0xff));
	wdt_cr &= 0xf3ffffff;	//(!DANUBE_BIU_WDT_CR_PWL_SET(3));
	wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
		   DANUBE_BIU_WDT_CR_PWL_SET (pwl));

	/* Set reload value in second password access */
	writel(wdt_cr, DANUBE_BIU_WDT_CR);
}

void
danube_wdt_set_clkdiv (int clkdiv)
{
	unsigned int wdt_cr = 0;

	wdt_cr = readl(DANUBE_BIU_WDT_CR);
	writel(DANUBE_BIU_WDT_CR_PW_SET(DANUBE_WDT_PW1), DANUBE_BIU_WDT_CR);

	wdt_cr &= 0xff00ffff;	//(!DANUBE_BIU_WDT_CR_PW_SET(0xff));
	wdt_cr &= 0xfcffffff;	//(!DANUBE_BIU_WDT_CR_CLKDIV_SET(3));
	wdt_cr |= (DANUBE_BIU_WDT_CR_PW_SET (DANUBE_WDT_PW2) |
		   DANUBE_BIU_WDT_CR_CLKDIV_SET (clkdiv));

	/* Set reload value in second password access */
	writel(wdt_cr, DANUBE_BIU_WDT_CR);
}

static int
danube_wdt_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
	   unsigned long arg)
{
	int result = 0;
	int en = 0;
	int istatus;
	int pwl, clkdiv;
	static int timeout = -1;

	switch (cmd)
	{
	case DANUBE_WDT_IOC_START:
		printk("enable watch dog timer!\n");
		if (copy_from_user((void *) &timeout, (void *) arg, sizeof (int)))
		{
			printk ("invalid argument\n");
			result = -EINVAL;
		} else {
			if ((result = danube_wdt_enable (timeout)) < 0)
				timeout = -1;
		}
		break;

	case DANUBE_WDT_IOC_STOP:
		printk("disable watch dog timer\n");
		timeout = -1;
		danube_wdt_disable();
		break;

	case DANUBE_WDT_IOC_PING:
		if (timeout < 0)
			result = -EIO;
		else
			result = danube_wdt_enable(timeout);
		break;

	case DANUBE_WDT_IOC_SET_PWL:
		if (copy_from_user((void *) &pwl, (void *) arg, sizeof (int)))
			result = -EINVAL;
		danube_wdt_prewarning_limit(pwl);
		break;

	case DANUBE_WDT_IOC_SET_DSEN:
		if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
			result = -EINVAL;
		danube_wdt_debug_suspend (en);
		break;

	case DANUBE_WDT_IOC_SET_LPEN:
		if (copy_from_user((void *) &en, (void *) arg, sizeof (int)))
			result = -EINVAL;
		danube_wdt_low_power(en);
		break;

	case DANUBE_WDT_IOC_SET_CLKDIV:
		if (copy_from_user((void *) &clkdiv, (void *) arg, sizeof (int)))
			result = -EINVAL;
		danube_wdt_set_clkdiv (clkdiv);
		break;

	case DANUBE_WDT_IOC_GET_STATUS:
		istatus = readl(DANUBE_BIU_WDT_SR);
		copy_to_user((int *) arg, (int *) &istatus, sizeof (int));
		break;
	}

	return result;
}

static int
danube_wdt_open (struct inode *inode, struct file *file)
{
	if (danube_wdt_inuse)
		return -EBUSY;
	danube_wdt_inuse = 1;

	return 0;
}

static int
danube_wdt_release (struct inode *inode, struct file *file)
{
	danube_wdt_inuse = 0;

	return 0;
}

int
danube_wdt_register_proc_read (char *buf, char **start, off_t offset, int count,
			int *eof, void *data)
{
	int len = 0;
	len += sprintf (buf + len, "DANUBE_BIU_WDT_PROC_READ\n");

	len += sprintf (buf + len, "DANUBE_BIU_WDT_CR(0x%08x)	: 0x%08x\n",
			(unsigned int)DANUBE_BIU_WDT_CR,
			readl(DANUBE_BIU_WDT_CR));
	len += sprintf (buf + len, "DANUBE_BIU_WDT_SR(0x%08x)	: 0x%08x\n",
			(unsigned int)DANUBE_BIU_WDT_SR,
			readl(DANUBE_BIU_WDT_SR));

	*eof = 1;

	return len;
}

static struct file_operations wdt_fops = {
      .owner = THIS_MODULE,
      .ioctl = danube_wdt_ioctl,
      .open = danube_wdt_open,
      .release = danube_wdt_release,
};

int __init
danube_wdt_init_module (void)
{
	danube_wdt_major = register_chrdev(0, "wdt", &wdt_fops);

	if (danube_wdt_major < 0)
	{
		printk("cannot register watchdog device\n");

		return -EINVAL;
	}

	create_proc_read_entry("danube_wdt", 0, NULL, danube_wdt_register_proc_read, NULL);

	printk("danube watchdog loaded\n");

	return 0;
}

void
danube_wdt_cleanup_module (void)
{
	unregister_chrdev(danube_wdt_major, "wdt");
	remove_proc_entry("danube_wdt", NULL);
}

module_init(danube_wdt_init_module);
module_exit(danube_wdt_cleanup_module);