Path: blob/main/sys/riscv/cvitek/cvitek_restart.c
108657 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Bojan Novković <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627/*28* Driver for cvitek's poweroff/restart controller.29*/3031#include <sys/param.h>32#include <sys/bus.h>33#include <sys/types.h>34#include <sys/eventhandler.h>35#include <sys/kernel.h>36#include <sys/module.h>37#include <sys/reboot.h>38#include <sys/rman.h>3940#include <machine/bus.h>41#include <machine/resource.h>4243#include <dev/ofw/ofw_bus.h>44#include <dev/ofw/ofw_bus_subr.h>45#include <dev/ofw/openfirm.h>4647#define RTC_CTRL0_UNLOCK 0x448#define RTC_CTRL0_UNLOCK_KEY 0xAB184950#define RTC_CTRL0 0x851#define RTC_CTRL0_RESERVED_MASK 0xFFFF080052#define RTC_CTRL0_REQ_SHUTDOWN 0x0153#define RTC_CTRL0_REQ_POWERCYCLE 0x0854#define RTC_CTRL0_REQ_WARM_RESET 0x105556#define RTC_BASE_OFFSET 0x100057#define RTC_EN_SHDN_REQ (RTC_BASE_OFFSET + 0xC0)58#define RTC_EN_PWR_CYC_REQ (RTC_BASE_OFFSET + 0xC8)59#define RTC_EN_WARM_RST_REQ (RTC_BASE_OFFSET + 0xCC)6061struct cvitek_restart_softc {62int reg_rid;63struct resource *reg;64eventhandler_tag tag;65};6667static void68cvitek_restart_shutdown_final(device_t dev, int howto)69{70struct cvitek_restart_softc *sc;71uint32_t val;7273sc = device_get_softc(dev);74val = RTC_CTRL0_RESERVED_MASK;75if ((howto & RB_POWEROFF) != 0)76val |= RTC_CTRL0_REQ_SHUTDOWN;77else if ((howto & RB_POWERCYCLE) != 0)78val |= RTC_CTRL0_REQ_POWERCYCLE;79else80val |= RTC_CTRL0_REQ_WARM_RESET;8182/* Unlock writes to 'rtc_ctrl0'. */83bus_write_4(sc->reg, RTC_CTRL0_UNLOCK, RTC_CTRL0_UNLOCK_KEY);84bus_write_4(sc->reg, RTC_CTRL0, val);85DELAY(1000);8687device_printf(dev, "Poweroff request failed\n");88}8990static int91cvitek_restart_probe(device_t dev)92{9394if (!ofw_bus_status_okay(dev))95return (ENXIO);9697if (ofw_bus_is_compatible(dev, "cvitek,restart")) {98device_set_desc(dev, "Cvitek restart controller");99return (BUS_PROBE_DEFAULT);100}101return (ENXIO);102}103104static int105cvitek_restart_attach(device_t dev)106{107struct cvitek_restart_softc *sc;108109sc = device_get_softc(dev);110sc->reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->reg_rid,111RF_ACTIVE);112if (sc->reg == NULL) {113device_printf(dev, "can't map RTC regs\n");114return (ENXIO);115}116117/* Enable requests for various poweroff methods. */118bus_write_4(sc->reg, RTC_EN_SHDN_REQ, 0x1);119bus_write_4(sc->reg, RTC_EN_PWR_CYC_REQ, 0x1);120bus_write_4(sc->reg, RTC_EN_WARM_RST_REQ, 0x1);121122sc->tag = EVENTHANDLER_REGISTER(shutdown_final,123cvitek_restart_shutdown_final, dev, SHUTDOWN_PRI_LAST);124125return (0);126}127128static int129cvitek_restart_detach(device_t dev)130{131struct cvitek_restart_softc *sc;132133sc = device_get_softc(dev);134if (sc->reg == NULL)135return (0);136137bus_write_4(sc->reg, RTC_EN_SHDN_REQ, 0x0);138bus_write_4(sc->reg, RTC_EN_PWR_CYC_REQ, 0x0);139bus_write_4(sc->reg, RTC_EN_WARM_RST_REQ, 0x0);140141bus_release_resource(dev, SYS_RES_MEMORY, sc->reg_rid, sc->reg);142EVENTHANDLER_DEREGISTER(shutdown_final, sc->tag);143144return (0);145}146147static device_method_t cvitek_restart_methods[] = {148DEVMETHOD(device_probe, cvitek_restart_probe),149DEVMETHOD(device_attach, cvitek_restart_attach),150DEVMETHOD(device_detach, cvitek_restart_detach),151152DEVMETHOD_END153};154155DEFINE_CLASS_0(cvitek_restart, cvitek_restart_driver, cvitek_restart_methods,156sizeof(struct cvitek_restart_softc));157DRIVER_MODULE(cvitek_restart, simplebus, cvitek_restart_driver, NULL, NULL);158159160