From owner-svn-src-all@freebsd.org Sun Jul 26 18:19:51 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id A3B97368286; Sun, 26 Jul 2020 18:19:51 +0000 (UTC) (envelope-from jrtc27@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BFB6v3sMwz3dJ5; Sun, 26 Jul 2020 18:19:51 +0000 (UTC) (envelope-from jrtc27@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 67AD610BB8; Sun, 26 Jul 2020 18:19:51 +0000 (UTC) (envelope-from jrtc27@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 06QIJp8B030474; Sun, 26 Jul 2020 18:19:51 GMT (envelope-from jrtc27@FreeBSD.org) Received: (from jrtc27@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 06QIJpeB030472; Sun, 26 Jul 2020 18:19:51 GMT (envelope-from jrtc27@FreeBSD.org) Message-Id: <202007261819.06QIJpeB030472@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jrtc27 set sender to jrtc27@FreeBSD.org using -f From: Jessica Clarke Date: Sun, 26 Jul 2020 18:19:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r363573 - in head/sys: conf dev/extres/syscon X-SVN-Group: head X-SVN-Commit-Author: jrtc27 X-SVN-Commit-Paths: in head/sys: conf dev/extres/syscon X-SVN-Commit-Revision: 363573 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 26 Jul 2020 18:19:51 -0000 Author: jrtc27 Date: Sun Jul 26 18:19:50 2020 New Revision: 363573 URL: https://svnweb.freebsd.org/changeset/base/363573 Log: Add syscon power and reset control device driver This device driver supports both syscon-power and syscon-reset devices, as specified in [1] and [2]. These provide a very simple interface for power and reset control, and among other things are used by QEMU's virt machine on RISC-V. A separate commit will enable this on RISC-V, as that requires adding a RISC-V-specific riscv_syscon akin to r327936's aw_syscon. [1] https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-poweroff.txt [2] https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-reboot.txt Reviewed by: brooks (mentor), jhb (mentor) Approved by: brooks (mentor), jhb (mentor) Obtained from: CheriBSD Differential Revision: https://reviews.freebsd.org/D25724 Added: head/sys/dev/extres/syscon/syscon_power.c (contents, props changed) Modified: head/sys/conf/files Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Sun Jul 26 18:17:36 2020 (r363572) +++ head/sys/conf/files Sun Jul 26 18:19:50 2020 (r363573) @@ -1702,6 +1702,7 @@ dev/extres/regulator/regulator_fixed.c optional ext_re dev/extres/syscon/syscon.c optional ext_resources syscon dev/extres/syscon/syscon_generic.c optional ext_resources syscon fdt dev/extres/syscon/syscon_if.m optional ext_resources syscon +dev/extres/syscon/syscon_power.c optional ext_resources syscon syscon_power fdt dev/fb/fbd.c optional fbd | vt dev/fb/fb_if.m standard dev/fb/splash.c optional sc splash Added: head/sys/dev/extres/syscon/syscon_power.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/extres/syscon/syscon_power.c Sun Jul 26 18:19:50 2020 (r363573) @@ -0,0 +1,198 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Jessica Clarke + * + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#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);