From patchwork Tue Nov 1 02:46:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1697610 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=YPq9su88; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N1ZGx0Yt2z23lf for ; Tue, 1 Nov 2022 13:49:12 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ophJO-0002HE-N3; Mon, 31 Oct 2022 22:47:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ophJB-0002Ew-Ot for qemu-devel@nongnu.org; Mon, 31 Oct 2022 22:47:29 -0400 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ophJ7-0004DZ-Rb for qemu-devel@nongnu.org; Mon, 31 Oct 2022 22:47:20 -0400 Received: by mail-oi1-x236.google.com with SMTP id r187so14685814oia.8 for ; Mon, 31 Oct 2022 19:47:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wfUGhwU1VteacybJCdZSqbcjAdjKhCpH83N3+0mrIYo=; b=YPq9su88/mTod8ElHfV6XDj6UgUC0+SkKbkVZNcd0sNfHvkFAqcfkQppxSyIXm03qq 7LdB8sAG84U0ewfbXYfW/9Xy0Bnhqf+x6IGx6sZo0cHYovcw7nzGShyawfi/pgDUWSwd /e+eZqVtd/Sv9vHg6N8/bH3zGDn/MkL+34bzsxlg1vMvZsNPpAZ+c6AFxR8F3I+aJLx4 ng7pYCTziLxmdUnwfsNJELc/pVV6GQN7HubyiV1oP97atJtzXPGC0vRU8XIrsLs6rY6j M+7Ya3qNOs8xHF4YupCFaNtLlBreUIG9RlbW2eSg8IqKDHxYi8Pob7auruWM/3Ddviba rvhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wfUGhwU1VteacybJCdZSqbcjAdjKhCpH83N3+0mrIYo=; b=q4QVdTZ6r+n/pGqtN8+AIjeH3s5l0eZ5QjrgR2bXyHswJ4jk0jbIw8zTLGnhbwVJwT liiS3b9FB/UUJucHBT6vdaLAEBflq6pUtSfViPBwFLsS692k1bQ8Q4TfLNfQLDL1BVj3 ZIhd+Ljemrl5Yhn7tJdvR6nVLu1RYW3uXhYMUn2Pjqmb39D5/6cwBQ/0B9pnUqMDH/0a 5oamsa4BEXB+3znVwNOciTDP650/oaeEwlfyK8M1oNBV/fCxooMgmqO82QzC5PCCA/9k cGjmG+1xrnWpzOTUs5ywxO0efG4qIl9xhEtSREXIoNmzXpsmLhuL+dShMaiNAhCB2MEt LU2g== X-Gm-Message-State: ACrzQf03g+ZhDRdV+oD29yptc3luF9/WcfIJqA6MJUwehqSFxZqygIYk OD2y3ehmYr78Gwh6K15mhAvRCd9NL05SHSA8AaN/4C2q2TMobFMZ88eOooxDxB+bDh9PSHJ53xz UV/UKLprIoHazsNWeSyzDaBOrnDiQYTV2xkAxxdtQD0+6I3Fsy0+2tzl5iEvGnOV77Xr4Rzk= X-Google-Smtp-Source: AMsMyM5OZzURFD7VhQ6tjRrv7dKq+6FFFtjOPjMpUZDo6340rfy0bEMEC44FO/D29sw5cyv6sircLg== X-Received: by 2002:aca:1019:0:b0:359:e535:bf92 with SMTP id 25-20020aca1019000000b00359e535bf92mr8383435oiq.90.1667270836249; Mon, 31 Oct 2022 19:47:16 -0700 (PDT) Received: from sw05.internal.sifive.com ([64.62.193.194]) by smtp.gmail.com with ESMTPSA id r4-20020a05683001c400b00661a33883b8sm3367215ota.71.2022.10.31.19.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 19:47:15 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: alistair.francis@wdc.com, palmer@dabbelt.com, bin.meng@windriver.com, jim.shu@sifive.com, frank.chang@sifive.com, Tommy Wu Subject: [PATCH v2 1/3] hw/misc: sifive_e_aon: Support the watchdog timer of HiFive 1 rev b. Date: Mon, 31 Oct 2022 19:46:54 -0700 Message-Id: <20221101024656.517608-2-tommy.wu@sifive.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221101024656.517608-1-tommy.wu@sifive.com> References: <20221101024656.517608-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::236; envelope-from=tommy.wu@sifive.com; helo=mail-oi1-x236.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org+incoming=patchwork.ozlabs.org@nongnu.org The watchdog timer is in the always-on domain device of HiFive 1 rev b, so this patch added the AON device to the sifive_e machine. This patch only implemented the functionality of the watchdog timer. Signed-off-by: Tommy Wu --- hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/sifive_e_aon.c | 403 +++++++++++++++++++++++++++++++++ include/hw/misc/sifive_e_aon.h | 62 +++++ 4 files changed, 469 insertions(+) create mode 100644 hw/misc/sifive_e_aon.c create mode 100644 include/hw/misc/sifive_e_aon.h diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index cbabe9f78c..7d1247822e 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -162,6 +162,9 @@ config SIFIVE_TEST config SIFIVE_E_PRCI bool +config SIFIVE_E_AON + bool + config SIFIVE_U_OTP bool diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 95268eddc0..94170dce76 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -31,6 +31,7 @@ softmmu_ss.add(when: 'CONFIG_MCHP_PFSOC_IOSCB', if_true: files('mchp_pfsoc_ioscb softmmu_ss.add(when: 'CONFIG_MCHP_PFSOC_SYSREG', if_true: files('mchp_pfsoc_sysreg.c')) softmmu_ss.add(when: 'CONFIG_SIFIVE_TEST', if_true: files('sifive_test.c')) softmmu_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: files('sifive_e_prci.c')) +softmmu_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c')) softmmu_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c')) softmmu_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c')) diff --git a/hw/misc/sifive_e_aon.c b/hw/misc/sifive_e_aon.c new file mode 100644 index 0000000000..6c8b3815f4 --- /dev/null +++ b/hw/misc/sifive_e_aon.c @@ -0,0 +1,403 @@ +/* + * SiFive HiFive1 AON (Always On Domain) for QEMU. + * + * Copyright (c) 2022 SiFive, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "qemu/timer.h" +#include "qemu/log.h" +#include "hw/irq.h" +#include "hw/registerfields.h" +#include "hw/misc/sifive_e_aon.h" +#include "qapi/visitor.h" +#include "qapi/error.h" +#include "sysemu/watchdog.h" + +REG32(AON_WDT_WDOGCFG, 0x0) + FIELD(AON_WDT_WDOGCFG, + SCALE, 0, 4) + FIELD(AON_WDT_WDOGCFG, + RSVD0, 4, 4) + FIELD(AON_WDT_WDOGCFG, + RSTEN, 8, 1) + FIELD(AON_WDT_WDOGCFG, + ZEROCMP, 9, 1) + FIELD(AON_WDT_WDOGCFG, + RSVD1, 10, 2) + FIELD(AON_WDT_WDOGCFG, + EN_ALWAYS, 12, 1) + FIELD(AON_WDT_WDOGCFG, + EN_CORE_AWAKE, 13, 1) + FIELD(AON_WDT_WDOGCFG, + RSVD2, 14, 14) + FIELD(AON_WDT_WDOGCFG, + IP0, 28, 1) + FIELD(AON_WDT_WDOGCFG, + RSVD3, 29, 3) +REG32(AON_WDT_WDOGCOUNT, 0x8) +REG32(AON_WDT_WDOGS, 0x10) +REG32(AON_WDT_WDOGFEED, 0x18) +REG32(AON_WDT_WDOGKEY, 0x1c) +REG32(AON_WDT_WDOGCMP0, 0x20) + +static void sifive_e_aon_wdt_update_wdogcount(SiFiveEAONState *r) +{ + int64_t now; + if (0 == FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + EN_ALWAYS) && + 0 == FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)) { + return; + } + + now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + r->wdogcount += muldiv64(now - r->wdog_restart_time, + r->wdogclk_freq, NANOSECONDS_PER_SECOND); + /* Clean the most significant bit. */ + r->wdogcount = ((r->wdogcount << 1) >> 1); + r->wdog_restart_time = now; +} + +static void sifive_e_aon_wdt_update_state(SiFiveEAONState *r) +{ + uint16_t wdogs; + bool cmp_signal = false; + sifive_e_aon_wdt_update_wdogcount(r); + wdogs = (uint16_t)(r->wdogcount >> + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + SCALE)); + if (wdogs >= r->wdogcmp0) { + cmp_signal = true; + if (1 == FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + ZEROCMP)) { + r->wdogcount = 0; + wdogs = 0; + } + } + + if (cmp_signal) { + if (1 == FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + RSTEN)) { + watchdog_perform_action(); + } + r->wdogcfg = + FIELD_DP32(r->wdogcfg, + AON_WDT_WDOGCFG, + IP0, 1); + } + + qemu_set_irq(r->wdog_irq, + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + IP0)); + + if (wdogs < r->wdogcmp0 && + (FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + EN_ALWAYS) || + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE))) { + int64_t next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + next += muldiv64((r->wdogcmp0 - wdogs) << + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + SCALE), + NANOSECONDS_PER_SECOND, r->wdogclk_freq); + timer_mod(r->wdog_timer, next); + } else { + timer_mod(r->wdog_timer, INT64_MAX); + } +} + +/* + * Callback used when the timer set using timer_mod expires. + */ +static void sifive_e_aon_wdt_expired_cb(void *opaque) +{ + SiFiveEAONState *r = SIFIVE_E_AON(opaque); + sifive_e_aon_wdt_update_state(r); +} + +static uint64_t +sifive_e_aon_wdt_read(void *opaque, hwaddr addr, unsigned int size) +{ + SiFiveEAONState *r = SIFIVE_E_AON(opaque); + + switch (addr) { + case A_AON_WDT_WDOGCFG: + return r->wdogcfg; + case A_AON_WDT_WDOGCOUNT: + sifive_e_aon_wdt_update_wdogcount(r); + return r->wdogcount; + case A_AON_WDT_WDOGS: + sifive_e_aon_wdt_update_wdogcount(r); + return r->wdogcount >> + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + SCALE); + case A_AON_WDT_WDOGFEED: + return 0; + case A_AON_WDT_WDOGKEY: + return r->wdogunlock; + case A_AON_WDT_WDOGCMP0: + return r->wdogcmp0; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read: addr=0x%x\n", + __func__, (int)addr); + } + + return 0; +} + +static void +sifive_e_aon_wdt_write(void *opaque, hwaddr addr, + uint64_t val64, unsigned int size) +{ + SiFiveEAONState *r = SIFIVE_E_AON(opaque); + uint32_t value = val64; + + switch (addr) { + case A_AON_WDT_WDOGCFG: { + uint8_t new_en_always; + uint8_t new_en_core_awake; + uint8_t old_en_always; + uint8_t old_en_core_awake; + if (0 == r->wdogunlock) { + return; + } + + new_en_always = + FIELD_EX32(value, + AON_WDT_WDOGCFG, + EN_ALWAYS); + new_en_core_awake = + FIELD_EX32(value, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE); + old_en_always = + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + EN_ALWAYS); + old_en_core_awake = + FIELD_EX32(r->wdogcfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE); + if (1 == (old_en_always || + old_en_core_awake) && + 0 == (new_en_always || + new_en_core_awake)) { + sifive_e_aon_wdt_update_wdogcount(r); + } else if (0 == (old_en_always || + old_en_core_awake) && + 1 == (new_en_always || + new_en_core_awake)) { + r->wdog_restart_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + } + r->wdogcfg = value; + r->wdogunlock = 0; + break; + } + case A_AON_WDT_WDOGCOUNT: + if (0 == r->wdogunlock) { + return; + } + r->wdogcount = ((value << 1) >> 1); + r->wdog_restart_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + r->wdogunlock = 0; + break; + case A_AON_WDT_WDOGS: + return; + case A_AON_WDT_WDOGFEED: + if (0 == r->wdogunlock) { + return; + } + if (SIFIVE_E_AON_WDOGFEED == value) { + r->wdogcount = 0; + r->wdog_restart_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + } + r->wdogunlock = 0; + break; + case A_AON_WDT_WDOGKEY: + if (SIFIVE_E_AON_WDOGKEY == value) { + r->wdogunlock = 1; + } + break; + case A_AON_WDT_WDOGCMP0: + if (0 == r->wdogunlock) { + return; + } + r->wdogcmp0 = (uint16_t) value; + r->wdogunlock = 0; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=0x%x v=0x%x\n", + __func__, (int)addr, (int)value); + } + sifive_e_aon_wdt_update_state(r); +} + +static uint64_t +sifive_e_aon_read(void *opaque, hwaddr addr, unsigned int size) +{ + if (addr < SIFIVE_E_AON_RTC) { + return sifive_e_aon_wdt_read(opaque, addr, size); + } else if (addr < SIFIVE_E_AON_MAX) { + qemu_log_mask(LOG_UNIMP, "%s: Unimplemented read: addr=0x%x\n", + __func__, (int)addr); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read: addr=0x%x\n", + __func__, (int)addr); + } + return 0; +} + +static void +sifive_e_aon_write(void *opaque, hwaddr addr, + uint64_t val64, unsigned int size) +{ + if (addr < SIFIVE_E_AON_RTC) { + sifive_e_aon_wdt_write(opaque, addr, val64, size); + } else if (addr < SIFIVE_E_AON_MAX) { + qemu_log_mask(LOG_UNIMP, "%s: Unimplemented write: addr=0x%x\n", + __func__, (int)addr); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=0x%x\n", + __func__, (int)addr); + } +} + +static const MemoryRegionOps sifive_e_aon_ops = { + .read = sifive_e_aon_read, + .write = sifive_e_aon_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl = { + .min_access_size = 4, + .max_access_size = 4 + }, + .valid = { + .min_access_size = 4, + .max_access_size = 4 + } +}; + +SiFiveEAONState *sifive_e_aon_create(MemoryRegion *mr, hwaddr base, + qemu_irq wdt_irq) +{ + DeviceState *dev; + SysBusDevice *s; + SiFiveEAONState *r; + + dev = qdev_new(TYPE_SIFIVE_E_AON); + s = SYS_BUS_DEVICE(dev); + + sysbus_realize_and_unref(s, &error_fatal); + + memory_region_add_subregion(mr, base, + sysbus_mmio_get_region(s, 0)); + sysbus_connect_irq(s, 0, wdt_irq); + + r = SIFIVE_E_AON(dev); + return r; +} + +static void sifive_e_aon_reset(DeviceState *dev) +{ + SiFiveEAONState *s = SIFIVE_E_AON(dev); + + s->wdogcfg = + FIELD_DP32(s->wdogcfg, + AON_WDT_WDOGCFG, + RSTEN, 0); + s->wdogcfg = + FIELD_DP32(s->wdogcfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 0); + s->wdogcfg = + FIELD_DP32(s->wdogcfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE, 0); + s->wdogcmp0 = 0xbeef; +} + +static void sifive_e_aon_wdt_update_wdogclk_freq(Object *obj, Visitor *v, + const char *name, + void *opaque, + Error **errp) { + SiFiveEAONState *r = SIFIVE_E_AON(opaque); + uint64_t value; + if (!visit_type_uint64(v, name, &value, errp)) { + return; + } + + r->wdogclk_freq = value; + sifive_e_aon_wdt_update_state(r); +} + +static void sifive_e_aon_init(Object *obj) +{ + SysBusDevice *sbd = SYS_BUS_DEVICE(obj); + SiFiveEAONState *s = SIFIVE_E_AON(obj); + + memory_region_init_io(&s->mmio, OBJECT(s), &sifive_e_aon_ops, s, + TYPE_SIFIVE_E_AON, SIFIVE_E_AON_MAX); + sysbus_init_mmio(sbd, &s->mmio); + + /* watchdog timer */ + s->wdogclk_freq = SIFIVE_E_LFCLK_DEFAULT_FREQ; + sysbus_init_irq(sbd, &s->wdog_irq); + object_property_add(obj, "wdogclk-frequency", "uint64", + NULL, + sifive_e_aon_wdt_update_wdogclk_freq, + NULL, s); +} + +static void sifive_e_aon_realize(DeviceState *dev, Error **errp) +{ + SiFiveEAONState *s = SIFIVE_E_AON(dev); + + /* watchdog timer */ + s->wdog_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, + sifive_e_aon_wdt_expired_cb, s); +} + +static void sifive_e_aon_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(oc); + + dc->realize = sifive_e_aon_realize; + dc->reset = sifive_e_aon_reset; +} + +static const TypeInfo sifive_e_aon_info = { + .name = TYPE_SIFIVE_E_AON, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(SiFiveEAONState), + .instance_init = sifive_e_aon_init, + .class_init = sifive_e_aon_class_init, +}; + +static void sifive_e_aon_register_types(void) +{ + type_register_static(&sifive_e_aon_info); +} + +type_init(sifive_e_aon_register_types) diff --git a/include/hw/misc/sifive_e_aon.h b/include/hw/misc/sifive_e_aon.h new file mode 100644 index 0000000000..1759f3f785 --- /dev/null +++ b/include/hw/misc/sifive_e_aon.h @@ -0,0 +1,62 @@ +/* + * SiFive HiFive1 AON (Always On Domain) interface. + * + * Copyright (c) 2022 SiFive, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef HW_SIFIVE_AON_H +#define HW_SIFIVE_AON_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define TYPE_SIFIVE_E_AON "riscv.sifive.e.aon" +OBJECT_DECLARE_SIMPLE_TYPE(SiFiveEAONState, SIFIVE_E_AON) + +#define SIFIVE_E_AON_WDOGKEY (0x51F15E) +#define SIFIVE_E_AON_WDOGFEED (0xD09F00D) +#define SIFIVE_E_LFCLK_DEFAULT_FREQ (32768) + +enum { + SIFIVE_E_AON_WDT = 0x0, + SIFIVE_E_AON_RTC = 0x40, + SIFIVE_E_AON_LFROSC = 0x70, + SIFIVE_E_AON_BACKUP = 0x80, + SIFIVE_E_AON_PMU = 0x100, + SIFIVE_E_AON_MAX = 0x150 +}; + +struct SiFiveEAONState { + /*< private >*/ + SysBusDevice parent_obj; + + /*< public >*/ + MemoryRegion mmio; + + /*< watchdog timer >*/ + QEMUTimer *wdog_timer; + qemu_irq wdog_irq; + uint64_t wdog_restart_time; + uint64_t wdogclk_freq; + + uint32_t wdogcfg; + uint16_t wdogcmp0; + uint32_t wdogcount; + uint8_t wdogunlock; +}; + +SiFiveEAONState *sifive_e_aon_create(MemoryRegion *mr, hwaddr base, + qemu_irq irq); +#endif From patchwork Tue Nov 1 02:46:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1697611 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=E9FiDLiJ; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N1ZH404JDz23lV for ; Tue, 1 Nov 2022 13:49:20 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ophJO-0002Gh-1U; Mon, 31 Oct 2022 22:47:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ophJD-0002F1-MP for qemu-devel@nongnu.org; Mon, 31 Oct 2022 22:47:29 -0400 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ophJB-0004Dq-Hk for qemu-devel@nongnu.org; Mon, 31 Oct 2022 22:47:22 -0400 Received: by mail-oi1-x232.google.com with SMTP id c129so2892446oia.0 for ; Mon, 31 Oct 2022 19:47:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zYqMYuafMjaUotlufhtCIW3HJ2L3kxSptx+Y2MFaREk=; b=E9FiDLiJncelb152++R4wrfXwBz3W6U/vtLY/YyckY+2NKNkmcksxl9Y2I8CSVvYnP 3wpzqwrqOcGDqhVriKzmAcP4XLDabrZlhLVWWcFus/co5xVUucNEVelI8FoAUrniNvLb VdTbWm9oKjDWYoBRSrUx9AyzFXpagJj6CdgngWVBQxgbsSfffiZ0ndkZp5jr3FhhkBHT rwk6+dg7aI9t7t92gHutLlJjHkCjAfWHSAxju4L98JgQPGdpY0M1dSP6hwWoICeOlsVz IShAYDUr2EVe/CBGyQjvasbrZpqYiawFTqlwQRpwqpYqYITzenV8+yoCMIyf9MarWnac v8LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zYqMYuafMjaUotlufhtCIW3HJ2L3kxSptx+Y2MFaREk=; b=bVCVHsmflr4xXtbqrMO8hjrT5oTP1YNjWOKi89kQGpK14Azp0/sxT6MiffQdcADR56 /wAu/4sFt9tgggN+Le3JZR1yneMP6gpJzL7p3QDoWAri3AxsT+5HGP033CzP8BMOwLLc va94vRRd/YMMniptqfIeiq6XVUubwc+XN5bljX2jwuRRBaZaUELcTp7SyZpWrenqlDLC wSMJx3QbXyD0Is/HS4DwRGYoBAo0Yamn+WP/l05M9JCgs13LLRjLivAnyWa4OZn3KGsS ly3fQCqAcnEpbrxR4Q8aDmYdfO9EhfnmOglCVXm2j4u9aaL+E3yGotphyUsmPHUl0+3X asaA== X-Gm-Message-State: ACrzQf3CzeC7CZCSx4NQ6qSvawGLdOoKFlAbUHnk6/FlRXHvGHTGTBcb jUgTqsLmxoLw4ycwCMrgayv5khGxt0A2d9szlDfpeLtbZwU5rj2FnDDe6W4lxvBJreD6qVOTyFB SwFehr0G4wTZVTNDDSQc/v5yCy165e+TXGUsdNsngmqnqbcOtKEC5SJjNUN9Lh44YrOyD8T8= X-Google-Smtp-Source: AMsMyM6olggA1LHUblSC22kcEph67zxtn4o0DY8J8ORVhh2LC+JZJgk0+hIS/yjYgxNv9TUp7tr1Kg== X-Received: by 2002:a05:6808:656:b0:35a:167a:48e3 with SMTP id z22-20020a056808065600b0035a167a48e3mr2901307oih.237.1667270838632; Mon, 31 Oct 2022 19:47:18 -0700 (PDT) Received: from sw05.internal.sifive.com ([64.62.193.194]) by smtp.gmail.com with ESMTPSA id r4-20020a05683001c400b00661a33883b8sm3367215ota.71.2022.10.31.19.47.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 19:47:18 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: alistair.francis@wdc.com, palmer@dabbelt.com, bin.meng@windriver.com, jim.shu@sifive.com, frank.chang@sifive.com, Tommy Wu Subject: [PATCH v2 2/3] hw/riscv: sifive_e: Support the watchdog timer of HiFive 1 rev b. Date: Mon, 31 Oct 2022 19:46:55 -0700 Message-Id: <20221101024656.517608-3-tommy.wu@sifive.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221101024656.517608-1-tommy.wu@sifive.com> References: <20221101024656.517608-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::232; envelope-from=tommy.wu@sifive.com; helo=mail-oi1-x232.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org+incoming=patchwork.ozlabs.org@nongnu.org Create the AON device when we realize the sifive_e machine. This patch only implemented the functionality of the watchdog timer, not all the functionality of the AON device. Signed-off-by: Tommy Wu --- hw/riscv/Kconfig | 1 + hw/riscv/sifive_e.c | 13 +++++++++++-- include/hw/riscv/sifive_e.h | 8 +++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 79ff61c464..50890b1b75 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -59,6 +59,7 @@ config SIFIVE_E select SIFIVE_PLIC select SIFIVE_UART select SIFIVE_E_PRCI + select SIFIVE_E_AON select UNIMP config SIFIVE_U diff --git a/hw/riscv/sifive_e.c b/hw/riscv/sifive_e.c index d65d2fd869..c866ffe232 100644 --- a/hw/riscv/sifive_e.c +++ b/hw/riscv/sifive_e.c @@ -45,6 +45,7 @@ #include "hw/intc/riscv_aclint.h" #include "hw/intc/sifive_plic.h" #include "hw/misc/sifive_e_prci.h" +#include "hw/misc/sifive_e_aon.h" #include "chardev/char.h" #include "sysemu/sysemu.h" @@ -222,8 +223,13 @@ static void sifive_e_soc_realize(DeviceState *dev, Error **errp) RISCV_ACLINT_DEFAULT_MTIMER_SIZE, 0, ms->smp.cpus, RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME, RISCV_ACLINT_DEFAULT_TIMEBASE_FREQ, false); - create_unimplemented_device("riscv.sifive.e.aon", - memmap[SIFIVE_E_DEV_AON].base, memmap[SIFIVE_E_DEV_AON].size); + + s->aon = qdev_new(TYPE_SIFIVE_E_AON); + if (!sysbus_realize(SYS_BUS_DEVICE(s->aon), errp)) { + return; + } + sysbus_mmio_map(SYS_BUS_DEVICE(s->aon), 0, memmap[SIFIVE_E_DEV_AON].base); + sifive_e_prci_create(memmap[SIFIVE_E_DEV_PRCI].base); /* GPIO */ @@ -244,6 +250,9 @@ static void sifive_e_soc_realize(DeviceState *dev, Error **errp) qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_GPIO0_IRQ0 + i)); } + sysbus_connect_irq(SYS_BUS_DEVICE(s->aon), 0, + qdev_get_gpio_in(DEVICE(s->plic), + SIFIVE_E_AON_WDT_IRQ)); sifive_uart_create(sys_mem, memmap[SIFIVE_E_DEV_UART0].base, serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART0_IRQ)); diff --git a/include/hw/riscv/sifive_e.h b/include/hw/riscv/sifive_e.h index d738745925..e2de1564a7 100644 --- a/include/hw/riscv/sifive_e.h +++ b/include/hw/riscv/sifive_e.h @@ -35,6 +35,7 @@ typedef struct SiFiveESoCState { /*< public >*/ RISCVHartArrayState cpus; DeviceState *plic; + DeviceState *aon; SIFIVEGPIOState gpio; MemoryRegion xip_mem; MemoryRegion mask_rom; @@ -76,9 +77,10 @@ enum { }; enum { - SIFIVE_E_UART0_IRQ = 3, - SIFIVE_E_UART1_IRQ = 4, - SIFIVE_E_GPIO0_IRQ0 = 8 + SIFIVE_E_AON_WDT_IRQ = 1, + SIFIVE_E_UART0_IRQ = 3, + SIFIVE_E_UART1_IRQ = 4, + SIFIVE_E_GPIO0_IRQ0 = 8 }; #define SIFIVE_E_PLIC_HART_CONFIG "M" From patchwork Tue Nov 1 02:46:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tommy Wu X-Patchwork-Id: 1697609 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=O40H1fMl; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N1ZGw2cnSz23lV for ; Tue, 1 Nov 2022 13:49:12 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ophJR-0002Hu-GY; Mon, 31 Oct 2022 22:47:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ophJH-0002F8-98 for qemu-devel@nongnu.org; Mon, 31 Oct 2022 22:47:29 -0400 Received: from mail-oi1-x22f.google.com ([2607:f8b0:4864:20::22f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ophJD-0004E5-F4 for qemu-devel@nongnu.org; Mon, 31 Oct 2022 22:47:25 -0400 Received: by mail-oi1-x22f.google.com with SMTP id p127so14667926oih.9 for ; Mon, 31 Oct 2022 19:47:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2HNXbqpEcUn7DYG1idgGyglL0XhGdSXTe8CR2vhs1To=; b=O40H1fMlzbbCjMhAq9rozekSMH9yDk0TAgp4y836Qd/76p1q3ABVQehKXk8DMdfuzl WJuosZUHsqZyKm3pcqZbHXmgO9rNKPQxGJ4CDX/MywofY6jYCLsLjjHLK8/MOsKE+LVl TYZgQ5xJtbplZM5i1wq4EqA/+bXLEPzRk3VqM1K0QAt5jNnUP5lxITwfq/SkS/mGAYPu 3t0qlM2auq5+Eo2ynXOpPfvOYqhrqQOvoLMW2q4NvHOJ7wJtIXPsPpZrBCJJvh9DTBPE oJZ7Zgae0889YDHfbJ/JZjyQizHp06oJAnLEYtY6eCT4RNBhzGbzSylK2kSAkLZtjt1M Ekbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2HNXbqpEcUn7DYG1idgGyglL0XhGdSXTe8CR2vhs1To=; b=zvcQrNq+xGHhGY5GExiTib3PcAV51LE+Mrd3T/JkyKEbFzrRwRyyPJ3W7YUIBWmj1a /ol+Yu2B2afpsQRsBO3UKB7T3CDrAhxnaJ+/7MsBR+dm3iZe6Lb8cL+CbWItx4cyEIYZ z1owv1tqS+LzE6Fr6BxegWLvOH2uDhtNaxWm4qAnQFcMp4U6NUNZ4D6XrLtnx5+VpecM 4toC1mhJ9lTPqMxBChMFlxtbv7HANnlO5ZRO3yaa1q9F5W//3sGRSayaI5wBxYvxoE7R bCRTRX+8VOJTMCWMi0nNIpSn8LFzxEuTjwyH3vjngvgxuO4BmNCSypxWkO/UxJkr4hNW A4ww== X-Gm-Message-State: ACrzQf2gJCtiWpfiTyJzGMm5J0wdESqaadt9qZhfsAOdh8WsKSLpJpTg 052OKpvgnMTd9uG4ZKwF2dWX8E+MUmh+UPC6WQkVEqrTBq2U1J0w0KrI6ouNV2lTDypTsyrSmcF HyTtSKYOPIHMW0rgHrl2reUW1A9gQsk/gZs3w6cUQIvRz9XjuAK50j2poEnNmCqjAZdL1LTQ= X-Google-Smtp-Source: AMsMyM4D8J+0i/v0mlI0eI25EF0y75yCm8vQxByxDAXKeKM+B0EVz2GdQr5I4QlelhzsgPN/gOib8A== X-Received: by 2002:a05:6808:1595:b0:359:f805:cf84 with SMTP id t21-20020a056808159500b00359f805cf84mr6196834oiw.152.1667270840737; Mon, 31 Oct 2022 19:47:20 -0700 (PDT) Received: from sw05.internal.sifive.com ([64.62.193.194]) by smtp.gmail.com with ESMTPSA id r4-20020a05683001c400b00661a33883b8sm3367215ota.71.2022.10.31.19.47.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 19:47:20 -0700 (PDT) From: Tommy Wu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: alistair.francis@wdc.com, palmer@dabbelt.com, bin.meng@windriver.com, jim.shu@sifive.com, frank.chang@sifive.com, Tommy Wu Subject: [PATCH v2 3/3] tests/qtest: sifive-e-aon-watchdog-test.c : Add QTest of watchdog of sifive_e Date: Mon, 31 Oct 2022 19:46:56 -0700 Message-Id: <20221101024656.517608-4-tommy.wu@sifive.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20221101024656.517608-1-tommy.wu@sifive.com> References: <20221101024656.517608-1-tommy.wu@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::22f; envelope-from=tommy.wu@sifive.com; helo=mail-oi1-x22f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UPPERCASE_50_75=0.008 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org+incoming=patchwork.ozlabs.org@nongnu.org Add some simple tests of the watchdog timer in the always-on domain device of HiFive 1 rev b. Signed-off-by: Tommy Wu --- tests/qtest/meson.build | 3 + tests/qtest/sifive-e-aon-watchdog-test.c | 650 +++++++++++++++++++++++ 2 files changed, 653 insertions(+) create mode 100644 tests/qtest/sifive-e-aon-watchdog-test.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index c07a5b1a5f..4a38bcb829 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -230,6 +230,9 @@ qtests_s390x = \ 'cpu-plug-test', 'migration-test'] +qtests_riscv32 = \ + (config_all_devices.has_key('CONFIG_SIFIVE_E_AON') ? ['sifive-e-aon-watchdog-test'] : []) + qos_test_ss = ss.source_set() qos_test_ss.add( 'ac97-test.c', diff --git a/tests/qtest/sifive-e-aon-watchdog-test.c b/tests/qtest/sifive-e-aon-watchdog-test.c new file mode 100644 index 0000000000..1f454c266a --- /dev/null +++ b/tests/qtest/sifive-e-aon-watchdog-test.c @@ -0,0 +1,650 @@ +#include "qemu/osdep.h" +#include "qemu/timer.h" +#include "qemu/bitops.h" +#include "libqtest-single.h" +#include "hw/registerfields.h" +#include "hw/misc/sifive_e_aon.h" + +FIELD(AON_WDT_WDOGCFG, + SCALE, 0, 4) +FIELD(AON_WDT_WDOGCFG, + RSVD0, 4, 4) +FIELD(AON_WDT_WDOGCFG, + RSTEN, 8, 1) +FIELD(AON_WDT_WDOGCFG, + ZEROCMP, 9, 1) +FIELD(AON_WDT_WDOGCFG, + RSVD1, 10, 2) +FIELD(AON_WDT_WDOGCFG, + EN_ALWAYS, 12, 1) +FIELD(AON_WDT_WDOGCFG, + EN_CORE_AWAKE, 13, 1) +FIELD(AON_WDT_WDOGCFG, + RSVD2, 14, 14) +FIELD(AON_WDT_WDOGCFG, + IP0, 28, 1) +FIELD(AON_WDT_WDOGCFG, + RSVD3, 29, 3) + +#define WDOG_BASE (0x10000000) +#define WDOGCFG (0x0) +#define WDOGCOUNT (0x8) +#define WDOGS (0x10) +#define WDOGFEED (0x18) +#define WDOGKEY (0x1c) +#define WDOGCMP0 (0x20) + +#define SIFIVE_E_AON_WDOGKEY (0x51F15E) +#define SIFIVE_E_AON_WDOGFEED (0xD09F00D) +#define SIFIVE_E_LFCLK_DEFAULT_FREQ (32768) + +static void test_init(void) +{ + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, 0); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, 0); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCMP0, 0xBEEF); +} + +static void test_wdogcount(void) +{ + uint64_t tmp; + + test_init(); + + tmp = readl(WDOG_BASE + WDOGCOUNT); + writel(WDOG_BASE + WDOGCOUNT, 0xBEEF); + g_assert(readl(WDOG_BASE + WDOGCOUNT) == tmp); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, 0xBEEF); + g_assert(0xBEEF == readl(WDOG_BASE + WDOGCOUNT)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, 0xAAAAAAAA); + g_assert(0x2AAAAAAA == readl(WDOG_BASE + WDOGCOUNT)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGFEED, 0xAAAAAAAA); + g_assert(0x2AAAAAAA == readl(WDOG_BASE + WDOGCOUNT)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGFEED, SIFIVE_E_AON_WDOGFEED); + g_assert(0 == readl(WDOG_BASE + WDOGCOUNT)); +} + +static void test_wdogcfg(void) +{ + uint32_t tmp_cfg; + + test_init(); + + tmp_cfg = readl(WDOG_BASE + WDOGCFG); + writel(WDOG_BASE + WDOGCFG, 0xFFFFFFFF); + g_assert(readl(WDOG_BASE + WDOGCFG) == tmp_cfg); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, 0xFFFFFFFF); + g_assert(0xFFFFFFFF == readl(WDOG_BASE + WDOGCFG)); + + tmp_cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(15 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(1 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(1 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(1 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(1 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + IP0)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, 0); + tmp_cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(0 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(0 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(0 == + FIELD_EX32(tmp_cfg, + AON_WDT_WDOGCFG, + IP0)); + g_assert(0 == readl(WDOG_BASE + WDOGCFG)); +} + +static void test_wdogcmp0(void) +{ + uint32_t tmp; + + test_init(); + + tmp = readl(WDOG_BASE + WDOGCMP0); + writel(WDOG_BASE + WDOGCMP0, 0xBEEF); + g_assert(readl(WDOG_BASE + WDOGCMP0) == tmp); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCMP0, 0xBEEF); + g_assert(0xBEEF == readl(WDOG_BASE + WDOGCMP0)); +} + +static void test_wdogkey(void) +{ + test_init(); + + g_assert(0 == readl(WDOG_BASE + WDOGKEY)); + + writel(WDOG_BASE + WDOGKEY, 0xFFFF); + g_assert(0 == readl(WDOG_BASE + WDOGKEY)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + g_assert(1 == readl(WDOG_BASE + WDOGKEY)); + + writel(WDOG_BASE + WDOGFEED, 0xAAAAAAAA); + g_assert(0 == readl(WDOG_BASE + WDOGKEY)); +} + +static void test_wdogfeed(void) +{ + test_init(); + + g_assert(0 == readl(WDOG_BASE + WDOGFEED)); + + writel(WDOG_BASE + WDOGFEED, 0xFFFF); + g_assert(0 == readl(WDOG_BASE + WDOGFEED)); +} + +static void test_scaled_wdogs(void) +{ + uint32_t cfg; + uint32_t fake_count = 0x12345678; + + test_init(); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, fake_count); + g_assert(readl(WDOG_BASE + WDOGCOUNT) == fake_count); + g_assert((uint16_t)readl(WDOG_BASE + WDOGS) == + (uint16_t)fake_count); + + for (int i = 0; i < 16; i++) { + cfg = readl(WDOG_BASE + WDOGCFG); + cfg = FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + SCALE, i); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + g_assert((uint16_t)readl(WDOG_BASE + WDOGS) == + (uint16_t)(fake_count >> + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE))); + } +} + +static void test_watchdog(void) +{ + uint32_t cfg; + + test_init(); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCMP0, SIFIVE_E_LFCLK_DEFAULT_FREQ); + + cfg = readl(WDOG_BASE + WDOGCFG); + cfg = FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + SCALE, 0); + cfg = FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 1); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + + clock_step(NANOSECONDS_PER_SECOND); + + g_assert(readl(WDOG_BASE + WDOGCOUNT) == + SIFIVE_E_LFCLK_DEFAULT_FREQ); + + g_assert(readl(WDOG_BASE + WDOGS) == + SIFIVE_E_LFCLK_DEFAULT_FREQ); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, 0); + cfg = FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + IP0, 0); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); +} + +static void test_scaled_watchdog(void) +{ + uint32_t cfg; + + test_init(); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCMP0, 10); + + cfg = readl(WDOG_BASE + WDOGCFG); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + SCALE, 15); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 1); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + + clock_step(NANOSECONDS_PER_SECOND * 10); + + g_assert(readl(WDOG_BASE + WDOGCOUNT) == + SIFIVE_E_LFCLK_DEFAULT_FREQ * 10); + + g_assert(10 == readl(WDOG_BASE + WDOGS)); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(15 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, 0); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + IP0, 0); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); +} + +static void test_periodic_int(void) +{ + uint32_t cfg; + + test_init(); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCMP0, SIFIVE_E_LFCLK_DEFAULT_FREQ); + + cfg = readl(WDOG_BASE + WDOGCFG); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + SCALE, 0); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP, 1); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 1); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + + clock_step(NANOSECONDS_PER_SECOND); + + g_assert(0 == readl(WDOG_BASE + WDOGCOUNT)); + + g_assert(0 == readl(WDOG_BASE + WDOGS)); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + cfg = FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + IP0, 0); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + clock_step(NANOSECONDS_PER_SECOND); + + g_assert(0 == readl(WDOG_BASE + WDOGCOUNT)); + + g_assert(0 == readl(WDOG_BASE + WDOGS)); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + IP0, 0); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); +} + +static void test_enable_disable(void) +{ + uint32_t cfg; + + test_init(); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCMP0, 10); + + cfg = readl(WDOG_BASE + WDOGCFG); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + SCALE, 15); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 1); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + + clock_step(NANOSECONDS_PER_SECOND * 2); + + g_assert(readl(WDOG_BASE + WDOGCOUNT) == + SIFIVE_E_LFCLK_DEFAULT_FREQ * 2); + + g_assert(2 == readl(WDOG_BASE + WDOGS)); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(15 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 0); + writel(WDOG_BASE + WDOGCFG, cfg); + + clock_step(NANOSECONDS_PER_SECOND * 8); + + g_assert(readl(WDOG_BASE + WDOGCOUNT) == + SIFIVE_E_LFCLK_DEFAULT_FREQ * 2); + + g_assert(2 == readl(WDOG_BASE + WDOGS)); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(15 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS, 1); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + + clock_step(NANOSECONDS_PER_SECOND * 8); + + g_assert(readl(WDOG_BASE + WDOGCOUNT) == + SIFIVE_E_LFCLK_DEFAULT_FREQ * 10); + + g_assert(10 == readl(WDOG_BASE + WDOGS)); + + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(15 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + SCALE)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + RSTEN)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + ZEROCMP)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_ALWAYS)); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + EN_CORE_AWAKE)); + g_assert(1 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); + + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCOUNT, 0); + cfg = + FIELD_DP32(cfg, + AON_WDT_WDOGCFG, + IP0, 0); + writel(WDOG_BASE + WDOGKEY, SIFIVE_E_AON_WDOGKEY); + writel(WDOG_BASE + WDOGCFG, cfg); + cfg = readl(WDOG_BASE + WDOGCFG); + g_assert(0 == + FIELD_EX32(cfg, + AON_WDT_WDOGCFG, + IP0)); +} + +int main(int argc, char *argv[]) +{ + int r; + + g_test_init(&argc, &argv, NULL); + + qtest_start("-machine sifive_e"); + + qtest_add_func("/sifive-e-aon-watchdog-test/wdogcount", + test_wdogcount); + qtest_add_func("/sifive-e-aon-watchdog-test/wdogcfg", + test_wdogcfg); + qtest_add_func("/sifive-e-aon-watchdog-test/wdogcmp0", + test_wdogcmp0); + qtest_add_func("/sifive-e-aon-watchdog-test/wdogkey", + test_wdogkey); + qtest_add_func("/sifive-e-aon-watchdog-test/wdogfeed", + test_wdogfeed); + qtest_add_func("/sifive-e-aon-watchdog-test/scaled_wdogs", + test_scaled_wdogs); + qtest_add_func("/sifive-e-aon-watchdog-test/watchdog", + test_watchdog); + qtest_add_func("/sifive-e-aon-watchdog-test/scaled_watchdog", + test_scaled_watchdog); + qtest_add_func("/sifive-e-aon-watchdog-test/periodic_int", + test_periodic_int); + qtest_add_func("/sifive-e-aon-watchdog-test/enable_disable", + test_enable_disable); + + r = g_test_run(); + qtest_end(); + + return r; +}