From nobody Mon Feb 9 10:39:48 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1645730273; cv=none; d=zohomail.com; s=zohoarc; b=I/MyU1brjVbl2eXKMRFMf6UbvRJzNJSiTO93aeENbxyrFVuYPexYaHohwkb0yElG9mMx5wYDyCibdBBmLW3q/1UTY2eMfcq4M0AVwJzNKbI162IdcR0PhOFDD2A5y3Jj3fRcAuMb2PTITIOi1KlPY/XjfBOJbx0a50mrdL+VNVs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645730273; h=Content-Type:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=aoFDj0g9j291WWzqDA+b+r5vA2xS69km+c9mqJpaUfc=; b=WGdok/dGGqNlWhdX4IUCY5NB7SL8T7jcvSz6hAeEydexcZGu5FrNIr8rLOB0LLNe9rDEGn3fTqlEaTuJRIACC7TTRYdgBV0+r2BTD7c0Y27A/Md2RUh18o/fVWu1VHPIaFcnJTKBIsL5cc1lIjwTH2MVU9zw/vDFAGT9VOZBv7s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16457302735671023.5680506758273; Thu, 24 Feb 2022 11:17:53 -0800 (PST) Received: from localhost ([::1]:48732 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nNJce-0005qo-AM for importer@patchew.org; Thu, 24 Feb 2022 14:17:52 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58856) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3jNYXYggKCq8ljWPdihWVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--wuhaotsh.bounces.google.com>) id 1nNJVW-0007Rz-Ff for qemu-devel@nongnu.org; Thu, 24 Feb 2022 14:10:31 -0500 Received: from [2607:f8b0:4864:20::24a] (port=42575 helo=mail-oi1-x24a.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3jNYXYggKCq8ljWPdihWVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--wuhaotsh.bounces.google.com>) id 1nNJVM-0008UG-3L for qemu-devel@nongnu.org; Thu, 24 Feb 2022 14:10:23 -0500 Received: by mail-oi1-x24a.google.com with SMTP id bh17-20020a056808181100b002d4f3396ec3so1634732oib.9 for ; Thu, 24 Feb 2022 11:10:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=aoFDj0g9j291WWzqDA+b+r5vA2xS69km+c9mqJpaUfc=; b=pY+6bLfAJc73a81nFQnpVGIaEysq2xcXGBaGufLzO9OakHxMnHbTwJmyQeTUNftug/ Y5kGR1SV4VRJv15oLmXkQSqDnI1tVuId98LUoB3SazLuvwmlg4iEABXm3ZxRc94YjrfU oyihUk8GTPgKT4LtBM29PIqkdx3+4uNkxwayB/ptjBo+jb7wC0e2Epc2Pf7bZrCy6cJ5 bAcXXD+qWm8LL4qm/mjh73EiVrE2y2TTODW1cJ5bPiWN2tQi1lkKWW/Tuay7IUR9+/x6 3PSnYzcgVpiViv8+vxqImKdEN6GAZ/x3ENIbLBNCd4350j+w82aoUrPbVbo3c8UPw+gb nURg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=aoFDj0g9j291WWzqDA+b+r5vA2xS69km+c9mqJpaUfc=; b=1Q06eBvTOC2qDUlP5H+WAMT1WUsoX/TzoYYNaawI1p+m/Pt7u3cZpNPSySxSKJ5H43 969qbofVSkiJzSH29bjPNAgXiex2LNPjVp5s5ioZt8QV26Zsq1GY7iUgkSdPzDPSKrfD hh/XP9F90dUzyZVTAVqhfaoTcXJ7stvNDs4PAX1bBMEqe7wk42dz086W5u51k2JiYPjM eg3BrP3+HQdmaBDDuyQGO13rnfGHJahYQZAIoIRuj1Km2MQHRVRL/VB6wrpElGm3ne0M YjtEMK7xOB+P6lCm77HLkDvABIfhVRr8G+yAtYvBuYBRCwY8Af8n2DuEPUqtGfVvADMH EmDw== X-Gm-Message-State: AOAM531n0Lxc+RjBsNCouwNPzwSf4g8fzMvTW4EQwAa7PgqLsMjI8xbI b9ba/87oVHy46yiE/kGzcF2Upzlb61vo6g== X-Google-Smtp-Source: ABdhPJwnGvoY+6gtg6LWWrC9U75IQSpy/pY003GzL+hUVyoSRBeX4sXjKTn4b2YxHYQ94xdqu5UjQHc0fbbnEw== X-Received: from mimik.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4e]) (user=wuhaotsh job=sendgmr) by 2002:a17:90b:4f43:b0:1bc:7e5c:e024 with SMTP id pj3-20020a17090b4f4300b001bc7e5ce024mr137812pjb.0.1645729420437; Thu, 24 Feb 2022 11:03:40 -0800 (PST) Date: Thu, 24 Feb 2022 11:03:25 -0800 Message-Id: <20220224190325.2990336-1-wuhaotsh@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4] tests/qtest: add qtests for npcm7xx sdhci From: Hao Wu To: thuth@redhat.com, peter.maydell@linaro.org, lvivier@redhat.com, pbonzini@redhat.com Cc: qemu-arm@nongnu.org, qemu-devel@nongnu.org, wuhaotsh@google.com, venture@google.com, Avi.Fishman@nuvoton.com, kfting@nuvoton.com, hskinnemoen@google.com, f4bug@amsat.org, bin.meng@windriver.com, qemu-block@nongnu.org, maoshengtan2011@gmail.com, richard.henderson@linaro.org, Shengtan Mao , Chris Rauer X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::24a (failed) 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::24a; envelope-from=3jNYXYggKCq8ljWPdihWVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--wuhaotsh.bounces.google.com; helo=mail-oi1-x24a.google.com X-Spam_score_int: -81 X-Spam_score: -8.2 X-Spam_bar: -------- X-Spam_report: (-8.2 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, USER_IN_DEF_DKIM_WL=-7.5 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1645730276865100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Shengtan Mao Reviewed-by: Hao Wu Reviewed-by: Chris Rauer Signed-off-by: Shengtan Mao Signed-off-by: Patrick Venture Signed-off-by: Hao Wu --- v4: * use strncmp to compare fixed length strings v3: * fixup compilation from missing macro value v2: * update copyright year * check result of open * use g_free instead of free * move declarations to the top * use g_file_open_tmp --- tests/qtest/meson.build | 1 + tests/qtest/npcm7xx_sdhci-test.c | 215 +++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 tests/qtest/npcm7xx_sdhci-test.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 842b1df420..2b566999cd 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -189,6 +189,7 @@ qtests_npcm7xx =3D \ 'npcm7xx_gpio-test', 'npcm7xx_pwm-test', 'npcm7xx_rng-test', + 'npcm7xx_sdhci-test', 'npcm7xx_smbus-test', 'npcm7xx_timer-test', 'npcm7xx_watchdog_timer-test'] + \ diff --git a/tests/qtest/npcm7xx_sdhci-test.c b/tests/qtest/npcm7xx_sdhci-t= est.c new file mode 100644 index 0000000000..7de28f900b --- /dev/null +++ b/tests/qtest/npcm7xx_sdhci-test.c @@ -0,0 +1,215 @@ +/* + * QTests for NPCM7xx SD-3.0 / MMC-4.51 Host Controller + * + * Copyright (c) 2022 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/sd/npcm7xx_sdhci.h" + +#include "libqos/libqtest.h" +#include "libqtest-single.h" +#include "libqos/sdhci-cmd.h" + +#define NPCM7XX_REG_SIZE 0x100 +#define NPCM7XX_MMC_BA 0xF0842000 +#define NPCM7XX_BLK_SIZE 512 --- tests/qtest/meson.build | 1 + tests/qtest/npcm7xx_sdhci-test.c | 215 +++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 tests/qtest/npcm7xx_sdhci-test.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index f33d84d19b..721eafad12 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -190,6 +190,7 @@ qtests_npcm7xx =3D \ 'npcm7xx_gpio-test', 'npcm7xx_pwm-test', 'npcm7xx_rng-test', + 'npcm7xx_sdhci-test', 'npcm7xx_smbus-test', 'npcm7xx_timer-test', 'npcm7xx_watchdog_timer-test'] + \ diff --git a/tests/qtest/npcm7xx_sdhci-test.c b/tests/qtest/npcm7xx_sdhci-t= est.c new file mode 100644 index 0000000000..fbb4e2f2e1 --- /dev/null +++ b/tests/qtest/npcm7xx_sdhci-test.c @@ -0,0 +1,215 @@ +/* + * QTests for NPCM7xx SD-3.0 / MMC-4.51 Host Controller + * + * Copyright (c) 2022 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/sd/npcm7xx_sdhci.h" + +#include "libqos/libqtest.h" +#include "libqtest-single.h" +#include "libqos/sdhci-cmd.h" + +#define NPCM7XX_REG_SIZE 0x100 +#define NPCM7XX_MMC_BA 0xF0842000 +#define NPCM7XX_BLK_SIZE 512 +#define NPCM7XX_TEST_IMAGE_SIZE (1 << 30) + +char *sd_path; + +static QTestState *setup_sd_card(void) +{ + QTestState *qts =3D qtest_initf( + "-machine kudo-bmc " + "-device sd-card,drive=3Ddrive0 " + "-drive id=3Ddrive0,if=3Dnone,file=3D%s,format=3Draw,auto-read-onl= y=3Doff", + sd_path); + + qtest_writew(qts, NPCM7XX_MMC_BA + SDHC_SWRST, SDHC_RESET_ALL); + qtest_writew(qts, NPCM7XX_MMC_BA + SDHC_CLKCON, + SDHC_CLOCK_SDCLK_EN | SDHC_CLOCK_INT_STABLE | + SDHC_CLOCK_INT_EN); + sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_APP_CMD); + sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0x41200000, 0, (41 << 8)); + sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_ALL_SEND_CID); + sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_SEND_RELATIVE_ADD= R); + sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0x45670000, 0, + SDHC_SELECT_DESELECT_CARD); + + return qts; +} + +static void write_sdread(QTestState *qts, const char *msg) +{ + int fd, ret; + size_t len =3D strlen(msg); + char *rmsg =3D g_malloc(len); + + /* write message to sd */ + fd =3D open(sd_path, O_WRONLY); + g_assert(fd >=3D 0); + ret =3D write(fd, msg, len); + close(fd); + g_assert(ret =3D=3D len); + + /* read message using sdhci */ + ret =3D sdhci_read_cmd(qts, NPCM7XX_MMC_BA, rmsg, len); + g_assert(ret =3D=3D len); + g_assert(!strncmp(rmsg, msg, len)); + + g_free(rmsg); +} + +/* Check MMC can read values from sd */ +static void test_read_sd(void) +{ + QTestState *qts =3D setup_sd_card(); + + write_sdread(qts, "hello world"); + write_sdread(qts, "goodbye"); + + qtest_quit(qts); +} + +static void sdwrite_read(QTestState *qts, const char *msg) +{ + int fd, ret; + size_t len =3D strlen(msg); + char *rmsg =3D g_malloc(len); + + /* write message using sdhci */ + sdhci_write_cmd(qts, NPCM7XX_MMC_BA, msg, len, NPCM7XX_BLK_SIZE); + + /* read message from sd */ + fd =3D open(sd_path, O_RDONLY); + g_assert(fd >=3D 0); + ret =3D read(fd, rmsg, len); + close(fd); + g_assert(ret =3D=3D len); + + g_assert(!strncmp(rmsg, msg, len)); + + g_free(rmsg); +} + +/* Check MMC can write values to sd */ +static void test_write_sd(void) +{ + QTestState *qts =3D setup_sd_card(); + + sdwrite_read(qts, "hello world"); + sdwrite_read(qts, "goodbye"); + + qtest_quit(qts); +} + +/* Check SDHCI has correct default values. */ +static void test_reset(void) +{ + QTestState *qts =3D qtest_init("-machine kudo-bmc"); + uint64_t addr =3D NPCM7XX_MMC_BA; + uint64_t end_addr =3D addr + NPCM7XX_REG_SIZE; + uint16_t prstvals_resets[] =3D {NPCM7XX_PRSTVALS_0_RESET, + NPCM7XX_PRSTVALS_1_RESET, + 0, + NPCM7XX_PRSTVALS_3_RESET, + 0, + 0}; + int i; + uint32_t mask; + + while (addr < end_addr) { + switch (addr - NPCM7XX_MMC_BA) { + case SDHC_PRNSTS: + /* + * ignores bits 20 to 24: they are changed when reading regist= ers + */ + mask =3D 0x1f00000; + g_assert_cmphex(qtest_readl(qts, addr) | mask, =3D=3D, + NPCM7XX_PRSNTS_RESET | mask); + addr +=3D 4; + break; + case SDHC_BLKGAP: + g_assert_cmphex(qtest_readb(qts, addr), =3D=3D, NPCM7XX_BLKGAP= _RESET); + addr +=3D 1; + break; + case SDHC_CAPAB: + g_assert_cmphex(qtest_readq(qts, addr), =3D=3D, NPCM7XX_CAPAB_= RESET); + addr +=3D 8; + break; + case SDHC_MAXCURR: + g_assert_cmphex(qtest_readq(qts, addr), =3D=3D, NPCM7XX_MAXCUR= R_RESET); + addr +=3D 8; + break; + case SDHC_HCVER: + g_assert_cmphex(qtest_readw(qts, addr), =3D=3D, NPCM7XX_HCVER_= RESET); + addr +=3D 2; + break; + case NPCM7XX_PRSTVALS: + for (i =3D 0; i < NPCM7XX_PRSTVALS_SIZE; ++i) { + g_assert_cmphex(qtest_readw(qts, addr + 2 * i), =3D=3D, + prstvals_resets[i]); + } + addr +=3D NPCM7XX_PRSTVALS_SIZE * 2; + break; + default: + g_assert_cmphex(qtest_readb(qts, addr), =3D=3D, 0); + addr +=3D 1; + } + } + + qtest_quit(qts); +} + +static void drive_destroy(void) +{ + unlink(sd_path); + g_free(sd_path); +} + +static void drive_create(void) +{ + int fd, ret; + GError *error =3D NULL; + + /* Create a temporary raw image */ + fd =3D g_file_open_tmp("sdhci_XXXXXX", &sd_path, &error); + if (fd =3D=3D -1) { + fprintf(stderr, "unable to create sdhci file: %s\n", error->messag= e); + g_error_free(error); + } + g_assert(sd_path !=3D NULL); + + ret =3D ftruncate(fd, NPCM7XX_TEST_IMAGE_SIZE); + g_assert_cmpint(ret, =3D=3D, 0); + g_message("%s", sd_path); + close(fd); +} + +int main(int argc, char **argv) +{ + int ret; + + drive_create(); + + g_test_init(&argc, &argv, NULL); + + qtest_add_func("npcm7xx_sdhci/reset", test_reset); + qtest_add_func("npcm7xx_sdhci/write_sd", test_write_sd); + qtest_add_func("npcm7xx_sdhci/read_sd", test_read_sd); + + ret =3D g_test_run(); + drive_destroy(); + return ret; +} --=20 2.35.1.574.g5d30c73bfb-goog