summaryrefslogtreecommitdiff
path: root/sys/arch/sh3/dev/wdog.c
blob: 0df57ed2badf28857506ee3327f561744ca09e39 (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
/*	$NetBSD: wdog.c,v 1.18 2014/07/25 08:10:34 dholland Exp $ */

/*-
 * Copyright (C) 2000 SAITOH Masanobu.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.18 2014/07/25 08:10:34 dholland Exp $");

#include <sys/param.h>
#include <sys/buf.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/uio.h>
#include <sys/device.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/syslog.h>
#include <sys/conf.h>

#include <machine/cpu.h>
#include <machine/intr.h>

#include <sh3/frame.h>
#include <sh3/wdtreg.h>
#include <sh3/wdogvar.h>
#include <sh3/exception.h>

struct wdog_softc {
	device_t sc_dev;
	int flags;
};

static int wdogmatch(device_t, cfdata_t, void *);
static void wdogattach(device_t, device_t, void *);
static int wdogintr(void *);

CFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc),
    wdogmatch, wdogattach, NULL, NULL);

extern struct cfdriver wdog_cd;

dev_type_open(wdogopen);
dev_type_close(wdogclose);
dev_type_ioctl(wdogioctl);

const struct cdevsw wdog_cdevsw = {
	.d_open = wdogopen,
	.d_close = wdogclose,
	.d_read = noread,
	.d_write = nowrite,
	.d_ioctl = wdogioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

void
wdog_wr_cnt(unsigned char x)
{

	SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x;
}

void
wdog_wr_csr(unsigned char x)
{

	SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x;
}

static int
wdogmatch(device_t parent, cfdata_t cfp, void *aux)
{

	if (strcmp(cfp->cf_name, "wdog"))
		return (0);

	return (1);
}

/*
 *  functions for probeing.
 */
/* ARGSUSED */
static void
wdogattach(device_t parent, device_t self, void *aux)
{
	struct wdog_softc *sc;

	sc = device_private(self);
	sc->sc_dev = self;

	aprint_naive("\n");
	aprint_normal(": internal watchdog timer\n");

	wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);	/* default to wt mode */

	intc_intr_establish(SH_INTEVT_WDT_ITI, IST_LEVEL, IPL_SOFTCLOCK,
	    wdogintr, 0);
}

/*ARGSUSED*/
int
wdogopen(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct wdog_softc *sc;

	sc = device_lookup_private(&wdog_cd, minor(dev));
	if (sc == NULL)
		return (ENXIO);

	if (sc->flags & WDOGF_OPEN)
		return (EBUSY);
	sc->flags |= WDOGF_OPEN;
	return (0);
}

/*ARGSUSED*/
int
wdogclose(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct wdog_softc *sc;

	sc = device_lookup_private(&wdog_cd, minor(dev));

	if (sc->flags & WDOGF_OPEN)
		sc->flags = 0;

	return (0);
}

extern unsigned int maxwdog;

/*ARGSUSED*/
int
wdogioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	int error = 0;
	int request;

	switch (cmd) {
	case SIOWDOGSETMODE:
		request = *(int *)data;

		switch (request) {
		case WDOGM_RESET:
			wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT);
			break;
		case WDOGM_INTR:
			wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT);
			break;
		default:
			error = EINVAL;
			break;
		}
		break;
	case SIORESETWDOG:
		wdog_wr_cnt(0);		/* reset to zero */
		break;
	case SIOSTARTWDOG:
		wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
		wdog_wr_cnt(0);		/* reset to zero */
		wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME);	/* start!!! */
		break;
	case SIOSTOPWDOG:
		wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
		break;
	case SIOSETWDOG:
		request = *(int *)data;

		if (request > 2) {
			error = EINVAL;
			break;
		}
		break;
	default:
		error = EINVAL;
		break;
	}

	return (error);
}

int
wdogintr(void *arg)
{
	struct trapframe *frame = arg;

	wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */
	wdog_wr_cnt(0);			/* reset to zero */
	printf("wdog trapped: spc = %x\n", frame->tf_spc);

	return (0);
}