From nobody Tue Dec 17 14:25:55 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YCJyC2d1jz5hHKk; Tue, 17 Dec 2024 14:25:55 +0000 (UTC) (envelope-from git@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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YCJyC1t2Yz4nBR; Tue, 17 Dec 2024 14:25:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734445555; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SAwTBBc7/MVgRFFe69zxPTghi221RPAGXyqxvFMU2mE=; b=hLNljdzD5/ZvoHwRArfSYGPg/o4C6fQGumv3/OOlUMsK/P5ZRV/PKbVYP+BwBSsf/QEcA8 iE48FapJjP4CBIbTNj+n87bNtnHwWqaCpAILeabJbTHNXZOEywoB+q3k45JCtzdeCrNpTV MGrA5HF9fLCtaVqLVRugQM1/52jmjgdhvFpsjDZLf5Kna7kKAMbyOIDDjsaFDQtt36oZwz sreztsPTqyRzSPuNG7QOYuLQZr3SuzF5x/3z+aS0nf1mLgTdb4BM9oBmFPSN0cst49s/ek bvgcaeLxyeuSV/dwEvIiIFG7rDkyAijaf+KuMw972KQMCkhftEUnDRBSxTGwWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1734445555; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SAwTBBc7/MVgRFFe69zxPTghi221RPAGXyqxvFMU2mE=; b=JltPgO9iav9sIOGnGPrPO+0P6g7hpeKYLllIR7N3NPsPtjwzsnO9c60Pfu3RWZBNfAVIII AMcVPzXQTJ1oB5zug7i2dI8/YVKZekJpUrzK+VqYmaG65Lb1NHS9iZYZippZxQMsAYo4ZW cVQ9gxyaWHJGJyx2n+9UCH2BK5Q0jc91bwx8Z+qnplo5CcIWXUwBEW1A4AABTawPZl2N+Y 75yGf7GFID+icO1CINSxgLIwh4t56+jCUyz5Zmg6Sqnx4o1YfspC9sj27ncuvmxeVUzYTu cUpwDIAen6DY0VmdKL8vd+J/YvFTIUHT2vTH342CCVImPH3XRr6rEkB2aFuQdg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1734445555; a=rsa-sha256; cv=none; b=x67/mat4/vEnNcC+K2dCA+H5JcQzLsIqNiWeKj4gbXqeJWmftbXTiL8U+O80BiANhjh4kp +Vl0mcwPNKwjhKKYyGYumJMv81qPWQH44hIRO9NmmXzk0tn3LY6DNZS2P+r/nJo/g+a2vV 9s23lXdOE/e7hmXI8ajB8zbFlxo2ER38RBeoLQZgbkMSeFH9mq6aSkb4WyjdAU0aHvZHgJ NB+oRj7igmCiQTZgq4gQJlDHMzWc3UO4MC9kd9j/DGSQS3tieMdjaJsJPN3KZXOj0Nmnjt DQCHekLmTyFLs1XgJqUOnm4XDD7goT6EQXFVUnMYKS5eZUQ3qJTZ839J3h0bVw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YCJyC1SwFzRYY; Tue, 17 Dec 2024 14:25:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4BHEPtS5010194; Tue, 17 Dec 2024 14:25:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BHEPtkW010191; Tue, 17 Dec 2024 14:25:55 GMT (envelope-from git) Date: Tue, 17 Dec 2024 14:25:55 GMT Message-Id: <202412171425.4BHEPtkW010191@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 2eace89d00b3 - main - arm64: add a driver for the Apple watchdog List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2eace89d00b38740a5f7414ce82adf150f4009c6 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=2eace89d00b38740a5f7414ce82adf150f4009c6 commit 2eace89d00b38740a5f7414ce82adf150f4009c6 Author: Kyle Evans AuthorDate: 2024-12-17 14:14:54 +0000 Commit: Kyle Evans CommitDate: 2024-12-17 14:24:14 +0000 arm64: add a driver for the Apple watchdog Ensure it's disarmed upon attach, provide basic reset functionality. Register definitions/usage obtained from OpenBSD. Reviewed by: ray Differential Revision: https://reviews.freebsd.org/D39824 --- sys/arm64/apple/apple_wdog.c | 236 +++++++++++++++++++++++++++++++++++++++++++ sys/conf/files.arm64 | 3 + 2 files changed, 239 insertions(+) diff --git a/sys/arm64/apple/apple_wdog.c b/sys/arm64/apple/apple_wdog.c new file mode 100644 index 000000000000..7bd34e48ba03 --- /dev/null +++ b/sys/arm64/apple/apple_wdog.c @@ -0,0 +1,236 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2022 Michael J. Karels + * Copyright (c) 2012 Alexander Rybalko + * All rights reserved. + * + * 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. + */ +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include + +#define APPLE_WDOG_WD0_TIMER 0x0000 +#define APPLE_WDOG_WD0_RESET 0x0004 +#define APPLE_WDOG_WD0_INTR 0x0008 +#define APPLE_WDOG_WD0_CNTL 0x000c + +#define APPLE_WDOG_WD1_TIMER 0x0010 +#define APPLE_WDOG_WD1_RESET 0x0014 +#define APPLE_WDOG_WD1_CNTL 0x001c + +#define APPLE_WDOG_WD2_TIMER 0x0020 +#define APPLE_WDOG_WD2_RESET 0x0024 +#define APPLE_WDOG_WD2_CNTL 0x002c + +#define APPLE_WDOG_CNTL_INTENABLE 0x0001 +#define APPLE_WDOG_CNTL_INTSTAT 0x0002 +#define APPLE_WDOG_CNTL_RSTENABLE 0x0004 + +#define READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r)) +#define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r), (_v)) + +struct apple_wdog_softc { + device_t dev; + struct resource * res; + bus_space_tag_t bst; + bus_space_handle_t bsh; + clk_t clk; + uint64_t clk_freq; + struct mtx mtx; +}; + +static struct ofw_compat_data compat_data[] = { + {"apple,wdt", 1}, + {NULL, 0} +}; + +static void apple_wdog_watchdog_fn(void *private, u_int cmd, int *error); +static void apple_wdog_reboot_system(void *, int); + +static int +apple_wdog_probe(device_t dev) +{ + + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + + if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) + return (ENXIO); + + device_set_desc(dev, "Apple Watchdog"); + + return (BUS_PROBE_DEFAULT); +} + +static int +apple_wdog_attach(device_t dev) +{ + struct apple_wdog_softc *sc; + int error, rid; + + sc = device_get_softc(dev); + sc->dev = dev; + + rid = 0; + sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); + if (sc->res == NULL) { + device_printf(dev, "could not allocate memory resource\n"); + return (ENXIO); + } + + sc->bst = rman_get_bustag(sc->res); + sc->bsh = rman_get_bushandle(sc->res); + + error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk); + if (error != 0) { + device_printf(dev, "cannot get clock\n"); + goto fail; + } + error = clk_enable(sc->clk); + if (error != 0) { + device_printf(dev, "cannot enable clock\n"); + goto fail; + } + error = clk_get_freq(sc->clk, &sc->clk_freq); + if (error != 0) { + device_printf(dev, "cannot get base frequency\n"); + goto fail_clk; + } + + mtx_init(&sc->mtx, "Apple Watchdog", "apple_wdog", MTX_DEF); + EVENTHANDLER_REGISTER(watchdog_list, apple_wdog_watchdog_fn, sc, 0); + EVENTHANDLER_REGISTER(shutdown_final, apple_wdog_reboot_system, sc, + SHUTDOWN_PRI_LAST); + + /* Reset the watchdog timers. */ + WRITE(sc, APPLE_WDOG_WD0_CNTL, 0); + WRITE(sc, APPLE_WDOG_WD1_CNTL, 0); + + return (0); + +fail_clk: + clk_disable(sc->clk); +fail: + bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->res); + return (error); +} + +static void +apple_wdog_watchdog_fn(void *private, u_int cmd, int *error) +{ + struct apple_wdog_softc *sc; + uint64_t sec; + uint32_t ticks, sec_max; + + sc = private; + mtx_lock(&sc->mtx); + + cmd &= WD_INTERVAL; + + if (cmd > 0) { + sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000; + sec_max = UINT_MAX / sc->clk_freq; + if (sec == 0 || sec > sec_max) { + /* + * Can't arm + * disable watchdog as watchdog(9) requires + */ + device_printf(sc->dev, + "Can't arm, timeout must be between 1-%d seconds\n", + sec_max); + WRITE(sc, APPLE_WDOG_WD1_CNTL, 0); + mtx_unlock(&sc->mtx); + *error = EINVAL; + return; + } + + ticks = sec * sc->clk_freq; + WRITE(sc, APPLE_WDOG_WD1_TIMER, 0); + WRITE(sc, APPLE_WDOG_WD1_RESET, ticks); + WRITE(sc, APPLE_WDOG_WD1_CNTL, APPLE_WDOG_CNTL_RSTENABLE); + + *error = 0; + } else + WRITE(sc, APPLE_WDOG_WD1_CNTL, 0); + + mtx_unlock(&sc->mtx); +} + +static void +apple_wdog_reboot_system(void *private, int howto) +{ + struct apple_wdog_softc *sc = private; + + /* Only handle reset. */ + if ((howto & (RB_HALT | RB_POWEROFF)) != 0) + return; + + printf("Resetting system ... "); + + WRITE(sc, APPLE_WDOG_WD1_CNTL, APPLE_WDOG_CNTL_RSTENABLE); + WRITE(sc, APPLE_WDOG_WD1_RESET, 1); + WRITE(sc, APPLE_WDOG_WD1_TIMER, 0); + + /* Wait for watchdog timeout; should take milliseconds. */ + DELAY(2000000); + + /* Not reached ... one hopes. */ + printf("failed to reset.\n"); +} + +static device_method_t apple_wdog_methods[] = { + DEVMETHOD(device_probe, apple_wdog_probe), + DEVMETHOD(device_attach, apple_wdog_attach), + + DEVMETHOD_END +}; + +static driver_t apple_wdog_driver = { + "apple_wdog", + apple_wdog_methods, + sizeof(struct apple_wdog_softc), +}; + +DRIVER_MODULE(apple_wdog, simplebus, apple_wdog_driver, 0, 0); diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64 index 3846301977d1..d13953a0bff3 100644 --- a/sys/conf/files.arm64 +++ b/sys/conf/files.arm64 @@ -538,6 +538,9 @@ arm/annapurna/alpine/alpine_serdes.c optional al_serdes fdt \ no-depend \ compile-with "${CC} -c -o ${.TARGET} ${CFLAGS} -I$S/contrib/alpine-hal -I$S/contrib/alpine-hal/eth ${.IMPSRC}" +# Apple +arm64/apple/apple_wdog.c optional soc_apple_t8103 fdt + # Broadcom arm64/broadcom/brcmmdio/mdio_mux_iproc.c optional soc_brcm_ns2 fdt arm64/broadcom/brcmmdio/mdio_nexus_iproc.c optional soc_brcm_ns2 fdt