linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhang Xiaoxu <zhangxiaoxu@huaweicloud.com>
To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com,
	linux-kernel@vger.kernel.org, broonie@kernel.org,
	rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com,
	linux-spi@vger.kernel.org
Subject: [PATCH -next 13/14] kddv/tests/mtd: Add test cases for mchp23k256 driver
Date: Sat, 18 Nov 2023 18:40:39 +0800	[thread overview]
Message-ID: <20231118104040.386381-14-zhangxiaoxu@huaweicloud.com> (raw)
In-Reply-To: <20231118104040.386381-1-zhangxiaoxu@huaweicloud.com>

From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>

This add test case for mchp23k256 mtd driver, test for
create mchp23k256 device, default size of the device and
read/write data from device.

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu@huawei.com>
---
 .../kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c   | 72 +++++++++++++++++++
 .../kddv/kddv/tests/mtd/test_mchp23k256.py    | 41 +++++++++++
 2 files changed, 113 insertions(+)
 create mode 100644 tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c
 create mode 100755 tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py

diff --git a/tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c b/tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c
new file mode 100644
index 000000000000..b1aa8a25edc0
--- /dev/null
+++ b/tools/testing/kddv/kddv/data/bpf/mtd/mtd-mchp23k256.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2022-2023 Huawei Technologies Co., Ltd */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <errno.h>
+
+#include "spi-xfer-base.h"
+
+#define MCHP23K256_CMD_WRITE_STATUS	0x01
+#define MCHP23K256_CMD_WRITE		0x02
+#define MCHP23K256_CMD_READ		0x03
+
+#define CHIP_REGS_SIZE			0x20000
+
+#define MAX_CMD_SIZE			4
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, CHIP_REGS_SIZE);
+	__type(key, __u32);
+	__type(value, __u8);
+} regs_mtd_mchp23k256 SEC(".maps");
+
+static unsigned int chip_reg;
+
+static int spi_transfer_read(struct spi_msg_ctx *msg, unsigned int len)
+{
+	return spi_xfer_read_u8(msg, len, &regs_mtd_mchp23k256, chip_reg, 0);
+}
+
+static int spi_transfer_write(struct spi_msg_ctx *msg, unsigned int len)
+{
+	u8 opcode = msg->data[0];
+	int i;
+
+	switch (opcode) {
+	case MCHP23K256_CMD_READ:
+	case MCHP23K256_CMD_WRITE:
+		if (len < 2)
+			return -EINVAL;
+
+		chip_reg = 0;
+		for (i = 0; i < MAX_CMD_SIZE && i < len - 1; i++)
+			chip_reg = (chip_reg << 8) + msg->data[1 + i];
+
+		return 0;
+	case MCHP23K256_CMD_WRITE_STATUS:
+		// ignore write status
+		return 0;
+	default:
+		break;
+	}
+
+	return spi_xfer_write_u8(msg, len, &regs_mtd_mchp23k256, chip_reg, 0);
+}
+
+SEC("raw_tp.w/spi_transfer_writeable")
+int BPF_PROG(mtd_mchp23k256, struct spi_msg_ctx *msg, u8 chip, unsigned int len)
+{
+	int ret = 0;
+
+	if (msg->tx_nbits)
+		ret = spi_transfer_write(msg, len);
+	else if (msg->rx_nbits)
+		ret = spi_transfer_read(msg, len);
+
+	return ret;
+}
+
+char LICENSE[] SEC("license") = "GPL";
diff --git a/tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py b/tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py
new file mode 100755
index 000000000000..eefad9d70483
--- /dev/null
+++ b/tools/testing/kddv/kddv/tests/mtd/test_mchp23k256.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel device driver verification
+#
+# Copyright (C) 2022-2023 Huawei Technologies Co., Ltd
+# Author: Wei Yongjun <weiyongjun1@huawei.com>
+
+from kddv.core import SPIDriverTest
+from . import MTDDriver
+
+MCHP23K256_TEST_DATA = [0x78] * 16
+
+class TestMCHP23K256(SPIDriverTest, MTDDriver):
+    name = "mchp23k256"
+
+    @property
+    def bpf(self):
+        return f"mtd-{self.name}"
+
+    def test_device_probe(self):
+        with self.assertRaisesFault():
+            with self.device() as _:
+                pass
+
+    def test_device_size(self):
+        with self.device() as dev:
+            size = self.mtd_read_attr(dev, 'size')
+            self.assertEqual(size, '32768')
+
+    def test_read_data(self):
+        with self.device() as dev:
+            self.write_regs(0x00, MCHP23K256_TEST_DATA)
+            data = self.mtd_read_bytes(dev, 16)
+            self.assertEqual(data, bytes(MCHP23K256_TEST_DATA))
+
+    def test_write_data(self):
+        with self.device() as dev:
+            self.write_regs(0x00, [0] * 16)
+            self.mtd_write_bytes(dev, bytes(MCHP23K256_TEST_DATA))
+            self.assertRegsEqual(0x00, MCHP23K256_TEST_DATA)
-- 
2.34.1


  parent reply	other threads:[~2023-11-18 10:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-18 10:40 [PATCH -next 00/14] Implement a ligth weight device driver test framework Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 01/14] kddv/core: " Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 02/14] kddv/core: Allow test case config bpf program Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 03/14] kddv/core: Add io fault support to " Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 04/14] kddv/core: Check kmsg before return from test case Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 05/14] kddv/core: Support kernel memory leak detector Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 06/14] kddv/core: Add page and slab fault inject support Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 07/14] kddv/cmd: Add command to create/remove mockup device Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 08/14] kddv/cmd: Add command to run testcases Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 09/14] kddv/core: Add test support for SPI driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 10/14] kddv/tests: Add support for testing hwmon driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 11/14] kddv/tests/hwmon: Add test cases for max31722 driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 12/14] kddv/tests: Add support for testing mtd driver Zhang Xiaoxu
2023-11-18 10:40 ` Zhang Xiaoxu [this message]
2023-11-18 10:40 ` [PATCH -next 14/14] kddv: Add document for kddv Zhang Xiaoxu

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=20231118104040.386381-14-zhangxiaoxu@huaweicloud.com \
    --to=zhangxiaoxu@huaweicloud.com \
    --cc=broonie@kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=weiyongjun1@huawei.com \
    --cc=zhangxiaoxu5@huawei.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).