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
|
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2020 Jessica Clarke <jrtc27@FreeBSD.org>
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*/
/*
* Driver for simple syscon poweroff and reset devices. The device tree
* specifications are fully described at:
*
* https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-poweroff.txt
* https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-reboot.txt
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/types.h>
#include <sys/eventhandler.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/reboot.h>
#include <machine/bus.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include "syscon_if.h"
#include "syscon.h"
struct syscon_power_softc {
struct syscon *regmap;
uint32_t offset;
uint32_t value;
uint32_t mask;
bool reboot;
eventhandler_tag shutdown_tag;
};
static void
syscon_power_shutdown_final(device_t dev, int howto)
{
struct syscon_power_softc *sc;
bool write;
sc = device_get_softc(dev);
if (sc->reboot)
write = (howto & RB_HALT) == 0;
else
write = (howto & RB_POWEROFF) != 0;
if (write)
SYSCON_MODIFY_4(sc->regmap, sc->offset, sc->mask,
sc->value & sc->mask);
}
static int
syscon_power_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_is_compatible(dev, "syscon-poweroff")) {
device_set_desc(dev, "Syscon poweroff");
return (BUS_PROBE_DEFAULT);
} else if (ofw_bus_is_compatible(dev, "syscon-reboot")) {
device_set_desc(dev, "Syscon reboot");
return (BUS_PROBE_DEFAULT);
}
return (ENXIO);
}
static int
syscon_power_attach(device_t dev)
{
struct syscon_power_softc *sc;
phandle_t node;
int error, len;
bool has_mask;
sc = device_get_softc(dev);
node = ofw_bus_get_node(dev);
if (!OF_hasprop(node, "regmap")) {
device_printf(dev, "could not find regmap\n");
return (ENXIO);
}
error = syscon_get_by_ofw_property(dev, node, "regmap", &sc->regmap);
if (error != 0) {
device_printf(dev, "could not get syscon\n");
return (ENXIO);
}
len = OF_getproplen(node, "offset");
if (len != 4) {
device_printf(dev, "could not get offset\n");
return (ENXIO);
}
OF_getencprop(node, "offset", &sc->offset, sizeof(sc->offset));
/* Optional mask */
has_mask = OF_hasprop(node, "mask");
if (has_mask) {
len = OF_getproplen(node, "mask");
if (len != 4) {
device_printf(dev, "cannot handle mask\n");
return (ENXIO);
}
OF_getencprop(node, "mask", &sc->mask, sizeof(sc->mask));
} else {
sc->mask = 0xffffffff;
}
/*
* From the device tree specification:
*
* Legacy usage: If a node doesn't contain a value property but
* contains a mask property, the mask property is used as the value.
*/
if (!OF_hasprop(node, "value")) {
if (!has_mask) {
device_printf(dev, "must have a value or a mask\n");
return (ENXIO);
}
sc->value = sc->mask;
} else {
len = OF_getproplen(node, "value");
if (len != 4) {
device_printf(dev, "cannot handle value\n");
return (ENXIO);
}
OF_getencprop(node, "value", &sc->value, sizeof(sc->value));
}
sc->reboot = ofw_bus_is_compatible(dev, "syscon-reboot");
sc->shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
syscon_power_shutdown_final, dev, SHUTDOWN_PRI_LAST);
return (0);
}
static int
syscon_power_detach(device_t dev)
{
struct syscon_power_softc *sc;
sc = device_get_softc(dev);
EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_tag);
return (0);
}
static device_method_t syscon_power_methods[] = {
DEVMETHOD(device_probe, syscon_power_probe),
DEVMETHOD(device_attach, syscon_power_attach),
DEVMETHOD(device_detach, syscon_power_detach),
DEVMETHOD_END
};
DEFINE_CLASS_0(syscon_power, syscon_power_driver, syscon_power_methods,
sizeof(struct syscon_power_softc));
static devclass_t syscon_power_devclass;
DRIVER_MODULE(syscon_power, simplebus, syscon_power_driver,
syscon_power_devclass, NULL, NULL);
|