summaryrefslogtreecommitdiff
path: root/board/ssv/common/wd_pio.c
blob: 9b384259e4b75646d3a66bd3de97648bf9ce0586 (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
/*
 * (C) Copyright 2004, Li-Pro.Net <www.li-pro.net>
 * Stephan Linz <linz@li-pro.net>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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
 */

#include <common.h>
#include <command.h>
#include <nios.h>
#include <nios-io.h>

#if	defined(CONFIG_HW_WATCHDOG)

#if !defined(CONFIG_HW_WDENA_BASE)
#error "*** CONFIG_HW_WDENA_BASE not defined ***"
#if !defined(CONFIG_HW_WDENA_BIT)
#error "*** CONFIG_HW_WDENA_BIT not defined ***"
#endif
#endif

#if !defined(CONFIG_HW_WDTOG_BASE)
#error "*** CONFIG_HW_WDTOG_BASE not defined ***"
#if !defined(CONFIG_HW_WDTOG_BIT)
#error "*** CONFIG_HW_WDTOG_BIT not defined ***"
#endif
#endif

#ifdef	CONFIG_HW_WDPORT_WRONLY	/* emulate read access */
static unsigned __wd_ena_pio_portval = 0;
#endif

#define	WD_PIO_INIT_DONE(V)	((V) & (1 << CONFIG_HW_WDENA_BIT))

void ssv_wd_pio_init(void)
{
	nios_pio_t *ena_piop	 = (nios_pio_t*)CONFIG_HW_WDENA_BASE;
	nios_pio_t *trg_piop	 = (nios_pio_t*)CONFIG_HW_WDTOG_BASE;

	trg_piop->data		&= ~(1 << CONFIG_HW_WDTOG_BIT);

#ifdef	CONFIG_HW_WDPORT_WRONLY	/* emulate read access */

	__wd_ena_pio_portval	|= (1 << CONFIG_HW_WDENA_BIT);
	ena_piop->data		 = __wd_ena_pio_portval;

#else	/* !CONFIG_HW_WDPORT_WRONLY */

	trg_piop->direction	|= (1 << CONFIG_HW_WDTOG_BIT);

	ena_piop->data		|= (1 << CONFIG_HW_WDENA_BIT);
	ena_piop->direction	|= (1 << CONFIG_HW_WDENA_BIT);

#endif	/* CONFIG_HW_WDPORT_WRONLY */
}

void ssv_wd_pio_done(void)
{
	nios_pio_t *piop	 = (nios_pio_t*)CONFIG_HW_WDENA_BASE;

#ifdef	CONFIG_HW_WDPORT_WRONLY	/* emulate read access */

	__wd_ena_pio_portval	&= ~(1 << CONFIG_HW_WDENA_BIT);
	piop->data		 = __wd_ena_pio_portval;

#else	/* !CONFIG_HW_WDPORT_WRONLY */

	piop->data		&= ~(1 << CONFIG_HW_WDENA_BIT);

#endif	/* CONFIG_HW_WDPORT_WRONLY */
}

void ssv_wd_pio_reset(void)
{
	nios_pio_t *trg_piop	 = (nios_pio_t*)CONFIG_HW_WDTOG_BASE;

#ifdef	CONFIG_HW_WDPORT_WRONLY
	if (WD_PIO_INIT_DONE(__wd_ena_pio_portval))
#else
	nios_pio_t *ena_piop	 = (nios_pio_t*)CONFIG_HW_WDENA_BASE;

	if (WD_PIO_INIT_DONE(ena_piop->data))
#endif
	{
		trg_piop->data	|=  (1 << CONFIG_HW_WDTOG_BIT);
		trg_piop->data	&= ~(1 << CONFIG_HW_WDTOG_BIT);
	}
}

void hw_watchdog_reset(void)
{
	int re_enable = disable_interrupts ();

	ssv_wd_pio_reset();
	if (re_enable)
		enable_interrupts ();
}

#if defined(CONFIG_CMD_BSP)
int do_wd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	nios_pio_t *ena_piop	 = (nios_pio_t*)CONFIG_HW_WDENA_BASE;

	switch (argc)
	{
	case 1:
		printf ("Watchdog timer status is %s\n",
#ifdef	CONFIG_HW_WDPORT_WRONLY
				WD_PIO_INIT_DONE(__wd_ena_pio_portval)
#else
				WD_PIO_INIT_DONE(ena_piop->data)
#endif
				? "on" : "off");
		return 0;
	case 2:
		if (!strcmp(argv[1],"on"))
		{
			ssv_wd_pio_init();
			printf("Watchdog timer now is on\n");
			return 0;
		}
		else if (!strcmp(argv[1],"off"))
		{
			ssv_wd_pio_done();
			printf("Watchdog timer now is off\n");
			return 0;
		}
		break;
	default:
		break;
	}
	cmd_usage(cmdtp);
	return 1;
}

U_BOOT_CMD(
	wd,	2,	1,	do_wd,
	"check and set watchdog",
	"on   - switch watchDog on\n"
	"wd off  - switch watchdog off\n"
	"wd      - print current status\n"
);
#endif
#endif	/* CONFIG_HW_WATCHDOG */