1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Jessica Clarke <[email protected]>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Driver for simple syscon poweroff and reset devices. The device tree
30  * specifications are fully described at:
31  *
32  * https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-poweroff.txt
33  * https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-reboot.txt
34  */
35 
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/types.h>
40 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/reboot.h>
44 
45 #include <machine/bus.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/ofw/openfirm.h>
50 
51 #include "syscon_if.h"
52 #include "syscon.h"
53 
54 struct syscon_power_softc {
55 	struct syscon		*regmap;
56 	uint32_t		offset;
57 	uint32_t		value;
58 	uint32_t		mask;
59 	bool			reboot;
60 	eventhandler_tag	shutdown_tag;
61 };
62 
63 static void
syscon_power_shutdown_final(device_t dev,int howto)64 syscon_power_shutdown_final(device_t dev, int howto)
65 {
66 	struct syscon_power_softc *sc;
67 	bool write;
68 
69 	sc = device_get_softc(dev);
70 	if (sc->reboot)
71 		write = (howto & RB_HALT) == 0;
72 	else
73 		write = (howto & RB_POWEROFF) != 0;
74 
75 	if (write)
76 		SYSCON_MODIFY_4(sc->regmap, sc->offset, sc->mask,
77 		    sc->value & sc->mask);
78 }
79 
80 static int
syscon_power_probe(device_t dev)81 syscon_power_probe(device_t dev)
82 {
83 
84 	if (!ofw_bus_status_okay(dev))
85 		return (ENXIO);
86 
87 	if (ofw_bus_is_compatible(dev, "syscon-poweroff")) {
88 		device_set_desc(dev, "Syscon poweroff");
89 		return (BUS_PROBE_DEFAULT);
90 	} else if (ofw_bus_is_compatible(dev, "syscon-reboot")) {
91 		device_set_desc(dev, "Syscon reboot");
92 		return (BUS_PROBE_DEFAULT);
93 	}
94 
95 	return (ENXIO);
96 }
97 
98 static int
syscon_power_attach(device_t dev)99 syscon_power_attach(device_t dev)
100 {
101 	struct syscon_power_softc *sc;
102 	phandle_t node;
103 	int error, len;
104 	bool has_mask;
105 
106 	sc = device_get_softc(dev);
107 	node = ofw_bus_get_node(dev);
108 
109 	if (!OF_hasprop(node, "regmap")) {
110 		device_printf(dev, "could not find regmap\n");
111 		return (ENXIO);
112 	}
113 
114 	error = syscon_get_by_ofw_property(dev, node, "regmap", &sc->regmap);
115 	if (error != 0) {
116 		device_printf(dev, "could not get syscon\n");
117 		return (ENXIO);
118 	}
119 
120 	len = OF_getproplen(node, "offset");
121 	if (len != 4) {
122 		device_printf(dev, "could not get offset\n");
123 		return (ENXIO);
124 	}
125 
126 	OF_getencprop(node, "offset", &sc->offset, sizeof(sc->offset));
127 
128 	/* Optional mask */
129 	has_mask = OF_hasprop(node, "mask");
130 	if (has_mask) {
131 		len = OF_getproplen(node, "mask");
132 		if (len != 4) {
133 			device_printf(dev, "cannot handle mask\n");
134 			return (ENXIO);
135 		}
136 
137 		OF_getencprop(node, "mask", &sc->mask, sizeof(sc->mask));
138 	} else {
139 		sc->mask = 0xffffffff;
140 	}
141 
142 	/*
143 	 * From the device tree specification:
144 	 *
145 	 *   Legacy usage: If a node doesn't contain a value property but
146 	 *   contains a mask property, the mask property is used as the value.
147 	 */
148 	if (!OF_hasprop(node, "value")) {
149 		if (!has_mask) {
150 			device_printf(dev, "must have a value or a mask\n");
151 			return (ENXIO);
152 		}
153 
154 		sc->value = sc->mask;
155 	} else {
156 		len = OF_getproplen(node, "value");
157 		if (len != 4) {
158 			device_printf(dev, "cannot handle value\n");
159 			return (ENXIO);
160 		}
161 
162 		OF_getencprop(node, "value", &sc->value, sizeof(sc->value));
163 	}
164 
165 	/* Handle reboot after shutdown_panic. */
166 	sc->reboot = ofw_bus_is_compatible(dev, "syscon-reboot");
167 	sc->shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
168 	    syscon_power_shutdown_final, dev,
169 	    sc->reboot ? SHUTDOWN_PRI_LAST + 150 : SHUTDOWN_PRI_LAST);
170 
171 	return (0);
172 }
173 
174 static int
syscon_power_detach(device_t dev)175 syscon_power_detach(device_t dev)
176 {
177 	struct syscon_power_softc *sc;
178 
179 	sc = device_get_softc(dev);
180 	EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_tag);
181 
182 	return (0);
183 }
184 
185 static device_method_t syscon_power_methods[] = {
186 	DEVMETHOD(device_probe,		syscon_power_probe),
187 	DEVMETHOD(device_attach,	syscon_power_attach),
188 	DEVMETHOD(device_detach,	syscon_power_detach),
189 
190 	DEVMETHOD_END
191 };
192 
193 DEFINE_CLASS_0(syscon_power, syscon_power_driver, syscon_power_methods,
194     sizeof(struct syscon_power_softc));
195 
196 DRIVER_MODULE(syscon_power, simplebus, syscon_power_driver, NULL, NULL);
197