syscon.c revision 1.2
1/*	$OpenBSD: syscon.c,v 1.2 2017/09/19 15:37:47 patrick Exp $	*/
2/*
3 * Copyright (c) 2017 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/device.h>
21
22#include <machine/bus.h>
23#include <machine/fdt.h>
24
25#include <dev/ofw/openfirm.h>
26#include <dev/ofw/ofw_misc.h>
27#include <dev/ofw/fdt.h>
28
29extern void (*cpuresetfn)(void);
30extern void (*powerdownfn)(void);
31
32struct syscon_softc {
33	struct device		sc_dev;
34	bus_space_tag_t		sc_iot;
35	bus_space_handle_t	sc_ioh;
36	uint32_t		sc_regmap;
37	bus_size_t		sc_offset;
38	uint32_t		sc_mask;
39};
40
41struct syscon_softc *syscon_reboot_sc;
42struct syscon_softc *syscon_poweroff_sc;
43
44int	syscon_match(struct device *, void *, void *);
45void	syscon_attach(struct device *, struct device *, void *);
46
47struct cfattach syscon_ca = {
48	sizeof(struct syscon_softc), syscon_match, syscon_attach
49};
50
51struct cfdriver syscon_cd = {
52	NULL, "syscon", DV_DULL
53};
54
55void	syscon_reset(void);
56void	syscon_powerdown(void);
57
58int
59syscon_match(struct device *parent, void *match, void *aux)
60{
61	struct fdt_attach_args *faa = aux;
62
63	return OF_is_compatible(faa->fa_node, "syscon") ||
64	    OF_is_compatible(faa->fa_node, "syscon-reboot") ||
65	    OF_is_compatible(faa->fa_node, "syscon-poweroff");
66}
67
68void
69syscon_attach(struct device *parent, struct device *self, void *aux)
70{
71	struct syscon_softc *sc = (struct syscon_softc *)self;
72	struct fdt_attach_args *faa = aux;
73
74	if (OF_is_compatible(faa->fa_node, "syscon")) {
75		if (faa->fa_nreg < 1) {
76			printf(": no registers\n");
77			return;
78		}
79
80		sc->sc_iot = faa->fa_iot;
81
82		if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
83		    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
84			printf(": can't map registers\n");
85			return;
86		}
87
88		regmap_register(faa->fa_node, sc->sc_iot, sc->sc_ioh,
89		    faa->fa_reg[0].size);
90		printf("\n");
91	}
92
93	if (OF_is_compatible(faa->fa_node, "syscon-reboot") ||
94	    OF_is_compatible(faa->fa_node, "syscon-poweroff")) {
95		printf("\n");
96
97		sc->sc_regmap = OF_getpropint(faa->fa_node, "regmap", 0);
98		if (sc->sc_regmap == 0)
99			return;
100
101		if (OF_getproplen(faa->fa_node, "offset") != sizeof(uint32_t) ||
102		    OF_getproplen(faa->fa_node, "mask") != sizeof(uint32_t))
103			return;
104
105		sc->sc_offset = OF_getpropint(faa->fa_node, "offset", 0);
106		sc->sc_mask = OF_getpropint(faa->fa_node, "mask", 0);
107
108		if (OF_is_compatible(faa->fa_node, "syscon-reboot")) {
109			syscon_reboot_sc = sc;
110			cpuresetfn = syscon_reset;
111		} else if (OF_is_compatible(faa->fa_node, "syscon-poweroff")) {
112			syscon_poweroff_sc = sc;
113			powerdownfn = syscon_powerdown;
114		}
115	}
116}
117
118void
119syscon_reset(void)
120{
121	struct syscon_softc *sc = syscon_reboot_sc;
122	struct regmap *rm;
123
124	rm = regmap_byphandle(sc->sc_regmap);
125	if (rm == NULL)
126		return;
127
128	regmap_write_4(rm, sc->sc_offset, sc->sc_mask);
129	delay(1000000);
130}
131
132void
133syscon_powerdown(void)
134{
135	struct syscon_softc *sc = syscon_poweroff_sc;
136	struct regmap *rm;
137
138	rm = regmap_byphandle(sc->sc_regmap);
139	if (rm == NULL)
140		return;
141
142	regmap_write_4(rm, sc->sc_offset, sc->sc_mask);
143	delay(1000000);
144}
145