summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/sxidog.c
blob: 59c41c1e895b14c138f0d58f295fce4bb6f9765a (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
/* $OpenBSD: sxidog.c,v 1.2 2019/10/17 22:26:32 kettenis Exp $ */
/*
 * Copyright (c) 2007,2009 Dale Rahn <drahn@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/bus.h>
#include <machine/fdt.h>

#include <dev/fdt/sunxireg.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

extern void (*cpuresetfn)(void);

/* Allwinner A10 registers */
#define WDOG_CTRL_REG		0x00
#define  WDOG_KEY		(0x0a57 << 1)
#define  WDOG_RSTART		0x01
#define WDOG_MODE_REG		0x04
#define  WDOG_EN		(1 << 0)
#define  WDOG_RST_EN		(1 << 1) /* system reset */
#define  WDOG_INTV_VALUE(x)	((x) << 3)

/* Allwinner A31 registers */
#define WDOG0_CTRL_REG		0x10
#define  WDOG0_KEY		(0x0a57 << 1)
#define  WDOG0_RSTART		(1 << 0)
#define WDOG0_CFG_REG		0x14
#define  WDOG0_RST_EN		(1 << 0)
#define WDOG0_MODE_REG		0x18
#define  WDOG0_EN		(1 << 0)
#define  WDOG0_INTV_VALUE(x)	((x) << 4)

struct sxidog_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	int			sc_type;
#define SXIDOG_A10		0
#define SXIDOG_A31		1
};

struct sxidog_softc *sxidog_sc = NULL;	/* for sxidog_reset() */

int sxidog_match(struct device *, void *, void *);
void sxidog_attach(struct device *, struct device *, void *);
int sxidog_activate(struct device *, int);
int sxidog_callback(void *, int);
void sxidog_reset(void);

struct cfattach	sxidog_ca = {
	sizeof (struct sxidog_softc), sxidog_match, sxidog_attach,
	NULL, sxidog_activate
};

struct cfdriver sxidog_cd = {
	NULL, "sxidog", DV_DULL
};

int
sxidog_match(struct device *parent, void *match, void *aux)
{
	struct fdt_attach_args *faa = aux;

	return (OF_is_compatible(faa->fa_node, "allwinner,sun4i-a10-wdt") ||
	    OF_is_compatible(faa->fa_node, "allwinner,sun6i-a31-wdt"));
}

void
sxidog_attach(struct device *parent, struct device *self, void *aux)
{
	struct sxidog_softc *sc = (struct sxidog_softc *)self;
	struct fdt_attach_args *faa = aux;

	if (faa->fa_nreg < 1)
		return;

	sc->sc_iot = faa->fa_iot;
	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
		panic("sxidog_attach: bus_space_map failed!");

	if (OF_is_compatible(faa->fa_node, "allwinner,sun6i-a31-wdt")) {
		SXIWRITE4(sc, WDOG0_CFG_REG, WDOG0_RST_EN);
		sc->sc_type = SXIDOG_A31;
	} else
		sc->sc_type = SXIDOG_A10;

	sxidog_sc = sc;
	cpuresetfn = sxidog_reset;

#ifndef SMALL_KERNEL
	wdog_register(sxidog_callback, sc);
#endif

	printf("\n");
}

int
sxidog_activate(struct device *self, int act)
{
	switch (act) {
	case DVACT_POWERDOWN:
#ifndef SMALL_KERNEL
		wdog_shutdown(self);
#endif
		break;
	}

	return (0);
}

int
sxidog_callback(void *arg, int period)
{
	struct sxidog_softc *sc = (struct sxidog_softc *)arg;
	int enable;

	if (period > 16)
		period = 16;
	else if (period < 0)
		period = 0;

	/* Convert to register encoding. */
	if (period > 6)
		period = 6 + (period - 5) / 2;

	switch (sc->sc_type) {
	case SXIDOG_A10:
		enable = (period > 0) ? WDOG_RST_EN : 0;
		SXIWRITE4(sc, WDOG_MODE_REG,
		    enable | WDOG_EN | WDOG_INTV_VALUE(period));
		SXIWRITE4(sc, WDOG_CTRL_REG, WDOG_KEY | WDOG_RSTART);
		break;
	case SXIDOG_A31:
		enable = (period > 0) ? WDOG0_EN : 0;
		SXIWRITE4(sc, WDOG0_MODE_REG,
		    enable | WDOG0_INTV_VALUE(period));
		SXIWRITE4(sc, WDOG0_CTRL_REG, WDOG0_KEY | WDOG0_RSTART);
		break;
	}

	/* Convert back to seconds. */
	if (period > 6)
		period = 6 + (period - 6) * 2;

	return period;
}

void
sxidog_reset(void)
{
	if (sxidog_sc == NULL)
		return;

	sxidog_callback(sxidog_sc, 1);
	delay(1500000);
}