summaryrefslogtreecommitdiff
path: root/sys/arch/armv7/vexpress/sysreg.c
blob: 4479ae0b55074aac90a6576fb96536effd1afd24 (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
/*	$OpenBSD: sysreg.c,v 1.1 2015/06/08 06:33:16 jsg Exp $	*/

/*
 * Copyright (c) 2015 Jonathan Gray <jsg@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 <armv7/armv7/armv7var.h>

#define SYS_ID			0x00
#define SYS_PROCID0		0x84
#define SYS_PROCID1		0x88
#define SYS_CFGDATA		0xa0
#define SYS_CFGCTRL		0xa4
#define SYS_CFGSTAT		0xa8

#define SYS_CFG_WRITE		(1 << 30)
#define SYS_CFG_START		(1U << 31)

#define SYS_CFG_RESET		5
#define SYS_CFG_SHUTDOWN	8
#define SYS_CFG_REBOOT		9

#define SYS_CFGSTAT_COMPLETE	(1 << 0)
#define SYS_CFGSTAT_ERROR	(1 << 1)

struct sysreg_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
};

struct sysreg_softc *sysreg_sc;

void sysreg_attach(struct device *, struct device *, void *);
void sysreg_reset(void);

struct cfattach sysreg_ca = {
	sizeof (struct sysreg_softc), NULL, sysreg_attach
};

struct cfdriver sysreg_cd = {
	NULL, "sysreg", DV_DULL
};

void
sysreg_attach(struct device *parent, struct device *self, void *args)
{
	struct armv7_attach_args *aa = args;
	struct sysreg_softc *sc = (struct sysreg_softc *)self;
	uint32_t id;

	sc->sc_iot = aa->aa_iot;
	if (bus_space_map(sc->sc_iot, aa->aa_dev->mem[0].addr,
	    aa->aa_dev->mem[0].size, 0, &sc->sc_ioh))
		panic(": bus_space_map failed!");
	sysreg_sc = sc;
	
	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_ID);
	printf(": ID 0x%x", id);

	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_PROCID0);
	printf(" PROCID0 0x%x\n", id);
}

void
sysconf_function(struct sysreg_softc *sc, int function)
{
	int dcc, site, position, device;

	dcc = 0;
	site = 0;
	position = 0;
	device = 0;

	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SYS_CFGSTAT, 0);
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SYS_CFGCTRL,
	    SYS_CFG_START | SYS_CFG_WRITE |
	    (dcc << 26) | (function << 20) | (site << 16) |
	    (position << 12) | device);

	while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_CFGSTAT) &
	    SYS_CFGSTAT_COMPLETE) == 0);

	if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_CFGSTAT) &
	    SYS_CFGSTAT_ERROR)
		printf("SYS_CFGSTAT error\n");
}

void
sysconf_reboot(void)
{
	struct sysreg_softc *sc = sysreg_sc;

	if (sc == NULL)
		return;

	sysconf_function(sc, SYS_CFG_REBOOT);
}

void
sysconf_shutdown(void)
{
	struct sysreg_softc *sc = sysreg_sc;

	if (sc == NULL)
		return;

	sysconf_function(sc, SYS_CFG_SHUTDOWN);
}