From: Thomas Huth <thuth@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Stefan Berger" <stefanb@linux.ibm.com>,
"Cédric Le Goater" <clg@kaod.org>,
"Ninad Palsule" <ninad@linux.ibm.com>
Subject: [PULL 20/23] qtest: Add functions for accessing devices on Aspeed I2C controller
Date: Thu, 20 Apr 2023 12:12:13 +0200 [thread overview]
Message-ID: <20230420101216.786304-21-thuth@redhat.com> (raw)
In-Reply-To: <20230420101216.786304-1-thuth@redhat.com>
From: Stefan Berger <stefanb@linux.ibm.com>
Add read and write functions for accessing registers of I2C devices
connected to the Aspeed I2C controller.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Cédric Le Goater <clg@kaod.org>
Reviewed-by: Ninad Palsule <ninad@linux.ibm.com>
Message-Id: <20230331173051.3857801-2-stefanb@linux.ibm.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/hw/i2c/aspeed_i2c.h | 7 +++
tests/qtest/qtest_aspeed.h | 41 +++++++++++++
tests/qtest/qtest_aspeed.c | 117 ++++++++++++++++++++++++++++++++++++
3 files changed, 165 insertions(+)
create mode 100644 tests/qtest/qtest_aspeed.h
create mode 100644 tests/qtest/qtest_aspeed.c
diff --git a/include/hw/i2c/aspeed_i2c.h b/include/hw/i2c/aspeed_i2c.h
index adc904d6c1..51c944efea 100644
--- a/include/hw/i2c/aspeed_i2c.h
+++ b/include/hw/i2c/aspeed_i2c.h
@@ -38,6 +38,13 @@ OBJECT_DECLARE_TYPE(AspeedI2CState, AspeedI2CClass, ASPEED_I2C)
#define ASPEED_I2C_OLD_NUM_REG 11
#define ASPEED_I2C_NEW_NUM_REG 22
+#define A_I2CD_M_STOP_CMD BIT(5)
+#define A_I2CD_M_RX_CMD BIT(3)
+#define A_I2CD_M_TX_CMD BIT(1)
+#define A_I2CD_M_START_CMD BIT(0)
+
+#define A_I2CD_MASTER_EN BIT(0)
+
/* Tx State Machine */
#define I2CD_TX_STATE_MASK 0xf
#define I2CD_IDLE 0x0
diff --git a/tests/qtest/qtest_aspeed.h b/tests/qtest/qtest_aspeed.h
new file mode 100644
index 0000000000..235dfaa186
--- /dev/null
+++ b/tests/qtest/qtest_aspeed.h
@@ -0,0 +1,41 @@
+/*
+ * Aspeed i2c bus interface to reading and writing to i2c device registers
+ *
+ * Copyright (c) 2023 IBM Corporation
+ *
+ * Authors:
+ * Stefan Berger <stefanb@linux.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QTEST_ASPEED_H
+#define QTEST_ASPEED_H
+
+#include <stdint.h>
+
+#include "libqtest.h"
+
+#define AST2600_ASPEED_I2C_BASE_ADDR 0x1e78a000
+
+/* Implements only AST2600 I2C controller */
+
+static inline uint32_t ast2600_i2c_calc_bus_addr(uint8_t bus_num)
+{
+ return AST2600_ASPEED_I2C_BASE_ADDR + 0x80 + bus_num * 0x80;
+}
+
+uint8_t aspeed_i2c_readb(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr, uint8_t reg);
+uint16_t aspeed_i2c_readw(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr, uint8_t reg);
+uint32_t aspeed_i2c_readl(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr, uint8_t reg);
+void aspeed_i2c_writeb(QTestState *s, uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint8_t v);
+void aspeed_i2c_writew(QTestState *s, uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint16_t v);
+void aspeed_i2c_writel(QTestState *s, uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint32_t v);
+
+#endif
diff --git a/tests/qtest/qtest_aspeed.c b/tests/qtest/qtest_aspeed.c
new file mode 100644
index 0000000000..f6da9adea9
--- /dev/null
+++ b/tests/qtest/qtest_aspeed.c
@@ -0,0 +1,117 @@
+/*
+ * Aspeed i2c bus interface for reading from and writing to i2c device registers
+ *
+ * Copyright (c) 2023 IBM Corporation
+ *
+ * Authors:
+ * Stefan Berger <stefanb@linux.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qtest_aspeed.h"
+#include "hw/i2c/aspeed_i2c.h"
+
+static void aspeed_i2c_startup(QTestState *s, uint32_t baseaddr,
+ uint8_t slave_addr, uint8_t reg)
+{
+ uint32_t v;
+ static int once;
+
+ if (!once) {
+ /* one time: enable master */
+ qtest_writel(s, baseaddr + A_I2CC_FUN_CTRL, 0);
+ v = qtest_readl(s, baseaddr + A_I2CC_FUN_CTRL) | A_I2CD_MASTER_EN;
+ qtest_writel(s, baseaddr + A_I2CC_FUN_CTRL, v);
+ once = 1;
+ }
+
+ /* select device */
+ qtest_writel(s, baseaddr + A_I2CD_BYTE_BUF, slave_addr << 1);
+ qtest_writel(s, baseaddr + A_I2CD_CMD,
+ A_I2CD_M_START_CMD | A_I2CD_M_RX_CMD);
+
+ /* select the register to write to */
+ qtest_writel(s, baseaddr + A_I2CD_BYTE_BUF, reg);
+ qtest_writel(s, baseaddr + A_I2CD_CMD, A_I2CD_M_TX_CMD);
+}
+
+static uint32_t aspeed_i2c_read_n(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, size_t nbytes)
+{
+ uint32_t res = 0;
+ uint32_t v;
+ size_t i;
+
+ aspeed_i2c_startup(s, baseaddr, slave_addr, reg);
+
+ for (i = 0; i < nbytes; i++) {
+ qtest_writel(s, baseaddr + A_I2CD_CMD, A_I2CD_M_RX_CMD);
+ v = qtest_readl(s, baseaddr + A_I2CD_BYTE_BUF) >> 8;
+ res |= (v & 0xff) << (i * 8);
+ }
+
+ qtest_writel(s, baseaddr + A_I2CD_CMD, A_I2CD_M_STOP_CMD);
+
+ return res;
+}
+
+uint32_t aspeed_i2c_readl(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr, uint8_t reg)
+{
+ return aspeed_i2c_read_n(s, baseaddr, slave_addr, reg, sizeof(uint32_t));
+}
+
+uint16_t aspeed_i2c_readw(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr, uint8_t reg)
+{
+ return aspeed_i2c_read_n(s, baseaddr, slave_addr, reg, sizeof(uint16_t));
+}
+
+uint8_t aspeed_i2c_readb(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr, uint8_t reg)
+{
+ return aspeed_i2c_read_n(s, baseaddr, slave_addr, reg, sizeof(uint8_t));
+}
+
+static void aspeed_i2c_write_n(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint32_t v, size_t nbytes)
+{
+ size_t i;
+
+ aspeed_i2c_startup(s, baseaddr, slave_addr, reg);
+
+ for (i = 0; i < nbytes; i++) {
+ qtest_writel(s, baseaddr + A_I2CD_BYTE_BUF, v & 0xff);
+ v >>= 8;
+ qtest_writel(s, baseaddr + A_I2CD_CMD, A_I2CD_M_TX_CMD);
+ }
+
+ qtest_writel(s, baseaddr + A_I2CD_CMD, A_I2CD_M_STOP_CMD);
+}
+
+void aspeed_i2c_writel(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint32_t v)
+{
+ aspeed_i2c_write_n(s, baseaddr, slave_addr, reg, v, sizeof(v));
+}
+
+void aspeed_i2c_writew(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint16_t v)
+{
+ aspeed_i2c_write_n(s, baseaddr, slave_addr, reg, v, sizeof(v));
+}
+
+void aspeed_i2c_writeb(QTestState *s,
+ uint32_t baseaddr, uint8_t slave_addr,
+ uint8_t reg, uint8_t v)
+{
+ aspeed_i2c_write_n(s, baseaddr, slave_addr, reg, v, sizeof(v));
+}
--
2.31.1
next prev parent reply other threads:[~2023-04-20 10:17 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-20 10:11 [PULL 00/23] First batch of testing and misc patches for 8.1 Thomas Huth
2023-04-20 10:11 ` [PULL 01/23] hw: Add compat machines " Thomas Huth
2023-04-20 10:11 ` [PULL 02/23] docs: Fix typo (wphx => whpx) Thomas Huth
2023-04-20 10:11 ` [PULL 03/23] docs/cxl: Fix sentence Thomas Huth
2023-04-20 10:11 ` [PULL 04/23] test: Fix test-crypto-secret when compiling without keyring support Thomas Huth
2023-04-20 10:11 ` [PULL 05/23] qtest: Don't assert on "-qtest chardev:myid" Thomas Huth
2023-04-20 10:11 ` [PULL 06/23] chardev: Allow setting file chardev input file on the command line Thomas Huth
2023-04-20 10:12 ` [PULL 07/23] travis.yml: Add missing clang-10 package to the 'Clang (disable-tcg)' job Thomas Huth
2023-04-20 10:12 ` [PULL 08/23] travis.yml: Add missing 'flex', 'bison' packages to 'GCC (user)' job Thomas Huth
2023-04-20 10:12 ` [PULL 09/23] tests/migration: Only run auto_converge in slow mode Thomas Huth
2023-04-20 10:12 ` [PULL 10/23] target/i386: Set family/model/stepping of the "max" CPU according to LM bit Thomas Huth
2023-04-20 10:12 ` [PULL 11/23] hw/char: Move two more files from specific_ss to softmmu_ss Thomas Huth
2023-04-20 10:12 ` [PULL 12/23] softmmu/qtest: Move the target-specific pseries RTAS code out of qtest.c Thomas Huth
2023-04-20 10:12 ` [PULL 13/23] include/exec: Provide the tswap() functions for target independent code, too Thomas Huth
2023-04-20 10:12 ` [PULL 14/23] softmmu: Make qtest.c target independent Thomas Huth
2023-04-20 10:12 ` [PULL 15/23] hw/display: Compile vga.c as target-independent code Thomas Huth
2023-04-20 10:12 ` [PULL 16/23] softmmu: Move dirtylimit.c into the target independent source set Thomas Huth
2023-04-20 10:12 ` [PULL 17/23] hw/core: Move numa.c " Thomas Huth
2023-04-20 10:12 ` [PULL 18/23] cpu: Remove parameter of list_cpus() Thomas Huth
2023-04-20 10:12 ` [PULL 19/23] MAINTAINERS: Add Juan Quintela to developer guides review Thomas Huth
2023-04-20 10:12 ` Thomas Huth [this message]
2023-04-20 10:12 ` [PULL 21/23] qtest: Move tpm_util_tis_transmit() into tpm-tis-utils.c and rename it Thomas Huth
2023-04-20 10:12 ` [PULL 22/23] qtest: Add a test case for TPM TIS I2C connected to Aspeed I2C controller Thomas Huth
2023-04-20 10:12 ` [PULL 23/23] tests/vm/freebsd: Update to FreeBSD 13.2 Thomas Huth
2023-04-21 17:28 ` [PULL 00/23] First batch of testing and misc patches for 8.1 Richard Henderson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230420101216.786304-21-thuth@redhat.com \
--to=thuth@redhat.com \
--cc=clg@kaod.org \
--cc=ninad@linux.ibm.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanb@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).