summaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/tnetv107x_wdt.c
blob: 3d3f366c021e031c2e06ac76963fbba155cc3b80 (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
/*
 * TNETV107X: Watchdog timer implementation (for reset)
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <asm/io.h>
#include <asm/arch/clock.h>

#define MAX_DIV		0xFFFE0001

struct wdt_regs {
	u32 kick_lock;
#define KICK_LOCK_1	0x5555
#define KICK_LOCK_2	0xaaaa
	u32 kick;

	u32 change_lock;
#define CHANGE_LOCK_1	0x6666
#define CHANGE_LOCK_2	0xbbbb
	u32 change;

	u32 disable_lock;
#define DISABLE_LOCK_1	0x7777
#define DISABLE_LOCK_2	0xcccc
#define DISABLE_LOCK_3	0xdddd
	u32 disable;

	u32 prescale_lock;
#define PRESCALE_LOCK_1	0x5a5a
#define PRESCALE_LOCK_2	0xa5a5
	u32 prescale;
};

static struct wdt_regs* regs = (struct wdt_regs *)TNETV107X_WDT0_ARM_BASE;

#define wdt_reg_read(reg)	__raw_readl(&regs->reg)
#define wdt_reg_write(reg, val)	__raw_writel((val), &regs->reg)

static int write_prescale_reg(unsigned long prescale_value)
{
	wdt_reg_write(prescale_lock, PRESCALE_LOCK_1);
	if ((wdt_reg_read(prescale_lock) & 0x3) != 0x1)
		return -1;

	wdt_reg_write(prescale_lock, PRESCALE_LOCK_2);
	if ((wdt_reg_read(prescale_lock) & 0x3) != 0x3)
		return -1;

	wdt_reg_write(prescale, prescale_value);

	return 0;
}

static int write_change_reg(unsigned long initial_timer_value)
{
	wdt_reg_write(change_lock, CHANGE_LOCK_1);
	if ((wdt_reg_read(change_lock) & 0x3) != 0x1)
		return -1;

	wdt_reg_write(change_lock, CHANGE_LOCK_2);
	if ((wdt_reg_read(change_lock) & 0x3) != 0x3)
		return -1;

	wdt_reg_write(change, initial_timer_value);

	return 0;
}

static int wdt_control(unsigned long disable_value)
{
	wdt_reg_write(disable_lock, DISABLE_LOCK_1);
	if ((wdt_reg_read(disable_lock) & 0x3) != 0x1)
		return -1;

	wdt_reg_write(disable_lock, DISABLE_LOCK_2);
	if ((wdt_reg_read(disable_lock) & 0x3) != 0x2)
		return -1;

	wdt_reg_write(disable_lock, DISABLE_LOCK_3);
	if ((wdt_reg_read(disable_lock) & 0x3) != 0x3)
		return -1;

	wdt_reg_write(disable, disable_value);
	return 0;
}

static int wdt_set_period(unsigned long msec)
{
	unsigned long change_value, count_value;
	unsigned long prescale_value = 1;
	unsigned long refclk_khz, maxdiv;
	int ret;

	refclk_khz = clk_get_rate(TNETV107X_LPSC_WDT_ARM);
	maxdiv = (MAX_DIV / refclk_khz);

	if ((!msec) || (msec > maxdiv))
		return -1;

	count_value = refclk_khz * msec;
	if (count_value > 0xffff) {
		change_value = count_value / 0xffff + 1;
		prescale_value = count_value / change_value;
	} else {
		change_value = count_value;
	}

	ret = write_prescale_reg(prescale_value - 1);
	if (ret)
		return ret;

	ret = write_change_reg(change_value);
	if (ret)
		return ret;

	return 0;
}

unsigned long last_wdt = -1;

int wdt_start(unsigned long msecs)
{
	int ret;
	ret = wdt_control(0);
	if (ret)
		return ret;
	ret = wdt_set_period(msecs);
	if (ret)
		return ret;
	ret = wdt_control(1);
	if (ret)
		return ret;
	ret = wdt_kick();
	last_wdt = msecs;
	return ret;
}

int wdt_stop(void)
{
	last_wdt = -1;
	return wdt_control(0);
}

int wdt_kick(void)
{
	wdt_reg_write(kick_lock, KICK_LOCK_1);
	if ((wdt_reg_read(kick_lock) & 0x3) != 0x1)
		return -1;

	wdt_reg_write(kick_lock, KICK_LOCK_2);
	if ((wdt_reg_read(kick_lock) & 0x3) != 0x3)
		return -1;

	wdt_reg_write(kick, 1);
	return 0;
}

void reset_cpu(ulong addr)
{
	clk_enable(TNETV107X_LPSC_WDT_ARM);
	wdt_start(1);
	wdt_kick();
}
OpenPOWER on IntegriCloud