162e8ccc3SEmmanuel Vadot /*-
262e8ccc3SEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause
362e8ccc3SEmmanuel Vadot *
462e8ccc3SEmmanuel Vadot * Copyright (c) 2020 Jessica Clarke <jrtc27@FreeBSD.org>
562e8ccc3SEmmanuel Vadot *
662e8ccc3SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
762e8ccc3SEmmanuel Vadot * modification, are permitted provided that the following conditions
862e8ccc3SEmmanuel Vadot * are met:
962e8ccc3SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
1062e8ccc3SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
1162e8ccc3SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
1262e8ccc3SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
1362e8ccc3SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
1462e8ccc3SEmmanuel Vadot *
1562e8ccc3SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1662e8ccc3SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1762e8ccc3SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1862e8ccc3SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1962e8ccc3SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2062e8ccc3SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2162e8ccc3SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2262e8ccc3SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2362e8ccc3SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2462e8ccc3SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2562e8ccc3SEmmanuel Vadot * SUCH DAMAGE.
2662e8ccc3SEmmanuel Vadot */
2762e8ccc3SEmmanuel Vadot
2862e8ccc3SEmmanuel Vadot /*
2962e8ccc3SEmmanuel Vadot * Driver for simple syscon poweroff and reset devices. The device tree
3062e8ccc3SEmmanuel Vadot * specifications are fully described at:
3162e8ccc3SEmmanuel Vadot *
3262e8ccc3SEmmanuel Vadot * https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-poweroff.txt
3362e8ccc3SEmmanuel Vadot * https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-reboot.txt
3462e8ccc3SEmmanuel Vadot */
3562e8ccc3SEmmanuel Vadot
3662e8ccc3SEmmanuel Vadot #include <sys/param.h>
3762e8ccc3SEmmanuel Vadot #include <sys/bus.h>
3862e8ccc3SEmmanuel Vadot #include <sys/types.h>
3962e8ccc3SEmmanuel Vadot #include <sys/eventhandler.h>
4062e8ccc3SEmmanuel Vadot #include <sys/kernel.h>
4162e8ccc3SEmmanuel Vadot #include <sys/module.h>
4262e8ccc3SEmmanuel Vadot #include <sys/reboot.h>
4362e8ccc3SEmmanuel Vadot
4462e8ccc3SEmmanuel Vadot #include <machine/bus.h>
4562e8ccc3SEmmanuel Vadot
4662e8ccc3SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
4762e8ccc3SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
4862e8ccc3SEmmanuel Vadot #include <dev/ofw/openfirm.h>
4962e8ccc3SEmmanuel Vadot
5062e8ccc3SEmmanuel Vadot #include "syscon_if.h"
5162e8ccc3SEmmanuel Vadot #include "syscon.h"
5262e8ccc3SEmmanuel Vadot
5362e8ccc3SEmmanuel Vadot struct syscon_power_softc {
5462e8ccc3SEmmanuel Vadot struct syscon *regmap;
5562e8ccc3SEmmanuel Vadot uint32_t offset;
5662e8ccc3SEmmanuel Vadot uint32_t value;
5762e8ccc3SEmmanuel Vadot uint32_t mask;
5862e8ccc3SEmmanuel Vadot bool reboot;
5962e8ccc3SEmmanuel Vadot eventhandler_tag shutdown_tag;
6062e8ccc3SEmmanuel Vadot };
6162e8ccc3SEmmanuel Vadot
6262e8ccc3SEmmanuel Vadot static void
syscon_power_shutdown_final(device_t dev,int howto)6362e8ccc3SEmmanuel Vadot syscon_power_shutdown_final(device_t dev, int howto)
6462e8ccc3SEmmanuel Vadot {
6562e8ccc3SEmmanuel Vadot struct syscon_power_softc *sc;
6662e8ccc3SEmmanuel Vadot bool write;
6762e8ccc3SEmmanuel Vadot
6862e8ccc3SEmmanuel Vadot sc = device_get_softc(dev);
6962e8ccc3SEmmanuel Vadot if (sc->reboot)
7062e8ccc3SEmmanuel Vadot write = (howto & RB_HALT) == 0;
7162e8ccc3SEmmanuel Vadot else
7262e8ccc3SEmmanuel Vadot write = (howto & RB_POWEROFF) != 0;
7362e8ccc3SEmmanuel Vadot
7462e8ccc3SEmmanuel Vadot if (write)
7562e8ccc3SEmmanuel Vadot SYSCON_MODIFY_4(sc->regmap, sc->offset, sc->mask,
7662e8ccc3SEmmanuel Vadot sc->value & sc->mask);
7762e8ccc3SEmmanuel Vadot }
7862e8ccc3SEmmanuel Vadot
7962e8ccc3SEmmanuel Vadot static int
syscon_power_probe(device_t dev)8062e8ccc3SEmmanuel Vadot syscon_power_probe(device_t dev)
8162e8ccc3SEmmanuel Vadot {
8262e8ccc3SEmmanuel Vadot
8362e8ccc3SEmmanuel Vadot if (!ofw_bus_status_okay(dev))
8462e8ccc3SEmmanuel Vadot return (ENXIO);
8562e8ccc3SEmmanuel Vadot
8662e8ccc3SEmmanuel Vadot if (ofw_bus_is_compatible(dev, "syscon-poweroff")) {
8762e8ccc3SEmmanuel Vadot device_set_desc(dev, "Syscon poweroff");
8862e8ccc3SEmmanuel Vadot return (BUS_PROBE_DEFAULT);
8962e8ccc3SEmmanuel Vadot } else if (ofw_bus_is_compatible(dev, "syscon-reboot")) {
9062e8ccc3SEmmanuel Vadot device_set_desc(dev, "Syscon reboot");
9162e8ccc3SEmmanuel Vadot return (BUS_PROBE_DEFAULT);
9262e8ccc3SEmmanuel Vadot }
9362e8ccc3SEmmanuel Vadot
9462e8ccc3SEmmanuel Vadot return (ENXIO);
9562e8ccc3SEmmanuel Vadot }
9662e8ccc3SEmmanuel Vadot
9762e8ccc3SEmmanuel Vadot static int
syscon_power_attach(device_t dev)9862e8ccc3SEmmanuel Vadot syscon_power_attach(device_t dev)
9962e8ccc3SEmmanuel Vadot {
10062e8ccc3SEmmanuel Vadot struct syscon_power_softc *sc;
10162e8ccc3SEmmanuel Vadot phandle_t node;
10262e8ccc3SEmmanuel Vadot int error, len;
10362e8ccc3SEmmanuel Vadot bool has_mask;
10462e8ccc3SEmmanuel Vadot
10562e8ccc3SEmmanuel Vadot sc = device_get_softc(dev);
10662e8ccc3SEmmanuel Vadot node = ofw_bus_get_node(dev);
10762e8ccc3SEmmanuel Vadot
10862e8ccc3SEmmanuel Vadot if (!OF_hasprop(node, "regmap")) {
10962e8ccc3SEmmanuel Vadot device_printf(dev, "could not find regmap\n");
11062e8ccc3SEmmanuel Vadot return (ENXIO);
11162e8ccc3SEmmanuel Vadot }
11262e8ccc3SEmmanuel Vadot
11362e8ccc3SEmmanuel Vadot error = syscon_get_by_ofw_property(dev, node, "regmap", &sc->regmap);
11462e8ccc3SEmmanuel Vadot if (error != 0) {
11562e8ccc3SEmmanuel Vadot device_printf(dev, "could not get syscon\n");
11662e8ccc3SEmmanuel Vadot return (ENXIO);
11762e8ccc3SEmmanuel Vadot }
11862e8ccc3SEmmanuel Vadot
11962e8ccc3SEmmanuel Vadot len = OF_getproplen(node, "offset");
12062e8ccc3SEmmanuel Vadot if (len != 4) {
12162e8ccc3SEmmanuel Vadot device_printf(dev, "could not get offset\n");
12262e8ccc3SEmmanuel Vadot return (ENXIO);
12362e8ccc3SEmmanuel Vadot }
12462e8ccc3SEmmanuel Vadot
12562e8ccc3SEmmanuel Vadot OF_getencprop(node, "offset", &sc->offset, sizeof(sc->offset));
12662e8ccc3SEmmanuel Vadot
12762e8ccc3SEmmanuel Vadot /* Optional mask */
12862e8ccc3SEmmanuel Vadot has_mask = OF_hasprop(node, "mask");
12962e8ccc3SEmmanuel Vadot if (has_mask) {
13062e8ccc3SEmmanuel Vadot len = OF_getproplen(node, "mask");
13162e8ccc3SEmmanuel Vadot if (len != 4) {
13262e8ccc3SEmmanuel Vadot device_printf(dev, "cannot handle mask\n");
13362e8ccc3SEmmanuel Vadot return (ENXIO);
13462e8ccc3SEmmanuel Vadot }
13562e8ccc3SEmmanuel Vadot
13662e8ccc3SEmmanuel Vadot OF_getencprop(node, "mask", &sc->mask, sizeof(sc->mask));
13762e8ccc3SEmmanuel Vadot } else {
13862e8ccc3SEmmanuel Vadot sc->mask = 0xffffffff;
13962e8ccc3SEmmanuel Vadot }
14062e8ccc3SEmmanuel Vadot
14162e8ccc3SEmmanuel Vadot /*
14262e8ccc3SEmmanuel Vadot * From the device tree specification:
14362e8ccc3SEmmanuel Vadot *
14462e8ccc3SEmmanuel Vadot * Legacy usage: If a node doesn't contain a value property but
14562e8ccc3SEmmanuel Vadot * contains a mask property, the mask property is used as the value.
14662e8ccc3SEmmanuel Vadot */
14762e8ccc3SEmmanuel Vadot if (!OF_hasprop(node, "value")) {
14862e8ccc3SEmmanuel Vadot if (!has_mask) {
14962e8ccc3SEmmanuel Vadot device_printf(dev, "must have a value or a mask\n");
15062e8ccc3SEmmanuel Vadot return (ENXIO);
15162e8ccc3SEmmanuel Vadot }
15262e8ccc3SEmmanuel Vadot
15362e8ccc3SEmmanuel Vadot sc->value = sc->mask;
15462e8ccc3SEmmanuel Vadot } else {
15562e8ccc3SEmmanuel Vadot len = OF_getproplen(node, "value");
15662e8ccc3SEmmanuel Vadot if (len != 4) {
15762e8ccc3SEmmanuel Vadot device_printf(dev, "cannot handle value\n");
15862e8ccc3SEmmanuel Vadot return (ENXIO);
15962e8ccc3SEmmanuel Vadot }
16062e8ccc3SEmmanuel Vadot
16162e8ccc3SEmmanuel Vadot OF_getencprop(node, "value", &sc->value, sizeof(sc->value));
16262e8ccc3SEmmanuel Vadot }
16362e8ccc3SEmmanuel Vadot
164*5f7312a0SAndriy Gapon /* Handle reboot after shutdown_panic. */
16562e8ccc3SEmmanuel Vadot sc->reboot = ofw_bus_is_compatible(dev, "syscon-reboot");
16662e8ccc3SEmmanuel Vadot sc->shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
167*5f7312a0SAndriy Gapon syscon_power_shutdown_final, dev,
168*5f7312a0SAndriy Gapon sc->reboot ? SHUTDOWN_PRI_LAST + 150 : SHUTDOWN_PRI_LAST);
16962e8ccc3SEmmanuel Vadot
17062e8ccc3SEmmanuel Vadot return (0);
17162e8ccc3SEmmanuel Vadot }
17262e8ccc3SEmmanuel Vadot
17362e8ccc3SEmmanuel Vadot static int
syscon_power_detach(device_t dev)17462e8ccc3SEmmanuel Vadot syscon_power_detach(device_t dev)
17562e8ccc3SEmmanuel Vadot {
17662e8ccc3SEmmanuel Vadot struct syscon_power_softc *sc;
17762e8ccc3SEmmanuel Vadot
17862e8ccc3SEmmanuel Vadot sc = device_get_softc(dev);
17962e8ccc3SEmmanuel Vadot EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_tag);
18062e8ccc3SEmmanuel Vadot
18162e8ccc3SEmmanuel Vadot return (0);
18262e8ccc3SEmmanuel Vadot }
18362e8ccc3SEmmanuel Vadot
18462e8ccc3SEmmanuel Vadot static device_method_t syscon_power_methods[] = {
18562e8ccc3SEmmanuel Vadot DEVMETHOD(device_probe, syscon_power_probe),
18662e8ccc3SEmmanuel Vadot DEVMETHOD(device_attach, syscon_power_attach),
18762e8ccc3SEmmanuel Vadot DEVMETHOD(device_detach, syscon_power_detach),
18862e8ccc3SEmmanuel Vadot
18962e8ccc3SEmmanuel Vadot DEVMETHOD_END
19062e8ccc3SEmmanuel Vadot };
19162e8ccc3SEmmanuel Vadot
19262e8ccc3SEmmanuel Vadot DEFINE_CLASS_0(syscon_power, syscon_power_driver, syscon_power_methods,
19362e8ccc3SEmmanuel Vadot sizeof(struct syscon_power_softc));
19462e8ccc3SEmmanuel Vadot
19562e8ccc3SEmmanuel Vadot DRIVER_MODULE(syscon_power, simplebus, syscon_power_driver, NULL, NULL);
196