From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607651729; cv=none; d=zohomail.com; s=zohoarc; b=QK6334E6AnVTWzjdrklTlP7RDEUJsImcdokmHx6reFWqChekACKB+B1PA96VPh3wcMyuO2IRKbrB2Z7J8AYn+ukYxPTRC+K05iqvlWd5FEGCm4qao4Z2PPzoSQjmuunGXkLaEuYLKPxVXqzAHMzgFviYYZGGx2HLlHu+rg1c88Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607651729; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=dkE51dgsh/r/QJJlsHppgz0JYeyUgmuCUafOW8ww0OU=; b=geJEPa6s8GBjjuiUVuRTqidcl2dixJ5RnZh+BdJ2XaXgIO9EX25vxjkNye/RlWafRsVHssk9qW44N7VLSwJ5TZ5nEXdZMBr6UlCW75iQoBheAIPXScOsF+t224f4Li1hTwDFfNSg2zOjdeJQ3bZKzmtsBUZBme2l3fooKjurcfQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607651729549180.08019091999643; Thu, 10 Dec 2020 17:55:29 -0800 (PST) Received: from localhost ([::1]:33978 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXea-0003Tb-3V for importer@patchew.org; Thu, 10 Dec 2020 20:55:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58158) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3ztDSXwgKCr81zmftyxmlttlqj.htrvjrz-ij0jqstslsz.twl@flex--wuhaotsh.bounces.google.com>) id 1knXbZ-0001U8-0A for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:21 -0500 Received: from mail-qt1-x849.google.com ([2607:f8b0:4864:20::849]:46976) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3ztDSXwgKCr81zmftyxmlttlqj.htrvjrz-ij0jqstslsz.twl@flex--wuhaotsh.bounces.google.com>) id 1knXbU-00078I-Pm for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:19 -0500 Received: by mail-qt1-x849.google.com with SMTP id j1so5373902qtd.13 for ; Thu, 10 Dec 2020 17:52:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=dkE51dgsh/r/QJJlsHppgz0JYeyUgmuCUafOW8ww0OU=; b=YJsKu3+cAeGCXL2XqKvloqiqmZv+SAyL1ARqnMAnlrjCO4AzdDsZMiFfhB1au0Nbcg 9XVgCaTZdsjJErgnwbvRo+QZkvx3Oebx4x6MnYgvZAu/9oqKOpsKkPJLhXdBlOIKIwB7 xH5RCGU1Cf725Sxxb+9mRFBX+rvJqbKOXpcBGjzOBZmFYym/yunfsyID0vpk0mE9BRAt 5OMbCKr+SdA/5oQeTGBRq93Tjwt6tmOPDmF47pBB3D5dnAjygyMPDhBtffGrz/4siAKn w6LBh/09iEbjnzxWuXRM60blELQ24BKfbMFfXA4K7lBTsKYHPoNcB1LrdlsjZ6ASSFYH jP1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dkE51dgsh/r/QJJlsHppgz0JYeyUgmuCUafOW8ww0OU=; b=V60ofXjpFstdrf9mErFimSY563UGdNy7E325nNH/DV/gOJwEi+RfNeq99afPjfrIuR z5MLEdKGj8aIHa3lHKlwvRikqaWtuy0is2RG4nD4gVmWV7SYiggHRqQr6iEJTfWVaRx8 rQ1HMIofy0N+K716bFLgbR0vi6OsKjlfh5qG33+SmJapasFL632gOPJuBT3zclZsgQ7n 57nE2GSRpb3Sn1Y+grbxJOHzZy9ugixZMLi+68ViG5qgqTMwDyTdXdPLTPEIfb71yqzc OtvFxiWWd9Rty1LeJ5T8OGwupF9YVgpkyZ03D7j6vdpjV+4aKxrNd0Kmr6cVEEC221QQ U7rQ== X-Gm-Message-State: AOAM530209BE64AwuY/wZiUItMMmHejVLJBYjVapgbvJtcIKDL7aj0P5 n+goffrxqdy/dNKlHDhOAqsAqsuL0nOP1w== X-Google-Smtp-Source: ABdhPJyS530EZWJBH6+WhCqzxcL2Nc6dnRFFV5pvbwe8Kb5Nr8QeeyZMo/MiTaMXZ7kvvsEoNN9yJTJwbKfZyA== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:ad4:4c8c:: with SMTP id bs12mr13165875qvb.11.1607651534682; Thu, 10 Dec 2020 17:52:14 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:50 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-2-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 1/7] hw/misc: Add clock converter in NPCM7XX CLK module To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::849; envelope-from=3ztDSXwgKCr81zmftyxmlttlqj.htrvjrz-ij0jqstslsz.twl@flex--wuhaotsh.bounces.google.com; helo=mail-qt1-x849.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch allows NPCM7XX CLK module to compute clocks that are used by other NPCM7XX modules. Add a new struct NPCM7xxClockConverterState which represents a single converter. Each clock converter in CLK module represents one converter in NPCM7XX CLK Module(PLL, SEL or Divider). Each converter takes one or more input clocks and converts them into one output clock. They form a clock hierarchy in the CLK module and are responsible for outputing clocks for various other modules in an NPCM7XX SoC. Each converter has a function pointer called "convert" which represents the unique logic for that converter. The clock contains two initialization information: ConverterInitInfo and ConverterConnectionInfo. They represent the vertices and edges in the clock diagram respectively. Reviewed-by: Havard Skinnemoen Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- hw/misc/npcm7xx_clk.c | 795 +++++++++++++++++++++++++++++++++- include/hw/misc/npcm7xx_clk.h | 140 +++++- 2 files changed, 927 insertions(+), 8 deletions(-) diff --git a/hw/misc/npcm7xx_clk.c b/hw/misc/npcm7xx_clk.c index 6732437fe2..48bc9bdda5 100644 --- a/hw/misc/npcm7xx_clk.c +++ b/hw/misc/npcm7xx_clk.c @@ -18,6 +18,7 @@ =20 #include "hw/misc/npcm7xx_clk.h" #include "hw/timer/npcm7xx_timer.h" +#include "hw/qdev-clock.h" #include "migration/vmstate.h" #include "qemu/error-report.h" #include "qemu/log.h" @@ -27,9 +28,22 @@ #include "trace.h" #include "sysemu/watchdog.h" =20 +/* + * The reference clock hz, and the SECCNT and CNTR25M registers in this mo= dule, + * is always 25 MHz. + */ +#define NPCM7XX_CLOCK_REF_HZ (25000000) + +/* Register Field Definitions */ +#define NPCM7XX_CLK_WDRCR_CA9C BIT(0) /* Cortex A9 Cores */ + #define PLLCON_LOKI BIT(31) #define PLLCON_LOKS BIT(30) #define PLLCON_PWDEN BIT(12) +#define PLLCON_FBDV(con) extract32((con), 16, 12) +#define PLLCON_OTDV2(con) extract32((con), 13, 3) +#define PLLCON_OTDV1(con) extract32((con), 8, 3) +#define PLLCON_INDV(con) extract32((con), 0, 6) =20 enum NPCM7xxCLKRegisters { NPCM7XX_CLK_CLKEN1, @@ -89,12 +103,609 @@ static const uint32_t cold_reset_values[NPCM7XX_CLK_N= R_REGS] =3D { [NPCM7XX_CLK_AHBCKFI] =3D 0x000000c8, }; =20 -/* Register Field Definitions */ -#define NPCM7XX_CLK_WDRCR_CA9C BIT(0) /* Cortex A9 Cores */ - /* The number of watchdogs that can trigger a reset. */ #define NPCM7XX_NR_WATCHDOGS (3) =20 +/* Clock converter functions */ + +#define TYPE_NPCM7XX_CLOCK_PLL "npcm7xx-clock-pll" +#define NPCM7XX_CLOCK_PLL(obj) OBJECT_CHECK(NPCM7xxClockPLLState, \ + (obj), TYPE_NPCM7XX_CLOCK_PLL) +#define TYPE_NPCM7XX_CLOCK_SEL "npcm7xx-clock-sel" +#define NPCM7XX_CLOCK_SEL(obj) OBJECT_CHECK(NPCM7xxClockSELState, \ + (obj), TYPE_NPCM7XX_CLOCK_SEL) +#define TYPE_NPCM7XX_CLOCK_DIVIDER "npcm7xx-clock-divider" +#define NPCM7XX_CLOCK_DIVIDER(obj) OBJECT_CHECK(NPCM7xxClockDividerState, \ + (obj), TYPE_NPCM7XX_CLOCK_DIVIDER) + +static void npcm7xx_clk_update_pll(void *opaque) +{ + NPCM7xxClockPLLState *s =3D opaque; + uint32_t con =3D s->clk->regs[s->reg]; + uint64_t freq; + + /* The PLL is grounded if it is not locked yet. */ + if (con & PLLCON_LOKI) { + freq =3D clock_get_hz(s->clock_in); + freq *=3D PLLCON_FBDV(con); + freq /=3D PLLCON_INDV(con) * PLLCON_OTDV1(con) * PLLCON_OTDV2(con); + } else { + freq =3D 0; + } + + clock_update_hz(s->clock_out, freq); +} + +static void npcm7xx_clk_update_sel(void *opaque) +{ + NPCM7xxClockSELState *s =3D opaque; + uint32_t index =3D extract32(s->clk->regs[NPCM7XX_CLK_CLKSEL], s->offs= et, + s->len); + + if (index >=3D s->input_size) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: SEL index: %u out of range\n", + __func__, index); + index =3D 0; + } + clock_update_hz(s->clock_out, clock_get_hz(s->clock_in[index])); +} + +static void npcm7xx_clk_update_divider(void *opaque) +{ + NPCM7xxClockDividerState *s =3D opaque; + uint32_t freq; + + freq =3D s->divide(s); + clock_update_hz(s->clock_out, freq); +} + +static uint32_t divide_by_constant(NPCM7xxClockDividerState *s) +{ + return clock_get_hz(s->clock_in) / s->divisor; +} + +static uint32_t divide_by_reg_divisor(NPCM7xxClockDividerState *s) +{ + return clock_get_hz(s->clock_in) / + (extract32(s->clk->regs[s->reg], s->offset, s->len) + 1); +} + +static uint32_t divide_by_reg_divisor_times_2(NPCM7xxClockDividerState *s) +{ + return divide_by_reg_divisor(s) / 2; +} + +static uint32_t shift_by_reg_divisor(NPCM7xxClockDividerState *s) +{ + return clock_get_hz(s->clock_in) >> + extract32(s->clk->regs[s->reg], s->offset, s->len); +} + +static NPCM7xxClockPLL find_pll_by_reg(enum NPCM7xxCLKRegisters reg) +{ + switch (reg) { + case NPCM7XX_CLK_PLLCON0: + return NPCM7XX_CLOCK_PLL0; + case NPCM7XX_CLK_PLLCON1: + return NPCM7XX_CLOCK_PLL1; + case NPCM7XX_CLK_PLLCON2: + return NPCM7XX_CLOCK_PLL2; + case NPCM7XX_CLK_PLLCONG: + return NPCM7XX_CLOCK_PLLG; + default: + g_assert_not_reached(); + } +} + +static void npcm7xx_clk_update_all_plls(NPCM7xxCLKState *clk) +{ + int i; + + for (i =3D 0; i < NPCM7XX_CLOCK_NR_PLLS; ++i) { + npcm7xx_clk_update_pll(&clk->plls[i]); + } +} + +static void npcm7xx_clk_update_all_sels(NPCM7xxCLKState *clk) +{ + int i; + + for (i =3D 0; i < NPCM7XX_CLOCK_NR_SELS; ++i) { + npcm7xx_clk_update_sel(&clk->sels[i]); + } +} + +static void npcm7xx_clk_update_all_dividers(NPCM7xxCLKState *clk) +{ + int i; + + for (i =3D 0; i < NPCM7XX_CLOCK_NR_DIVIDERS; ++i) { + npcm7xx_clk_update_divider(&clk->dividers[i]); + } +} + +static void npcm7xx_clk_update_all_clocks(NPCM7xxCLKState *clk) +{ + clock_update_hz(clk->clkref, NPCM7XX_CLOCK_REF_HZ); + npcm7xx_clk_update_all_plls(clk); + npcm7xx_clk_update_all_sels(clk); + npcm7xx_clk_update_all_dividers(clk); +} + +/* Types of clock sources. */ +typedef enum ClockSrcType { + CLKSRC_REF, + CLKSRC_PLL, + CLKSRC_SEL, + CLKSRC_DIV, +} ClockSrcType; + +typedef struct PLLInitInfo { + const char *name; + ClockSrcType src_type; + int src_index; + int reg; + const char *public_name; +} PLLInitInfo; + +typedef struct SELInitInfo { + const char *name; + uint8_t input_size; + ClockSrcType src_type[NPCM7XX_CLK_SEL_MAX_INPUT]; + int src_index[NPCM7XX_CLK_SEL_MAX_INPUT]; + int offset; + int len; + const char *public_name; +} SELInitInfo; + +typedef struct DividerInitInfo { + const char *name; + ClockSrcType src_type; + int src_index; + uint32_t (*divide)(NPCM7xxClockDividerState *s); + int reg; /* not used when type =3D=3D CONSTANT */ + int offset; /* not used when type =3D=3D CONSTANT */ + int len; /* not used when type =3D=3D CONSTANT */ + int divisor; /* used only when type =3D=3D CONSTANT */ + const char *public_name; +} DividerInitInfo; + +static const PLLInitInfo pll_init_info_list[] =3D { + [NPCM7XX_CLOCK_PLL0] =3D { + .name =3D "pll0", + .src_type =3D CLKSRC_REF, + .reg =3D NPCM7XX_CLK_PLLCON0, + }, + [NPCM7XX_CLOCK_PLL1] =3D { + .name =3D "pll1", + .src_type =3D CLKSRC_REF, + .reg =3D NPCM7XX_CLK_PLLCON1, + }, + [NPCM7XX_CLOCK_PLL2] =3D { + .name =3D "pll2", + .src_type =3D CLKSRC_REF, + .reg =3D NPCM7XX_CLK_PLLCON2, + }, + [NPCM7XX_CLOCK_PLLG] =3D { + .name =3D "pllg", + .src_type =3D CLKSRC_REF, + .reg =3D NPCM7XX_CLK_PLLCONG, + }, +}; + +static const SELInitInfo sel_init_info_list[] =3D { + [NPCM7XX_CLOCK_PIXCKSEL] =3D { + .name =3D "pixcksel", + .input_size =3D 2, + .src_type =3D {CLKSRC_PLL, CLKSRC_REF}, + .src_index =3D {NPCM7XX_CLOCK_PLLG, 0}, + .offset =3D 5, + .len =3D 1, + .public_name =3D "pixel-clock", + }, + [NPCM7XX_CLOCK_MCCKSEL] =3D { + .name =3D "mccksel", + .input_size =3D 4, + .src_type =3D {CLKSRC_DIV, CLKSRC_REF, CLKSRC_REF, + /*MCBPCK, shouldn't be used in normal operation*/ + CLKSRC_REF}, + .src_index =3D {NPCM7XX_CLOCK_PLL1D2, 0, 0, 0}, + .offset =3D 12, + .len =3D 2, + .public_name =3D "mc-phy-clock", + }, + [NPCM7XX_CLOCK_CPUCKSEL] =3D { + .name =3D "cpucksel", + .input_size =3D 4, + .src_type =3D {CLKSRC_PLL, CLKSRC_DIV, CLKSRC_REF, + /*SYSBPCK, shouldn't be used in normal operation*/ + CLKSRC_REF}, + .src_index =3D {NPCM7XX_CLOCK_PLL0, NPCM7XX_CLOCK_PLL1D2, 0, 0}, + .offset =3D 0, + .len =3D 2, + .public_name =3D "system-clock", + }, + [NPCM7XX_CLOCK_CLKOUTSEL] =3D { + .name =3D "clkoutsel", + .input_size =3D 5, + .src_type =3D {CLKSRC_PLL, CLKSRC_DIV, CLKSRC_REF, + CLKSRC_PLL, CLKSRC_DIV}, + .src_index =3D {NPCM7XX_CLOCK_PLL0, NPCM7XX_CLOCK_PLL1D2, 0, + NPCM7XX_CLOCK_PLLG, NPCM7XX_CLOCK_PLL2D2}, + .offset =3D 18, + .len =3D 3, + .public_name =3D "tock", + }, + [NPCM7XX_CLOCK_UARTCKSEL] =3D { + .name =3D "uartcksel", + .input_size =3D 4, + .src_type =3D {CLKSRC_PLL, CLKSRC_DIV, CLKSRC_REF, CLKSRC_DIV}, + .src_index =3D {NPCM7XX_CLOCK_PLL0, NPCM7XX_CLOCK_PLL1D2, 0, + NPCM7XX_CLOCK_PLL2D2}, + .offset =3D 8, + .len =3D 2, + }, + [NPCM7XX_CLOCK_TIMCKSEL] =3D { + .name =3D "timcksel", + .input_size =3D 4, + .src_type =3D {CLKSRC_PLL, CLKSRC_DIV, CLKSRC_REF, CLKSRC_DIV}, + .src_index =3D {NPCM7XX_CLOCK_PLL0, NPCM7XX_CLOCK_PLL1D2, 0, + NPCM7XX_CLOCK_PLL2D2}, + .offset =3D 14, + .len =3D 2, + }, + [NPCM7XX_CLOCK_SDCKSEL] =3D { + .name =3D "sdcksel", + .input_size =3D 4, + .src_type =3D {CLKSRC_PLL, CLKSRC_DIV, CLKSRC_REF, CLKSRC_DIV}, + .src_index =3D {NPCM7XX_CLOCK_PLL0, NPCM7XX_CLOCK_PLL1D2, 0, + NPCM7XX_CLOCK_PLL2D2}, + .offset =3D 6, + .len =3D 2, + }, + [NPCM7XX_CLOCK_GFXMSEL] =3D { + .name =3D "gfxmksel", + .input_size =3D 2, + .src_type =3D {CLKSRC_REF, CLKSRC_PLL}, + .src_index =3D {0, NPCM7XX_CLOCK_PLL2}, + .offset =3D 21, + .len =3D 1, + }, + [NPCM7XX_CLOCK_SUCKSEL] =3D { + .name =3D "sucksel", + .input_size =3D 4, + .src_type =3D {CLKSRC_PLL, CLKSRC_DIV, CLKSRC_REF, CLKSRC_DIV}, + .src_index =3D {NPCM7XX_CLOCK_PLL0, NPCM7XX_CLOCK_PLL1D2, 0, + NPCM7XX_CLOCK_PLL2D2}, + .offset =3D 10, + .len =3D 2, + }, +}; + +static const DividerInitInfo divider_init_info_list[] =3D { + [NPCM7XX_CLOCK_PLL1D2] =3D { + .name =3D "pll1d2", + .src_type =3D CLKSRC_PLL, + .src_index =3D NPCM7XX_CLOCK_PLL1, + .divide =3D divide_by_constant, + .divisor =3D 2, + }, + [NPCM7XX_CLOCK_PLL2D2] =3D { + .name =3D "pll2d2", + .src_type =3D CLKSRC_PLL, + .src_index =3D NPCM7XX_CLOCK_PLL2, + .divide =3D divide_by_constant, + .divisor =3D 2, + }, + [NPCM7XX_CLOCK_MC_DIVIDER] =3D { + .name =3D "mc-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_MCCKSEL, + .divide =3D divide_by_constant, + .divisor =3D 2, + .public_name =3D "mc-clock" + }, + [NPCM7XX_CLOCK_AXI_DIVIDER] =3D { + .name =3D "axi-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_CPUCKSEL, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 0, + .len =3D 1, + .public_name =3D "clk2" + }, + [NPCM7XX_CLOCK_AHB_DIVIDER] =3D { + .name =3D "ahb-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AXI_DIVIDER, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 26, + .len =3D 2, + .public_name =3D "clk4" + }, + [NPCM7XX_CLOCK_AHB3_DIVIDER] =3D { + .name =3D "ahb3-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 6, + .len =3D 5, + .public_name =3D "ahb3-spi3-clock" + }, + [NPCM7XX_CLOCK_SPI0_DIVIDER] =3D { + .name =3D "spi0-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV3, + .offset =3D 6, + .len =3D 5, + .public_name =3D "spi0-clock", + }, + [NPCM7XX_CLOCK_SPIX_DIVIDER] =3D { + .name =3D "spix-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV3, + .offset =3D 1, + .len =3D 5, + .public_name =3D "spix-clock", + }, + [NPCM7XX_CLOCK_APB1_DIVIDER] =3D { + .name =3D "apb1-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 24, + .len =3D 2, + .public_name =3D "apb1-clock", + }, + [NPCM7XX_CLOCK_APB2_DIVIDER] =3D { + .name =3D "apb2-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 26, + .len =3D 2, + .public_name =3D "apb2-clock", + }, + [NPCM7XX_CLOCK_APB3_DIVIDER] =3D { + .name =3D "apb3-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 28, + .len =3D 2, + .public_name =3D "apb3-clock", + }, + [NPCM7XX_CLOCK_APB4_DIVIDER] =3D { + .name =3D "apb4-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 30, + .len =3D 2, + .public_name =3D "apb4-clock", + }, + [NPCM7XX_CLOCK_APB5_DIVIDER] =3D { + .name =3D "apb5-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_AHB_DIVIDER, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 22, + .len =3D 2, + .public_name =3D "apb5-clock", + }, + [NPCM7XX_CLOCK_CLKOUT_DIVIDER] =3D { + .name =3D "clkout-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_CLKOUTSEL, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 16, + .len =3D 5, + .public_name =3D "clkout", + }, + [NPCM7XX_CLOCK_UART_DIVIDER] =3D { + .name =3D "uart-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_UARTCKSEL, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 16, + .len =3D 5, + .public_name =3D "uart-clock", + }, + [NPCM7XX_CLOCK_TIMER_DIVIDER] =3D { + .name =3D "timer-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_TIMCKSEL, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 21, + .len =3D 5, + .public_name =3D "timer-clock", + }, + [NPCM7XX_CLOCK_ADC_DIVIDER] =3D { + .name =3D "adc-divider", + .src_type =3D CLKSRC_DIV, + .src_index =3D NPCM7XX_CLOCK_TIMER_DIVIDER, + .divide =3D shift_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 28, + .len =3D 3, + .public_name =3D "adc-clock", + }, + [NPCM7XX_CLOCK_MMC_DIVIDER] =3D { + .name =3D "mmc-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_SDCKSEL, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV1, + .offset =3D 11, + .len =3D 5, + .public_name =3D "mmc-clock", + }, + [NPCM7XX_CLOCK_SDHC_DIVIDER] =3D { + .name =3D "sdhc-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_SDCKSEL, + .divide =3D divide_by_reg_divisor_times_2, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 0, + .len =3D 4, + .public_name =3D "sdhc-clock", + }, + [NPCM7XX_CLOCK_GFXM_DIVIDER] =3D { + .name =3D "gfxm-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_GFXMSEL, + .divide =3D divide_by_constant, + .divisor =3D 3, + .public_name =3D "gfxm-clock", + }, + [NPCM7XX_CLOCK_UTMI_DIVIDER] =3D { + .name =3D "utmi-divider", + .src_type =3D CLKSRC_SEL, + .src_index =3D NPCM7XX_CLOCK_SUCKSEL, + .divide =3D divide_by_reg_divisor, + .reg =3D NPCM7XX_CLK_CLKDIV2, + .offset =3D 8, + .len =3D 5, + .public_name =3D "utmi-clock", + }, +}; + +static void npcm7xx_clk_pll_init(Object *obj) +{ + NPCM7xxClockPLLState *pll =3D NPCM7XX_CLOCK_PLL(obj); + + pll->clock_in =3D qdev_init_clock_in(DEVICE(pll), "clock-in", + npcm7xx_clk_update_pll, pll); + pll->clock_out =3D qdev_init_clock_out(DEVICE(pll), "clock-out"); +} + +static void npcm7xx_clk_sel_init(Object *obj) +{ + int i; + NPCM7xxClockSELState *sel =3D NPCM7XX_CLOCK_SEL(obj); + + for (i =3D 0; i < NPCM7XX_CLK_SEL_MAX_INPUT; ++i) { + sel->clock_in[i] =3D qdev_init_clock_in(DEVICE(sel), + g_strdup_printf("clock-in[%d]", i), + npcm7xx_clk_update_sel, sel); + } + sel->clock_out =3D qdev_init_clock_out(DEVICE(sel), "clock-out"); +} +static void npcm7xx_clk_divider_init(Object *obj) +{ + NPCM7xxClockDividerState *div =3D NPCM7XX_CLOCK_DIVIDER(obj); + + div->clock_in =3D qdev_init_clock_in(DEVICE(div), "clock-in", + npcm7xx_clk_update_divider, div); + div->clock_out =3D qdev_init_clock_out(DEVICE(div), "clock-out"); +} + +static void npcm7xx_init_clock_pll(NPCM7xxClockPLLState *pll, + NPCM7xxCLKState *clk, const PLLInitInfo *init_info) +{ + pll->name =3D init_info->name; + pll->clk =3D clk; + pll->reg =3D init_info->reg; + if (init_info->public_name !=3D NULL) { + qdev_alias_clock(DEVICE(pll), "clock-out", DEVICE(clk), + init_info->public_name); + } +} + +static void npcm7xx_init_clock_sel(NPCM7xxClockSELState *sel, + NPCM7xxCLKState *clk, const SELInitInfo *init_info) +{ + int input_size =3D init_info->input_size; + + sel->name =3D init_info->name; + sel->clk =3D clk; + sel->input_size =3D init_info->input_size; + g_assert(input_size <=3D NPCM7XX_CLK_SEL_MAX_INPUT); + sel->offset =3D init_info->offset; + sel->len =3D init_info->len; + if (init_info->public_name !=3D NULL) { + qdev_alias_clock(DEVICE(sel), "clock-out", DEVICE(clk), + init_info->public_name); + } +} + +static void npcm7xx_init_clock_divider(NPCM7xxClockDividerState *div, + NPCM7xxCLKState *clk, const DividerInitInfo *init_info) +{ + div->name =3D init_info->name; + div->clk =3D clk; + + div->divide =3D init_info->divide; + if (div->divide =3D=3D divide_by_constant) { + div->divisor =3D init_info->divisor; + } else { + div->reg =3D init_info->reg; + div->offset =3D init_info->offset; + div->len =3D init_info->len; + } + if (init_info->public_name !=3D NULL) { + qdev_alias_clock(DEVICE(div), "clock-out", DEVICE(clk), + init_info->public_name); + } +} + +static Clock *npcm7xx_get_clock(NPCM7xxCLKState *clk, ClockSrcType type, + int index) +{ + switch (type) { + case CLKSRC_REF: + return clk->clkref; + case CLKSRC_PLL: + return clk->plls[index].clock_out; + case CLKSRC_SEL: + return clk->sels[index].clock_out; + case CLKSRC_DIV: + return clk->dividers[index].clock_out; + default: + g_assert_not_reached(); + } +} + +static void npcm7xx_connect_clocks(NPCM7xxCLKState *clk) +{ + int i, j; + Clock *src; + + for (i =3D 0; i < NPCM7XX_CLOCK_NR_PLLS; ++i) { + src =3D npcm7xx_get_clock(clk, pll_init_info_list[i].src_type, + pll_init_info_list[i].src_index); + clock_set_source(clk->plls[i].clock_in, src); + } + for (i =3D 0; i < NPCM7XX_CLOCK_NR_SELS; ++i) { + for (j =3D 0; j < sel_init_info_list[i].input_size; ++j) { + src =3D npcm7xx_get_clock(clk, sel_init_info_list[i].src_type[= j], + sel_init_info_list[i].src_index[j]); + clock_set_source(clk->sels[i].clock_in[j], src); + } + } + for (i =3D 0; i < NPCM7XX_CLOCK_NR_DIVIDERS; ++i) { + src =3D npcm7xx_get_clock(clk, divider_init_info_list[i].src_type, + divider_init_info_list[i].src_index); + clock_set_source(clk->dividers[i].clock_in, src); + } +} + static uint64_t npcm7xx_clk_read(void *opaque, hwaddr offset, unsigned siz= e) { uint32_t reg =3D offset / sizeof(uint32_t); @@ -129,7 +740,7 @@ static uint64_t npcm7xx_clk_read(void *opaque, hwaddr o= ffset, unsigned size) * * The 4 LSBs are always zero: (1e9 / 640) << 4 =3D 25000000. */ - value =3D (((now_ns - s->ref_ns) / 640) << 4) % NPCM7XX_TIMER_REF_= HZ; + value =3D (((now_ns - s->ref_ns) / 640) << 4) % NPCM7XX_CLOCK_REF_= HZ; break; =20 default: @@ -183,6 +794,20 @@ static void npcm7xx_clk_write(void *opaque, hwaddr off= set, value |=3D (value & PLLCON_LOKS); } } + /* Only update PLL when it is locked. */ + if (value & PLLCON_LOKI) { + npcm7xx_clk_update_pll(&s->plls[find_pll_by_reg(reg)]); + } + break; + + case NPCM7XX_CLK_CLKSEL: + npcm7xx_clk_update_all_sels(s); + break; + + case NPCM7XX_CLK_CLKDIV1: + case NPCM7XX_CLK_CLKDIV2: + case NPCM7XX_CLK_CLKDIV3: + npcm7xx_clk_update_all_dividers(s); break; =20 case NPCM7XX_CLK_CNTR25M: @@ -234,6 +859,7 @@ static void npcm7xx_clk_enter_reset(Object *obj, ResetT= ype type) case RESET_TYPE_COLD: memcpy(s->regs, cold_reset_values, sizeof(cold_reset_values)); s->ref_ns =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + npcm7xx_clk_update_all_clocks(s); return; } =20 @@ -245,6 +871,42 @@ static void npcm7xx_clk_enter_reset(Object *obj, Reset= Type type) __func__, type); } =20 +static void npcm7xx_clk_init_clock_hierarchy(NPCM7xxCLKState *s) +{ + int i; + + s->clkref =3D qdev_init_clock_in(DEVICE(s), "clkref", NULL, NULL); + + /* First pass: init all converter modules */ + QEMU_BUILD_BUG_ON(ARRAY_SIZE(pll_init_info_list) !=3D NPCM7XX_CLOCK_NR= _PLLS); + QEMU_BUILD_BUG_ON(ARRAY_SIZE(sel_init_info_list) !=3D NPCM7XX_CLOCK_NR= _SELS); + QEMU_BUILD_BUG_ON(ARRAY_SIZE(divider_init_info_list) + !=3D NPCM7XX_CLOCK_NR_DIVIDERS); + for (i =3D 0; i < NPCM7XX_CLOCK_NR_PLLS; ++i) { + object_initialize_child(OBJECT(s), pll_init_info_list[i].name, + &s->plls[i], TYPE_NPCM7XX_CLOCK_PLL); + npcm7xx_init_clock_pll(&s->plls[i], s, + &pll_init_info_list[i]); + } + for (i =3D 0; i < NPCM7XX_CLOCK_NR_SELS; ++i) { + object_initialize_child(OBJECT(s), sel_init_info_list[i].name, + &s->sels[i], TYPE_NPCM7XX_CLOCK_SEL); + npcm7xx_init_clock_sel(&s->sels[i], s, + &sel_init_info_list[i]); + } + for (i =3D 0; i < NPCM7XX_CLOCK_NR_DIVIDERS; ++i) { + object_initialize_child(OBJECT(s), divider_init_info_list[i].name, + &s->dividers[i], TYPE_NPCM7XX_CLOCK_DIVIDER); + npcm7xx_init_clock_divider(&s->dividers[i], s, + ÷r_init_info_list[i]); + } + + /* Second pass: connect converter modules */ + npcm7xx_connect_clocks(s); + + clock_update_hz(s->clkref, NPCM7XX_CLOCK_REF_HZ); +} + static void npcm7xx_clk_init(Object *obj) { NPCM7xxCLKState *s =3D NPCM7XX_CLK(obj); @@ -252,21 +914,114 @@ static void npcm7xx_clk_init(Object *obj) memory_region_init_io(&s->iomem, obj, &npcm7xx_clk_ops, s, TYPE_NPCM7XX_CLK, 4 * KiB); sysbus_init_mmio(&s->parent, &s->iomem); +} + +static int npcm7xx_clk_post_load(void *opaque, int version_id) +{ + if (version_id >=3D 1) { + NPCM7xxCLKState *clk =3D opaque; + + npcm7xx_clk_update_all_clocks(clk); + } + + return 0; +} + +static void npcm7xx_clk_realize(DeviceState *dev, Error **errp) +{ + int i; + NPCM7xxCLKState *s =3D NPCM7XX_CLK(dev); + qdev_init_gpio_in_named(DEVICE(s), npcm7xx_clk_perform_watchdog_reset, NPCM7XX_WATCHDOG_RESET_GPIO_IN, NPCM7XX_NR_WATCHDOGS); + npcm7xx_clk_init_clock_hierarchy(s); + + /* Realize child devices */ + for (i =3D 0; i < NPCM7XX_CLOCK_NR_PLLS; ++i) { + if (!qdev_realize(DEVICE(&s->plls[i]), NULL, errp)) { + return; + } + } + for (i =3D 0; i < NPCM7XX_CLOCK_NR_SELS; ++i) { + if (!qdev_realize(DEVICE(&s->sels[i]), NULL, errp)) { + return; + } + } + for (i =3D 0; i < NPCM7XX_CLOCK_NR_DIVIDERS; ++i) { + if (!qdev_realize(DEVICE(&s->dividers[i]), NULL, errp)) { + return; + } + } } =20 -static const VMStateDescription vmstate_npcm7xx_clk =3D { - .name =3D "npcm7xx-clk", +static const VMStateDescription vmstate_npcm7xx_clk_pll =3D { + .name =3D "npcm7xx-clock-pll", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_CLOCK(clock_in, NPCM7xxClockPLLState), + VMSTATE_END_OF_LIST(), + }, +}; + +static const VMStateDescription vmstate_npcm7xx_clk_sel =3D { + .name =3D "npcm7xx-clock-sel", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(clock_in, NPCM7xxClockSELState, + NPCM7XX_CLK_SEL_MAX_INPUT, 0, vmstate_clock, Clock), + VMSTATE_END_OF_LIST(), + }, +}; + +static const VMStateDescription vmstate_npcm7xx_clk_divider =3D { + .name =3D "npcm7xx-clock-divider", .version_id =3D 0, .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_CLOCK(clock_in, NPCM7xxClockDividerState), + VMSTATE_END_OF_LIST(), + }, +}; + +static const VMStateDescription vmstate_npcm7xx_clk =3D { + .name =3D "npcm7xx-clk", + .version_id =3D 1, + .minimum_version_id =3D 1, + .post_load =3D npcm7xx_clk_post_load, .fields =3D (VMStateField[]) { VMSTATE_UINT32_ARRAY(regs, NPCM7xxCLKState, NPCM7XX_CLK_NR_REGS), VMSTATE_INT64(ref_ns, NPCM7xxCLKState), + VMSTATE_CLOCK(clkref, NPCM7xxCLKState), VMSTATE_END_OF_LIST(), }, }; =20 +static void npcm7xx_clk_pll_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->desc =3D "NPCM7xx Clock PLL Module"; + dc->vmsd =3D &vmstate_npcm7xx_clk_pll; +} + +static void npcm7xx_clk_sel_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->desc =3D "NPCM7xx Clock SEL Module"; + dc->vmsd =3D &vmstate_npcm7xx_clk_sel; +} + +static void npcm7xx_clk_divider_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->desc =3D "NPCM7xx Clock Divider Module"; + dc->vmsd =3D &vmstate_npcm7xx_clk_divider; +} + static void npcm7xx_clk_class_init(ObjectClass *klass, void *data) { ResettableClass *rc =3D RESETTABLE_CLASS(klass); @@ -276,9 +1031,34 @@ static void npcm7xx_clk_class_init(ObjectClass *klass= , void *data) =20 dc->desc =3D "NPCM7xx Clock Control Registers"; dc->vmsd =3D &vmstate_npcm7xx_clk; + dc->realize =3D npcm7xx_clk_realize; rc->phases.enter =3D npcm7xx_clk_enter_reset; } =20 +static const TypeInfo npcm7xx_clk_pll_info =3D { + .name =3D TYPE_NPCM7XX_CLOCK_PLL, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(NPCM7xxClockPLLState), + .instance_init =3D npcm7xx_clk_pll_init, + .class_init =3D npcm7xx_clk_pll_class_init, +}; + +static const TypeInfo npcm7xx_clk_sel_info =3D { + .name =3D TYPE_NPCM7XX_CLOCK_SEL, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(NPCM7xxClockSELState), + .instance_init =3D npcm7xx_clk_sel_init, + .class_init =3D npcm7xx_clk_sel_class_init, +}; + +static const TypeInfo npcm7xx_clk_divider_info =3D { + .name =3D TYPE_NPCM7XX_CLOCK_DIVIDER, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(NPCM7xxClockDividerState), + .instance_init =3D npcm7xx_clk_divider_init, + .class_init =3D npcm7xx_clk_divider_class_init, +}; + static const TypeInfo npcm7xx_clk_info =3D { .name =3D TYPE_NPCM7XX_CLK, .parent =3D TYPE_SYS_BUS_DEVICE, @@ -289,6 +1069,9 @@ static const TypeInfo npcm7xx_clk_info =3D { =20 static void npcm7xx_clk_register_type(void) { + type_register_static(&npcm7xx_clk_pll_info); + type_register_static(&npcm7xx_clk_sel_info); + type_register_static(&npcm7xx_clk_divider_info); type_register_static(&npcm7xx_clk_info); } type_init(npcm7xx_clk_register_type); diff --git a/include/hw/misc/npcm7xx_clk.h b/include/hw/misc/npcm7xx_clk.h index 2338fbbdb5..f641f95f3e 100644 --- a/include/hw/misc/npcm7xx_clk.h +++ b/include/hw/misc/npcm7xx_clk.h @@ -17,6 +17,7 @@ #define NPCM7XX_CLK_H =20 #include "exec/memory.h" +#include "hw/clock.h" #include "hw/sysbus.h" =20 /* @@ -33,16 +34,151 @@ =20 #define NPCM7XX_WATCHDOG_RESET_GPIO_IN "npcm7xx-clk-watchdog-reset-gpio-in" =20 -typedef struct NPCM7xxCLKState { +/* Maximum amount of clock inputs in a SEL module. */ +#define NPCM7XX_CLK_SEL_MAX_INPUT 5 + +/* PLLs in CLK module. */ +typedef enum NPCM7xxClockPLL { + NPCM7XX_CLOCK_PLL0, + NPCM7XX_CLOCK_PLL1, + NPCM7XX_CLOCK_PLL2, + NPCM7XX_CLOCK_PLLG, + NPCM7XX_CLOCK_NR_PLLS, +} NPCM7xxClockPLL; + +/* SEL/MUX in CLK module. */ +typedef enum NPCM7xxClockSEL { + NPCM7XX_CLOCK_PIXCKSEL, + NPCM7XX_CLOCK_MCCKSEL, + NPCM7XX_CLOCK_CPUCKSEL, + NPCM7XX_CLOCK_CLKOUTSEL, + NPCM7XX_CLOCK_UARTCKSEL, + NPCM7XX_CLOCK_TIMCKSEL, + NPCM7XX_CLOCK_SDCKSEL, + NPCM7XX_CLOCK_GFXMSEL, + NPCM7XX_CLOCK_SUCKSEL, + NPCM7XX_CLOCK_NR_SELS, +} NPCM7xxClockSEL; + +/* Dividers in CLK module. */ +typedef enum NPCM7xxClockDivider { + NPCM7XX_CLOCK_PLL1D2, /* PLL1/2 */ + NPCM7XX_CLOCK_PLL2D2, /* PLL2/2 */ + NPCM7XX_CLOCK_MC_DIVIDER, + NPCM7XX_CLOCK_AXI_DIVIDER, + NPCM7XX_CLOCK_AHB_DIVIDER, + NPCM7XX_CLOCK_AHB3_DIVIDER, + NPCM7XX_CLOCK_SPI0_DIVIDER, + NPCM7XX_CLOCK_SPIX_DIVIDER, + NPCM7XX_CLOCK_APB1_DIVIDER, + NPCM7XX_CLOCK_APB2_DIVIDER, + NPCM7XX_CLOCK_APB3_DIVIDER, + NPCM7XX_CLOCK_APB4_DIVIDER, + NPCM7XX_CLOCK_APB5_DIVIDER, + NPCM7XX_CLOCK_CLKOUT_DIVIDER, + NPCM7XX_CLOCK_UART_DIVIDER, + NPCM7XX_CLOCK_TIMER_DIVIDER, + NPCM7XX_CLOCK_ADC_DIVIDER, + NPCM7XX_CLOCK_MMC_DIVIDER, + NPCM7XX_CLOCK_SDHC_DIVIDER, + NPCM7XX_CLOCK_GFXM_DIVIDER, /* divide by 3 */ + NPCM7XX_CLOCK_UTMI_DIVIDER, + NPCM7XX_CLOCK_NR_DIVIDERS, +} NPCM7xxClockConverter; + +typedef struct NPCM7xxCLKState NPCM7xxCLKState; + +/** + * struct NPCM7xxClockPLLState - A PLL module in CLK module. + * @name: The name of the module. + * @clk: The CLK module that owns this module. + * @clock_in: The input clock of this module. + * @clock_out: The output clock of this module. + * @reg: The control registers for this PLL module. + */ +typedef struct NPCM7xxClockPLLState { + DeviceState parent; + + const char *name; + NPCM7xxCLKState *clk; + Clock *clock_in; + Clock *clock_out; + + int reg; +} NPCM7xxClockPLLState; + +/** + * struct NPCM7xxClockSELState - A SEL module in CLK module. + * @name: The name of the module. + * @clk: The CLK module that owns this module. + * @input_size: The size of inputs of this module. + * @clock_in: The input clocks of this module. + * @clock_out: The output clocks of this module. + * @offset: The offset of this module in the control register. + * @len: The length of this module in the control register. + */ +typedef struct NPCM7xxClockSELState { + DeviceState parent; + + const char *name; + NPCM7xxCLKState *clk; + uint8_t input_size; + Clock *clock_in[NPCM7XX_CLK_SEL_MAX_INPUT]; + Clock *clock_out; + + int offset; + int len; +} NPCM7xxClockSELState; + +/** + * struct NPCM7xxClockDividerState - A Divider module in CLK module. + * @name: The name of the module. + * @clk: The CLK module that owns this module. + * @clock_in: The input clock of this module. + * @clock_out: The output clock of this module. + * @divide: The function the divider uses to divide the input. + * @reg: The index of the control register that contains the divisor. + * @offset: The offset of the divisor in the control register. + * @len: The length of the divisor in the control register. + * @divisor: The divisor for a constant divisor + */ +typedef struct NPCM7xxClockDividerState { + DeviceState parent; + + const char *name; + NPCM7xxCLKState *clk; + Clock *clock_in; + Clock *clock_out; + + uint32_t (*divide)(struct NPCM7xxClockDividerState *s); + union { + struct { + int reg; + int offset; + int len; + }; + int divisor; + }; +} NPCM7xxClockDividerState; + +struct NPCM7xxCLKState { SysBusDevice parent; =20 MemoryRegion iomem; =20 + /* Clock converters */ + NPCM7xxClockPLLState plls[NPCM7XX_CLOCK_NR_PLLS]; + NPCM7xxClockSELState sels[NPCM7XX_CLOCK_NR_SELS]; + NPCM7xxClockDividerState dividers[NPCM7XX_CLOCK_NR_DIVIDERS]; + uint32_t regs[NPCM7XX_CLK_NR_REGS]; =20 /* Time reference for SECCNT and CNTR25M, initialized by power on rese= t */ int64_t ref_ns; -} NPCM7xxCLKState; + + /* The incoming reference clock. */ + Clock *clkref; +}; =20 #define TYPE_NPCM7XX_CLK "npcm7xx-clk" #define NPCM7XX_CLK(obj) OBJECT_CHECK(NPCM7xxCLKState, (obj), TYPE_NPCM7XX= _CLK) --=20 2.29.2.684.gfbc64c5ab5-goog From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607651852; cv=none; d=zohomail.com; s=zohoarc; b=cpF+A41yTPsVCxSqIjppTKG81jT5JVwZ9T5ty8lsavm7jRLL5vAxvx8tCTYFN8ZkqCTrHdRt/R17HStF8aE12A2lmKpXHYOx987Qd1xf28okFZIey1QqsJrIX8RTwAsNZ5FqGx1vDFSHFnajzYc+KIuVZnJcYxbrli9dILSGpBw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607651852; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=atO/eByn2GHwCNdU/vKptHQ0BjhHEFXcyGIO8itsblY=; b=P0LMES0mwBOnOGZu1bfG+QguXFSI1vb1wdf6CK17RrOldEjTz8GcgouZ520T1BXN9/97n6q2HbCb7cYdh9H92J3d+MbCqAYGjdi7QnG6T76K1wmM1giQPP/TiiPJMz3EZRkpjuW6XQpnWpQaOOyqzAwzFmTkGy0T4Lqi7/XKRBs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 160765185250860.79786026809859; Thu, 10 Dec 2020 17:57:32 -0800 (PST) Received: from localhost ([::1]:38510 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXgY-0005Zt-NP for importer@patchew.org; Thu, 10 Dec 2020 20:57:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58266) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <30NDSXwgKCsE31ohv0zonvvnsl.jvtxlt1-kl2lsuvunu1.vyn@flex--wuhaotsh.bounces.google.com>) id 1knXbc-0001dq-QW for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:24 -0500 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]:51424) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <30NDSXwgKCsE31ohv0zonvvnsl.jvtxlt1-kl2lsuvunu1.vyn@flex--wuhaotsh.bounces.google.com>) id 1knXbV-00078e-Vn for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:24 -0500 Received: by mail-yb1-xb49.google.com with SMTP id h75so9133706ybg.18 for ; Thu, 10 Dec 2020 17:52:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=atO/eByn2GHwCNdU/vKptHQ0BjhHEFXcyGIO8itsblY=; b=mwWpZmn9iLKVzbCUAn5OkQs7Fa+Ghc+qUrFLkWxZcCN411T00ueuEXsn1YVeU9zAvq aHPsSKkLNamvJSAC/stZ2KoTFTb2ffvWxvJYYtoHTRm+1FFGYec7yXzJYg9rfwDD7fgk XyllV0PkvZC7y2AQt+Y0UCroH7nt//ymVG7lMLt/WjsaCtGw4jyfmQIItQmnPQ+a985J HgI154XvkOWmUAH1NbJfdBUA3/P+RlnUIwGr781yhtzkBEIdtj1TNgUunaVA/Q2XsyKy UbKJ+74uOqUKB7PJ27s9jGnr8Q392Ng0Tq6zlNcAdTLP+xswL/ZonO+n0W5FINisVNIF pYdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=atO/eByn2GHwCNdU/vKptHQ0BjhHEFXcyGIO8itsblY=; b=LiVhBAJZb0CCb3wQHtHJSTtNImbPSIsVG24XdZdGtYq2d2qmvUBdwSsGfyB2IpNNFS st0Rr66dXNbp9JJPssPnZQVtzHCncclr56ew7s9amgjPqYiNAt7DYGba81sHxP1/Qklx mbM3evCAKegBmvp6i3bZ3VNhUvEbSWBrJvmN3+HSK9J2HkzHIe+7yWq4EEzhabCNzlS5 aI7qpZfcH7yebylf5eNUdkRY65Bae9XVvSrAqL23ujWx7ucuSR4w1fs0a+4nFF2/ejL+ Kjy7HWgZBb4GIvZaqZM9rckgvbDu1qnlKrI7/xt8SKOUNx3aDtrNUDiFyovQiO5TEqa2 WHiQ== X-Gm-Message-State: AOAM530i3+hE9fJULjjkH82szetOuC9VJLX81pJw/UQwfzfInFjpxs86 4tNIkD+nYWk0w+mKwdx2AY5fv02KQXVQiQ== X-Google-Smtp-Source: ABdhPJwtUcQHOblVU7qerpE8xs0T4deXroi8PE4LfHuNzjGG/4BQzdh9x3juaPFaW+jyKerlndfwZyg9DtjO1A== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a25:c386:: with SMTP id t128mr5374132ybf.208.1607651536229; Thu, 10 Dec 2020 17:52:16 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:51 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-3-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 2/7] hw/timer: Refactor NPCM7XX Timer to use CLK clock To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::b49; envelope-from=30NDSXwgKCsE31ohv0zonvvnsl.jvtxlt1-kl2lsuvunu1.vyn@flex--wuhaotsh.bounces.google.com; helo=mail-yb1-xb49.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 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, T_SPF_TEMPERROR=0.01, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch makes NPCM7XX Timer to use a the timer clock generated by the CLK module instead of the magic nubmer TIMER_REF_HZ. Reviewed-by: Havard Skinnemoen Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- hw/arm/npcm7xx.c | 5 +++++ hw/timer/npcm7xx_timer.c | 25 +++++++++++++++---------- include/hw/misc/npcm7xx_clk.h | 6 ------ include/hw/timer/npcm7xx_timer.h | 1 + 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/hw/arm/npcm7xx.c b/hw/arm/npcm7xx.c index 47e2b6fc40..fabfb1697b 100644 --- a/hw/arm/npcm7xx.c +++ b/hw/arm/npcm7xx.c @@ -22,6 +22,7 @@ #include "hw/char/serial.h" #include "hw/loader.h" #include "hw/misc/unimp.h" +#include "hw/qdev-clock.h" #include "hw/qdev-properties.h" #include "qapi/error.h" #include "qemu/units.h" @@ -420,6 +421,10 @@ static void npcm7xx_realize(DeviceState *dev, Error **= errp) int first_irq; int j; =20 + /* Connect the timer clock. */ + qdev_connect_clock_in(DEVICE(&s->tim[i]), "clock", qdev_get_clock_= out( + DEVICE(&s->clk), "timer-clock")); + sysbus_realize(sbd, &error_abort); sysbus_mmio_map(sbd, 0, npcm7xx_tim_addr[i]); =20 diff --git a/hw/timer/npcm7xx_timer.c b/hw/timer/npcm7xx_timer.c index d24445bd6e..9469c959e2 100644 --- a/hw/timer/npcm7xx_timer.c +++ b/hw/timer/npcm7xx_timer.c @@ -17,8 +17,8 @@ #include "qemu/osdep.h" =20 #include "hw/irq.h" +#include "hw/qdev-clock.h" #include "hw/qdev-properties.h" -#include "hw/misc/npcm7xx_clk.h" #include "hw/timer/npcm7xx_timer.h" #include "migration/vmstate.h" #include "qemu/bitops.h" @@ -130,7 +130,7 @@ static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer *= t, uint32_t count) { int64_t ns =3D count; =20 - ns *=3D NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ; + ns *=3D NANOSECONDS_PER_SECOND / clock_get_hz(t->ctrl->clock); ns *=3D npcm7xx_tcsr_prescaler(t->tcsr); =20 return ns; @@ -141,7 +141,7 @@ static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer = *t, int64_t ns) { int64_t count; =20 - count =3D ns / (NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ); + count =3D ns / (NANOSECONDS_PER_SECOND / clock_get_hz(t->ctrl->clock)); count /=3D npcm7xx_tcsr_prescaler(t->tcsr); =20 return count; @@ -166,8 +166,10 @@ static uint32_t npcm7xx_watchdog_timer_prescaler(const= NPCM7xxWatchdogTimer *t) static void npcm7xx_watchdog_timer_reset_cycles(NPCM7xxWatchdogTimer *t, int64_t cycles) { + g_assert(clock_get_hz(t->ctrl->clock) =3D=3D 25000000); uint32_t prescaler =3D npcm7xx_watchdog_timer_prescaler(t); - int64_t ns =3D (NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ) * cycle= s; + int64_t ns =3D (NANOSECONDS_PER_SECOND / clock_get_hz(t->ctrl->clock)) + * cycles; =20 /* * The reset function always clears the current timer. The caller of t= he @@ -606,10 +608,11 @@ static void npcm7xx_timer_hold_reset(Object *obj) qemu_irq_lower(s->watchdog_timer.irq); } =20 -static void npcm7xx_timer_realize(DeviceState *dev, Error **errp) +static void npcm7xx_timer_init(Object *obj) { - NPCM7xxTimerCtrlState *s =3D NPCM7XX_TIMER(dev); + NPCM7xxTimerCtrlState *s =3D NPCM7XX_TIMER(obj); SysBusDevice *sbd =3D &s->parent; + DeviceState *dev =3D DEVICE(obj); int i; NPCM7xxWatchdogTimer *w; =20 @@ -627,11 +630,12 @@ static void npcm7xx_timer_realize(DeviceState *dev, E= rror **errp) npcm7xx_watchdog_timer_expired, w); sysbus_init_irq(sbd, &w->irq); =20 - memory_region_init_io(&s->iomem, OBJECT(s), &npcm7xx_timer_ops, s, + memory_region_init_io(&s->iomem, obj, &npcm7xx_timer_ops, s, TYPE_NPCM7XX_TIMER, 4 * KiB); sysbus_init_mmio(sbd, &s->iomem); qdev_init_gpio_out_named(dev, &w->reset_signal, NPCM7XX_WATCHDOG_RESET_GPIO_OUT, 1); + s->clock =3D qdev_init_clock_in(dev, "clock", NULL, NULL); } =20 static const VMStateDescription vmstate_npcm7xx_base_timer =3D { @@ -675,10 +679,11 @@ static const VMStateDescription vmstate_npcm7xx_watch= dog_timer =3D { =20 static const VMStateDescription vmstate_npcm7xx_timer_ctrl =3D { .name =3D "npcm7xx-timer-ctrl", - .version_id =3D 1, - .minimum_version_id =3D 1, + .version_id =3D 2, + .minimum_version_id =3D 2, .fields =3D (VMStateField[]) { VMSTATE_UINT32(tisr, NPCM7xxTimerCtrlState), + VMSTATE_CLOCK(clock, NPCM7xxTimerCtrlState), VMSTATE_STRUCT_ARRAY(timer, NPCM7xxTimerCtrlState, NPCM7XX_TIMERS_PER_CTRL, 0, vmstate_npcm7xx_t= imer, NPCM7xxTimer), @@ -697,7 +702,6 @@ static void npcm7xx_timer_class_init(ObjectClass *klass= , void *data) QEMU_BUILD_BUG_ON(NPCM7XX_TIMER_REGS_END > NPCM7XX_TIMER_NR_REGS); =20 dc->desc =3D "NPCM7xx Timer Controller"; - dc->realize =3D npcm7xx_timer_realize; dc->vmsd =3D &vmstate_npcm7xx_timer_ctrl; rc->phases.enter =3D npcm7xx_timer_enter_reset; rc->phases.hold =3D npcm7xx_timer_hold_reset; @@ -708,6 +712,7 @@ static const TypeInfo npcm7xx_timer_info =3D { .parent =3D TYPE_SYS_BUS_DEVICE, .instance_size =3D sizeof(NPCM7xxTimerCtrlState), .class_init =3D npcm7xx_timer_class_init, + .instance_init =3D npcm7xx_timer_init, }; =20 static void npcm7xx_timer_register_type(void) diff --git a/include/hw/misc/npcm7xx_clk.h b/include/hw/misc/npcm7xx_clk.h index f641f95f3e..d5c8d16ca4 100644 --- a/include/hw/misc/npcm7xx_clk.h +++ b/include/hw/misc/npcm7xx_clk.h @@ -20,12 +20,6 @@ #include "hw/clock.h" #include "hw/sysbus.h" =20 -/* - * The reference clock frequency for the timer modules, and the SECCNT and - * CNTR25M registers in this module, is always 25 MHz. - */ -#define NPCM7XX_TIMER_REF_HZ (25000000) - /* * Number of registers in our device state structure. Don't change this wi= thout * incrementing the version_id in the vmstate. diff --git a/include/hw/timer/npcm7xx_timer.h b/include/hw/timer/npcm7xx_ti= mer.h index 6993fd723a..d45c051b56 100644 --- a/include/hw/timer/npcm7xx_timer.h +++ b/include/hw/timer/npcm7xx_timer.h @@ -101,6 +101,7 @@ struct NPCM7xxTimerCtrlState { =20 uint32_t tisr; =20 + Clock *clock; NPCM7xxTimer timer[NPCM7XX_TIMERS_PER_CTRL]; NPCM7xxWatchdogTimer watchdog_timer; }; --=20 2.29.2.684.gfbc64c5ab5-goog From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607651910; cv=none; d=zohomail.com; s=zohoarc; b=b7fhMv/tMELXOKTihuPXyvP/1VA5pgbtzQFJ+xPTPm3plQhwt2EnMObzb5cl0BwPYsciY2TE1YsFJDd4dj7ET2fKG4Wo9hZZeUYfs409g8OisLjYSCe5XnDl9rzCmnT8jmPf73jqJtitw9ZFpxTtwQdgRXdD10hvSYRZ2rjBpU0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607651910; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=ozQirFGu/TDbLmARRp51zBJNHv5SnsIrRWky5Q6wT3M=; b=Eq7uOjjqMldf0NO9v8dAMQnt1LG+3tnmsNyxpfb845lu5/GnzQ2R2WzsTYPwkq2pWkfhsy6WFJvW+YZtr4yQXbzAvKxo7PWJ3DdqzilP0gHN9jXNCKajyPFNWUJrLx/p8FXETQ98U4BqnBPcWWJ1BJLiqM7SV5xRW0PqE6XtqCk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607651910539491.5554315512204; Thu, 10 Dec 2020 17:58:30 -0800 (PST) Received: from localhost ([::1]:40352 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXhV-0006Om-25 for importer@patchew.org; Thu, 10 Dec 2020 20:58:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58294) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <30dDSXwgKCsI42piw10powwotm.kwuymu2-lm3mtvwvov2.wzo@flex--wuhaotsh.bounces.google.com>) id 1knXbd-0001gQ-R5 for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:25 -0500 Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]:35880) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <30dDSXwgKCsI42piw10powwotm.kwuymu2-lm3mtvwvov2.wzo@flex--wuhaotsh.bounces.google.com>) id 1knXbZ-00079f-K8 for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:25 -0500 Received: by mail-qv1-xf4a.google.com with SMTP id d30so5215171qvf.3 for ; Thu, 10 Dec 2020 17:52:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=ozQirFGu/TDbLmARRp51zBJNHv5SnsIrRWky5Q6wT3M=; b=eeW6x3aTAS4KSFUP0qI0j+Z5rpEeYAi5pO/aXKiE0ZXUILC3jx6N2KZUFhCBXh+ifp 8ZyW/zCGW6xgSjybh//WPKuv4LBxYMcVv3xffWVRghHimRKfm2XbmQbXanC9n1WQUEPJ /XQCW02mx0QedIEDmWHvrTE2FCVxwru6Gk8zkyrgydFfQKaUQTHA7tYnBkQ8dUPbocUG 310VcRGaKIGOYBE2YwvXVIKdJNF6CQUoHgEu49lDUzOH/K4MKxbiwn/CV+JGcQfLb4pK fe6YWOyrD/z2fxOD7vxxR9SMgV+H7nN4cs+m2ZuUvzMkGYUnL7SYDn5PNp4L13St5Q0I xKGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ozQirFGu/TDbLmARRp51zBJNHv5SnsIrRWky5Q6wT3M=; b=J3A3vOriO8KlSzmVTVgR+kiK4QJMugB4jPKd6PiugpxSX12iYqQCnPJk32zyOEh9Ya 5xn6MWqwYV+oib1wYkMEup2KaerCCKDDyi3MejFrt5l5SkSAHD9+GTCpqM8O8KMwlqLy 1U0G8CSIpZC7VQuSS+f12zfyiQV7bZVesH7xaTkeLsGBar8wL8x8jwYZYSYjR4CX68ax XB8kfDjnZWuD7pWBsPJD8lyI+TqquwkknJT6JOJ4M9qL6NtPtW+1rxx3+kqmPinhi+aQ E4DWg7Pv63Vq25C7y2Xt9dPhxYmjFDPVegulg36sYlv/zd/0bVtmejAhNaTsXpFMD8/3 WL4g== X-Gm-Message-State: AOAM5324E16Pcy36zhYFd84MTzG0XCnQ/mVEFnWsaz1Fwv0uTw8zIW30 gL94Byfr1a17DQtVkzGy21U/brwg387xvg== X-Google-Smtp-Source: ABdhPJyXm0cjyduUXfoNdBUdFcZegfy3M7oRcIKpfDBeFxpI7TSBT+0IFmvTgcYRVtSIeEywn6iYf0NBEdI9cw== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a0c:99c8:: with SMTP id y8mr8958361qve.35.1607651537893; Thu, 10 Dec 2020 17:52:17 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:52 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-4-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 3/7] hw/adc: Add an ADC module for NPCM7XX To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::f4a; envelope-from=30dDSXwgKCsI42piw10powwotm.kwuymu2-lm3mtvwvov2.wzo@flex--wuhaotsh.bounces.google.com; helo=mail-qv1-xf4a.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ADC is part of NPCM7XX Module. Its behavior is controled by the ADC_CON register. It converts one of the eight analog inputs into a digital input and stores it in the ADC_DATA register when enabled. Reviewed-by: Havard Skinnemoen Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- docs/system/arm/nuvoton.rst | 2 +- hw/adc/meson.build | 1 + hw/adc/npcm7xx_adc.c | 318 ++++++++++++++++++++++++++ hw/arm/npcm7xx.c | 24 +- include/hw/adc/npcm7xx_adc.h | 72 ++++++ include/hw/arm/npcm7xx.h | 2 + tests/qtest/meson.build | 3 +- tests/qtest/npcm7xx_adc-test.c | 400 +++++++++++++++++++++++++++++++++ 8 files changed, 819 insertions(+), 3 deletions(-) create mode 100644 hw/adc/npcm7xx_adc.c create mode 100644 include/hw/adc/npcm7xx_adc.h create mode 100644 tests/qtest/npcm7xx_adc-test.c diff --git a/docs/system/arm/nuvoton.rst b/docs/system/arm/nuvoton.rst index b00d405d52..35829f8d0b 100644 --- a/docs/system/arm/nuvoton.rst +++ b/docs/system/arm/nuvoton.rst @@ -41,6 +41,7 @@ Supported devices * Random Number Generator (RNG) * USB host (USBH) * GPIO controller + * Analog to Digital Converter (ADC) =20 Missing devices --------------- @@ -58,7 +59,6 @@ Missing devices * USB device (USBD) * SMBus controller (SMBF) * Peripheral SPI controller (PSPI) - * Analog to Digital Converter (ADC) * SD/MMC host * PECI interface * Pulse Width Modulation (PWM) diff --git a/hw/adc/meson.build b/hw/adc/meson.build index 0d62ae96ae..6ddee23813 100644 --- a/hw/adc/meson.build +++ b/hw/adc/meson.build @@ -1 +1,2 @@ softmmu_ss.add(when: 'CONFIG_STM32F2XX_ADC', if_true: files('stm32f2xx_adc= .c')) +softmmu_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_adc.c')) diff --git a/hw/adc/npcm7xx_adc.c b/hw/adc/npcm7xx_adc.c new file mode 100644 index 0000000000..4492303977 --- /dev/null +++ b/hw/adc/npcm7xx_adc.c @@ -0,0 +1,318 @@ +/* + * Nuvoton NPCM7xx ADC Module + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "hw/adc/npcm7xx_adc.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "migration/vmstate.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/timer.h" +#include "qemu/units.h" + +/* 32-bit register indices. */ +enum NPCM7xxADCRegisters { + NPCM7XX_ADC_CON, + NPCM7XX_ADC_DATA, + NPCM7XX_ADC_REGS_END, +}; + +/* Register field definitions. */ +#define NPCM7XX_ADC_CON_MUX(rv) extract32(rv, 24, 4) +#define NPCM7XX_ADC_CON_INT_EN BIT(21) +#define NPCM7XX_ADC_CON_REFSEL BIT(19) +#define NPCM7XX_ADC_CON_INT BIT(18) +#define NPCM7XX_ADC_CON_EN BIT(17) +#define NPCM7XX_ADC_CON_RST BIT(16) +#define NPCM7XX_ADC_CON_CONV BIT(14) +#define NPCM7XX_ADC_CON_DIV(rv) extract32(rv, 1, 8) + +#define NPCM7XX_ADC_MAX_RESULT 1023 +#define NPCM7XX_ADC_DEFAULT_IREF 2000000 +#define NPCM7XX_ADC_CONV_CYCLES 20 +#define NPCM7XX_ADC_RESET_CYCLES 10 +#define NPCM7XX_ADC_R0_INPUT 500000 +#define NPCM7XX_ADC_R1_INPUT 1500000 + +static void npcm7xx_adc_reset(NPCM7xxADCState *s) +{ + timer_del(&s->conv_timer); + timer_del(&s->reset_timer); + s->con =3D 0x000c0001; + s->data =3D 0x00000000; +} + +static uint32_t npcm7xx_adc_convert(uint32_t input, uint32_t ref) +{ + uint32_t result; + + result =3D input * (NPCM7XX_ADC_MAX_RESULT + 1) / ref; + if (result > NPCM7XX_ADC_MAX_RESULT) { + result =3D NPCM7XX_ADC_MAX_RESULT; + } + + return result; +} + +static uint32_t npcm7xx_adc_prescaler(NPCM7xxADCState *s) +{ + return 2 * (NPCM7XX_ADC_CON_DIV(s->con) + 1); +} + +static void npcm7xx_adc_start_timer(Clock *clk, QEMUTimer *timer, + uint32_t cycles, uint32_t prescaler) +{ + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + int64_t freq =3D clock_get_hz(clk); + int64_t ns; + + ns =3D (NANOSECONDS_PER_SECOND * cycles * prescaler / freq); + ns +=3D now; + timer_mod(timer, ns); +} + +static void npcm7xx_adc_start_reset(NPCM7xxADCState *s) +{ + uint32_t prescaler =3D npcm7xx_adc_prescaler(s); + + npcm7xx_adc_start_timer(s->clock, &s->reset_timer, NPCM7XX_ADC_RESET_C= YCLES, + prescaler); +} + +static void npcm7xx_adc_start_convert(NPCM7xxADCState *s) +{ + uint32_t prescaler =3D npcm7xx_adc_prescaler(s); + + npcm7xx_adc_start_timer(s->clock, &s->conv_timer, NPCM7XX_ADC_CONV_CYC= LES, + prescaler); +} + +static void npcm7xx_adc_reset_done(void *opaque) +{ + NPCM7xxADCState *s =3D opaque; + + npcm7xx_adc_reset(s); +} + +static void npcm7xx_adc_convert_done(void *opaque) +{ + NPCM7xxADCState *s =3D opaque; + uint32_t input =3D NPCM7XX_ADC_CON_MUX(s->con); + uint32_t ref =3D (s->con & NPCM7XX_ADC_CON_REFSEL) + ? s->iref : s->vref; + + g_assert(input < NPCM7XX_ADC_NUM_INPUTS); + s->data =3D npcm7xx_adc_convert(s->adci[input], ref); + if (s->con & NPCM7XX_ADC_CON_INT_EN) { + s->con |=3D NPCM7XX_ADC_CON_INT; + qemu_irq_raise(s->irq); + } + s->con &=3D ~NPCM7XX_ADC_CON_CONV; +} + +static void npcm7xx_adc_calibrate(NPCM7xxADCState *adc) +{ + adc->calibration_r_values[0] =3D npcm7xx_adc_convert(NPCM7XX_ADC_R0_IN= PUT, + adc->iref); + adc->calibration_r_values[1] =3D npcm7xx_adc_convert(NPCM7XX_ADC_R1_IN= PUT, + adc->iref); +} + +static void npcm7xx_adc_write_con(NPCM7xxADCState *s, uint32_t new_con) +{ + uint32_t old_con =3D s->con; + + /* Write ADC_INT to 1 to clear it */ + if (new_con & NPCM7XX_ADC_CON_INT) { + new_con &=3D ~NPCM7XX_ADC_CON_INT; + } else if (old_con & NPCM7XX_ADC_CON_INT) { + new_con |=3D NPCM7XX_ADC_CON_INT; + } + + s->con =3D new_con; + + if (s->con & NPCM7XX_ADC_CON_RST) { + if (!(old_con & NPCM7XX_ADC_CON_RST)) { + npcm7xx_adc_start_reset(s); + } + } else { + timer_del(&s->reset_timer); + } + + if ((s->con & NPCM7XX_ADC_CON_EN)) { + if (s->con & NPCM7XX_ADC_CON_CONV) { + if (!(old_con & NPCM7XX_ADC_CON_CONV)) { + npcm7xx_adc_start_convert(s); + } + } else { + timer_del(&s->conv_timer); + } + } +} + +static uint64_t npcm7xx_adc_read(void *opaque, hwaddr offset, unsigned siz= e) +{ + uint64_t value =3D 0; + NPCM7xxADCState *s =3D opaque; + hwaddr reg =3D offset / sizeof(uint32_t); + + switch (reg) { + case NPCM7XX_ADC_CON: + value =3D s->con; + break; + + case NPCM7XX_ADC_DATA: + value =3D s->data; + break; + + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid offset 0x%04" HWADDR_PRIx "\n", + __func__, offset); + break; + } + + return value; +} + +static void npcm7xx_adc_write(void *opaque, hwaddr offset, uint64_t v, + unsigned size) +{ + NPCM7xxADCState *s =3D opaque; + hwaddr reg =3D offset / sizeof(uint32_t); + + switch (reg) { + case NPCM7XX_ADC_CON: + npcm7xx_adc_write_con(s, v); + break; + + case NPCM7XX_ADC_DATA: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: register @ 0x%04" HWADDR_PRIx " is read-only\n", + __func__, offset); + break; + + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid offset 0x%04" HWADDR_PRIx "\n", + __func__, offset); + break; + } + +} + +static const struct MemoryRegionOps npcm7xx_adc_ops =3D { + .read =3D npcm7xx_adc_read, + .write =3D npcm7xx_adc_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + .unaligned =3D false, + }, +}; + +static void npcm7xx_adc_enter_reset(Object *obj, ResetType type) +{ + NPCM7xxADCState *s =3D NPCM7XX_ADC(obj); + + npcm7xx_adc_reset(s); +} + +static void npcm7xx_adc_hold_reset(Object *obj) +{ + NPCM7xxADCState *s =3D NPCM7XX_ADC(obj); + + qemu_irq_lower(s->irq); +} + +static void npcm7xx_adc_init(Object *obj) +{ + NPCM7xxADCState *s =3D NPCM7XX_ADC(obj); + SysBusDevice *sbd =3D &s->parent; + int i; + + sysbus_init_irq(sbd, &s->irq); + + timer_init_ns(&s->conv_timer, QEMU_CLOCK_VIRTUAL, + npcm7xx_adc_convert_done, s); + timer_init_ns(&s->reset_timer, QEMU_CLOCK_VIRTUAL, + npcm7xx_adc_reset_done, s); + memory_region_init_io(&s->iomem, obj, &npcm7xx_adc_ops, s, + TYPE_NPCM7XX_ADC, 4 * KiB); + sysbus_init_mmio(sbd, &s->iomem); + s->clock =3D qdev_init_clock_in(DEVICE(s), "clock", NULL, NULL); + + for (i =3D 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) { + object_property_add_uint32_ptr(obj, "adci[*]", + &s->adci[i], OBJ_PROP_FLAG_WRITE); + } + object_property_add_uint32_ptr(obj, "vref", + &s->vref, OBJ_PROP_FLAG_WRITE); + npcm7xx_adc_calibrate(s); +} + +static const VMStateDescription vmstate_npcm7xx_adc =3D { + .name =3D "npcm7xx-adc", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_TIMER(conv_timer, NPCM7xxADCState), + VMSTATE_TIMER(reset_timer, NPCM7xxADCState), + VMSTATE_UINT32(con, NPCM7xxADCState), + VMSTATE_UINT32(data, NPCM7xxADCState), + VMSTATE_CLOCK(clock, NPCM7xxADCState), + VMSTATE_UINT32_ARRAY(adci, NPCM7xxADCState, NPCM7XX_ADC_NUM_INPUTS= ), + VMSTATE_UINT32(vref, NPCM7xxADCState), + VMSTATE_UINT32(iref, NPCM7xxADCState), + VMSTATE_UINT16_ARRAY(calibration_r_values, NPCM7xxADCState, + NPCM7XX_ADC_NUM_CALIB), + VMSTATE_END_OF_LIST(), + }, +}; + +static Property npcm7xx_timer_properties[] =3D { + DEFINE_PROP_UINT32("iref", NPCM7xxADCState, iref, NPCM7XX_ADC_DEFAULT_= IREF), + DEFINE_PROP_END_OF_LIST(), +}; + +static void npcm7xx_adc_class_init(ObjectClass *klass, void *data) +{ + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->desc =3D "NPCM7xx ADC Module"; + dc->vmsd =3D &vmstate_npcm7xx_adc; + rc->phases.enter =3D npcm7xx_adc_enter_reset; + rc->phases.hold =3D npcm7xx_adc_hold_reset; + + device_class_set_props(dc, npcm7xx_timer_properties); +} + +static const TypeInfo npcm7xx_adc_info =3D { + .name =3D TYPE_NPCM7XX_ADC, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(NPCM7xxADCState), + .class_init =3D npcm7xx_adc_class_init, + .instance_init =3D npcm7xx_adc_init, +}; + +static void npcm7xx_adc_register_types(void) +{ + type_register_static(&npcm7xx_adc_info); +} + +type_init(npcm7xx_adc_register_types); diff --git a/hw/arm/npcm7xx.c b/hw/arm/npcm7xx.c index fabfb1697b..b22a8c966d 100644 --- a/hw/arm/npcm7xx.c +++ b/hw/arm/npcm7xx.c @@ -51,6 +51,9 @@ #define NPCM7XX_EHCI_BA (0xf0806000) #define NPCM7XX_OHCI_BA (0xf0807000) =20 +/* ADC Module */ +#define NPCM7XX_ADC_BA (0xf000c000) + /* Internal AHB SRAM */ #define NPCM7XX_RAM3_BA (0xc0008000) #define NPCM7XX_RAM3_SZ (4 * KiB) @@ -61,6 +64,7 @@ #define NPCM7XX_ROM_BA (0xffff0000) #define NPCM7XX_ROM_SZ (64 * KiB) =20 + /* Clock configuration values to be fixed up when bypassing bootloader */ =20 /* Run PLL1 at 1600 MHz */ @@ -73,6 +77,7 @@ * interrupts. */ enum NPCM7xxInterrupt { + NPCM7XX_ADC_IRQ =3D 0, NPCM7XX_UART0_IRQ =3D 2, NPCM7XX_UART1_IRQ, NPCM7XX_UART2_IRQ, @@ -296,6 +301,14 @@ static void npcm7xx_init_fuses(NPCM7xxState *s) sizeof(value)); } =20 +static void npcm7xx_write_adc_calibration(NPCM7xxState *s) +{ + /* Both ADC and the fuse array must have realized. */ + QEMU_BUILD_BUG_ON(sizeof(s->adc.calibration_r_values) !=3D 4); + npcm7xx_otp_array_write(&s->fuse_array, s->adc.calibration_r_values, + NPCM7XX_FUSE_ADC_CALIB, sizeof(s->adc.calibration_r_values)); +} + static qemu_irq npcm7xx_irq(NPCM7xxState *s, int n) { return qdev_get_gpio_in(DEVICE(&s->a9mpcore), n); @@ -322,6 +335,7 @@ static void npcm7xx_init(Object *obj) TYPE_NPCM7XX_FUSE_ARRAY); object_initialize_child(obj, "mc", &s->mc, TYPE_NPCM7XX_MC); object_initialize_child(obj, "rng", &s->rng, TYPE_NPCM7XX_RNG); + object_initialize_child(obj, "adc", &s->adc, TYPE_NPCM7XX_ADC); =20 for (i =3D 0; i < ARRAY_SIZE(s->tim); i++) { object_initialize_child(obj, "tim[*]", &s->tim[i], TYPE_NPCM7XX_TI= MER); @@ -414,6 +428,15 @@ static void npcm7xx_realize(DeviceState *dev, Error **= errp) sysbus_realize(SYS_BUS_DEVICE(&s->mc), &error_abort); sysbus_mmio_map(SYS_BUS_DEVICE(&s->mc), 0, NPCM7XX_MC_BA); =20 + /* ADC Modules. Cannot fail. */ + qdev_connect_clock_in(DEVICE(&s->adc), "clock", qdev_get_clock_out( + DEVICE(&s->clk), "adc-clock")); + sysbus_realize(SYS_BUS_DEVICE(&s->adc), &error_abort); + sysbus_mmio_map(SYS_BUS_DEVICE(&s->adc), 0, NPCM7XX_ADC_BA); + sysbus_connect_irq(SYS_BUS_DEVICE(&s->adc), 0, + npcm7xx_irq(s, NPCM7XX_ADC_IRQ)); + npcm7xx_write_adc_calibration(s); + /* Timer Modules (TIM). Cannot fail. */ QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_tim_addr) !=3D ARRAY_SIZE(s->tim)= ); for (i =3D 0; i < ARRAY_SIZE(s->tim); i++) { @@ -528,7 +551,6 @@ static void npcm7xx_realize(DeviceState *dev, Error **e= rrp) create_unimplemented_device("npcm7xx.vdmx", 0xe0800000, 4 * = KiB); create_unimplemented_device("npcm7xx.pcierc", 0xe1000000, 64 * = KiB); create_unimplemented_device("npcm7xx.kcs", 0xf0007000, 4 * = KiB); - create_unimplemented_device("npcm7xx.adc", 0xf000c000, 4 * = KiB); create_unimplemented_device("npcm7xx.gfxi", 0xf000e000, 4 * = KiB); create_unimplemented_device("npcm7xx.gpio[0]", 0xf0010000, 4 * = KiB); create_unimplemented_device("npcm7xx.gpio[1]", 0xf0011000, 4 * = KiB); diff --git a/include/hw/adc/npcm7xx_adc.h b/include/hw/adc/npcm7xx_adc.h new file mode 100644 index 0000000000..7f9acbeaa1 --- /dev/null +++ b/include/hw/adc/npcm7xx_adc.h @@ -0,0 +1,72 @@ +/* + * Nuvoton NPCM7xx ADC Module + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ +#ifndef NPCM7XX_ADC_H +#define NPCM7XX_ADC_H + +#include "qemu/osdep.h" +#include "hw/clock.h" +#include "hw/irq.h" +#include "hw/sysbus.h" +#include "qemu/timer.h" + +#define NPCM7XX_ADC_NUM_INPUTS 8 +/** + * This value should not be changed unless write_adc_calibration function = in + * hw/arm/npcm7xx.c is also changed. + */ +#define NPCM7XX_ADC_NUM_CALIB 2 + +/** + * struct NPCM7xxADCState - Analog to Digital Converter Module device stat= e. + * @parent: System bus device. + * @iomem: Memory region through which registers are accessed. + * @conv_timer: The timer counts down remaining cycles for the conversion. + * @reset_timer: The timer counts down remaining cycles for reset. + * @irq: GIC interrupt line to fire on expiration (if enabled). + * @con: The Control Register. + * @data: The Data Buffer. + * @clock: The ADC Clock. + * @adci: The input voltage in units of uV. 1uv =3D 1e-6V. + * @vref: The external reference voltage. + * @iref: The internal reference voltage, initialized at launch time. + * @rv: The calibrated output values of 0.5V and 1.5V for the ADC. + */ +typedef struct { + SysBusDevice parent; + + MemoryRegion iomem; + + QEMUTimer conv_timer; + QEMUTimer reset_timer; + + qemu_irq irq; + uint32_t con; + uint32_t data; + Clock *clock; + + /* Voltages are in unit of uV. 1V =3D 1000000uV. */ + uint32_t adci[NPCM7XX_ADC_NUM_INPUTS]; + uint32_t vref; + uint32_t iref; + + uint16_t calibration_r_values[NPCM7XX_ADC_NUM_CALIB]; +} NPCM7xxADCState; + +#define TYPE_NPCM7XX_ADC "npcm7xx-adc" +#define NPCM7XX_ADC(obj) \ + OBJECT_CHECK(NPCM7xxADCState, (obj), TYPE_NPCM7XX_ADC) + +#endif /* NPCM7XX_ADC_H */ diff --git a/include/hw/arm/npcm7xx.h b/include/hw/arm/npcm7xx.h index 5469247e38..51e1c7620d 100644 --- a/include/hw/arm/npcm7xx.h +++ b/include/hw/arm/npcm7xx.h @@ -17,6 +17,7 @@ #define NPCM7XX_H =20 #include "hw/boards.h" +#include "hw/adc/npcm7xx_adc.h" #include "hw/cpu/a9mpcore.h" #include "hw/gpio/npcm7xx_gpio.h" #include "hw/mem/npcm7xx_mc.h" @@ -76,6 +77,7 @@ typedef struct NPCM7xxState { NPCM7xxGCRState gcr; NPCM7xxCLKState clk; NPCM7xxTimerCtrlState tim[3]; + NPCM7xxADCState adc; NPCM7xxOTPState key_storage; NPCM7xxOTPState fuse_array; NPCM7xxMCState mc; diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 6a67c538be..955710d1c5 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -134,7 +134,8 @@ qtests_sparc64 =3D \ ['prom-env-test', 'boot-serial-test'] =20 qtests_npcm7xx =3D \ - ['npcm7xx_gpio-test', + ['npcm7xx_adc-test', + 'npcm7xx_gpio-test', 'npcm7xx_rng-test', 'npcm7xx_timer-test', 'npcm7xx_watchdog_timer-test'] diff --git a/tests/qtest/npcm7xx_adc-test.c b/tests/qtest/npcm7xx_adc-test.c new file mode 100644 index 0000000000..e63c544e51 --- /dev/null +++ b/tests/qtest/npcm7xx_adc-test.c @@ -0,0 +1,400 @@ +/* + * QTests for Nuvoton NPCM7xx ADCModules. + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" +#include "qemu/bitops.h" +#include "qemu/timer.h" +#include "libqos/libqtest.h" +#include "qapi/qmp/qdict.h" + +#define REF_HZ (25000000) + +#define CON_OFFSET 0x0 +#define DATA_OFFSET 0x4 + +#define NUM_INPUTS 8 +#define DEFAULT_IREF 2000000 +#define CONV_CYCLES 20 +#define RESET_CYCLES 10 +#define R0_INPUT 500000 +#define R1_INPUT 1500000 +#define MAX_RESULT 1023 + +#define DEFAULT_CLKDIV 5 + +#define FUSE_ARRAY_BA 0xf018a000 +#define FCTL_OFFSET 0x14 +#define FST_OFFSET 0x0 +#define FADDR_OFFSET 0x4 +#define FDATA_OFFSET 0x8 +#define ADC_CALIB_ADDR 24 +#define FUSE_READ 0x2 + +/* Register field definitions. */ +#define CON_MUX(rv) ((rv) << 24) +#define CON_INT_EN BIT(21) +#define CON_REFSEL BIT(19) +#define CON_INT BIT(18) +#define CON_EN BIT(17) +#define CON_RST BIT(16) +#define CON_CONV BIT(14) +#define CON_DIV(rv) extract32(rv, 1, 8) + +#define FST_RDST BIT(1) +#define FDATA_MASK 0xff + +#define MAX_ERROR 10000 +#define MIN_CALIB_INPUT 100000 +#define MAX_CALIB_INPUT 1800000 + +static const uint32_t input_list[] =3D { + 100000, + 500000, + 1000000, + 1500000, + 1800000, + 2000000, +}; + +static const uint32_t vref_list[] =3D { + 2000000, + 2200000, + 2500000, +}; + +static const uint32_t iref_list[] =3D { + 1800000, + 1900000, + 2000000, + 2100000, + 2200000, +}; + +static const uint32_t div_list[] =3D {0, 1, 3, 7, 15}; + +typedef struct ADC { + int irq; + uint64_t base_addr; +} ADC; + +ADC adc =3D { + .irq =3D 0, + .base_addr =3D 0xf000c000 +}; + +static uint32_t adc_read_con(QTestState *qts, const ADC *adc) +{ + return qtest_readl(qts, adc->base_addr + CON_OFFSET); +} + +static void adc_write_con(QTestState *qts, const ADC *adc, uint32_t value) +{ + qtest_writel(qts, adc->base_addr + CON_OFFSET, value); +} + +static uint32_t adc_read_data(QTestState *qts, const ADC *adc) +{ + return qtest_readl(qts, adc->base_addr + DATA_OFFSET); +} + +static uint32_t adc_calibrate(uint32_t measured, uint32_t *rv) +{ + return R0_INPUT + (R1_INPUT - R0_INPUT) * (int32_t)(measured - rv[0]) + / (int32_t)(rv[1] - rv[0]); +} + +static void adc_qom_set(QTestState *qts, const ADC *adc, + const char *name, uint32_t value) +{ + QDict *response; + const char *path =3D "/machine/soc/adc"; + + g_test_message("Setting properties %s of %s with value %u", + name, path, value); + response =3D qtest_qmp(qts, "{ 'execute': 'qom-set'," + " 'arguments': { 'path': %s, 'property': %s, 'value': %u}}", + path, name, value); + /* The qom set message returns successfully. */ + g_assert_true(qdict_haskey(response, "return")); +} + +static void adc_write_input(QTestState *qts, const ADC *adc, + uint32_t index, uint32_t value) +{ + char name[100]; + + sprintf(name, "adci[%u]", index); + adc_qom_set(qts, adc, name, value); +} + +static void adc_write_vref(QTestState *qts, const ADC *adc, uint32_t value) +{ + adc_qom_set(qts, adc, "vref", value); +} + +static uint32_t adc_calculate_output(uint32_t input, uint32_t ref) +{ + uint32_t output; + + g_assert_cmpuint(input, <=3D, ref); + output =3D (input * (MAX_RESULT + 1)) / ref; + if (output > MAX_RESULT) { + output =3D MAX_RESULT; + } + + return output; +} + +static uint32_t adc_prescaler(QTestState *qts, const ADC *adc) +{ + uint32_t div =3D extract32(adc_read_con(qts, adc), 1, 8); + + return 2 * (div + 1); +} + +static int64_t adc_calculate_steps(uint32_t cycles, uint32_t prescale, + uint32_t clkdiv) +{ + return (NANOSECONDS_PER_SECOND / (REF_HZ >> clkdiv)) * cycles * presca= le; +} + +static void adc_wait_conv_finished(QTestState *qts, const ADC *adc, + uint32_t clkdiv) +{ + uint32_t prescaler =3D adc_prescaler(qts, adc); + + /* + * ADC should takes roughly 20 cycles to convert one sample. So we ass= ert it + * should take 10~30 cycles here. + */ + qtest_clock_step(qts, adc_calculate_steps(CONV_CYCLES / 2, prescaler, + clkdiv)); + /* ADC is still converting. */ + g_assert_true(adc_read_con(qts, adc) & CON_CONV); + qtest_clock_step(qts, adc_calculate_steps(CONV_CYCLES, prescaler, clkd= iv)); + /* ADC has finished conversion. */ + g_assert_false(adc_read_con(qts, adc) & CON_CONV); +} + +/* Check ADC can be reset to default value. */ +static void test_init(gconstpointer adc_p) +{ + const ADC *adc =3D adc_p; + + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + adc_write_con(qts, adc, CON_REFSEL | CON_INT); + g_assert_cmphex(adc_read_con(qts, adc), =3D=3D, CON_REFSEL); + qtest_quit(qts); +} + +/* Check ADC can convert from an internal reference. */ +static void test_convert_internal(gconstpointer adc_p) +{ + const ADC *adc =3D adc_p; + uint32_t index, input, output, expected_output; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + qtest_irq_intercept_in(qts, "/machine/soc/a9mpcore/gic"); + + for (index =3D 0; index < NUM_INPUTS; ++index) { + for (size_t i =3D 0; i < ARRAY_SIZE(input_list); ++i) { + input =3D input_list[i]; + expected_output =3D adc_calculate_output(input, DEFAULT_IREF); + + adc_write_input(qts, adc, index, input); + adc_write_con(qts, adc, CON_MUX(index) | CON_REFSEL | CON_INT | + CON_EN | CON_CONV); + adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV); + g_assert_cmphex(adc_read_con(qts, adc), =3D=3D, CON_MUX(index)= | + CON_REFSEL | CON_EN); + g_assert_false(qtest_get_irq(qts, adc->irq)); + output =3D adc_read_data(qts, adc); + g_assert_cmpuint(output, =3D=3D, expected_output); + } + } + + qtest_quit(qts); +} + +/* Check ADC can convert from an external reference. */ +static void test_convert_external(gconstpointer adc_p) +{ + const ADC *adc =3D adc_p; + uint32_t index, input, vref, output, expected_output; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + qtest_irq_intercept_in(qts, "/machine/soc/a9mpcore/gic"); + + for (index =3D 0; index < NUM_INPUTS; ++index) { + for (size_t i =3D 0; i < ARRAY_SIZE(input_list); ++i) { + for (size_t j =3D 0; j < ARRAY_SIZE(vref_list); ++j) { + input =3D input_list[i]; + vref =3D vref_list[j]; + expected_output =3D adc_calculate_output(input, vref); + + adc_write_input(qts, adc, index, input); + adc_write_vref(qts, adc, vref); + adc_write_con(qts, adc, CON_MUX(index) | CON_INT | CON_EN | + CON_CONV); + adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV); + g_assert_cmphex(adc_read_con(qts, adc), =3D=3D, + CON_MUX(index) | CON_EN); + g_assert_false(qtest_get_irq(qts, adc->irq)); + output =3D adc_read_data(qts, adc); + g_assert_cmpuint(output, =3D=3D, expected_output); + } + } + } + + qtest_quit(qts); +} + +/* Check ADC interrupt files if and only if CON_INT_EN is set. */ +static void test_interrupt(gconstpointer adc_p) +{ + const ADC *adc =3D adc_p; + uint32_t index, input, output, expected_output; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + + index =3D 1; + input =3D input_list[1]; + expected_output =3D adc_calculate_output(input, DEFAULT_IREF); + + qtest_irq_intercept_in(qts, "/machine/soc/a9mpcore/gic"); + adc_write_input(qts, adc, index, input); + g_assert_false(qtest_get_irq(qts, adc->irq)); + adc_write_con(qts, adc, CON_MUX(index) | CON_INT_EN | CON_REFSEL | CON= _INT + | CON_EN | CON_CONV); + adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV); + g_assert_cmphex(adc_read_con(qts, adc), =3D=3D, CON_MUX(index) | CON_I= NT_EN + | CON_REFSEL | CON_INT | CON_EN); + g_assert_true(qtest_get_irq(qts, adc->irq)); + output =3D adc_read_data(qts, adc); + g_assert_cmpuint(output, =3D=3D, expected_output); + + qtest_quit(qts); +} + +/* Check ADC is reset after setting ADC_RST for 10 ADC cycles. */ +static void test_reset(gconstpointer adc_p) +{ + const ADC *adc =3D adc_p; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + + for (size_t i =3D 0; i < ARRAY_SIZE(div_list); ++i) { + uint32_t div =3D div_list[i]; + + adc_write_con(qts, adc, CON_INT | CON_EN | CON_RST | CON_DIV(div)); + qtest_clock_step(qts, adc_calculate_steps(RESET_CYCLES, + adc_prescaler(qts, adc), DEFAULT_CLKDIV) - 1); + g_assert_true(adc_read_con(qts, adc) & CON_EN); + qtest_clock_step(qts, 1); + g_assert_false(adc_read_con(qts, adc) & CON_EN); + } + qtest_quit(qts); +} + +/* Check ADC is not reset if we set ADC_RST for <10 ADC cycles. */ +static void test_premature_reset(gconstpointer adc_p) +{ + const ADC *adc =3D adc_p; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + + for (size_t i =3D 0; i < ARRAY_SIZE(div_list); ++i) { + uint32_t div =3D div_list[i]; + + adc_write_con(qts, adc, CON_INT | CON_EN | CON_RST | CON_DIV(div)); + qtest_clock_step(qts, adc_calculate_steps(RESET_CYCLES, + adc_prescaler(qts, adc), DEFAULT_CLKDIV) - 1); + g_assert_true(adc_read_con(qts, adc) & CON_EN); + adc_write_con(qts, adc, CON_INT | CON_EN | CON_DIV(div)); + qtest_clock_step(qts, 1000); + g_assert_true(adc_read_con(qts, adc) & CON_EN); + } + qtest_quit(qts); +} + +/* Check ADC Calibration works as desired. */ +static void test_calibrate(gconstpointer adc_p) +{ + int i, j; + const ADC *adc =3D adc_p; + + for (j =3D 0; j < ARRAY_SIZE(iref_list); ++j) { + uint32_t iref =3D iref_list[j]; + uint32_t expected_rv[] =3D { + adc_calculate_output(R0_INPUT, iref), + adc_calculate_output(R1_INPUT, iref), + }; + char buf[100]; + QTestState *qts; + + sprintf(buf, "-machine quanta-gsj -global npcm7xx-adc.iref=3D%u", = iref); + qts =3D qtest_init(buf); + + /* Check the converted value is correct using the calibration valu= e. */ + for (i =3D 0; i < ARRAY_SIZE(input_list); ++i) { + uint32_t input; + uint32_t output; + uint32_t expected_output; + uint32_t calibrated_voltage; + uint32_t index =3D 0; + + input =3D input_list[i]; + /* Calibration only works for input range 0.1V ~ 1.8V. */ + if (input < MIN_CALIB_INPUT || input > MAX_CALIB_INPUT) { + continue; + } + expected_output =3D adc_calculate_output(input, iref); + + adc_write_input(qts, adc, index, input); + adc_write_con(qts, adc, CON_MUX(index) | CON_REFSEL | CON_INT | + CON_EN | CON_CONV); + adc_wait_conv_finished(qts, adc, DEFAULT_CLKDIV); + g_assert_cmphex(adc_read_con(qts, adc), =3D=3D, + CON_REFSEL | CON_MUX(index) | CON_EN); + output =3D adc_read_data(qts, adc); + g_assert_cmpuint(output, =3D=3D, expected_output); + + calibrated_voltage =3D adc_calibrate(output, expected_rv); + g_assert_cmpuint(calibrated_voltage, >, input - MAX_ERROR); + g_assert_cmpuint(calibrated_voltage, <, input + MAX_ERROR); + } + + qtest_quit(qts); + } +} + +static void adc_add_test(const char *name, const ADC* wd, + GTestDataFunc fn) +{ + g_autofree char *full_name =3D g_strdup_printf("npcm7xx_adc/%s", name= ); + qtest_add_data_func(full_name, wd, fn); +} +#define add_test(name, td) adc_add_test(#name, td, test_##name) + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + add_test(init, &adc); + add_test(convert_internal, &adc); + add_test(convert_external, &adc); + add_test(interrupt, &adc); + add_test(reset, &adc); + add_test(premature_reset, &adc); + add_test(calibrate, &adc); + + return g_test_run(); +} --=20 2.29.2.684.gfbc64c5ab5-goog From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607651668; cv=none; d=zohomail.com; s=zohoarc; b=LTcvLeLli12PGb4bJRdP7mlv5ninf4oSP38iE9j9PtFb7xSZkyRQgatTy2sqj2gG7s0UVjaaxtQ3j4akSctN24E15DsJGZSsigXKBz7zAGU51n2wX3weSjMd4GgXNaz6fIDPmrQa/J5b+UdK2jlzcBIRO3ZXvqvygPf73S/4jDU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607651668; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=MsF+FCr9oTX2SC1xIYssCKl/y7N0YEXtQGB/Tub/9Bw=; b=XLRXd8wiiP7J4DMXbqZ3gKJnhdKluvX6Urq8O8A8QMcENBoWHZGHA49w8xaLj77617bRXa4Zvm1X8XkSahKSiVJYEbxdteyUidjMdT0WxUy0cB7kF1LqSRv9dk18G8di4rQq+dPRCK4ZIUn+psxz+H+uaKd6VncDTbPJsPuiG0g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607651668359983.9534603614994; Thu, 10 Dec 2020 17:54:28 -0800 (PST) Received: from localhost ([::1]:33146 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXda-00037g-S8 for importer@patchew.org; Thu, 10 Dec 2020 20:54:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <309DSXwgKCsQ64rky32rqyyqvo.myw0ow4-no5ovxyxqx4.y1q@flex--wuhaotsh.bounces.google.com>) id 1knXbd-0001fZ-FL for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:25 -0500 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]:37340) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <309DSXwgKCsQ64rky32rqyyqvo.myw0ow4-no5ovxyxqx4.y1q@flex--wuhaotsh.bounces.google.com>) id 1knXbZ-0007A8-EK for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:25 -0500 Received: by mail-pl1-x649.google.com with SMTP id 1so4485310plb.4 for ; Thu, 10 Dec 2020 17:52:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=MsF+FCr9oTX2SC1xIYssCKl/y7N0YEXtQGB/Tub/9Bw=; b=EL/JKpQde/+ouzuaX3K8uKAAb3g+fthrC2I39fvuBW8HwYNSfxgVPuVuRsze3hXtwZ ZJkgoGMUCNR7z5Zrr8l4AkHHN52FPvck53FilsYw4itEaw262+qAp3fA9+ZwrtxcGrJo RJmeMNQxPRiIquvqwwkVzFGdlcAKKjKEexoUQ2mzo+pJjOeE0Vx5mD8iD+pmMWIDRXXD 52F4HTbf6cTA2R4ugtOlPI0ZFn4e8tAdjlUhgt8wpPa+ci2fXH20PFFmzTBjgrSRRNAo IRbmbN1aR8kCH8e88psHl4VQRlabsJMs5BRTGztxuwdgesslPH8248JLXKagJUrP4f7u bIPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=MsF+FCr9oTX2SC1xIYssCKl/y7N0YEXtQGB/Tub/9Bw=; b=cAx4AvaoVRu1NbgK2qxcZMwDrb7UGngCeGp2sHqqQOBEiFuhIOqK0tTV05pZIk5LYL cTbn1HDuR7yChTjfnDcLxLzJJR9sNl6aVaUyQfAUqFBFn++HxJxh+L/cHvUwXQQaYja7 rA/LHfTR6hHfJFarWXws4tbj/qaTOu638V1heDZB0rKniccvEVDrMUpDqCjryjjUgdm0 u2wnC1hE1qU3MuURIXWG3ZoFh/vFIW3jj/j2AIl9nNZ2bsRR3Wcxft8+pr3DSxE6bADw UcjjU97fuZfK60kOQ+FkauE+DuIwCuXmcH/Ac+f1AwspXtrCHtXqivmZATWVMYrlzSP/ fUbg== X-Gm-Message-State: AOAM531wto033AMncAV9H0rTV+6Bz92ithTns9KMHoI63tH58di+X/m2 wQC7gWu7pKzG6iCL9FVThrRHfsOqMxkiJg== X-Google-Smtp-Source: ABdhPJyMPqo0DBuk/WsbiN5CTGY2EHuSMuy3a4qw26tjbMnrhhLK4pHmq7BfTGFPr57889U2OfUhnP+/J08B7Q== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a17:902:b406:b029:db:3c3:e4cd with SMTP id x6-20020a170902b406b02900db03c3e4cdmr8732108plr.79.1607651539537; Thu, 10 Dec 2020 17:52:19 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:53 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-5-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 4/7] hw/misc: Add a PWM module for NPCM7XX To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::649; envelope-from=309DSXwgKCsQ64rky32rqyyqvo.myw0ow4-no5ovxyxqx4.y1q@flex--wuhaotsh.bounces.google.com; helo=mail-pl1-x649.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The PWM module is part of NPCM7XX module. Each NPCM7XX module has two identical PWM modules. Each module contains 4 PWM entries. Each PWM has two outputs: frequency and duty_cycle. Both are computed using inputs from software side. This module does not model detail pulse signals since it is expensive. It also does not model interrupts and watchdogs that are dependant on the detail models. The interfaces for these are left in the module so that anyone in need for these functionalities can implement on their own. Reviewed-by: Havard Skinnemoen Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- docs/system/arm/nuvoton.rst | 2 +- hw/arm/npcm7xx.c | 26 +- hw/misc/meson.build | 1 + hw/misc/npcm7xx_pwm.c | 535 +++++++++++++++++++++++++++++++++ include/hw/arm/npcm7xx.h | 2 + include/hw/misc/npcm7xx_pwm.h | 106 +++++++ tests/qtest/meson.build | 1 + tests/qtest/npcm7xx_pwm-test.c | 490 ++++++++++++++++++++++++++++++ 8 files changed, 1160 insertions(+), 3 deletions(-) create mode 100644 hw/misc/npcm7xx_pwm.c create mode 100644 include/hw/misc/npcm7xx_pwm.h create mode 100644 tests/qtest/npcm7xx_pwm-test.c diff --git a/docs/system/arm/nuvoton.rst b/docs/system/arm/nuvoton.rst index 35829f8d0b..a1786342e2 100644 --- a/docs/system/arm/nuvoton.rst +++ b/docs/system/arm/nuvoton.rst @@ -42,6 +42,7 @@ Supported devices * USB host (USBH) * GPIO controller * Analog to Digital Converter (ADC) + * Pulse Width Modulation (PWM) =20 Missing devices --------------- @@ -61,7 +62,6 @@ Missing devices * Peripheral SPI controller (PSPI) * SD/MMC host * PECI interface - * Pulse Width Modulation (PWM) * Tachometer * PCI and PCIe root complex and bridges * VDM and MCTP support diff --git a/hw/arm/npcm7xx.c b/hw/arm/npcm7xx.c index b22a8c966d..72040d4079 100644 --- a/hw/arm/npcm7xx.c +++ b/hw/arm/npcm7xx.c @@ -102,6 +102,8 @@ enum NPCM7xxInterrupt { NPCM7XX_WDG2_IRQ, /* Timer Module 2 Watchdog */ NPCM7XX_EHCI_IRQ =3D 61, NPCM7XX_OHCI_IRQ =3D 62, + NPCM7XX_PWM0_IRQ =3D 93, /* PWM module 0 */ + NPCM7XX_PWM1_IRQ, /* PWM module 1 */ NPCM7XX_GPIO0_IRQ =3D 116, NPCM7XX_GPIO1_IRQ, NPCM7XX_GPIO2_IRQ, @@ -144,6 +146,12 @@ static const hwaddr npcm7xx_fiu3_flash_addr[] =3D { 0xb8000000, /* CS3 */ }; =20 +/* Register base address for each PWM Module */ +static const hwaddr npcm7xx_pwm_addr[] =3D { + 0xf0103000, + 0xf0104000, +}; + static const struct { hwaddr regs_addr; uint32_t unconnected_pins; @@ -353,6 +361,10 @@ static void npcm7xx_init(Object *obj) object_initialize_child(obj, npcm7xx_fiu[i].name, &s->fiu[i], TYPE_NPCM7XX_FIU); } + + for (i =3D 0; i < ARRAY_SIZE(s->pwm); i++) { + object_initialize_child(obj, "pwm[*]", &s->pwm[i], TYPE_NPCM7XX_PW= M); + } } =20 static void npcm7xx_realize(DeviceState *dev, Error **errp) @@ -513,6 +525,18 @@ static void npcm7xx_realize(DeviceState *dev, Error **= errp) sysbus_connect_irq(SYS_BUS_DEVICE(&s->ohci), 0, npcm7xx_irq(s, NPCM7XX_OHCI_IRQ)); =20 + /* PWM Modules. Cannot fail. */ + QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_pwm_addr) !=3D ARRAY_SIZE(s->pwm)= ); + for (i =3D 0; i < ARRAY_SIZE(s->pwm); i++) { + SysBusDevice *sbd =3D SYS_BUS_DEVICE(&s->pwm[i]); + + qdev_connect_clock_in(DEVICE(&s->pwm[i]), "clock", qdev_get_clock_= out( + DEVICE(&s->clk), "apb3-clock")); + sysbus_realize(sbd, &error_abort); + sysbus_mmio_map(sbd, 0, npcm7xx_pwm_addr[i]); + sysbus_connect_irq(sbd, i, npcm7xx_irq(s, NPCM7XX_PWM0_IRQ + i)); + } + /* * Flash Interface Unit (FIU). Can fail if incorrect number of chip se= lects * specified, but this is a programming error. @@ -580,8 +604,6 @@ static void npcm7xx_realize(DeviceState *dev, Error **e= rrp) create_unimplemented_device("npcm7xx.peci", 0xf0100000, 4 * = KiB); create_unimplemented_device("npcm7xx.siox[1]", 0xf0101000, 4 * = KiB); create_unimplemented_device("npcm7xx.siox[2]", 0xf0102000, 4 * = KiB); - create_unimplemented_device("npcm7xx.pwm[0]", 0xf0103000, 4 * = KiB); - create_unimplemented_device("npcm7xx.pwm[1]", 0xf0104000, 4 * = KiB); create_unimplemented_device("npcm7xx.mft[0]", 0xf0180000, 4 * = KiB); create_unimplemented_device("npcm7xx.mft[1]", 0xf0181000, 4 * = KiB); create_unimplemented_device("npcm7xx.mft[2]", 0xf0182000, 4 * = KiB); diff --git a/hw/misc/meson.build b/hw/misc/meson.build index ce15ffceb9..607cd38a21 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -64,6 +64,7 @@ softmmu_ss.add(when: 'CONFIG_MAINSTONE', if_true: files('= mst_fpga.c')) softmmu_ss.add(when: 'CONFIG_NPCM7XX', if_true: files( 'npcm7xx_clk.c', 'npcm7xx_gcr.c', + 'npcm7xx_pwm.c', 'npcm7xx_rng.c', )) softmmu_ss.add(when: 'CONFIG_OMAP', if_true: files( diff --git a/hw/misc/npcm7xx_pwm.c b/hw/misc/npcm7xx_pwm.c new file mode 100644 index 0000000000..c1753b2e3d --- /dev/null +++ b/hw/misc/npcm7xx_pwm.c @@ -0,0 +1,535 @@ +/* + * Nuvoton NPCM7xx PWM Module + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" + +#include "hw/irq.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "hw/misc/npcm7xx_pwm.h" +#include "migration/vmstate.h" +#include "qemu/bitops.h" +#include "qemu/error-report.h" +#include "qemu/log.h" +#include "qemu/module.h" +#include "qemu/units.h" +#include "trace.h" + +/* 32-bit register indices. */ +enum NPCM7xxPWMRegisters { + NPCM7XX_PWM_PPR, + NPCM7XX_PWM_CSR, + NPCM7XX_PWM_PCR, + NPCM7XX_PWM_CNR0, + NPCM7XX_PWM_CMR0, + NPCM7XX_PWM_PDR0, + NPCM7XX_PWM_CNR1, + NPCM7XX_PWM_CMR1, + NPCM7XX_PWM_PDR1, + NPCM7XX_PWM_CNR2, + NPCM7XX_PWM_CMR2, + NPCM7XX_PWM_PDR2, + NPCM7XX_PWM_CNR3, + NPCM7XX_PWM_CMR3, + NPCM7XX_PWM_PDR3, + NPCM7XX_PWM_PIER, + NPCM7XX_PWM_PIIR, + NPCM7XX_PWM_PWDR0, + NPCM7XX_PWM_PWDR1, + NPCM7XX_PWM_PWDR2, + NPCM7XX_PWM_PWDR3, + NPCM7XX_PWM_REGS_END, +}; + +/* Register field definitions. */ +#define NPCM7XX_PPR(rv, index) extract32((rv), npcm7xx_ppr_base[index= ], 8) +#define NPCM7XX_CSR(rv, index) extract32((rv), npcm7xx_csr_base[index= ], 3) +#define NPCM7XX_CH(rv, index) extract32((rv), npcm7xx_ch_base[index]= , 4) +#define NPCM7XX_CH_EN BIT(0) +#define NPCM7XX_CH_INV BIT(2) +#define NPCM7XX_CH_MOD BIT(3) + +/* Offset of each PWM channel's prescaler in the PPR register. */ +static const int npcm7xx_ppr_base[] =3D { 0, 0, 8, 8 }; +/* Offset of each PWM channel's clock selector in the CSR register. */ +static const int npcm7xx_csr_base[] =3D { 0, 4, 8, 12 }; +/* Offset of each PWM channel's control variable in the PCR register. */ +static const int npcm7xx_ch_base[] =3D { 0, 8, 12, 16 }; + +static uint32_t npcm7xx_pwm_calculate_freq(NPCM7xxPWM *p) +{ + uint32_t ppr; + uint32_t csr; + uint32_t freq; + + if (!p->running) { + return 0; + } + + csr =3D NPCM7XX_CSR(p->module->csr, p->index); + ppr =3D NPCM7XX_PPR(p->module->ppr, p->index); + freq =3D clock_get_hz(p->module->clock); + freq /=3D ppr + 1; + /* csr can only be 0~4 */ + if (csr > 4) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid csr value %u\n", + __func__, csr); + csr =3D 4; + } + /* freq won't be changed if csr =3D=3D 4. */ + if (csr < 4) { + freq >>=3D csr + 1; + } + + return freq / (p->cnr + 1); +} + +static uint32_t npcm7xx_pwm_calculate_duty(NPCM7xxPWM *p) +{ + uint64_t duty; + + if (p->running) { + if (p->cnr =3D=3D 0) { + duty =3D 0; + } else if (p->cmr >=3D p->cnr) { + duty =3D NPCM7XX_PWM_MAX_DUTY; + } else { + duty =3D NPCM7XX_PWM_MAX_DUTY * (p->cmr + 1) / (p->cnr + 1); + } + } else { + duty =3D 0; + } + + if (p->inverted) { + duty =3D NPCM7XX_PWM_MAX_DUTY - duty; + } + + return duty; +} + +static void npcm7xx_pwm_calculate_output(NPCM7xxPWM *p) +{ + p->freq =3D npcm7xx_pwm_calculate_freq(p); + p->duty =3D npcm7xx_pwm_calculate_duty(p); +} + +static void npcm7xx_pwm_write_ppr(NPCM7xxPWMState *s, uint32_t new_ppr) +{ + int i; + uint32_t old_ppr =3D s->ppr; + + QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_ppr_base) !=3D NPCM7XX_PWM_PER_MO= DULE); + s->ppr =3D new_ppr; + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; ++i) { + if (NPCM7XX_PPR(old_ppr, i) !=3D NPCM7XX_PPR(new_ppr, i)) { + s->pwm[i].freq =3D npcm7xx_pwm_calculate_freq(&s->pwm[i]); + } + } +} + +static void npcm7xx_pwm_write_csr(NPCM7xxPWMState *s, uint32_t new_csr) +{ + int i; + uint32_t old_csr =3D s->csr; + + QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_csr_base) !=3D NPCM7XX_PWM_PER_MO= DULE); + s->csr =3D new_csr; + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; ++i) { + if (NPCM7XX_CSR(old_csr, i) !=3D NPCM7XX_CSR(new_csr, i)) { + s->pwm[i].freq =3D npcm7xx_pwm_calculate_freq(&s->pwm[i]); + } + } +} + +static void npcm7xx_pwm_write_pcr(NPCM7xxPWMState *s, uint32_t new_pcr) +{ + int i; + bool inverted; + uint32_t pcr; + NPCM7xxPWM *p; + + s->pcr =3D new_pcr; + QEMU_BUILD_BUG_ON(ARRAY_SIZE(npcm7xx_ch_base) !=3D NPCM7XX_PWM_PER_MOD= ULE); + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; ++i) { + p =3D &s->pwm[i]; + pcr =3D NPCM7XX_CH(new_pcr, i); + inverted =3D pcr & NPCM7XX_CH_INV; + + /* + * We only run a PWM channel with toggle mode. Single-shot mode do= es not + * generate frequency and duty-cycle values. + */ + if ((pcr & NPCM7XX_CH_EN) && (pcr & NPCM7XX_CH_MOD)) { + if (p->running) { + /* Re-run this PWM channel if inverted changed. */ + if (p->inverted ^ inverted) { + p->inverted =3D inverted; + p->duty =3D npcm7xx_pwm_calculate_duty(p); + } + } else { + /* Run this PWM channel. */ + p->running =3D true; + p->inverted =3D inverted; + npcm7xx_pwm_calculate_output(p); + } + } else { + /* Clear this PWM channel. */ + p->running =3D false; + p->inverted =3D inverted; + npcm7xx_pwm_calculate_output(p); + } + } + +} + +static hwaddr npcm7xx_cnr_index(hwaddr reg) +{ + switch (reg) { + case NPCM7XX_PWM_CNR0: + return 0; + case NPCM7XX_PWM_CNR1: + return 1; + case NPCM7XX_PWM_CNR2: + return 2; + case NPCM7XX_PWM_CNR3: + return 3; + default: + g_assert_not_reached(); + } +} + +static hwaddr npcm7xx_cmr_index(hwaddr reg) +{ + switch (reg) { + case NPCM7XX_PWM_CMR0: + return 0; + case NPCM7XX_PWM_CMR1: + return 1; + case NPCM7XX_PWM_CMR2: + return 2; + case NPCM7XX_PWM_CMR3: + return 3; + default: + g_assert_not_reached(); + } +} + +static hwaddr npcm7xx_pdr_index(hwaddr reg) +{ + switch (reg) { + case NPCM7XX_PWM_PDR0: + return 0; + case NPCM7XX_PWM_PDR1: + return 1; + case NPCM7XX_PWM_PDR2: + return 2; + case NPCM7XX_PWM_PDR3: + return 3; + default: + g_assert_not_reached(); + } +} + +static hwaddr npcm7xx_pwdr_index(hwaddr reg) +{ + switch (reg) { + case NPCM7XX_PWM_PWDR0: + return 0; + case NPCM7XX_PWM_PWDR1: + return 1; + case NPCM7XX_PWM_PWDR2: + return 2; + case NPCM7XX_PWM_PWDR3: + return 3; + default: + g_assert_not_reached(); + } +} + +static uint64_t npcm7xx_pwm_read(void *opaque, hwaddr offset, unsigned siz= e) +{ + NPCM7xxPWMState *s =3D opaque; + hwaddr reg =3D offset / sizeof(uint32_t); + uint64_t value =3D 0; + + switch (reg) { + case NPCM7XX_PWM_CNR0: + case NPCM7XX_PWM_CNR1: + case NPCM7XX_PWM_CNR2: + case NPCM7XX_PWM_CNR3: + value =3D s->pwm[npcm7xx_cnr_index(reg)].cnr; + break; + + case NPCM7XX_PWM_CMR0: + case NPCM7XX_PWM_CMR1: + case NPCM7XX_PWM_CMR2: + case NPCM7XX_PWM_CMR3: + value =3D s->pwm[npcm7xx_cmr_index(reg)].cmr; + break; + + case NPCM7XX_PWM_PDR0: + case NPCM7XX_PWM_PDR1: + case NPCM7XX_PWM_PDR2: + case NPCM7XX_PWM_PDR3: + value =3D s->pwm[npcm7xx_pdr_index(reg)].pdr; + break; + + case NPCM7XX_PWM_PWDR0: + case NPCM7XX_PWM_PWDR1: + case NPCM7XX_PWM_PWDR2: + case NPCM7XX_PWM_PWDR3: + value =3D s->pwm[npcm7xx_pwdr_index(reg)].pwdr; + break; + + case NPCM7XX_PWM_PPR: + value =3D s->ppr; + break; + + case NPCM7XX_PWM_CSR: + value =3D s->csr; + break; + + case NPCM7XX_PWM_PCR: + value =3D s->pcr; + break; + + case NPCM7XX_PWM_PIER: + value =3D s->pier; + break; + + case NPCM7XX_PWM_PIIR: + value =3D s->piir; + break; + + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid offset 0x%04" HWADDR_PRIx "\n", + __func__, offset); + break; + } + + return value; +} + +static void npcm7xx_pwm_write(void *opaque, hwaddr offset, + uint64_t v, unsigned size) +{ + NPCM7xxPWMState *s =3D opaque; + NPCM7xxPWM *p; + hwaddr reg =3D offset / sizeof(uint32_t); + uint32_t value =3D v; + + switch (reg) { + case NPCM7XX_PWM_CNR0: + case NPCM7XX_PWM_CNR1: + case NPCM7XX_PWM_CNR2: + case NPCM7XX_PWM_CNR3: + p =3D &s->pwm[npcm7xx_cnr_index(reg)]; + p->cnr =3D value; + npcm7xx_pwm_calculate_output(p); + break; + + case NPCM7XX_PWM_CMR0: + case NPCM7XX_PWM_CMR1: + case NPCM7XX_PWM_CMR2: + case NPCM7XX_PWM_CMR3: + p =3D &s->pwm[npcm7xx_cmr_index(reg)]; + p->cmr =3D value; + npcm7xx_pwm_calculate_output(p); + break; + + case NPCM7XX_PWM_PDR0: + case NPCM7XX_PWM_PDR1: + case NPCM7XX_PWM_PDR2: + case NPCM7XX_PWM_PDR3: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: register @ 0x%04" HWADDR_PRIx " is read-only\n", + __func__, offset); + break; + + case NPCM7XX_PWM_PWDR0: + case NPCM7XX_PWM_PWDR1: + case NPCM7XX_PWM_PWDR2: + case NPCM7XX_PWM_PWDR3: + qemu_log_mask(LOG_UNIMP, + "%s: register @ 0x%04" HWADDR_PRIx " is not implement= ed\n", + __func__, offset); + break; + + case NPCM7XX_PWM_PPR: + npcm7xx_pwm_write_ppr(s, value); + break; + + case NPCM7XX_PWM_CSR: + npcm7xx_pwm_write_csr(s, value); + break; + + case NPCM7XX_PWM_PCR: + npcm7xx_pwm_write_pcr(s, value); + break; + + case NPCM7XX_PWM_PIER: + qemu_log_mask(LOG_UNIMP, + "%s: register @ 0x%04" HWADDR_PRIx " is not implement= ed\n", + __func__, offset); + break; + + case NPCM7XX_PWM_PIIR: + qemu_log_mask(LOG_UNIMP, + "%s: register @ 0x%04" HWADDR_PRIx " is not implement= ed\n", + __func__, offset); + break; + + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: invalid offset 0x%04" HWADDR_PRIx "\n", + __func__, offset); + break; + } +} + + +static const struct MemoryRegionOps npcm7xx_pwm_ops =3D { + .read =3D npcm7xx_pwm_read, + .write =3D npcm7xx_pwm_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + .unaligned =3D false, + }, +}; + +static void npcm7xx_pwm_enter_reset(Object *obj, ResetType type) +{ + NPCM7xxPWMState *s =3D NPCM7XX_PWM(obj); + int i; + + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; i++) { + NPCM7xxPWM *p =3D &s->pwm[i]; + + p->cnr =3D 0x00000000; + p->cmr =3D 0x00000000; + p->pdr =3D 0x00000000; + p->pwdr =3D 0x00000000; + } + + s->ppr =3D 0x00000000; + s->csr =3D 0x00000000; + s->pcr =3D 0x00000000; + s->pier =3D 0x00000000; + s->piir =3D 0x00000000; +} + +static void npcm7xx_pwm_hold_reset(Object *obj) +{ + NPCM7xxPWMState *s =3D NPCM7XX_PWM(obj); + int i; + + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; i++) { + qemu_irq_lower(s->pwm[i].irq); + } +} + +static void npcm7xx_pwm_init(Object *obj) +{ + NPCM7xxPWMState *s =3D NPCM7XX_PWM(obj); + SysBusDevice *sbd =3D &s->parent; + int i; + + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; i++) { + NPCM7xxPWM *p =3D &s->pwm[i]; + p->module =3D s; + p->index =3D i; + sysbus_init_irq(sbd, &p->irq); + } + + memory_region_init_io(&s->iomem, obj, &npcm7xx_pwm_ops, s, + TYPE_NPCM7XX_PWM, 4 * KiB); + sysbus_init_mmio(sbd, &s->iomem); + s->clock =3D qdev_init_clock_in(DEVICE(s), "clock", NULL, NULL); + + for (i =3D 0; i < NPCM7XX_PWM_PER_MODULE; ++i) { + object_property_add_uint32_ptr(obj, "freq[*]", + &s->pwm[i].freq, OBJ_PROP_FLAG_READ); + object_property_add_uint32_ptr(obj, "duty[*]", + &s->pwm[i].duty, OBJ_PROP_FLAG_READ); + } +} + +static const VMStateDescription vmstate_npcm7xx_pwm =3D { + .name =3D "npcm7xx-pwm", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_BOOL(running, NPCM7xxPWM), + VMSTATE_BOOL(inverted, NPCM7xxPWM), + VMSTATE_UINT8(index, NPCM7xxPWM), + VMSTATE_UINT32(cnr, NPCM7xxPWM), + VMSTATE_UINT32(cmr, NPCM7xxPWM), + VMSTATE_UINT32(pdr, NPCM7xxPWM), + VMSTATE_UINT32(pwdr, NPCM7xxPWM), + VMSTATE_UINT32(freq, NPCM7xxPWM), + VMSTATE_UINT32(duty, NPCM7xxPWM), + VMSTATE_END_OF_LIST(), + }, +}; + +static const VMStateDescription vmstate_npcm7xx_pwm_module =3D { + .name =3D "npcm7xx-pwm-module", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_CLOCK(clock, NPCM7xxPWMState), + VMSTATE_STRUCT_ARRAY(pwm, NPCM7xxPWMState, + NPCM7XX_PWM_PER_MODULE, 0, vmstate_npcm7xx_pw= m, + NPCM7xxPWM), + VMSTATE_UINT32(ppr, NPCM7xxPWMState), + VMSTATE_UINT32(csr, NPCM7xxPWMState), + VMSTATE_UINT32(pcr, NPCM7xxPWMState), + VMSTATE_UINT32(pier, NPCM7xxPWMState), + VMSTATE_UINT32(piir, NPCM7xxPWMState), + VMSTATE_END_OF_LIST(), + }, +}; + +static void npcm7xx_pwm_class_init(ObjectClass *klass, void *data) +{ + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + DeviceClass *dc =3D DEVICE_CLASS(klass); + + QEMU_BUILD_BUG_ON(NPCM7XX_PWM_REGS_END > NPCM7XX_PWM_NR_REGS); + + dc->desc =3D "NPCM7xx PWM Controller"; + dc->vmsd =3D &vmstate_npcm7xx_pwm_module; + rc->phases.enter =3D npcm7xx_pwm_enter_reset; + rc->phases.hold =3D npcm7xx_pwm_hold_reset; +} + +static const TypeInfo npcm7xx_pwm_info =3D { + .name =3D TYPE_NPCM7XX_PWM, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(NPCM7xxPWMState), + .class_init =3D npcm7xx_pwm_class_init, + .instance_init =3D npcm7xx_pwm_init, +}; + +static void npcm7xx_pwm_register_type(void) +{ + type_register_static(&npcm7xx_pwm_info); +} +type_init(npcm7xx_pwm_register_type); diff --git a/include/hw/arm/npcm7xx.h b/include/hw/arm/npcm7xx.h index 51e1c7620d..f6227aa8aa 100644 --- a/include/hw/arm/npcm7xx.h +++ b/include/hw/arm/npcm7xx.h @@ -23,6 +23,7 @@ #include "hw/mem/npcm7xx_mc.h" #include "hw/misc/npcm7xx_clk.h" #include "hw/misc/npcm7xx_gcr.h" +#include "hw/misc/npcm7xx_pwm.h" #include "hw/misc/npcm7xx_rng.h" #include "hw/nvram/npcm7xx_otp.h" #include "hw/timer/npcm7xx_timer.h" @@ -78,6 +79,7 @@ typedef struct NPCM7xxState { NPCM7xxCLKState clk; NPCM7xxTimerCtrlState tim[3]; NPCM7xxADCState adc; + NPCM7xxPWMState pwm[2]; NPCM7xxOTPState key_storage; NPCM7xxOTPState fuse_array; NPCM7xxMCState mc; diff --git a/include/hw/misc/npcm7xx_pwm.h b/include/hw/misc/npcm7xx_pwm.h new file mode 100644 index 0000000000..b83f965f1a --- /dev/null +++ b/include/hw/misc/npcm7xx_pwm.h @@ -0,0 +1,106 @@ +/* + * Nuvoton NPCM7xx PWM Module + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ +#ifndef NPCM7XX_PWM_H +#define NPCM7XX_PWM_H + +#include "qemu/osdep.h" +#include "hw/clock.h" +#include "hw/sysbus.h" +#include "hw/irq.h" + +/* Each PWM module holds 4 PWM channels. */ +#define NPCM7XX_PWM_PER_MODULE 4 + +/* + * Number of registers in one pwm module. Don't change this without increa= sing + * the version_id in vmstate. + */ +#define NPCM7XX_PWM_NR_REGS (0x54 / sizeof(uint32_t)) + +/* + * The maximum duty values. Each duty unit represents 1/NPCM7XX_PWM_MAX_DU= TY + * cycles. For example, if NPCM7XX_PWM_MAX_DUTY=3D1,000,000 and a PWM has = a duty + * value of 100,000 the duty cycle for that PWM is 10%. + */ +#define NPCM7XX_PWM_MAX_DUTY 1000000 + +typedef struct NPCM7xxPWMState NPCM7xxPWMState; + +/** + * struct NPCM7xxPWM - The state of a single PWM channel. + * @module: The PWM module that contains this channel. + * @irq: GIC interrupt line to fire on expiration if enabled. + * @running: Whether this PWM channel is generating output. + * @inverted: Whether this PWM channel is inverted. + * @index: The index of this PWM channel. + * @cnr: The counter register. + * @cmr: The comparator register. + * @pdr: The data register. + * @pwdr: The watchdog register. + * @freq: The frequency of this PWM channel. + * @duty: The duty cycle of this PWM channel. One unit represents + * 1/NPCM7XX_MAX_DUTY cycles. + */ +typedef struct NPCM7xxPWM { + NPCM7xxPWMState *module; + + qemu_irq irq; + + bool running; + bool inverted; + + uint8_t index; + uint32_t cnr; + uint32_t cmr; + uint32_t pdr; + uint32_t pwdr; + + uint32_t freq; + uint32_t duty; +} NPCM7xxPWM; + +/** + * struct NPCM7xxPWMState - Pulse Width Modulation device state. + * @parent: System bus device. + * @iomem: Memory region through which registers are accessed. + * @clock: The PWM clock. + * @pwm: The PWM channels owned by this module. + * @ppr: The prescaler register. + * @csr: The clock selector register. + * @pcr: The control register. + * @pier: The interrupt enable register. + * @piir: The interrupt indication register. + */ +struct NPCM7xxPWMState { + SysBusDevice parent; + + MemoryRegion iomem; + + Clock *clock; + NPCM7xxPWM pwm[NPCM7XX_PWM_PER_MODULE]; + + uint32_t ppr; + uint32_t csr; + uint32_t pcr; + uint32_t pier; + uint32_t piir; +}; + +#define TYPE_NPCM7XX_PWM "npcm7xx-pwm" +#define NPCM7XX_PWM(obj) \ + OBJECT_CHECK(NPCM7xxPWMState, (obj), TYPE_NPCM7XX_PWM) + +#endif /* NPCM7XX_PWM_H */ diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 955710d1c5..0b5467f084 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -136,6 +136,7 @@ qtests_sparc64 =3D \ qtests_npcm7xx =3D \ ['npcm7xx_adc-test', 'npcm7xx_gpio-test', + 'npcm7xx_pwm-test', 'npcm7xx_rng-test', 'npcm7xx_timer-test', 'npcm7xx_watchdog_timer-test'] diff --git a/tests/qtest/npcm7xx_pwm-test.c b/tests/qtest/npcm7xx_pwm-test.c new file mode 100644 index 0000000000..33fbdf5f54 --- /dev/null +++ b/tests/qtest/npcm7xx_pwm-test.c @@ -0,0 +1,490 @@ +/* + * QTests for Nuvoton NPCM7xx PWM Modules. + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" +#include "qemu/bitops.h" +#include "libqos/libqtest.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qnum.h" + +#define REF_HZ 25000000 + +/* Register field definitions. */ +#define CH_EN BIT(0) +#define CH_INV BIT(2) +#define CH_MOD BIT(3) + +/* Registers shared between all PWMs in a module */ +#define PPR 0x00 +#define CSR 0x04 +#define PCR 0x08 +#define PIER 0x3c +#define PIIR 0x40 + +/* CLK module related */ +#define CLK_BA 0xf0801000 +#define CLKSEL 0x04 +#define CLKDIV1 0x08 +#define CLKDIV2 0x2c +#define PLLCON0 0x0c +#define PLLCON1 0x10 +#define PLL_INDV(rv) extract32((rv), 0, 6) +#define PLL_FBDV(rv) extract32((rv), 16, 12) +#define PLL_OTDV1(rv) extract32((rv), 8, 3) +#define PLL_OTDV2(rv) extract32((rv), 13, 3) +#define APB3CKDIV(rv) extract32((rv), 28, 2) +#define CLK2CKDIV(rv) extract32((rv), 0, 1) +#define CLK4CKDIV(rv) extract32((rv), 26, 2) +#define CPUCKSEL(rv) extract32((rv), 0, 2) + +#define MAX_DUTY 1000000 + +typedef struct PWMModule { + int irq; + uint64_t base_addr; +} PWMModule; + +typedef struct PWM { + uint32_t cnr_offset; + uint32_t cmr_offset; + uint32_t pdr_offset; + uint32_t pwdr_offset; +} PWM; + +typedef struct TestData { + const PWMModule *module; + const PWM *pwm; +} TestData; + +static const PWMModule pwm_module_list[] =3D { + { + .irq =3D 93, + .base_addr =3D 0xf0103000 + }, + { + .irq =3D 94, + .base_addr =3D 0xf0104000 + } +}; + +static const PWM pwm_list[] =3D { + { + .cnr_offset =3D 0x0c, + .cmr_offset =3D 0x10, + .pdr_offset =3D 0x14, + .pwdr_offset =3D 0x44, + }, + { + .cnr_offset =3D 0x18, + .cmr_offset =3D 0x1c, + .pdr_offset =3D 0x20, + .pwdr_offset =3D 0x48, + }, + { + .cnr_offset =3D 0x24, + .cmr_offset =3D 0x28, + .pdr_offset =3D 0x2c, + .pwdr_offset =3D 0x4c, + }, + { + .cnr_offset =3D 0x30, + .cmr_offset =3D 0x34, + .pdr_offset =3D 0x38, + .pwdr_offset =3D 0x50, + }, +}; + +static const int ppr_base[] =3D { 0, 0, 8, 8 }; +static const int csr_base[] =3D { 0, 4, 8, 12 }; +static const int pcr_base[] =3D { 0, 8, 12, 16 }; + +static const uint32_t ppr_list[] =3D { + 0, + 1, + 10, + 100, + 255, /* Max possible value. */ +}; + +static const uint32_t csr_list[] =3D { + 0, + 1, + 2, + 3, + 4, /* Max possible value. */ +}; + +static const uint32_t cnr_list[] =3D { + 0, + 1, + 50, + 100, + 150, + 200, + 1000, + 10000, + 65535, /* Max possible value. */ +}; + +static const uint32_t cmr_list[] =3D { + 0, + 1, + 10, + 50, + 100, + 150, + 200, + 1000, + 10000, + 65535, /* Max possible value. */ +}; + +/* Returns the index of the PWM module. */ +static int pwm_module_index(const PWMModule *module) +{ + ptrdiff_t diff =3D module - pwm_module_list; + + g_assert_true(diff >=3D 0 && diff < ARRAY_SIZE(pwm_module_list)); + + return diff; +} + +/* Returns the index of the PWM entry. */ +static int pwm_index(const PWM *pwm) +{ + ptrdiff_t diff =3D pwm - pwm_list; + + g_assert_true(diff >=3D 0 && diff < ARRAY_SIZE(pwm_list)); + + return diff; +} + +static uint64_t pwm_qom_get(QTestState *qts, const char *path, const char = *name) +{ + QDict *response; + + g_test_message("Getting properties %s from %s", name, path); + response =3D qtest_qmp(qts, "{ 'execute': 'qom-get'," + " 'arguments': { 'path': %s, 'property': %s}}", + path, name); + /* The qom set message returns successfully. */ + g_assert_true(qdict_haskey(response, "return")); + return qnum_get_uint(qobject_to(QNum, qdict_get(response, "return"))); +} + +static uint64_t pwm_get_freq(QTestState *qts, int module_index, int pwm_in= dex) +{ + char path[100]; + char name[100]; + + sprintf(path, "/machine/soc/pwm[%d]", module_index); + sprintf(name, "freq[%d]", pwm_index); + + return pwm_qom_get(qts, path, name); +} + +static uint64_t pwm_get_duty(QTestState *qts, int module_index, int pwm_in= dex) +{ + char path[100]; + char name[100]; + + sprintf(path, "/machine/soc/pwm[%d]", module_index); + sprintf(name, "duty[%d]", pwm_index); + + return pwm_qom_get(qts, path, name); +} + +static uint32_t get_pll(uint32_t con) +{ + return REF_HZ * PLL_FBDV(con) / (PLL_INDV(con) * PLL_OTDV1(con) + * PLL_OTDV2(con)); +} + +static uint64_t read_pclk(QTestState *qts) +{ + uint64_t freq =3D REF_HZ; + uint32_t clksel =3D qtest_readl(qts, CLK_BA + CLKSEL); + uint32_t pllcon; + uint32_t clkdiv1 =3D qtest_readl(qts, CLK_BA + CLKDIV1); + uint32_t clkdiv2 =3D qtest_readl(qts, CLK_BA + CLKDIV2); + + switch (CPUCKSEL(clksel)) { + case 0: + pllcon =3D qtest_readl(qts, CLK_BA + PLLCON0); + freq =3D get_pll(pllcon); + break; + case 1: + pllcon =3D qtest_readl(qts, CLK_BA + PLLCON1); + freq =3D get_pll(pllcon); + break; + case 2: + break; + case 3: + break; + default: + g_assert_not_reached(); + } + + freq >>=3D (CLK2CKDIV(clkdiv1) + CLK4CKDIV(clkdiv1) + APB3CKDIV(clkdiv= 2)); + + return freq; +} + +static uint32_t pwm_selector(uint32_t csr) +{ + switch (csr) { + case 0: + return 2; + case 1: + return 4; + case 2: + return 8; + case 3: + return 16; + case 4: + return 1; + default: + g_assert_not_reached(); + } +} + +static uint64_t pwm_compute_freq(QTestState *qts, uint32_t ppr, uint32_t c= sr, + uint32_t cnr) +{ + return read_pclk(qts) / ((ppr + 1) * pwm_selector(csr) * (cnr + 1)); +} + +static uint64_t pwm_compute_duty(uint32_t cnr, uint32_t cmr, bool inverted) +{ + uint64_t duty; + + if (cnr =3D=3D 0) { + /* PWM is stopped. */ + duty =3D 0; + } else if (cmr >=3D cnr) { + duty =3D MAX_DUTY; + } else { + duty =3D MAX_DUTY * (cmr + 1) / (cnr + 1); + } + + if (inverted) { + duty =3D MAX_DUTY - duty; + } + + return duty; +} + +static uint32_t pwm_read(QTestState *qts, const TestData *td, unsigned off= set) +{ + return qtest_readl(qts, td->module->base_addr + offset); +} + +static void pwm_write(QTestState *qts, const TestData *td, unsigned offset, + uint32_t value) +{ + qtest_writel(qts, td->module->base_addr + offset, value); +} + +static uint32_t pwm_read_ppr(QTestState *qts, const TestData *td) +{ + return extract32(pwm_read(qts, td, PPR), ppr_base[pwm_index(td->pwm)],= 8); +} + +static void pwm_write_ppr(QTestState *qts, const TestData *td, uint32_t va= lue) +{ + pwm_write(qts, td, PPR, value << ppr_base[pwm_index(td->pwm)]); +} + +static uint32_t pwm_read_csr(QTestState *qts, const TestData *td) +{ + return extract32(pwm_read(qts, td, CSR), csr_base[pwm_index(td->pwm)],= 3); +} + +static void pwm_write_csr(QTestState *qts, const TestData *td, uint32_t va= lue) +{ + pwm_write(qts, td, CSR, value << csr_base[pwm_index(td->pwm)]); +} + +static uint32_t pwm_read_pcr(QTestState *qts, const TestData *td) +{ + return extract32(pwm_read(qts, td, PCR), pcr_base[pwm_index(td->pwm)],= 4); +} + +static void pwm_write_pcr(QTestState *qts, const TestData *td, uint32_t va= lue) +{ + pwm_write(qts, td, PCR, value << pcr_base[pwm_index(td->pwm)]); +} + +static uint32_t pwm_read_cnr(QTestState *qts, const TestData *td) +{ + return pwm_read(qts, td, td->pwm->cnr_offset); +} + +static void pwm_write_cnr(QTestState *qts, const TestData *td, uint32_t va= lue) +{ + pwm_write(qts, td, td->pwm->cnr_offset, value); +} + +static uint32_t pwm_read_cmr(QTestState *qts, const TestData *td) +{ + return pwm_read(qts, td, td->pwm->cmr_offset); +} + +static void pwm_write_cmr(QTestState *qts, const TestData *td, uint32_t va= lue) +{ + pwm_write(qts, td, td->pwm->cmr_offset, value); +} + +/* Check pwm registers can be reset to default value */ +static void test_init(gconstpointer test_data) +{ + const TestData *td =3D test_data; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + int module =3D pwm_module_index(td->module); + int pwm =3D pwm_index(td->pwm); + + g_assert_cmpuint(pwm_get_freq(qts, module, pwm), =3D=3D, 0); + g_assert_cmpuint(pwm_get_duty(qts, module, pwm), =3D=3D, 0); + + qtest_quit(qts); +} + +/* One-shot mode should not change frequency and duty cycle. */ +static void test_oneshot(gconstpointer test_data) +{ + const TestData *td =3D test_data; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + int module =3D pwm_module_index(td->module); + int pwm =3D pwm_index(td->pwm); + uint32_t ppr, csr, pcr; + int i, j; + + pcr =3D CH_EN; + for (i =3D 0; i < ARRAY_SIZE(ppr_list); ++i) { + ppr =3D ppr_list[i]; + pwm_write_ppr(qts, td, ppr); + + for (j =3D 0; j < ARRAY_SIZE(csr_list); ++j) { + csr =3D csr_list[j]; + pwm_write_csr(qts, td, csr); + pwm_write_pcr(qts, td, pcr); + + g_assert_cmpuint(pwm_read_ppr(qts, td), =3D=3D, ppr); + g_assert_cmpuint(pwm_read_csr(qts, td), =3D=3D, csr); + g_assert_cmpuint(pwm_read_pcr(qts, td), =3D=3D, pcr); + g_assert_cmpuint(pwm_get_freq(qts, module, pwm), =3D=3D, 0); + g_assert_cmpuint(pwm_get_duty(qts, module, pwm), =3D=3D, 0); + } + } + + qtest_quit(qts); +} + +/* In toggle mode, the PWM generates correct outputs. */ +static void test_toggle(gconstpointer test_data) +{ + const TestData *td =3D test_data; + QTestState *qts =3D qtest_init("-machine quanta-gsj"); + int module =3D pwm_module_index(td->module); + int pwm =3D pwm_index(td->pwm); + uint32_t ppr, csr, pcr, cnr, cmr; + int i, j, k, l; + uint64_t expected_freq, expected_duty; + + pcr =3D CH_EN | CH_MOD; + for (i =3D 0; i < ARRAY_SIZE(ppr_list); ++i) { + ppr =3D ppr_list[i]; + pwm_write_ppr(qts, td, ppr); + + for (j =3D 0; j < ARRAY_SIZE(csr_list); ++j) { + csr =3D csr_list[j]; + pwm_write_csr(qts, td, csr); + + for (k =3D 0; k < ARRAY_SIZE(cnr_list); ++k) { + cnr =3D cnr_list[k]; + pwm_write_cnr(qts, td, cnr); + + for (l =3D 0; l < ARRAY_SIZE(cmr_list); ++l) { + cmr =3D cmr_list[l]; + pwm_write_cmr(qts, td, cmr); + expected_freq =3D pwm_compute_freq(qts, ppr, csr, cnr); + expected_duty =3D pwm_compute_duty(cnr, cmr, false); + + pwm_write_pcr(qts, td, pcr); + g_assert_cmpuint(pwm_read_ppr(qts, td), =3D=3D, ppr); + g_assert_cmpuint(pwm_read_csr(qts, td), =3D=3D, csr); + g_assert_cmpuint(pwm_read_pcr(qts, td), =3D=3D, pcr); + g_assert_cmpuint(pwm_read_cnr(qts, td), =3D=3D, cnr); + g_assert_cmpuint(pwm_read_cmr(qts, td), =3D=3D, cmr); + g_assert_cmpuint(pwm_get_duty(qts, module, pwm), + =3D=3D, expected_duty); + if (expected_duty !=3D 0 && expected_duty !=3D 100) { + /* Duty cycle with 0 or 100 doesn't need frequency= . */ + g_assert_cmpuint(pwm_get_freq(qts, module, pwm), + =3D=3D, expected_freq); + } + + /* Test inverted mode */ + expected_duty =3D pwm_compute_duty(cnr, cmr, true); + pwm_write_pcr(qts, td, pcr | CH_INV); + g_assert_cmpuint(pwm_read_pcr(qts, td), =3D=3D, pcr | = CH_INV); + g_assert_cmpuint(pwm_get_duty(qts, module, pwm), + =3D=3D, expected_duty); + if (expected_duty !=3D 0 && expected_duty !=3D 100) { + /* Duty cycle with 0 or 100 doesn't need frequency= . */ + g_assert_cmpuint(pwm_get_freq(qts, module, pwm), + =3D=3D, expected_freq); + } + + } + } + } + } + + qtest_quit(qts); +} + +static void pwm_add_test(const char *name, const TestData* td, + GTestDataFunc fn) +{ + g_autofree char *full_name =3D g_strdup_printf( + "npcm7xx_pwm/module[%d]/pwm[%d]/%s", pwm_module_index(td->modu= le), + pwm_index(td->pwm), name); + qtest_add_data_func(full_name, td, fn); +} +#define add_test(name, td) pwm_add_test(#name, td, test_##name) + +int main(int argc, char **argv) +{ + TestData test_data_list[ARRAY_SIZE(pwm_module_list) * ARRAY_SIZE(pwm_l= ist)]; + + g_test_init(&argc, &argv, NULL); + + for (int i =3D 0; i < ARRAY_SIZE(pwm_module_list); ++i) { + for (int j =3D 0; j < ARRAY_SIZE(pwm_list); ++j) { + TestData *td =3D &test_data_list[i * ARRAY_SIZE(pwm_list) + j]; + + td->module =3D &pwm_module_list[i]; + td->pwm =3D &pwm_list[j]; + + add_test(init, td); + add_test(oneshot, td); + add_test(toggle, td); + } + } + + return g_test_run(); +} --=20 2.29.2.684.gfbc64c5ab5-goog From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607652005; cv=none; d=zohomail.com; s=zohoarc; b=MESKnxsyzDFC28xl/0FZzQICUSvWvNPbZy1sCwXz6WftoO4LaehyX/z/KM+XEM5Nf80jm3PUjbRc1qeDcS8IyRivVpVdmXOlXvWPFAeyun7YqmLW2zOPNxL2zdVU5Oy8P09xdzJ5sJ0DFu+ccv5ko54zvvbySFf5uq8SFTgPIwg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607652005; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=cKCtRn1dkFOSwY3ieDqlEqe46CjfgnOoKtbkb72JIac=; b=IVcfPCtsMt3ARzQahvZ2hNoW1pmlGGNt+qJRYwnsXwdHdxbrA+HvY9dzUo3f+dAcf6cd486oVL8QLriQw/fxphfRPDf0MoJWw+GoEr7COhAmAqjcWeoCTQtiRx73SmlRqak3Gin8bQTbADH0ER6UrZF5kKy6Ie1yrQrgtMakV/4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607652005620356.83752063770044; Thu, 10 Dec 2020 18:00:05 -0800 (PST) Received: from localhost ([::1]:42978 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXj2-0007p9-3r for importer@patchew.org; Thu, 10 Dec 2020 21:00:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58344) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <31dDSXwgKCsY86tm054ts00sxq.o0y2qy6-pq7qxz0zsz6.03s@flex--wuhaotsh.bounces.google.com>) id 1knXbf-0001hu-Ry for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:28 -0500 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]:57106) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <31dDSXwgKCsY86tm054ts00sxq.o0y2qy6-pq7qxz0zsz6.03s@flex--wuhaotsh.bounces.google.com>) id 1knXbb-0007BJ-0k for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:27 -0500 Received: by mail-pg1-x54a.google.com with SMTP id z2so5390130pgb.23 for ; Thu, 10 Dec 2020 17:52:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=cKCtRn1dkFOSwY3ieDqlEqe46CjfgnOoKtbkb72JIac=; b=iOoF3iSknyFAfnAqW3e3MzqsRKI7VlfXAo8EpS8JgDm2fuIe7q4XzUb6mnyW8qpwU7 rl7UXbjJXrksi7FxMS3hSYF2EVdyfWwJcgK4To+lAstsz4q7/Nyn0Yyn5fQ+4+GYxtkr zszkO+q4GVOzIrXvfCGvF1A4XC6WaQRmbJdIzIM3lmTZEx8MCwgrB2ZjrXIkgpBFFehP bT9iN8a41v0tg6OF0TWl9ZQnv5gveL08GunuLUcw3weC8sgvDp2KI8EmZrQFD5xH3vV8 7U8qtB98OMD62irdHuxz2VAVAPPKF59a1pGwp/cjdGcPCdFuR6nUN23PYScRX+y2UpiB NOFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=cKCtRn1dkFOSwY3ieDqlEqe46CjfgnOoKtbkb72JIac=; b=BQHOkN0WUZt1mGeWhrtMHp+Yj/IkgD6fZeBomnrQlk4yx0G8nw+Dnb6xbjeOnAkPNf Hcwn/2ru1cWFPLA+EHv6vy2Rs/CRgUT8tfQLJfCcCNhprheW3ha7kZSUN7EiKQoOpOLs hJZT4PkmdcTAJZtZkXxHiRNy474K+vll5XsXgSpCkj5p8n6JNmVpVPin4K3wQ4a0w5J3 Tzfo0p7YVs9w33w6eSIFOXAb1PR7ZJywEgnWZRZXdfek8BNOd9GQfVBCG2PiMNViKE6a mndDMOgMt1L52BzKuz0jq6I4d+HKiwNp1fw/v+SVrbXwXzgmLVw/oJBoKCE/70176ff1 9Lsg== X-Gm-Message-State: AOAM5331BoKCueb7CHOcC1O3UMBDiSxVx8AFj1zXqKasn7oIp2P22FD8 6SfqYZvQo8OgMN/bW9agXI3jOnXXNDy7Hg== X-Google-Smtp-Source: ABdhPJzwf5RN2mEBMzRT/uUThoBhcIswoexZ/B9Z10MdeunXlmJ9wldYMXGRFHNYqKACz/0ULWPirj5Wqzeq8A== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a17:902:8f82:b029:da:f37f:79bf with SMTP id z2-20020a1709028f82b02900daf37f79bfmr8976578plo.79.1607651541057; Thu, 10 Dec 2020 17:52:21 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:54 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-6-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 5/7] hw/ipmi: Add an IPMI host interface To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::54a; envelope-from=31dDSXwgKCsY86tm054ts00sxq.o0y2qy6-pq7qxz0zsz6.03s@flex--wuhaotsh.bounces.google.com; helo=mail-pg1-x54a.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The IPMI host interface is used to emulate IPMI related devices on a System-on-Chip (SoC) used for Baseband Management Controller (BMC). This interface consists of two components: IPMI host and IPMI responder. An IPMI responder is a device to intercept reads and writes to the system interface registers in the host. An IPMI host emulates the host behavior on a BMC emulation. For more information see docs/spec/ipmi.rst. Reviewed-by: Havard Skinnemoen Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- default-configs/devices/arm-softmmu.mak | 2 + hw/ipmi/Kconfig | 5 ++ hw/ipmi/ipmi_host.c | 40 +++++++++++++++ hw/ipmi/meson.build | 1 + include/hw/ipmi/ipmi_host.h | 56 +++++++++++++++++++++ include/hw/ipmi/ipmi_responder.h | 66 +++++++++++++++++++++++++ 6 files changed, 170 insertions(+) create mode 100644 hw/ipmi/ipmi_host.c create mode 100644 include/hw/ipmi/ipmi_host.h create mode 100644 include/hw/ipmi/ipmi_responder.h diff --git a/default-configs/devices/arm-softmmu.mak b/default-configs/devi= ces/arm-softmmu.mak index 08a32123b4..864bac4501 100644 --- a/default-configs/devices/arm-softmmu.mak +++ b/default-configs/devices/arm-softmmu.mak @@ -27,6 +27,8 @@ CONFIG_GUMSTIX=3Dy CONFIG_SPITZ=3Dy CONFIG_TOSA=3Dy CONFIG_Z2=3Dy +CONFIG_IPMI=3Dy +CONFIG_IPMI_HOST=3Dy CONFIG_NPCM7XX=3Dy CONFIG_COLLIE=3Dy CONFIG_ASPEED_SOC=3Dy diff --git a/hw/ipmi/Kconfig b/hw/ipmi/Kconfig index 9befd4f422..9e487eb42f 100644 --- a/hw/ipmi/Kconfig +++ b/hw/ipmi/Kconfig @@ -6,6 +6,11 @@ config IPMI_LOCAL default y depends on IPMI =20 +config IPMI_HOST + bool + default y + depends on IPMI + config IPMI_EXTERN bool default y diff --git a/hw/ipmi/ipmi_host.c b/hw/ipmi/ipmi_host.c new file mode 100644 index 0000000000..7a6d4eb323 --- /dev/null +++ b/hw/ipmi/ipmi_host.c @@ -0,0 +1,40 @@ +/* + * IPMI Host emulation + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "hw/ipmi/ipmi_host.h" +#include "hw/ipmi/ipmi_responder.h" + +static TypeInfo ipmi_responder_type_info =3D { + .name =3D TYPE_IPMI_RESPONDER, + .parent =3D TYPE_INTERFACE, + .class_size =3D sizeof(IPMIResponderClass), +}; + +static TypeInfo ipmi_host_type_info =3D { + .name =3D TYPE_IPMI_HOST, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(IPMIHost), + .abstract =3D true, + .class_size =3D sizeof(IPMIHostClass), +}; + +static void ipmi_register_types(void) +{ + type_register_static(&ipmi_responder_type_info); + type_register_static(&ipmi_host_type_info); +} + +type_init(ipmi_register_types) diff --git a/hw/ipmi/meson.build b/hw/ipmi/meson.build index 9622ea2a2c..9ec4dcb957 100644 --- a/hw/ipmi/meson.build +++ b/hw/ipmi/meson.build @@ -7,5 +7,6 @@ ipmi_ss.add(when: 'CONFIG_PCI_IPMI_KCS', if_true: files('pc= i_ipmi_kcs.c')) ipmi_ss.add(when: 'CONFIG_ISA_IPMI_BT', if_true: files('isa_ipmi_bt.c')) ipmi_ss.add(when: 'CONFIG_PCI_IPMI_BT', if_true: files('pci_ipmi_bt.c')) ipmi_ss.add(when: 'CONFIG_IPMI_SSIF', if_true: files('smbus_ipmi.c')) +ipmi_ss.add(when: 'CONFIG_IPMI_HOST', if_true: files('ipmi_host.c')) =20 softmmu_ss.add_all(when: 'CONFIG_IPMI', if_true: ipmi_ss) diff --git a/include/hw/ipmi/ipmi_host.h b/include/hw/ipmi/ipmi_host.h new file mode 100644 index 0000000000..a703cc3854 --- /dev/null +++ b/include/hw/ipmi/ipmi_host.h @@ -0,0 +1,56 @@ +/* + * IPMI host interface + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef HW_IPMI_HOST_H +#define HW_IPMI_HOST_H + +#include "hw/ipmi/ipmi_responder.h" + +#define TYPE_IPMI_HOST "ipmi-host" +#define IPMI_HOST(obj) \ + OBJECT_CHECK(IPMIHost, (obj), TYPE_IPMI_HOST) +#define IPMI_HOST_CLASS(obj_class) \ + OBJECT_CLASS_CHECK(IPMIHostClass, (obj_class), TYPE_IPMI_HOST) +#define IPMI_HOST_GET_CLASS(obj) \ + OBJECT_GET_CLASS(IPMIHostClass, (obj), TYPE_IPMI_HOST) + +/** + * struct IPMIHost defines an IPMI host interface. It can be a simulator o= r a + * connection to an emulated or real host. + * @responder: The IPMI responder that handles an IPMI message. + */ +typedef struct IPMIHost { + DeviceState parent; + + IPMIResponder *responder; +} IPMIHost; + +/** + * struct IPMIHostClass defines an IPMI host class. + * @handle_command: Handle a command to the host. + */ +typedef struct IPMIHostClass { + DeviceClass parent; + + /* + * Handle a command to the bmc. + */ + void (*handle_command)(struct IPMIHost *s, + uint8_t *cmd, unsigned int cmd_len, + unsigned int max_cmd_len, uint8_t msg_id); +} IPMIHostClass; + +#endif /* HW_IPMI_HOST_H */ diff --git a/include/hw/ipmi/ipmi_responder.h b/include/hw/ipmi/ipmi_respon= der.h new file mode 100644 index 0000000000..e3e4ef39d4 --- /dev/null +++ b/include/hw/ipmi/ipmi_responder.h @@ -0,0 +1,66 @@ +/* + * IPMI responder interface + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef HW_IPMI_RESPONDER_H +#define HW_IPMI_RESPONDER_H + +#include "qemu/osdep.h" +#include "hw/qdev-core.h" +#include "qom/object.h" + +#define TYPE_IPMI_RESPONDER_PREFIX "ipmi-responder-" +#define TYPE_IPMI_RESPONDER "ipmi-responder" +#define IPMI_RESPONDER(obj) \ + INTERFACE_CHECK(IPMIResponder, (obj), TYPE_IPMI_RESPONDER) +#define IPMI_RESPONDER_CLASS(class) \ + OBJECT_CLASS_CHECK(IPMIResponderClass, (class), TYPE_IPMI_RESPONDER) +#define IPMI_RESPONDER_GET_CLASS(class) \ + OBJECT_GET_CLASS(IPMIResponderClass, (class), TYPE_IPMI_RESPONDER) + +struct IPMIHost; + +/** + * This interface is implemented by each IPMI responder device (KCS, BT, P= CI, + * etc.) An IPMI host device uses it to transfer data to the emulated BMC. + */ +typedef struct IPMIResponder IPMIResponder; + +/** + * struct IPMIResponderClass implemented by an IPMI responder device like = KCS to + * handle commands from connected IPMI host device. + * @get_host: Return the IPMI host (e.g. ipmi-host-extern) that uses this + * responder. + * @set_host: Set the IPMI host (e.g. ipmi-host-extern) that uses this + * responder. + * @get_backend_data: Return the backend device (e.g. KCS, BT) of the + * corresponding responder. + * @handle_req: The IPMI Host device calls this function when it receives = a sane + * IPMI message. A responder should handle this message. + */ +typedef struct IPMIResponderClass { + InterfaceClass parent; + + struct IPMIHost *(*get_host)(struct IPMIResponder *s); + + void (*set_host)(struct IPMIResponder *s, struct IPMIHost *h); + + void *(*get_backend_data)(struct IPMIResponder *s); + + void (*handle_req)(struct IPMIResponder *s, uint8_t msg_id, + unsigned char *req, unsigned req_len); +} IPMIResponderClass; + +#endif /* HW_IPMI_RESPONDER_H */ --=20 2.29.2.684.gfbc64c5ab5-goog From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607652072; cv=none; d=zohomail.com; s=zohoarc; b=YXJyBxIpNBMKhz3yHHwpD+tGvh6cO62MJ3hDllDpe+a1VOcSxa4svxXCC+AZdQjIjPph9b0cyg3t6hwPNuOfbz1gpus3+y2lJOq14npPF3jCuDmYZCz5MYWrPK0yhrQ3/DUKYLSwpxC6LOHAjopRf3pmgCWVZtNhf8Iu1eB6QPA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607652072; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=KaejCfR3XIMBW9+kHrvCrE/ARFgjCDfumPKiprTzBVo=; b=ZE1CZTMTbKNw32S9uRFi7cY31CGfvwJ6znGo2KuX9E9JQ8msQXaRXIVSWNitrzj5w31/WdgBjquhZXTR1/alSvR0l2dDuvxfxem+iNHwm22ZRR8qBqXmJfVe7SRTwzB2NPjffXHVWo7YC/uU/mCCunJ1XYAT9xb2lJ3Dc1TpeOo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607652072516331.2394352752391; Thu, 10 Dec 2020 18:01:12 -0800 (PST) Received: from localhost ([::1]:44750 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXk7-0000Fl-15 for importer@patchew.org; Thu, 10 Dec 2020 21:01:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58376) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <31tDSXwgKCsc97un165ut11tyr.p1z3rz7-qr8ry010t07.14t@flex--wuhaotsh.bounces.google.com>) id 1knXbh-0001jg-LU for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:29 -0500 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]:50515) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <31tDSXwgKCsc97un165ut11tyr.p1z3rz7-qr8ry010t07.14t@flex--wuhaotsh.bounces.google.com>) id 1knXbc-0007BU-G0 for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:28 -0500 Received: by mail-pg1-x54a.google.com with SMTP id j30so5406149pgj.17 for ; Thu, 10 Dec 2020 17:52:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=KaejCfR3XIMBW9+kHrvCrE/ARFgjCDfumPKiprTzBVo=; b=eMW2C0xAHjbgTBHVItwmvC8LqIpMfvd6hRGLEXhscoQV3M4BkGSLpXlHBu5vtV93mo 17WtvcNppcia65Yoyh85K0H/0oVzr2Nw35OHDByNCymA8v9KTe0tBQ+r6LgvQPFDnj8A rr/TkRIbZdhRrISkDpGfnDIfglBezqldcrLgUSlZgTBgk9DH8/dOI5WcOmmt1U25TYbl GMdFkXpbWxaXCHREDfw92fgNU27AhyW/fziua0ij9rsQb6f1bz5jI/sY5pQ2r5Hq9FWP z6E8/PNk5MJ2lQEqL37dZnxgtR1pmhIdZrbv0id4krbZX329NlIdIUfCJpOBuUAp02rQ HTfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=KaejCfR3XIMBW9+kHrvCrE/ARFgjCDfumPKiprTzBVo=; b=WdeEBkV8LaBUzEJFvaHnGpi79s8/Mj3wqeY6gMay+rgaKkxAjFt79ZK+02BXzE1C5e JaimYc3heaMZIeI32JfWBFoIpJR7WsFUbTpwY3VWawR0bzkItQSrNzpz5mK5+pSuKHK1 VuXKaYzXB7o6RaeCh0KKW8iolFDnbvUutngT/L3EJ/VvhMKYss+4b65rLT0tiic7Agt4 gztLnB0dF/0Z2PLOHPgERtqsD7psJDc5G8X1lCFhFAHy0IOrWhRyA+fA/EtaRYFKOqAI /OWxAySQ7ojAqJIcIWhRA5bZUgFYVfsyOEWeWC7TdikbfLKGfWLkXlQIExEjPpNIxbHd keFw== X-Gm-Message-State: AOAM533nfX3SF4l4+S+iVH8cS5vpKUqIISG18vzjp1XBh93TzRPo/lZ0 Lk7wW5vs7YoKmzFX/xsUsx+LNqlBzYn6VA== X-Google-Smtp-Source: ABdhPJwQAabsUg7mxm3bsNvB0LKnuMuFLBCZXNUPtuCtO9sUFjKS71hdEGzAmwbF0m3ZsLSlhn2qmLPC3EAbDA== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a17:902:c215:b029:da:b079:b9a3 with SMTP id 21-20020a170902c215b02900dab079b9a3mr8856777pll.67.1607651542761; Thu, 10 Dec 2020 17:52:22 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:55 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-7-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 6/7] hw/ipmi: Add a KCS Module for NPCM7XX To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::54a; envelope-from=31tDSXwgKCsc97un165ut11tyr.p1z3rz7-qr8ry010t07.14t@flex--wuhaotsh.bounces.google.com; helo=mail-pg1-x54a.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KCS module for NPCM7xx SoC. This module implements the IPMI responder interface and is responsible to communicate with an external host via the KCS channels in an NPCM7xx SoC. Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- docs/system/arm/nuvoton.rst | 2 +- hw/arm/npcm7xx.c | 10 +- hw/ipmi/meson.build | 1 + hw/ipmi/npcm7xx_kcs.c | 570 ++++++++++++++++++++++++++++++++++ include/hw/arm/npcm7xx.h | 2 + include/hw/ipmi/npcm7xx_kcs.h | 105 +++++++ 6 files changed, 688 insertions(+), 2 deletions(-) create mode 100644 hw/ipmi/npcm7xx_kcs.c create mode 100644 include/hw/ipmi/npcm7xx_kcs.h diff --git a/docs/system/arm/nuvoton.rst b/docs/system/arm/nuvoton.rst index a1786342e2..21a02deba5 100644 --- a/docs/system/arm/nuvoton.rst +++ b/docs/system/arm/nuvoton.rst @@ -43,6 +43,7 @@ Supported devices * GPIO controller * Analog to Digital Converter (ADC) * Pulse Width Modulation (PWM) + * Keyboard Controller Style (KCS) channels =20 Missing devices --------------- @@ -50,7 +51,6 @@ Missing devices * LPC/eSPI host-to-BMC interface, including =20 * Keyboard and mouse controller interface (KBCI) - * Keyboard Controller Style (KCS) channels * BIOS POST code FIFO * System Wake-up Control (SWC) * Shared memory (SHM) diff --git a/hw/arm/npcm7xx.c b/hw/arm/npcm7xx.c index 72040d4079..4b7116d496 100644 --- a/hw/arm/npcm7xx.c +++ b/hw/arm/npcm7xx.c @@ -46,6 +46,7 @@ #define NPCM7XX_CLK_BA (0xf0801000) #define NPCM7XX_MC_BA (0xf0824000) #define NPCM7XX_RNG_BA (0xf000b000) +#define NPCM7XX_KCS_BA (0xf0007000) =20 /* USB Host modules */ #define NPCM7XX_EHCI_BA (0xf0806000) @@ -82,6 +83,7 @@ enum NPCM7xxInterrupt { NPCM7XX_UART1_IRQ, NPCM7XX_UART2_IRQ, NPCM7XX_UART3_IRQ, + NPCM7XX_KCS_HIB_IRQ =3D 9, NPCM7XX_TIMER0_IRQ =3D 32, /* Timer Module 0 */ NPCM7XX_TIMER1_IRQ, NPCM7XX_TIMER2_IRQ, @@ -353,6 +355,7 @@ static void npcm7xx_init(Object *obj) object_initialize_child(obj, "gpio[*]", &s->gpio[i], TYPE_NPCM7XX_= GPIO); } =20 + object_initialize_child(obj, "kcs", &s->kcs, TYPE_NPCM7XX_KCS); object_initialize_child(obj, "ehci", &s->ehci, TYPE_NPCM7XX_EHCI); object_initialize_child(obj, "ohci", &s->ohci, TYPE_SYSBUS_OHCI); =20 @@ -509,6 +512,12 @@ static void npcm7xx_realize(DeviceState *dev, Error **= errp) npcm7xx_irq(s, NPCM7XX_GPIO0_IRQ + i)); } =20 + /* KCS modules. Cannot fail. */ + sysbus_realize(SYS_BUS_DEVICE(&s->kcs), &error_abort); + sysbus_mmio_map(SYS_BUS_DEVICE(&s->kcs), 0, NPCM7XX_KCS_BA); + sysbus_connect_irq(SYS_BUS_DEVICE(&s->kcs), 0, + npcm7xx_irq(s, NPCM7XX_KCS_HIB_IRQ)); + /* USB Host */ object_property_set_bool(OBJECT(&s->ehci), "companion-enable", true, &error_abort); @@ -574,7 +583,6 @@ static void npcm7xx_realize(DeviceState *dev, Error **e= rrp) create_unimplemented_device("npcm7xx.shm", 0xc0001000, 4 * = KiB); create_unimplemented_device("npcm7xx.vdmx", 0xe0800000, 4 * = KiB); create_unimplemented_device("npcm7xx.pcierc", 0xe1000000, 64 * = KiB); - create_unimplemented_device("npcm7xx.kcs", 0xf0007000, 4 * = KiB); create_unimplemented_device("npcm7xx.gfxi", 0xf000e000, 4 * = KiB); create_unimplemented_device("npcm7xx.gpio[0]", 0xf0010000, 4 * = KiB); create_unimplemented_device("npcm7xx.gpio[1]", 0xf0011000, 4 * = KiB); diff --git a/hw/ipmi/meson.build b/hw/ipmi/meson.build index 9ec4dcb957..1261489fbd 100644 --- a/hw/ipmi/meson.build +++ b/hw/ipmi/meson.build @@ -8,5 +8,6 @@ ipmi_ss.add(when: 'CONFIG_ISA_IPMI_BT', if_true: files('isa= _ipmi_bt.c')) ipmi_ss.add(when: 'CONFIG_PCI_IPMI_BT', if_true: files('pci_ipmi_bt.c')) ipmi_ss.add(when: 'CONFIG_IPMI_SSIF', if_true: files('smbus_ipmi.c')) ipmi_ss.add(when: 'CONFIG_IPMI_HOST', if_true: files('ipmi_host.c')) +ipmi_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_kcs.c')) =20 softmmu_ss.add_all(when: 'CONFIG_IPMI', if_true: ipmi_ss) diff --git a/hw/ipmi/npcm7xx_kcs.c b/hw/ipmi/npcm7xx_kcs.c new file mode 100644 index 0000000000..f568f0cf20 --- /dev/null +++ b/hw/ipmi/npcm7xx_kcs.c @@ -0,0 +1,570 @@ +/* + * Nuvoton NPCM7xx KCS Module + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "qemu/osdep.h" + +#include "hw/ipmi/ipmi_host.h" +#include "hw/ipmi/npcm7xx_kcs.h" +#include "migration/vmstate.h" +#include "qemu/bitops.h" +#include "qemu/log.h" +#include "qemu/units.h" + +/* Registers in each KCS channel. */ +typedef enum NPCM7xxKCSRegister { + NPCM7XX_KCSST, + NPCM7XX_KCSDO, + NPCM7XX_KCSDI, + NPCM7XX_KCSDOC, + NPCM7XX_KCSDOM, + NPCM7XX_KCSDIC, + NPCM7XX_KCSCTL, + NPCM7XX_KCSIC, + NPCM7XX_KCSIE, + NPCM7XX_KCS_REGS_END, +} NPCM7xxKCSRegister; + +static const hwaddr npcm7xx_kcs_channel_base[] =3D { + 0x0c, 0x1e, 0x30, 0x42 +}; + +#define NPCM7XX_KCS_REG_DIFF 2 + +/* Register field definitions. */ +#define NPCM7XX_CTL_OBEIE BIT(1) +#define NPCM7XX_CTL_IBFIE BIT(0) + +/* IPMI 2.0 Table 9.1 status register bits */ +#define KCS_ST_STATE(rv) extract8(rv, 6, 2) +#define KCS_ST_CMD BIT(3) +#define KCS_ST_SMS_ATN BIT(2) +#define KCS_ST_IBF BIT(1) +#define KCS_ST_OBF BIT(0) + +/* IPMI 2.0 Table 9.2 state bits */ +enum KCSState { + IDLE_STATE, + READ_STATE, + WRITE_STATE, + ERROR_STATE, +}; + +/* IPMI 2.0 Table 9.3 control codes */ +#define KCS_CMD_GET_STATUS_ABORT 0x60 +#define KCS_CMD_WRITE_START 0x61 +#define KCS_CMD_WRITE_END 0x62 +#define KCS_CMD_READ 0x68 + +/* Host Side Operations. */ + +static uint8_t npcm7xx_kcs_host_read_byte(NPCM7xxKCSChannel *c) +{ + uint8_t v; + + v =3D c->dbbout; + c->status &=3D ~KCS_ST_OBF; + if (c->ctl & NPCM7XX_CTL_OBEIE) { + qemu_irq_raise(c->owner->irq); + } + + return v; +} + +static void npcm7xx_kcs_host_write_byte(NPCM7xxKCSChannel *c, uint8_t valu= e, + bool is_cmd) +{ + c->dbbin =3D value; + c->status |=3D KCS_ST_IBF; + if (is_cmd) { + c->status |=3D KCS_ST_CMD; + } else { + c->status &=3D ~KCS_ST_CMD; + } + if (c->ctl & NPCM7XX_CTL_IBFIE) { + qemu_irq_raise(c->owner->irq); + } +} + +static void npcm7xx_kcs_handle_event(NPCM7xxKCSChannel *c) +{ + uint8_t v; + IPMIHostClass *hk; + + switch (KCS_ST_STATE(c->status)) { + case IDLE_STATE: + if (c->status & KCS_ST_OBF) { + /* Read a dummy byte. */ + npcm7xx_kcs_host_read_byte(c); + if (c->outlen > 0) { + /* Send to ipmi host when msg ends. */ + hk =3D IPMI_HOST_GET_CLASS(c->host); + hk->handle_command(c->host, c->outmsg, c->outlen, + MAX_IPMI_MSG_SIZE, c->last_msg_id); + /* The last byte has been read. return to empty state. */ + c->outlen =3D 0; + } + } + if (c->inlen > 0) { + /* Send to bmc the next request */ + npcm7xx_kcs_host_write_byte(c, KCS_CMD_WRITE_START, true); + c->last_byte_not_ready =3D true; + } + break; + case READ_STATE: + if (c->status & KCS_ST_OBF) { + /* Read in a byte from BMC */ + v =3D npcm7xx_kcs_host_read_byte(c); + if (c->outlen < MAX_IPMI_MSG_SIZE) { + c->outmsg[c->outlen++] =3D v; + } + npcm7xx_kcs_host_write_byte(c, KCS_CMD_READ, false); + } + break; + case WRITE_STATE: + if (c->status & KCS_ST_IBF) { + /* The guest hasn't read the byte yet. We'll wait. */ + break; + } + /* Clear OBF. */ + c->status &=3D ~KCS_ST_OBF; + /* If it's the last byte written, we need to first send a command.= */ + if (c->last_byte_not_ready && c->inpos =3D=3D c->inlen - 1) { + npcm7xx_kcs_host_write_byte(c, KCS_CMD_WRITE_END, true); + c->last_byte_not_ready =3D false; + } else { + npcm7xx_kcs_host_write_byte(c, c->inmsg[c->inpos++], false); + if (!c->last_byte_not_ready) { + /* The last byte has been sent. */ + c->inlen =3D 0; + c->inpos =3D 0; + } + } + break; + case ERROR_STATE: + if (c->status & KCS_ST_OBF) { + /* Read in error byte from BMC */ + npcm7xx_kcs_host_read_byte(c); + } + /* Force abort */ + c->outlen =3D 0; + c->inlen =3D 0; + c->inpos =3D 0; + if (!(c->status & KCS_ST_IBF)) { + npcm7xx_kcs_host_write_byte(c, KCS_CMD_GET_STATUS_ABORT, true); + } + break; + default: + g_assert_not_reached(); + } +} + +/* Received a request from the host and send it to BMC core. */ +static void npcm7xx_kcs_handle_req(IPMIResponder *ii, uint8_t msg_id, + unsigned char *req, unsigned req_len) +{ + IPMIResponderClass *iic =3D IPMI_RESPONDER_GET_CLASS(ii); + NPCM7xxKCSChannel *c =3D iic->get_backend_data(ii); + + /* Drop the request if the last request is still not handled. */ + if (c->inlen > 0) { + return; + } + if (req_len =3D=3D 0) { + return; + } + if (req_len > MAX_IPMI_MSG_SIZE) { + /* Truncate the extra bytes that don't fit. */ + req_len =3D MAX_IPMI_MSG_SIZE; + } + memcpy(c->inmsg, req, req_len); + c->inpos =3D 0; + c->inlen =3D req_len; + + c->last_msg_id =3D msg_id; + + npcm7xx_kcs_handle_event(c); +} + +/* Core Side Operations. */ +/* Return the channel index for addr. If the addr is out of range, return = -1. */ +static int npcm7xx_kcs_channel_index(hwaddr addr) +{ + int index; + + if (unlikely(addr < npcm7xx_kcs_channel_base[0])) { + return -1; + } + if (unlikely(addr >=3D npcm7xx_kcs_channel_base[NPCM7XX_KCS_NR_CHANNEL= S])) { + return -1; + } + if (unlikely(addr % NPCM7XX_KCS_REG_DIFF)) { + return -1; + } + + for (index =3D 0; index < NPCM7XX_KCS_NR_CHANNELS; ++index) { + if (addr < npcm7xx_kcs_channel_base[index + 1]) { + return index; + } + } + + g_assert_not_reached(); +} + +static NPCM7xxKCSRegister npcm7xx_kcs_reg_index(hwaddr addr, int channel) +{ + NPCM7xxKCSRegister reg; + + reg =3D (addr - npcm7xx_kcs_channel_base[channel]) / NPCM7XX_KCS_REG_D= IFF; + return reg; +} + +static uint8_t npcm7xx_kcs_read_byte(NPCM7xxKCSChannel *c, + NPCM7xxKCSRegister reg) +{ + uint8_t v =3D 0; + + v =3D c->dbbin; + + c->status &=3D ~KCS_ST_IBF; + if (c->ctl & NPCM7XX_CTL_IBFIE) { + qemu_irq_lower(c->owner->irq); + } + + if (reg =3D=3D NPCM7XX_KCSDIC) { + qemu_log_mask(LOG_UNIMP, + "%s: Host nSCIPME interrupt is not implemented.\n", + __func__); + } + + npcm7xx_kcs_handle_event(c); + return v; +} + +static void npcm7xx_kcs_write_byte(NPCM7xxKCSChannel *c, + NPCM7xxKCSRegister reg, uint8_t value) +{ + c->dbbout =3D value; + + c->status |=3D KCS_ST_OBF; + if (c->ctl & NPCM7XX_CTL_OBEIE) { + qemu_irq_lower(c->owner->irq); + } + + if (reg =3D=3D NPCM7XX_KCSDOC) { + qemu_log_mask(LOG_UNIMP, + "%s: Host nSCIPME interrupt is not implemented.\n", + __func__); + } else if (reg =3D=3D NPCM7XX_KCSDOM) { + qemu_log_mask(LOG_UNIMP, + "%s: Host nSMI interrupt is not implemented.\n", + __func__); + } + + npcm7xx_kcs_handle_event(c); +} + +static uint8_t npcm7xx_kcs_read_status(NPCM7xxKCSChannel *c) +{ + uint8_t status =3D c->status; + + return status; +} + +static void npcm7xx_kcs_write_status(NPCM7xxKCSChannel *c, uint8_t value) +{ + static const uint8_t mask =3D + KCS_ST_CMD | KCS_ST_IBF | KCS_ST_OBF; + + if (value & mask) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: read-only bits in 0x%02x ignor= ed\n", + __func__, value); + value &=3D ~mask; + } + + c->status =3D (c->status & mask) | value; +} + +static void npcm7xx_kcs_write_ctl(NPCM7xxKCSChannel *c, uint8_t value) +{ + if (value & ~(NPCM7XX_CTL_OBEIE | NPCM7XX_CTL_IBFIE)) { + qemu_log_mask(LOG_UNIMP, "%s: Host interrupts are not implemented.= \n", + __func__); + } + + c->ctl =3D value; +} + +static uint64_t npcm7xx_kcs_read(void *opaque, hwaddr addr, unsigned int s= ize) +{ + NPCM7xxKCSState *s =3D opaque; + uint64_t value =3D 0; + int channel; + NPCM7xxKCSRegister reg; + + channel =3D npcm7xx_kcs_channel_index(addr); + if (channel < 0 || channel >=3D NPCM7XX_KCS_NR_CHANNELS) { + qemu_log_mask(LOG_UNIMP, + "%s: read from addr 0x%04" HWADDR_PRIx + " is invalid or unimplemented.\n", + __func__, addr); + return value; + } + + reg =3D npcm7xx_kcs_reg_index(addr, channel); + switch (reg) { + case NPCM7XX_KCSDI: + case NPCM7XX_KCSDIC: + value =3D npcm7xx_kcs_read_byte(&s->channels[channel], reg); + break; + case NPCM7XX_KCSDO: + case NPCM7XX_KCSDOC: + case NPCM7XX_KCSDOM: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: register @ 0x%04" HWADDR_PRIx " is write-only\n= ", + __func__, addr); + break; + case NPCM7XX_KCSST: + value =3D npcm7xx_kcs_read_status(&s->channels[channel]); + break; + case NPCM7XX_KCSCTL: + value =3D s->channels[channel].ctl; + break; + case NPCM7XX_KCSIC: + value =3D s->channels[channel].ic; + break; + case NPCM7XX_KCSIE: + value =3D s->channels[channel].ie; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: read from invalid addr 0x%04" HWADDR_PRIx "\n", + __func__, addr); + } + + return value; +} + +static void npcm7xx_kcs_write(void *opaque, hwaddr addr, uint64_t v, + unsigned int size) +{ + NPCM7xxKCSState *s =3D opaque; + int channel; + NPCM7xxKCSRegister reg; + + channel =3D npcm7xx_kcs_channel_index(addr); + if (channel < 0 || channel >=3D NPCM7XX_KCS_NR_CHANNELS) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: write to addr 0x%04" HWADDR_PRIx + " is invalid or unimplemented.\n", + __func__, addr); + return; + } + + reg =3D npcm7xx_kcs_reg_index(addr, channel); + switch (reg) { + case NPCM7XX_KCSDI: + case NPCM7XX_KCSDIC: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: register @ 0x%04" HWADDR_PRIx " is read-only\n", + __func__, addr); + break; + case NPCM7XX_KCSDO: + case NPCM7XX_KCSDOC: + case NPCM7XX_KCSDOM: + npcm7xx_kcs_write_byte(&s->channels[channel], reg, v); + break; + case NPCM7XX_KCSST: + npcm7xx_kcs_write_status(&s->channels[channel], v); + break; + case NPCM7XX_KCSCTL: + npcm7xx_kcs_write_ctl(&s->channels[channel], v); + break; + case NPCM7XX_KCSIC: + qemu_log_mask(LOG_UNIMP, "%s: Host interrupts are not implemented.= \n", + __func__); + break; + case NPCM7XX_KCSIE: + qemu_log_mask(LOG_UNIMP, "%s: Host interrupts are not implemented.= \n", + __func__); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: read from invalid addr 0x%04" HWADDR_PRIx "\n", + __func__, addr); + } +} + +static const MemoryRegionOps npcm7xx_kcs_ops =3D { + .read =3D npcm7xx_kcs_read, + .write =3D npcm7xx_kcs_write, + .endianness =3D DEVICE_NATIVE_ENDIAN, + .valid =3D { + .min_access_size =3D 1, /* KCS registers are 8-bits. */ + .max_access_size =3D 1, /* KCS registers are 8-bits. */ + .unaligned =3D false, + }, +}; + +static const VMStateDescription vmstate_npcm7xx_kcs_channel =3D { + .name =3D "npcm7xx-kcs-channel", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8(status, NPCM7xxKCSChannel), + VMSTATE_UINT8(dbbout, NPCM7xxKCSChannel), + VMSTATE_UINT8(dbbin, NPCM7xxKCSChannel), + VMSTATE_UINT8(ctl, NPCM7xxKCSChannel), + VMSTATE_UINT8(ic, NPCM7xxKCSChannel), + VMSTATE_UINT8(ie, NPCM7xxKCSChannel), + VMSTATE_END_OF_LIST(), + }, +}; + +static IPMIHost *npcm7xx_kcs_get_host(IPMIResponder *ii) +{ + NPCM7xxKCSChannel *c =3D NPCM7XX_KCS_CHANNEL(ii); + + return c->host; +} + +static void npcm7xx_kcs_set_host(IPMIResponder *ii, IPMIHost *h) +{ + NPCM7xxKCSChannel *c =3D NPCM7XX_KCS_CHANNEL(ii); + + c->host =3D h; +} + +static void *npcm7xx_kcs_get_backend_data(IPMIResponder *ii) +{ + NPCM7xxKCSChannel *c =3D NPCM7XX_KCS_CHANNEL(ii); + + return c; +} + +static void npcm7xx_kcs_channel_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + IPMIResponderClass *iic =3D IPMI_RESPONDER_CLASS(klass); + + QEMU_BUILD_BUG_ON(NPCM7XX_KCS_REGS_END !=3D NPCM7XX_KCS_CHANNEL_NR_REG= S); + + dc->desc =3D "NPCM7xx KCS Channel"; + dc->vmsd =3D &vmstate_npcm7xx_kcs_channel; + + iic->get_host =3D npcm7xx_kcs_get_host; + iic->set_host =3D npcm7xx_kcs_set_host; + iic->get_backend_data =3D npcm7xx_kcs_get_backend_data; + iic->handle_req =3D npcm7xx_kcs_handle_req; +} + +static const TypeInfo npcm7xx_kcs_channel_info =3D { + .name =3D TYPE_NPCM7XX_KCS_CHANNEL, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(NPCM7xxKCSChannel), + .class_init =3D npcm7xx_kcs_channel_class_init, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_IPMI_RESPONDER }, + { }, + }, +}; + +static void npcm7xx_kcs_enter_reset(Object *obj, ResetType type) +{ + NPCM7xxKCSState *s =3D NPCM7XX_KCS(obj); + int i; + + for (i =3D 0; i < NPCM7XX_KCS_NR_CHANNELS; i++) { + NPCM7xxKCSChannel *c =3D &s->channels[i]; + + c->status =3D 0x00; + c->ctl =3D 0xc0; + c->ic =3D 0x41; + c->ie =3D 0x00; + } +} + +static void npcm7xx_kcs_hold_reset(Object *obj) +{ + NPCM7xxKCSState *s =3D NPCM7XX_KCS(obj); + + qemu_irq_lower(s->irq); +} + +static const VMStateDescription vmstate_npcm7xx_kcs =3D { + .name =3D "npcm7xx-kcs", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_END_OF_LIST(), + }, +}; + +static void npcm7xx_kcs_realize(DeviceState *dev, Error **errp) +{ + NPCM7xxKCSState *s =3D NPCM7XX_KCS(dev); + SysBusDevice *sbd =3D &s->parent; + int i; + + memory_region_init_io(&s->iomem, OBJECT(s), &npcm7xx_kcs_ops, s, + TYPE_NPCM7XX_KCS, 4 * KiB); + sysbus_init_mmio(sbd, &s->iomem); + sysbus_init_irq(sbd, &s->irq); + + for (i =3D 0; i < NPCM7XX_KCS_NR_CHANNELS; i++) { + s->channels[i].owner =3D s; + if (!qdev_realize(DEVICE(&s->channels[i]), NULL, errp)) { + return; + } + } +} + +static void npcm7xx_kcs_init(Object *obj) +{ + NPCM7xxKCSState *s =3D NPCM7XX_KCS(obj); + int i; + + for (i =3D 0; i < NPCM7XX_KCS_NR_CHANNELS; i++) { + object_initialize_child(obj, g_strdup_printf("channels[%d]", i), + &s->channels[i], TYPE_NPCM7XX_KCS_CHANNEL); + } +} + +static void npcm7xx_kcs_class_init(ObjectClass *klass, void *data) +{ + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->desc =3D "NPCM7xx Timer Controller"; + dc->vmsd =3D &vmstate_npcm7xx_kcs; + dc->realize =3D npcm7xx_kcs_realize; + rc->phases.enter =3D npcm7xx_kcs_enter_reset; + rc->phases.hold =3D npcm7xx_kcs_hold_reset; +} + +static const TypeInfo npcm7xx_kcs_info =3D { + .name =3D TYPE_NPCM7XX_KCS, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(NPCM7xxKCSState), + .class_init =3D npcm7xx_kcs_class_init, + .instance_init =3D npcm7xx_kcs_init, +}; + +static void npcm7xx_kcs_register_type(void) +{ + type_register_static(&npcm7xx_kcs_channel_info); + type_register_static(&npcm7xx_kcs_info); +} +type_init(npcm7xx_kcs_register_type); diff --git a/include/hw/arm/npcm7xx.h b/include/hw/arm/npcm7xx.h index f6227aa8aa..7d5ea24355 100644 --- a/include/hw/arm/npcm7xx.h +++ b/include/hw/arm/npcm7xx.h @@ -20,6 +20,7 @@ #include "hw/adc/npcm7xx_adc.h" #include "hw/cpu/a9mpcore.h" #include "hw/gpio/npcm7xx_gpio.h" +#include "hw/ipmi/npcm7xx_kcs.h" #include "hw/mem/npcm7xx_mc.h" #include "hw/misc/npcm7xx_clk.h" #include "hw/misc/npcm7xx_gcr.h" @@ -85,6 +86,7 @@ typedef struct NPCM7xxState { NPCM7xxMCState mc; NPCM7xxRNGState rng; NPCM7xxGPIOState gpio[8]; + NPCM7xxKCSState kcs; EHCISysBusState ehci; OHCISysBusState ohci; NPCM7xxFIUState fiu[2]; diff --git a/include/hw/ipmi/npcm7xx_kcs.h b/include/hw/ipmi/npcm7xx_kcs.h new file mode 100644 index 0000000000..bae4d21f5d --- /dev/null +++ b/include/hw/ipmi/npcm7xx_kcs.h @@ -0,0 +1,105 @@ +/* + * Nuvoton NPCM7xx KCS Module + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ +#ifndef NPCM7XX_KCS_H +#define NPCM7XX_KCS_H + +#include "exec/memory.h" +#include "hw/ipmi/ipmi.h" +#include "hw/ipmi/ipmi_host.h" +#include "hw/ipmi/ipmi_responder.h" +#include "hw/irq.h" +#include "hw/sysbus.h" + +#define NPCM7XX_KCS_NR_CHANNELS 3 +/* + * Number of registers in each KCS channel. Don't change this without + * incrementing the version_id in the vmstate. + */ +#define NPCM7XX_KCS_CHANNEL_NR_REGS 9 + +typedef struct NPCM7xxKCSState NPCM7xxKCSState; + +/** + * struct NPCM7xxKCSChannel - KCS Channel that can be read or written by t= he + * host. + * @parent: Parent device. + * @owner: The KCS module that manages this KCS channel. + * @status: The status of this KCS module. + * @dbbout: The output buffer to the host. + * @dbbin: The input buffer from the host. + * @ctl: The control register. + * @ic: The host interrupt control register. Not implemented. + * @ie: The host interrupt enable register. Not implemented. + * @inmsg: The input message from the host. To be put in dbbin. + * @inpos: The current position of input message. + * @inlen: The length of input message. + * @outmsg: The input message from the host. To be put in dbbout. + * @outpos: The current position of output message. + * @outlen: The length of output message. + * @last_byte_not_ready: The last byte in inmsg is not ready to be sent. + * @last_msg_id: The message id of last incoming request from host. + */ +typedef struct NPCM7xxKCSChannel { + DeviceState parent; + + NPCM7xxKCSState *owner; + IPMIHost *host; + /* Core side registers. */ + uint8_t status; + uint8_t dbbout; + uint8_t dbbin; + uint8_t ctl; + uint8_t ic; + uint8_t ie; + + /* Host side buffers. */ + uint8_t inmsg[MAX_IPMI_MSG_SIZE]; + uint32_t inpos; + uint32_t inlen; + uint8_t outmsg[MAX_IPMI_MSG_SIZE]; + uint32_t outpos; + uint32_t outlen; + + /* Flags. */ + bool last_byte_not_ready; + uint8_t last_msg_id; +} NPCM7xxKCSChannel; + +/** + * struct NPCM7xxKCSState - Keyboard Control Style (KCS) Module device sta= te. + * @parent: System bus device. + * @iomem: Memory region through which registers are accessed. + * @irq: GIC interrupt line to fire on reading or writing the buffer. + * @channels: The KCS channels this module manages. + */ +struct NPCM7xxKCSState { + SysBusDevice parent; + + MemoryRegion iomem; + + qemu_irq irq; + NPCM7xxKCSChannel channels[NPCM7XX_KCS_NR_CHANNELS]; +}; + +#define TYPE_NPCM7XX_KCS_CHANNEL "npcm7xx-kcs-channel" +#define NPCM7XX_KCS_CHANNEL(obj) \ + OBJECT_CHECK(NPCM7xxKCSChannel, (obj), TYPE_NPCM7XX_KCS_CHANNEL) + +#define TYPE_NPCM7XX_KCS "npcm7xx-kcs" +#define NPCM7XX_KCS(obj) \ + OBJECT_CHECK(NPCM7xxKCSState, (obj), TYPE_NPCM7XX_KCS) + +#endif /* NPCM7XX_KCS_H */ --=20 2.29.2.684.gfbc64c5ab5-goog From nobody Fri May 17 08:24:46 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607652268; cv=none; d=zohomail.com; s=zohoarc; b=YjQJ6DQPMpt7/QHOh09kXMp3HpeI/5i+1hBt+FQOclbfgqqq1SPAJddn+4kxSNX74hdZEnzRDbWKRsI8LUq9+0loVCKQ4tMlbqRT7BF2DEuSv7+iPyEqEbeKnnu3aT3pKp8GZvDnAUOGtemALquJkeQClvDedt4FQUf3wcZqMWQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607652268; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=4VHKIK3Zl3M4dYV9Wc80s3WWD/x1jRZu8ubXlR1N6DM=; b=dz58dkOaQyjScLePAnx7d7lo+vq/RR+U1/aG9DngKXR6meEQ2H+MUal1TXfXAMG3xUk0QFmJRO99GZOmOy4enFuGv/8mUECOrfDzgzgvsWYENMBJDKrDSk1kVlF/oYuBi9j/HH++u3ohor7OqmlAwX7QqAqy8pzVr+W5UUoo804= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607652268615611.7022716763105; Thu, 10 Dec 2020 18:04:28 -0800 (PST) Received: from localhost ([::1]:49022 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knXnF-0002DW-LZ for importer@patchew.org; Thu, 10 Dec 2020 21:04:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58414) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <32NDSXwgKCskB9wp387wv33v0t.r315t19-stAt0232v29.36v@flex--wuhaotsh.bounces.google.com>) id 1knXbj-0001mp-T2 for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:31 -0500 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]:47881) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <32NDSXwgKCskB9wp387wv33v0t.r315t19-stAt0232v29.36v@flex--wuhaotsh.bounces.google.com>) id 1knXbg-0007DJ-Sq for qemu-devel@nongnu.org; Thu, 10 Dec 2020 20:52:31 -0500 Received: by mail-pf1-x449.google.com with SMTP id x21so2283742pff.14 for ; Thu, 10 Dec 2020 17:52:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=4VHKIK3Zl3M4dYV9Wc80s3WWD/x1jRZu8ubXlR1N6DM=; b=uQFAe2BpBghVBNc/UxrQNiWLx5F9dWy4QS68F88VuJyt2d0na3IygfiXqkO0Yic8pR P61dCepmArZITifLBTjO7Tn96mQZhBdKEzreU8KWkcNsVQepHLMzGj0KCj8xJalLJDgx pKkPRDVho8l9GL5BuXS1SCZihDLQB54q87pP17ALcVk1/Ny/fd7oX4xRBrjCoE/njMeZ lCuQVZrVV4JkJWldeY61RSfPOy0Er9dtBe8cXhc60YGzQ5gozFBkQxeKrlj7ZkB0F5ad O3s1QIWwdwrFpP2g2M1pO0mNOAwlx3UZOUrZJPXY/Y47nf6SiKyTdTZLT+7l3/X4rcKj vKqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4VHKIK3Zl3M4dYV9Wc80s3WWD/x1jRZu8ubXlR1N6DM=; b=P5NLOXEJVMwl3s/CHOgirQ5TWV8yo3sg3+0irWot6lDIKkvSmagNeq2Kf1ZsCglp9y QsMXBSyLsBkpjrbM+zIxu3jYRu1byitjE/a0RUVvu5quaubIIHK6L/W0ifoX+aUEoxGZ C0jwsoqCBphoQBDYaAo5+b/1VB1iwk3Qpqw5dEQQbLZgxieq2DCY/rfhPp3euX9c7106 69GIAA36+ubnEoXI71/r5GPwXR6m7OMB+NFoIP9IHYLLBT2UIXuD/dSHCi23bk4Hb46L SsDBakv1WcC+gs+gomziPiHg6Eo/NBI7a2IL9jCOuUyP3UXD4zrriO+EvkA5C9pFCj/U UBCg== X-Gm-Message-State: AOAM530PtQwJ4KjSdXSzFP2TSrMlwbxbvYFE2R3JbzUH2dLv6D558csJ 7GhIgToctkjl151rjK0nuMB7xME/6VH/7w== X-Google-Smtp-Source: ABdhPJwy0+jZcwcZ+gKJtVGJkJVSzKMWmTbD6ZaqSa0gZaii1fa6++vlodZFDlhHwSo2He8fFTwVHKnuLMKM+w== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:10:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a17:90a:414d:: with SMTP id m13mr10864989pjg.229.1607651544556; Thu, 10 Dec 2020 17:52:24 -0800 (PST) Date: Thu, 10 Dec 2020 17:51:56 -0800 In-Reply-To: <20201211015156.2053372-1-wuhaotsh@google.com> Message-Id: <20201211015156.2053372-8-wuhaotsh@google.com> Mime-Version: 1.0 References: <20201211015156.2053372-1-wuhaotsh@google.com> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog Subject: [PATCH 7/7] hw/ipmi: Add an IPMI external host device To: peter.maydell@linaro.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::449; envelope-from=32NDSXwgKCskB9wp387wv33v0t.r315t19-stAt0232v29.36v@flex--wuhaotsh.bounces.google.com; helo=mail-pf1-x449.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: minyard@acm.org, venture@google.com, qemu-devel@nongnu.org, hskinnemoen@google.com, wuhaotsh@google.com, kfting@nuvoton.com, qemu-arm@nongnu.org, Avi.Fishman@nuvoton.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Reply-to: Hao Wu From: Hao Wu via X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The IPMI external host device works for Baseband Management Controller (BMC) emulations. It works as a representation of a host class that connects to a given BMC. It can connect to a real host hardware or a emulated or simulated host device. In particular it can connect to a host QEMU instance with device ipmi-bmc-extern. For more details of IPMI host device in BMC emulation, see docs/specs/ipmi.rst. Reviewed-by: Tyrone Ting Signed-off-by: Hao Wu --- hw/ipmi/ipmi_host_extern.c | 435 +++++++++++++++++++++++++++++++++++++ hw/ipmi/meson.build | 1 + 2 files changed, 436 insertions(+) create mode 100644 hw/ipmi/ipmi_host_extern.c diff --git a/hw/ipmi/ipmi_host_extern.c b/hw/ipmi/ipmi_host_extern.c new file mode 100644 index 0000000000..6b5d45d403 --- /dev/null +++ b/hw/ipmi/ipmi_host_extern.c @@ -0,0 +1,435 @@ +/* + * IPMI Host external connection + * + * Copyright 2020 Google LLC + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WIT= HOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/* + * This is designed to connect to a host QEMU VM that runs ipmi_bmc_extern. + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "qemu/module.h" +#include "qapi/error.h" +#include "qemu/timer.h" +#include "chardev/char-fe.h" +#include "hw/ipmi/ipmi.h" +#include "hw/ipmi/ipmi_host.h" +#include "hw/ipmi/ipmi_responder.h" +#include "hw/qdev-properties.h" +#include "migration/vmstate.h" + +#define VM_MSG_CHAR 0xA0 /* Marks end of message */ +#define VM_CMD_CHAR 0xA1 /* Marks end of a command */ +#define VM_ESCAPE_CHAR 0xAA /* Set bit 4 from the next byte to 0 */ + +#define VM_PROTOCOL_VERSION 1 +#define VM_CMD_VERSION 0xff /* A version number byte follows */ +#define VM_CMD_RESET 0x04 +#define VM_CMD_CAPABILITIES 0x08 + +#define TYPE_IPMI_HOST_EXTERN "ipmi-host-extern" +#define IPMI_HOST_EXTERN(obj) OBJECT_CHECK(IPMIHostExtern, (obj), \ + TYPE_IPMI_HOST_EXTERN) + +typedef struct IPMIHostExtern { + IPMIHost parent; + CharBackend chr; + struct QEMUTimer *extern_timer; + + bool connected; + uint8_t capability; + + unsigned char inbuf[MAX_IPMI_MSG_SIZE + 2]; + unsigned int inpos; + bool in_escape; + bool in_too_many; + bool sending_cmd; + + unsigned char outbuf[(MAX_IPMI_MSG_SIZE + 2) * 2 + 1]; + unsigned int outpos; + unsigned int outlen; +} IPMIHostExtern; + +static unsigned char +ipmb_checksum(const unsigned char *data, int size, unsigned char start) +{ + unsigned char csum =3D start; + + for (; size > 0; size--, data++) { + csum +=3D *data; + } + return csum; +} + +static void continue_send(IPMIHostExtern *ihe) +{ + int ret; + + if (ihe->outlen =3D=3D 0) { + return; + } + ret =3D qemu_chr_fe_write(&ihe->chr, ihe->outbuf + ihe->outpos, + ihe->outlen - ihe->outpos); + if (ret > 0) { + ihe->outpos +=3D ret; + } + if (ihe->outpos < ihe->outlen) { + /* Not fully transmitted, try again in a 10ms */ + timer_mod_ns(ihe->extern_timer, + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 10000000); + } else { + /* Sent */ + ihe->outlen =3D 0; + ihe->outpos =3D 0; + } +} + +static void extern_timeout(void *opaque) +{ + IPMIHostExtern *ihe =3D opaque; + + if (ihe->connected) { + continue_send(ihe); + } +} + +static void addchar(IPMIHostExtern *ihe, unsigned char ch) +{ + switch (ch) { + case VM_MSG_CHAR: + case VM_CMD_CHAR: + case VM_ESCAPE_CHAR: + /* Escape the special characters. */ + ihe->outbuf[ihe->outlen++] =3D VM_ESCAPE_CHAR; + ch |=3D 0x10; + /* fall through */ + default: + ihe->outbuf[ihe->outlen++] =3D ch; + } +} + +static void send_version(IPMIHostExtern *ihe) +{ + addchar(ihe, VM_CMD_VERSION); + addchar(ihe, VM_PROTOCOL_VERSION); + ihe->outbuf[ihe->outlen++] =3D VM_CMD_CHAR; + continue_send(ihe); +} + +/* + * Handle a command (typically IPMI response) from IPMI responder + * and send it out to the external host. + */ +static void ipmi_host_extern_handle_command(IPMIHost *h, uint8_t *cmd, + unsigned cmd_len, unsigned max_cmd_len, uint8_t msg_id) +{ + IPMIHostExtern *ihe =3D IPMI_HOST_EXTERN(h); + uint8_t err =3D 0, csum; + int i; + + if (!ihe->connected) { + /* We are not connected to external host. Just do nothing. */ + return; + } + addchar(ihe, msg_id); + /* If it's too short or it was truncated, return an error. */ + if (cmd_len < 2) { + err =3D IPMI_CC_REQUEST_DATA_LENGTH_INVALID; + } else if ((cmd_len > max_cmd_len) || (cmd_len > MAX_IPMI_MSG_SIZE)) { + err =3D IPMI_CC_REQUEST_DATA_TRUNCATED; + } + if (err) { + /* Send out the error message */ + unsigned char rsp[3]; + + rsp[0] =3D cmd[0] | 0x04; + rsp[1] =3D cmd[1]; + rsp[2] =3D err; + for (i =3D 0; i < 3; ++i) { + addchar(ihe, rsp[i]); + } + csum =3D ipmb_checksum(&msg_id, 1, 0); + addchar(ihe, -ipmb_checksum(rsp, 3, csum)); + } else { + for (i =3D 0; i < cmd_len; i++) { + addchar(ihe, cmd[i]); + } + csum =3D ipmb_checksum(&msg_id, 1, 0); + addchar(ihe, -ipmb_checksum(cmd, cmd_len, csum)); + } + + ihe->outbuf[ihe->outlen++] =3D VM_MSG_CHAR; + + /* Start the transmit */ + continue_send(ihe); +} + +/* + * This function handles an IPMI message received from an external host by + * sending it to the IPMI responder class. + */ +static void handle_msg(IPMIHostExtern *ihe) +{ + IPMIResponderClass *k =3D IPMI_RESPONDER_GET_CLASS(ihe->parent.respond= er); + if (unlikely(ihe->in_escape)) { + ipmi_debug("msg escape not ended\n"); + return; + } + if (unlikely(ihe->inpos < 4)) { + ipmi_debug("msg too short\n"); + return; + } + if (unlikely(ihe->in_too_many)) { + ihe->inbuf[3] =3D IPMI_CC_REQUEST_DATA_TRUNCATED; + ihe->inpos =3D 4; + } else if (unlikely(ipmb_checksum(ihe->inbuf, ihe->inpos, 0) !=3D 0)) { + ipmi_debug("msg checksum failure\n"); + return; + } else { + ihe->inpos--; /* Remove checkum */ + } + + k->handle_req(ihe->parent.responder, ihe->inbuf[0], ihe->inbuf + 1, + ihe->inpos - 1); +} + +/* This function handles a control command from the host. */ +static void handle_command(IPMIHostExtern *ihe) +{ + uint8_t cmd; + + if (unlikely(ihe->in_too_many)) { + ipmi_debug("cmd in too many\n"); + return; + } + + if (unlikely(ihe->in_escape)) { + ipmi_debug("cmd ends with escape character\n"); + return; + } + + if (unlikely(ihe->inpos < 1)) { + ipmi_debug("empty command.\n"); + return; + } + + cmd =3D ihe->inbuf[0]; + switch (cmd) { + case VM_CMD_VERSION: + /* The host informs us the protocol version. */ + if (unlikely(ihe->inpos < 2)) { + ipmi_debug("Host cmd version truncated.\n"); + break; + } + if (unlikely(ihe->inbuf[1] !=3D VM_PROTOCOL_VERSION)) { + ipmi_debug("Host protocol version %u is different from our ver= sion" + " %u\n", ihe->inbuf[1], VM_PROTOCOL_VERSION); + } + break; + case VM_CMD_RESET: + /* The host tells us a reset has happened. */ + break; + case VM_CMD_CAPABILITIES: + /* The host tells us its capability. */ + if (unlikely(ihe->inpos < 2)) { + ipmi_debug("Host cmd capability truncated.\n"); + break; + } + ihe->capability =3D ihe->inbuf[1]; + break; + default: + /* The host shouldn't send us this command. Just ignore if they do= . */ + ipmi_debug("Host cmd type %02x is invalid.\n", cmd); + } +} + +/* Clear the state of ipmi-host-extern. Happens at the end of a message. */ +static void clear_state(IPMIHostExtern *ihe) +{ + ihe->in_escape =3D false; + ihe->in_too_many =3D false; + ihe->inpos =3D 0; +} + +/* We always welcome an incoming request. */ +static int can_receive(void *opaque) +{ + return 1; +} + +/* + * This function mirrors ipmi-bmc-extern. It handles an incoming character + * sequence and translates it into IPMI message. + */ +static void receive(void *opaque, const uint8_t *buf, int size) +{ + IPMIHostExtern *ihe =3D opaque; + int i; + + for (i =3D 0; i < size; ++i) { + uint8_t ch =3D buf[i]; + + switch (ch) { + case VM_MSG_CHAR: + /* The preceding characters are an IPMI message. */ + handle_msg(ihe); + clear_state(ihe); + break; + + case VM_CMD_CHAR: + /* The preceding characters are a control command. */ + handle_command(ihe); + clear_state(ihe); + break; + + case VM_ESCAPE_CHAR: + ihe->in_escape =3D true; + break; + + default: + if (ihe->in_escape) { + ch &=3D ~0x10; + ihe->in_escape =3D false; + } + if (ihe->in_too_many) { + break; + } + if (ihe->inpos >=3D ARRAY_SIZE(ihe->inbuf)) { + ihe->in_too_many =3D true; + break; + } + ihe->inbuf[ihe->inpos++] =3D ch; + break; + } + } + return; +} + +static void chr_event(void *opaque, QEMUChrEvent event) +{ + IPMIHostExtern *ihe =3D opaque; + + switch (event) { + case CHR_EVENT_OPENED: + ihe->connected =3D true; + clear_state(ihe); + send_version(ihe); + break; + + case CHR_EVENT_CLOSED: + ihe->connected =3D false; + break; + + case CHR_EVENT_BREAK: + case CHR_EVENT_MUX_IN: + case CHR_EVENT_MUX_OUT: + /* Ignore */ + break; + + default: + g_assert_not_reached(); + } +} + +static void ipmi_host_extern_responder_check(const Object *obj, + const char *name, Object *val, Error **errp) +{ + IPMIHostExtern *ihe =3D IPMI_HOST_EXTERN(obj); + + if (ihe->parent.responder) { + error_setg(errp, "IPMI host already has a responder"); + } +} + +static void ipmi_host_extern_realize(DeviceState *dev, Error **errp) +{ + IPMIHostExtern *ihe =3D IPMI_HOST_EXTERN(dev); + IPMIResponderClass *rk; + + if (!qemu_chr_fe_backend_connected(&ihe->chr)) { + error_setg(errp, "IPMI external host requires chardev attribute"); + return; + } + + qemu_chr_fe_set_handlers(&ihe->chr, can_receive, receive, + chr_event, NULL, ihe, NULL, true); + + if (ihe->parent.responder =3D=3D NULL) { + error_setg(errp, "IPMI host requires responder attribute"); + return; + } + rk =3D IPMI_RESPONDER_GET_CLASS(ihe->parent.responder); + rk->set_host(ihe->parent.responder, &ihe->parent); +} + +static const VMStateDescription vmstate_ipmi_host_extern =3D { + .name =3D TYPE_IPMI_HOST_EXTERN, + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_END_OF_LIST() + } +}; + +static void ipmi_host_extern_init(Object *obj) +{ + IPMIHostExtern *ihe =3D IPMI_HOST_EXTERN(obj); + + object_property_add_link(OBJECT(ihe), "responder", TYPE_IPMI_RESPONDER, + (Object **)(&ihe->parent.responder), + ipmi_host_extern_responder_check, OBJ_PROP_LINK_STRONG); + ihe->extern_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, extern_timeout,= ihe); + vmstate_register(NULL, 0, &vmstate_ipmi_host_extern, ihe); +} + +static void ipmi_host_extern_finalize(Object *obj) +{ + IPMIHostExtern *ihe =3D IPMI_HOST_EXTERN(obj); + + timer_del(ihe->extern_timer); + timer_free(ihe->extern_timer); +} + +static Property ipmi_host_extern_properties[] =3D { + DEFINE_PROP_CHR("chardev", IPMIHostExtern, chr), + DEFINE_PROP_END_OF_LIST(), +}; + +static void ipmi_host_extern_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + IPMIHostClass *bk =3D IPMI_HOST_CLASS(oc); + + bk->handle_command =3D ipmi_host_extern_handle_command; + dc->hotpluggable =3D false; + dc->realize =3D ipmi_host_extern_realize; + device_class_set_props(dc, ipmi_host_extern_properties); +} + +static const TypeInfo ipmi_host_extern_type =3D { + .name =3D TYPE_IPMI_HOST_EXTERN, + .parent =3D TYPE_IPMI_HOST, + .instance_size =3D sizeof(IPMIHostExtern), + .instance_init =3D ipmi_host_extern_init, + .instance_finalize =3D ipmi_host_extern_finalize, + .class_init =3D ipmi_host_extern_class_init, + }; + +static void ipmi_host_extern_register_types(void) +{ + type_register_static(&ipmi_host_extern_type); +} + +type_init(ipmi_host_extern_register_types) diff --git a/hw/ipmi/meson.build b/hw/ipmi/meson.build index 1261489fbd..8352913288 100644 --- a/hw/ipmi/meson.build +++ b/hw/ipmi/meson.build @@ -8,6 +8,7 @@ ipmi_ss.add(when: 'CONFIG_ISA_IPMI_BT', if_true: files('isa= _ipmi_bt.c')) ipmi_ss.add(when: 'CONFIG_PCI_IPMI_BT', if_true: files('pci_ipmi_bt.c')) ipmi_ss.add(when: 'CONFIG_IPMI_SSIF', if_true: files('smbus_ipmi.c')) ipmi_ss.add(when: 'CONFIG_IPMI_HOST', if_true: files('ipmi_host.c')) +ipmi_ss.add(when: 'CONFIG_IPMI_HOST', if_true: files('ipmi_host_extern.c')) ipmi_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_kcs.c')) =20 softmmu_ss.add_all(when: 'CONFIG_IPMI', if_true: ipmi_ss) --=20 2.29.2.684.gfbc64c5ab5-goog